hexo-theme-shokax 0.3.10 → 0.3.12
Sign up to get free protection for your applications and to get access to all the features.
- package/_config.yml +47 -57
- package/layout/_mixin/comment.pug +1 -1
- package/layout/_partials/head/head.pug +2 -7
- package/layout/_partials/layout.pug +5 -5
- package/package.json +16 -21
- package/scripts/filters/locals.js +6 -0
- package/scripts/filters/post.js +2 -0
- package/scripts/generaters/archive.js +13 -1
- package/scripts/generaters/config.js +4 -1
- package/scripts/generaters/images.js +2 -0
- package/scripts/generaters/index.js +2 -0
- package/scripts/generaters/pages.js +0 -2
- package/scripts/generaters/script.js +37 -11
- package/scripts/helpers/asset.js +17 -71
- package/scripts/helpers/engine.js +24 -1
- package/scripts/helpers/list_categories.js +4 -0
- package/scripts/helpers/summary_ai.js +4 -0
- package/scripts/helpers/symbols_count_time.js +14 -8
- package/scripts/plugin/check.js +5 -3
- package/scripts/plugin/index.js +39 -23
- package/scripts/plugin/lib/injects.js +15 -0
- package/scripts/tags/media.js +1 -0
- package/scripts/utils.js +14 -0
- package/source/js/_app/components/sidebar.js +54 -56
- package/source/js/_app/fireworks.js +6 -137
- package/source/js/_app/globals/globalVars.js +80 -96
- package/source/js/_app/globals/handles.js +81 -60
- package/source/js/_app/globals/themeColor.js +30 -26
- package/source/js/_app/globals/thirdparty.js +25 -24
- package/source/js/_app/globals/tools.js +36 -30
- package/source/js/_app/library/anime.js +30 -15
- package/source/js/_app/library/dom.js +12 -5
- package/source/js/_app/library/loadFile.js +10 -12
- package/source/js/_app/library/proto.js +59 -7
- package/source/js/_app/library/scriptPjax.js +14 -9
- package/source/js/_app/library/storage.js +2 -4
- package/source/js/_app/library/vue.js +16 -19
- package/source/js/_app/page/comment.js +10 -11
- package/source/js/_app/page/common.js +8 -12
- package/source/js/_app/page/fancybox.js +14 -14
- package/source/js/_app/page/post.js +43 -45
- package/source/js/_app/page/search.js +20 -20
- package/source/js/_app/page/tab.js +7 -10
- package/source/js/_app/pjax/domInit.js +29 -29
- package/source/js/_app/pjax/refresh.js +44 -50
- package/source/js/_app/pjax/siteInit.js +27 -31
- package/source/js/_app/player.js +44 -27
- package/test/dom.test.js +0 -86
@@ -1,59 +1,53 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
const
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
globalVars_1.sideBar.removeClass('on');
|
22
|
-
globalVars_1.menuToggle.removeClass('close');
|
23
|
-
});
|
1
|
+
import { $dom } from '../library/dom';
|
2
|
+
import { cardActive } from '../page/common';
|
3
|
+
import { postBeauty } from '../page/post';
|
4
|
+
import { pageScroll, transition } from '../library/anime';
|
5
|
+
import { vendorCss, vendorJs } from '../library/loadFile';
|
6
|
+
import { pjaxScript } from '../library/scriptPjax';
|
7
|
+
import { resizeHandle } from '../globals/handles';
|
8
|
+
import { loadCat, menuToggle, setLocalHash, setLocalUrl, setOriginTitle, sideBar, toolPlayer } from '../globals/globalVars';
|
9
|
+
import { mediaPlayer } from '../player';
|
10
|
+
import { pagePosition, positionInit } from '../globals/tools';
|
11
|
+
import { menuActive, sideBarTab, sidebarTOC } from '../components/sidebar';
|
12
|
+
import { Loader, isOutime, lazyload } from '../globals/thirdparty';
|
13
|
+
import { tabFormat } from '../page/tab';
|
14
|
+
export const pjaxReload = () => {
|
15
|
+
pagePosition();
|
16
|
+
if (sideBar.hasClass('on')) {
|
17
|
+
transition(sideBar, 0, () => {
|
18
|
+
sideBar.removeClass('on');
|
19
|
+
menuToggle.removeClass('close');
|
20
|
+
}); // 'transition.slideRightOut'
|
24
21
|
}
|
25
|
-
const mainNode =
|
22
|
+
const mainNode = $dom('#main');
|
26
23
|
mainNode.innerHTML = '';
|
27
|
-
mainNode.appendChild(
|
28
|
-
|
24
|
+
mainNode.appendChild(loadCat.lastChild.cloneNode(true));
|
25
|
+
pageScroll(0);
|
29
26
|
};
|
30
|
-
|
31
|
-
|
32
|
-
(
|
33
|
-
(
|
34
|
-
(
|
35
|
-
(
|
36
|
-
(0, loadFile_1.vendorCss)('mermaid');
|
37
|
-
(0, loadFile_1.vendorJs)('chart');
|
27
|
+
export const siteRefresh = (reload) => {
|
28
|
+
setLocalHash(0);
|
29
|
+
setLocalUrl(window.location.href);
|
30
|
+
vendorCss('katex');
|
31
|
+
vendorJs('copy_tex');
|
32
|
+
vendorCss('mermaid');
|
38
33
|
if (reload !== 1) {
|
39
|
-
|
34
|
+
$dom.each('script[data-pjax]', pjaxScript);
|
40
35
|
}
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
if (typeof
|
49
|
-
|
36
|
+
setOriginTitle(document.title);
|
37
|
+
resizeHandle();
|
38
|
+
menuActive();
|
39
|
+
sideBarTab();
|
40
|
+
sidebarTOC();
|
41
|
+
postBeauty();
|
42
|
+
tabFormat();
|
43
|
+
if (typeof mediaPlayer !== 'undefined') {
|
44
|
+
toolPlayer.player.load(LOCAL.audio || CONFIG.audio || {});
|
50
45
|
}
|
51
|
-
|
46
|
+
Loader.hide();
|
52
47
|
setTimeout(() => {
|
53
|
-
|
48
|
+
positionInit();
|
54
49
|
}, 500);
|
55
|
-
|
56
|
-
|
57
|
-
|
50
|
+
cardActive();
|
51
|
+
lazyload.observe();
|
52
|
+
isOutime();
|
58
53
|
};
|
59
|
-
exports.siteRefresh = siteRefresh;
|
@@ -1,23 +1,18 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
};
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
const handles_1 = require("../globals/handles");
|
13
|
-
const tools_1 = require("../globals/tools");
|
14
|
-
const fireworks_1 = require("../fireworks");
|
15
|
-
const theme_shokax_pjax_1 = __importDefault(require("theme-shokax-pjax"));
|
16
|
-
const vue_1 = require("../library/vue");
|
1
|
+
import domInit from './domInit';
|
2
|
+
import { pjaxReload, siteRefresh } from './refresh';
|
3
|
+
import { cloudflareInit } from '../library/scriptPjax';
|
4
|
+
import { algoliaSearch } from '../page/search';
|
5
|
+
import { pjax, setPjax } from '../globals/globalVars';
|
6
|
+
import { autoDarkmode, themeColorListener } from '../globals/themeColor';
|
7
|
+
import { resizeHandle, scrollHandle, visibilityListener } from '../globals/handles';
|
8
|
+
import { pagePosition } from '../globals/tools';
|
9
|
+
import { initFireworks } from '../fireworks';
|
10
|
+
import Pjax from 'theme-shokax-pjax';
|
11
|
+
import { initVue } from '../library/vue';
|
17
12
|
const siteInit = () => {
|
18
|
-
(
|
19
|
-
|
20
|
-
|
13
|
+
domInit();
|
14
|
+
initVue();
|
15
|
+
setPjax(new Pjax({
|
21
16
|
selectors: [
|
22
17
|
'head title',
|
23
18
|
'.languages',
|
@@ -30,23 +25,24 @@ const siteInit = () => {
|
|
30
25
|
}));
|
31
26
|
CONFIG.quicklink.ignores = LOCAL.ignores;
|
32
27
|
quicklink.listen(CONFIG.quicklink);
|
33
|
-
|
28
|
+
autoDarkmode();
|
34
29
|
if (!CONFIG.disableVL) {
|
35
|
-
|
30
|
+
visibilityListener();
|
36
31
|
}
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
window.addEventListener('scroll',
|
41
|
-
window.addEventListener('resize',
|
42
|
-
window.addEventListener('pjax:send',
|
43
|
-
window.addEventListener('pjax:success',
|
32
|
+
themeColorListener();
|
33
|
+
algoliaSearch(pjax);
|
34
|
+
initFireworks();
|
35
|
+
window.addEventListener('scroll', scrollHandle);
|
36
|
+
window.addEventListener('resize', resizeHandle);
|
37
|
+
window.addEventListener('pjax:send', pjaxReload);
|
38
|
+
window.addEventListener('pjax:success', siteRefresh); // 默认会传入一个event参数
|
44
39
|
window.addEventListener('beforeunload', () => {
|
45
|
-
|
40
|
+
pagePosition();
|
46
41
|
});
|
47
|
-
|
42
|
+
// clickMenu() TODO 暂时禁用
|
43
|
+
siteRefresh(1);
|
48
44
|
};
|
49
|
-
|
45
|
+
cloudflareInit();
|
50
46
|
window.addEventListener('DOMContentLoaded', siteInit, {
|
51
47
|
passive: true
|
52
48
|
});
|
package/source/js/_app/player.js
CHANGED
@@ -1,15 +1,12 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
const dom_1 = require("./library/dom");
|
8
|
-
const storage_1 = require("./library/storage");
|
9
|
-
const tab_1 = require("./page/tab");
|
1
|
+
import { originTitle } from './globals/globalVars';
|
2
|
+
import { showtip } from './globals/tools';
|
3
|
+
import { pageScroll } from './library/anime';
|
4
|
+
import { $dom } from './library/dom';
|
5
|
+
import { $storage } from './library/storage';
|
6
|
+
import { tabFormat } from './page/tab';
|
10
7
|
let NOWPLAYING = null;
|
11
8
|
const isMobile = /mobile/i.test(window.navigator.userAgent);
|
12
|
-
const mediaPlayer = (t, config) => {
|
9
|
+
export const mediaPlayer = (t, config) => {
|
13
10
|
if (CONFIG.noPlayer) {
|
14
11
|
return;
|
15
12
|
}
|
@@ -87,7 +84,7 @@ const mediaPlayer = (t, config) => {
|
|
87
84
|
t.player.options.mode = 'loop';
|
88
85
|
}
|
89
86
|
controller.btns.mode.className = 'mode ' + t.player.options.mode + ' btn';
|
90
|
-
|
87
|
+
$storage.set('_PlayerMode', t.player.options.mode);
|
91
88
|
},
|
92
89
|
volume(e) {
|
93
90
|
e.preventDefault();
|
@@ -246,7 +243,7 @@ const mediaPlayer = (t, config) => {
|
|
246
243
|
return null;
|
247
244
|
}
|
248
245
|
const id = 'list-' + t.player._id + '-' + item.group;
|
249
|
-
let tab =
|
246
|
+
let tab = $dom('#' + id);
|
250
247
|
if (!tab) {
|
251
248
|
tab = el.createChild('div', {
|
252
249
|
id,
|
@@ -255,6 +252,7 @@ const mediaPlayer = (t, config) => {
|
|
255
252
|
});
|
256
253
|
if (t.player.group) {
|
257
254
|
tab.attr('data-title', t.player.options.rawList[item.group].title)
|
255
|
+
// @ts-ignore
|
258
256
|
.attr('data-id', t.player._id);
|
259
257
|
}
|
260
258
|
}
|
@@ -278,7 +276,7 @@ const mediaPlayer = (t, config) => {
|
|
278
276
|
});
|
279
277
|
return item;
|
280
278
|
});
|
281
|
-
|
279
|
+
tabFormat();
|
282
280
|
},
|
283
281
|
current() {
|
284
282
|
return this.data[this.index];
|
@@ -293,7 +291,7 @@ const mediaPlayer = (t, config) => {
|
|
293
291
|
li && li.addClass('active');
|
294
292
|
tab = this.el.find('.tab')[item.group];
|
295
293
|
tab && tab.addClass('active');
|
296
|
-
|
294
|
+
pageScroll(item.el, item.el.offsetTop);
|
297
295
|
return this;
|
298
296
|
},
|
299
297
|
title() {
|
@@ -301,7 +299,7 @@ const mediaPlayer = (t, config) => {
|
|
301
299
|
return;
|
302
300
|
}
|
303
301
|
const current = this.current();
|
304
|
-
document.title = 'Now Playing...' + current.name + ' - ' + current.artist + ' | ' +
|
302
|
+
document.title = 'Now Playing...' + current.name + ' - ' + current.artist + ' | ' + originTitle;
|
305
303
|
},
|
306
304
|
error() {
|
307
305
|
const current = this.current();
|
@@ -394,8 +392,9 @@ const mediaPlayer = (t, config) => {
|
|
394
392
|
const meta = utils.parse(raw);
|
395
393
|
if (meta[0]) {
|
396
394
|
const skey = JSON.stringify(meta);
|
397
|
-
const playlist =
|
395
|
+
const playlist = $storage.get(skey);
|
398
396
|
if (playlist) {
|
397
|
+
// list.push.apply(list, JSON.parse(playlist))
|
399
398
|
list.push(...JSON.parse(playlist));
|
400
399
|
resolve(list);
|
401
400
|
}
|
@@ -404,10 +403,12 @@ const mediaPlayer = (t, config) => {
|
|
404
403
|
.then((response) => {
|
405
404
|
return response.json();
|
406
405
|
}).then((json) => {
|
407
|
-
|
406
|
+
$storage.set(skey, JSON.stringify(json));
|
407
|
+
// list.push.apply(list, json)
|
408
408
|
list.push(...json);
|
409
409
|
resolve(list);
|
410
410
|
}).catch((ex) => {
|
411
|
+
// (不)处理catch的异常
|
411
412
|
});
|
412
413
|
}
|
413
414
|
}
|
@@ -437,16 +438,19 @@ const mediaPlayer = (t, config) => {
|
|
437
438
|
t.player = {
|
438
439
|
_id: utils.random(999999),
|
439
440
|
group: true,
|
441
|
+
// 加载播放列表
|
440
442
|
load(newList) {
|
441
443
|
let d = '';
|
442
444
|
if (newList && newList.length > 0) {
|
443
445
|
if (this.options.rawList !== newList) {
|
444
446
|
this.options.rawList = newList;
|
445
447
|
playlist.clear();
|
448
|
+
// 获取新列表
|
446
449
|
this.fetch();
|
447
450
|
}
|
448
451
|
}
|
449
452
|
else {
|
453
|
+
// 没有列表时,隐藏按钮
|
450
454
|
d = 'none';
|
451
455
|
this.pause();
|
452
456
|
}
|
@@ -495,6 +499,7 @@ const mediaPlayer = (t, config) => {
|
|
495
499
|
}
|
496
500
|
});
|
497
501
|
},
|
502
|
+
// 根据模式切换当前曲目index
|
498
503
|
mode() {
|
499
504
|
const total = playlist.data.length;
|
500
505
|
if (!total || playlist.errnum === total) {
|
@@ -535,6 +540,7 @@ const mediaPlayer = (t, config) => {
|
|
535
540
|
}
|
536
541
|
this.init();
|
537
542
|
},
|
543
|
+
// 直接设置当前曲目index
|
538
544
|
switch(index) {
|
539
545
|
if (typeof index === 'number' &&
|
540
546
|
index !== playlist.index &&
|
@@ -544,6 +550,7 @@ const mediaPlayer = (t, config) => {
|
|
544
550
|
this.init();
|
545
551
|
}
|
546
552
|
},
|
553
|
+
// 更新source为当前曲目index
|
547
554
|
init() {
|
548
555
|
const item = playlist.current();
|
549
556
|
if (!item || item.error) {
|
@@ -557,8 +564,8 @@ const mediaPlayer = (t, config) => {
|
|
557
564
|
}
|
558
565
|
source.attr('src', item.url);
|
559
566
|
source.attr('title', item.name + ' - ' + item.artist);
|
560
|
-
this.volume(
|
561
|
-
this.muted(
|
567
|
+
this.volume($storage.get('_PlayerVolume') || '0.7');
|
568
|
+
this.muted($storage.get('_PlayerMuted'));
|
562
569
|
progress.create();
|
563
570
|
if (this.options.type === 'audio') {
|
564
571
|
preview.create();
|
@@ -576,16 +583,17 @@ const mediaPlayer = (t, config) => {
|
|
576
583
|
source.play().then(() => {
|
577
584
|
playlist.scroll();
|
578
585
|
}).catch((e) => {
|
586
|
+
// 不处理错误
|
579
587
|
});
|
580
588
|
},
|
581
589
|
pause() {
|
582
590
|
source.pause();
|
583
|
-
document.title =
|
591
|
+
document.title = originTitle;
|
584
592
|
},
|
585
593
|
stop() {
|
586
594
|
source.pause();
|
587
595
|
source.currentTime = 0;
|
588
|
-
document.title =
|
596
|
+
document.title = originTitle;
|
589
597
|
},
|
590
598
|
seek(time) {
|
591
599
|
time = Math.max(time, 0);
|
@@ -596,11 +604,11 @@ const mediaPlayer = (t, config) => {
|
|
596
604
|
muted(status) {
|
597
605
|
if (status === 'muted') {
|
598
606
|
source.muted = status;
|
599
|
-
|
607
|
+
$storage.set('_PlayerMuted', status);
|
600
608
|
controller.update(0);
|
601
609
|
}
|
602
610
|
else {
|
603
|
-
|
611
|
+
$storage.del('_PlayerMuted');
|
604
612
|
source.muted = false;
|
605
613
|
controller.update(source.volume);
|
606
614
|
}
|
@@ -608,7 +616,7 @@ const mediaPlayer = (t, config) => {
|
|
608
616
|
volume(percentage) {
|
609
617
|
if (!isNaN(percentage)) {
|
610
618
|
controller.update(percentage);
|
611
|
-
|
619
|
+
$storage.set('_PlayerVolume', percentage);
|
612
620
|
source.volume = percentage;
|
613
621
|
}
|
614
622
|
},
|
@@ -622,6 +630,7 @@ const mediaPlayer = (t, config) => {
|
|
622
630
|
index: 0,
|
623
631
|
create(box) {
|
624
632
|
const current = playlist.index;
|
633
|
+
// const that = this
|
625
634
|
const raw = playlist.current().lrc;
|
626
635
|
const callback = (body) => {
|
627
636
|
if (current !== playlist.index) {
|
@@ -655,6 +664,7 @@ const mediaPlayer = (t, config) => {
|
|
655
664
|
this.index = i;
|
656
665
|
const y = -(this.index - 1);
|
657
666
|
this.el.style.transform = 'translateY(' + y + 'rem)';
|
667
|
+
// this.el.style.webkitTransform = 'translateY(' + y + 'rem)';
|
658
668
|
this.el.getElementsByClassName('current')[0].removeClass('current');
|
659
669
|
this.el.getElementsByTagName('p')[i].addClass('current');
|
660
670
|
}
|
@@ -670,12 +680,15 @@ const mediaPlayer = (t, config) => {
|
|
670
680
|
let lrc = [];
|
671
681
|
const lyricLen = lyric.length;
|
672
682
|
for (let i = 0; i < lyricLen; i++) {
|
683
|
+
// match lrc time
|
673
684
|
const lrcTimes = lyric[i].match(/\[(\d{2}):(\d{2})(\.(\d{2,3}))?]/g);
|
685
|
+
// match lrc text
|
674
686
|
const lrcText = lyric[i]
|
675
687
|
.replace(/.*\[(\d{2}):(\d{2})(\.(\d{2,3}))?]/g, '')
|
676
688
|
.replace(/<(\d{2}):(\d{2})(\.(\d{2,3}))?>/g, '')
|
677
689
|
.trim();
|
678
690
|
if (lrcTimes) {
|
691
|
+
// handle multiple time tag
|
679
692
|
const timeLen = lrcTimes.length;
|
680
693
|
for (let j = 0; j < timeLen; j++) {
|
681
694
|
const oneTime = /\[(\d{2}):(\d{2})(\.(\d{2,3}))?]/.exec(lrcTimes[j]);
|
@@ -687,6 +700,7 @@ const mediaPlayer = (t, config) => {
|
|
687
700
|
}
|
688
701
|
}
|
689
702
|
}
|
703
|
+
// sort by time
|
690
704
|
lrc = lrc.filter((item) => item[1]);
|
691
705
|
lrc.sort((a, b) => a[0] - b[0]);
|
692
706
|
return lrc;
|
@@ -702,6 +716,7 @@ const mediaPlayer = (t, config) => {
|
|
702
716
|
}).then((body) => {
|
703
717
|
callback(body);
|
704
718
|
}).catch((ex) => {
|
719
|
+
// 不处理错误
|
705
720
|
});
|
706
721
|
}
|
707
722
|
};
|
@@ -721,7 +736,7 @@ const mediaPlayer = (t, config) => {
|
|
721
736
|
},
|
722
737
|
onplay() {
|
723
738
|
t.parentNode.addClass('playing');
|
724
|
-
|
739
|
+
showtip(this.attr('title'));
|
725
740
|
NOWPLAYING = t;
|
726
741
|
},
|
727
742
|
onpause() {
|
@@ -744,9 +759,12 @@ const mediaPlayer = (t, config) => {
|
|
744
759
|
return;
|
745
760
|
}
|
746
761
|
t.player.options = Object.assign(option, config);
|
747
|
-
t.player.options.mode =
|
762
|
+
t.player.options.mode = $storage.get('_PlayerMode') || t.player.options.mode;
|
763
|
+
// 初始化button、controls以及click事件
|
748
764
|
buttons.create();
|
765
|
+
// 初始化audio or video
|
749
766
|
source = t.createChild(t.player.options.type, events);
|
767
|
+
// 初始化播放列表、预览、控件按钮等
|
750
768
|
info.create();
|
751
769
|
t.parentNode.addClass(t.player.options.type);
|
752
770
|
t.player.created = true;
|
@@ -754,4 +772,3 @@ const mediaPlayer = (t, config) => {
|
|
754
772
|
init(config);
|
755
773
|
return t;
|
756
774
|
};
|
757
|
-
exports.mediaPlayer = mediaPlayer;
|
package/test/dom.test.js
DELETED
@@ -1,86 +0,0 @@
|
|
1
|
-
const { describe, beforeEach, afterEach, it } = require('mocha')
|
2
|
-
const jsdom = require('jsdom')
|
3
|
-
const { $dom } = require('../source/js/_app/library/dom')
|
4
|
-
const { expect } = require('chai')
|
5
|
-
|
6
|
-
describe('$dom', function () {
|
7
|
-
// 在每个测试用例之前创建一个虚拟文档
|
8
|
-
beforeEach(function () {
|
9
|
-
const html = `
|
10
|
-
<html lang="zh-cn">
|
11
|
-
<head><title>Test Page</title></head>
|
12
|
-
<body>
|
13
|
-
<div id="container">
|
14
|
-
<p class="text">Hello</p>
|
15
|
-
<p class="text">World</p>
|
16
|
-
</div>
|
17
|
-
</body>
|
18
|
-
</html>
|
19
|
-
`
|
20
|
-
const dom = new jsdom.JSDOM(html)
|
21
|
-
global.document = dom.window.document
|
22
|
-
global.HTMLElement = dom.window.HTMLElement
|
23
|
-
})
|
24
|
-
|
25
|
-
// 在每个测试用例之后清除虚拟文档
|
26
|
-
afterEach(function () {
|
27
|
-
global.document = null
|
28
|
-
})
|
29
|
-
|
30
|
-
it('should return the element with the given id selector', function () {
|
31
|
-
const element = $dom('#container')
|
32
|
-
expect(element).to.exist
|
33
|
-
expect(element).to.be.instanceOf(HTMLElement)
|
34
|
-
expect(element.id).to.equal('container')
|
35
|
-
})
|
36
|
-
|
37
|
-
it('should return the first element that matches the given selector', function () {
|
38
|
-
const element = $dom('.text')
|
39
|
-
expect(element).to.exist
|
40
|
-
expect(element).to.be.instanceOf(HTMLElement)
|
41
|
-
expect(element.textContent).to.equal('Hello')
|
42
|
-
})
|
43
|
-
|
44
|
-
it('should return null if no element matches the given selector', function () {
|
45
|
-
const element = $dom('.foo')
|
46
|
-
expect(element).to.be.null
|
47
|
-
})
|
48
|
-
})
|
49
|
-
|
50
|
-
describe('$dom.all & $dom.each', function () {
|
51
|
-
beforeEach(function () {
|
52
|
-
const html = `
|
53
|
-
<html lang="zh-cn">
|
54
|
-
<head><title>Test Page</title></head>
|
55
|
-
<body>
|
56
|
-
<div id="container">
|
57
|
-
<p class="text">Hello</p>
|
58
|
-
<p class="text">World</p>
|
59
|
-
</div>
|
60
|
-
</body>
|
61
|
-
</html>
|
62
|
-
`
|
63
|
-
const dom = new jsdom.JSDOM(html)
|
64
|
-
global.document = dom.window.document
|
65
|
-
global.HTMLElement = dom.window.HTMLElement
|
66
|
-
})
|
67
|
-
|
68
|
-
afterEach(function () {
|
69
|
-
global.document = null
|
70
|
-
})
|
71
|
-
|
72
|
-
it('should return all elements with the given selector', () => {
|
73
|
-
const elements = $dom.all('p')
|
74
|
-
expect(elements).to.be.exist
|
75
|
-
expect(elements).length(2)
|
76
|
-
})
|
77
|
-
|
78
|
-
it('should run callback with the given selector', () => {
|
79
|
-
$dom.each('p', function (value){
|
80
|
-
value.innerText = 'test'
|
81
|
-
})
|
82
|
-
const elements = $dom.all('p')
|
83
|
-
expect(elements[0].innerText).equal('test')
|
84
|
-
expect(elements[1].innerText).equal('test')
|
85
|
-
})
|
86
|
-
})
|