wikiparser-node 0.6.1 → 0.6.5-b

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/bundle/bundle.min.js +40 -0
  2. package/package.json +9 -11
  3. package/README.md +0 -39
  4. package/config/default.json +0 -832
  5. package/config/llwiki.json +0 -630
  6. package/config/moegirl.json +0 -727
  7. package/config/zhwiki.json +0 -1269
  8. package/index.js +0 -277
  9. package/lib/element.js +0 -612
  10. package/lib/node.js +0 -771
  11. package/lib/ranges.js +0 -130
  12. package/lib/text.js +0 -175
  13. package/lib/title.js +0 -70
  14. package/mixin/attributeParent.js +0 -113
  15. package/mixin/fixedToken.js +0 -40
  16. package/mixin/hidden.js +0 -21
  17. package/mixin/sol.js +0 -68
  18. package/parser/brackets.js +0 -118
  19. package/parser/commentAndExt.js +0 -63
  20. package/parser/converter.js +0 -45
  21. package/parser/externalLinks.js +0 -31
  22. package/parser/hrAndDoubleUnderscore.js +0 -36
  23. package/parser/html.js +0 -42
  24. package/parser/links.js +0 -97
  25. package/parser/list.js +0 -59
  26. package/parser/magicLinks.js +0 -41
  27. package/parser/quotes.js +0 -64
  28. package/parser/selector.js +0 -175
  29. package/parser/table.js +0 -114
  30. package/src/arg.js +0 -198
  31. package/src/atom/hidden.js +0 -13
  32. package/src/atom/index.js +0 -43
  33. package/src/attribute.js +0 -460
  34. package/src/charinsert.js +0 -91
  35. package/src/converter.js +0 -176
  36. package/src/converterFlags.js +0 -279
  37. package/src/converterRule.js +0 -259
  38. package/src/extLink.js +0 -175
  39. package/src/gallery.js +0 -146
  40. package/src/hasNowiki/index.js +0 -42
  41. package/src/hasNowiki/pre.js +0 -40
  42. package/src/heading.js +0 -123
  43. package/src/html.js +0 -230
  44. package/src/imageParameter.js +0 -276
  45. package/src/imagemap.js +0 -205
  46. package/src/imagemapLink.js +0 -43
  47. package/src/index.js +0 -842
  48. package/src/link/category.js +0 -49
  49. package/src/link/file.js +0 -321
  50. package/src/link/galleryImage.js +0 -125
  51. package/src/link/index.js +0 -347
  52. package/src/magicLink.js +0 -135
  53. package/src/nested/choose.js +0 -24
  54. package/src/nested/combobox.js +0 -23
  55. package/src/nested/index.js +0 -88
  56. package/src/nested/references.js +0 -23
  57. package/src/nowiki/comment.js +0 -71
  58. package/src/nowiki/dd.js +0 -59
  59. package/src/nowiki/doubleUnderscore.js +0 -56
  60. package/src/nowiki/hr.js +0 -41
  61. package/src/nowiki/index.js +0 -56
  62. package/src/nowiki/list.js +0 -16
  63. package/src/nowiki/noinclude.js +0 -28
  64. package/src/nowiki/quote.js +0 -63
  65. package/src/onlyinclude.js +0 -61
  66. package/src/paramTag/index.js +0 -83
  67. package/src/paramTag/inputbox.js +0 -42
  68. package/src/parameter.js +0 -214
  69. package/src/syntax.js +0 -91
  70. package/src/table/index.js +0 -981
  71. package/src/table/td.js +0 -308
  72. package/src/table/tr.js +0 -299
  73. package/src/tagPair/ext.js +0 -122
  74. package/src/tagPair/include.js +0 -60
  75. package/src/tagPair/index.js +0 -126
  76. package/src/transclude.js +0 -751
  77. package/tool/index.js +0 -1199
  78. package/util/base.js +0 -17
  79. package/util/debug.js +0 -73
  80. package/util/diff.js +0 -76
  81. package/util/lint.js +0 -40
  82. package/util/string.js +0 -105
package/tool/index.js DELETED
@@ -1,1199 +0,0 @@
1
- 'use strict';
2
-
3
- const {typeError} = require('../util/debug'),
4
- {text} = require('../util/string'),
5
- {isPlainObject} = require('../util/base'),
6
- Parser = require('..'),
7
- AstNode = require('../lib/node'),
8
- Token = require('../src'),
9
- AttributeToken = require('../src/attribute');
10
-
11
- const /** @type {WeakMap<Token, Record<string, *>>} */ cache = new WeakMap();
12
-
13
- /** Token集合 */
14
- class TokenCollection {
15
- /** @type {AstNode[]} */ array = [];
16
- /** @type {Set<Token>} */ #roots = new Set();
17
- /** @type {TokenCollection} */ prevObject;
18
-
19
- /**
20
- * 生成匹配函数
21
- * @param {string} method
22
- * @param {string|AstNode|Iterable<AstNode>} selector
23
- * @returns {(token: AstNode) => boolean}
24
- */
25
- static #matchesGenerator = (method, selector) => {
26
- if (selector === undefined || typeof selector === 'string') {
27
- return token => token instanceof Token && token.matches(selector);
28
- } else if (selector?.[Symbol.iterator]) {
29
- return token => new Set(selector).has(token);
30
- }
31
- return selector instanceof AstNode
32
- ? token => token === selector
33
- : typeError(TokenCollection, method, 'String', 'AstNode', 'Iterable');
34
- };
35
-
36
- /** @ignore */
37
- [Symbol.iterator]() {
38
- return this.array[Symbol.iterator]();
39
- }
40
-
41
- /** 数组长度 */
42
- get length() {
43
- return this.array.length;
44
- }
45
-
46
- /** 数组长度 */
47
- size() {
48
- return this.array.length;
49
- }
50
-
51
- /**
52
- * 筛选Token节点
53
- * @returns {Token[]}
54
- */
55
- get #tokens() {
56
- return this.array.filter(ele => ele instanceof Token);
57
- }
58
-
59
- /**
60
- * 第一个Token节点
61
- * @returns {Token}
62
- */
63
- get #firstToken() {
64
- return this.array.find(ele => ele instanceof Token);
65
- }
66
-
67
- /**
68
- * @param {Iterable<AstNode>} arr 节点数组
69
- * @param {TokenCollection} prevObject 前身
70
- */
71
- constructor(arr, prevObject) {
72
- if (prevObject && !(prevObject instanceof TokenCollection)) {
73
- this.typeError('constructor', 'TokenCollection');
74
- }
75
- for (const token of arr) {
76
- if (token === undefined) {
77
- continue;
78
- } else if (!(token instanceof AstNode)) {
79
- this.typeError('constructor', 'AstNode');
80
- } else if (!this.array.includes(token)) {
81
- this.#roots.add(token.getRootNode());
82
- this.array.push(token);
83
- }
84
- }
85
- this.prevObject = prevObject;
86
- this.#sort();
87
- Object.defineProperties(this, {
88
- array: {writable: false, configurable: false},
89
- prevObject: {enumerable: prevObject, writable: false, configurable: false},
90
- });
91
- Object.freeze(this.array);
92
- }
93
-
94
- /**
95
- * 抛出TypeError
96
- * @param {string} method
97
- * @param {...string} types 可接受的参数类型
98
- */
99
- typeError(method, ...types) {
100
- return typeError(this.constructor, method, ...types);
101
- }
102
-
103
- /** 节点排序 */
104
- #sort() {
105
- const rootArray = [...this.#roots];
106
- this.array.sort(/** @type {(a: AstNode, b: AstNode) => boolean} */ (a, b) => {
107
- const aRoot = a.getRootNode(),
108
- bRoot = b.getRootNode();
109
- return aRoot === bRoot ? a.compareDocumentPosition(b) : rootArray.indexOf(aRoot) - rootArray.indexOf(bRoot);
110
- });
111
- }
112
-
113
- /** 转换为数组 */
114
- toArray() {
115
- return [...this];
116
- }
117
-
118
- /**
119
- * 提取第n个元素
120
- * @template {unknown} T
121
- * @param {T} n 序号
122
- * @returns {T extends number ? AstNode : AstNode[]}
123
- */
124
- get(n) {
125
- if (Number.isInteger(n)) {
126
- return this.array.at(n);
127
- }
128
- return n === undefined ? this.toArray() : this.typeError('get', 'Number');
129
- }
130
-
131
- /**
132
- * 循环
133
- * @param {CollectionCallback<void, AstNode>} callback
134
- */
135
- each(callback) {
136
- for (let i = 0; i < this.length; i++) {
137
- const ele = this.array[i];
138
- callback.call(ele, i, ele);
139
- }
140
- return this;
141
- }
142
-
143
- /**
144
- * map方法
145
- * @param {CollectionCallback<*, AstNode>} callback
146
- */
147
- map(callback) {
148
- const arr = this.array.map((ele, i) => callback.call(ele, i, ele));
149
- try {
150
- return new TokenCollection(arr, this);
151
- } catch {
152
- return arr;
153
- }
154
- }
155
-
156
- /**
157
- * 子序列
158
- * @param {number} start 起点
159
- * @param {number} end 终点
160
- */
161
- slice(start, end) {
162
- return new TokenCollection(this.array.slice(start, end), this);
163
- }
164
-
165
- /** 第一个元素 */
166
- first() {
167
- return this.slice(0, 1);
168
- }
169
-
170
- /** 最后一个元素 */
171
- last() {
172
- return this.slice(-1);
173
- }
174
-
175
- /**
176
- * 任一元素
177
- * @param {number} i 序号
178
- */
179
- eq(i) {
180
- return this.slice(i, i === -1 ? undefined : i + 1);
181
- }
182
-
183
- /** 使用空字符串join */
184
- toString() {
185
- return this.array.map(String).join('');
186
- }
187
-
188
- /**
189
- * 输出有效部分或设置文本
190
- * @template {unknown} T
191
- * @param {T} str 新文本
192
- * @returns {T extends string|CollectionCallback<string, string> ? this : string}
193
- */
194
- text(str) {
195
- const /** @type {CollectionCallback<string, string> */ callback = typeof str === 'function' ? str : () => str;
196
- if (typeof str === 'string' || typeof str === 'function') {
197
- for (let i = 0; i < this.length; i++) {
198
- const ele = this.array[i];
199
- if (ele instanceof Token) {
200
- try {
201
- ele.replaceChildren(callback.call(ele, i, ele.text()));
202
- } catch {}
203
- }
204
- }
205
- return this;
206
- }
207
- return str === undefined ? text(this.toArray()) : this.typeError('text', 'String', 'Function');
208
- }
209
-
210
- /**
211
- * 判断是否存在元素满足选择器
212
- * @param {string|AstNode|Iterable<AstNode>|CollectionCallback<boolean, AstNode>} selector
213
- */
214
- is(selector) {
215
- return typeof selector === 'function'
216
- ? this.array.some((ele, i) => selector.call(ele, i, ele))
217
- : this.array.some(TokenCollection.#matchesGenerator('is', selector));
218
- }
219
-
220
- /**
221
- * 筛选满足选择器的元素
222
- * @param {string|AstNode|Iterable<AstNode>|CollectionCallback<boolean, AstNode>} selector
223
- */
224
- filter(selector) {
225
- return new TokenCollection(this.array.filter(
226
- (ele, i) => typeof selector === 'function'
227
- ? selector.call(ele, i, ele)
228
- : TokenCollection.#matchesGenerator('filter', selector)(ele),
229
- ), this);
230
- }
231
-
232
- /**
233
- * 筛选不满足选择器的元素
234
- * @param {string|AstNode|Iterable<AstNode>|CollectionCallback<boolean, AstNode>} selector
235
- */
236
- not(selector) {
237
- let /** @type {(ele: AstNode, i: number) => boolean} */ callback;
238
- if (typeof selector === 'function') {
239
- callback = /** @implements */ (ele, i) => !selector.call(ele, i, ele);
240
- } else {
241
- const matches = TokenCollection.#matchesGenerator('not', selector);
242
- callback = /** @implements */ ele => !matches(ele);
243
- }
244
- return new TokenCollection(this.array.filter(callback), this);
245
- }
246
-
247
- /**
248
- * 搜索满足选择器的子节点
249
- * @param {string|AstNode|Iterable<AstNode>} selector
250
- */
251
- find(selector) {
252
- let arr;
253
- if (selector === undefined || typeof selector === 'string') {
254
- arr = this.array.flatMap(token => token instanceof Token ? token.querySelectorAll(selector) : []);
255
- } else if (selector?.[Symbol.iterator]) {
256
- arr = [...selector].filter(ele => this.array.some(token => token.contains(ele)));
257
- } else if (selector instanceof AstNode) {
258
- arr = this.array.some(token => token.contains(selector)) ? [selector] : [];
259
- } else {
260
- this.typeError('find', 'String', 'AstNode', 'Iterable');
261
- }
262
- return new TokenCollection(arr, this);
263
- }
264
-
265
- /**
266
- * 是否存在满足条件的后代节点
267
- * @param {string|AstNode} selector
268
- */
269
- has(selector) {
270
- if (selector === undefined || typeof selector === 'string') {
271
- return this.array.some(ele => ele instanceof Token && ele.querySelector(selector));
272
- }
273
- return selector instanceof AstNode
274
- ? this.array.some(ele => ele.contains(selector))
275
- : this.typeError('has', 'String', 'AstNode');
276
- }
277
-
278
- /**
279
- * 最近的祖先
280
- * @param {string} selector
281
- */
282
- closest(selector) {
283
- if (selector === undefined) {
284
- return new TokenCollection(this.array.map(ele => ele.parentNode), this);
285
- }
286
- return typeof selector === 'string'
287
- ? new TokenCollection(this.#tokens.map(ele => ele.closest(selector)), this)
288
- : this.typeError('closest', 'String');
289
- }
290
-
291
- /** 第一个Token节点在父容器中的序号 */
292
- index() {
293
- const {array: [firstNode]} = this;
294
- if (firstNode) {
295
- const {parentNode} = firstNode;
296
- return parentNode ? parentNode.childNodes.indexOf(firstNode) : 0;
297
- }
298
- return -1;
299
- }
300
-
301
- /**
302
- * 添加元素
303
- * @param {AstNode|Iterable<AstNode>} elements 新增的元素
304
- */
305
- add(elements) {
306
- return new TokenCollection([...this, ...Symbol.iterator in elements ? elements : [elements]], this);
307
- }
308
-
309
- /**
310
- * 添加prevObject
311
- * @param {string} selector
312
- */
313
- addBack(selector) {
314
- return this.add(selector ? this.prevObject.filter(selector) : this.prevObject);
315
- }
316
-
317
- /**
318
- * 带选择器筛选的map
319
- * @param {string} method
320
- * @param {(ele: AstNode) => Token} callback
321
- * @param {string} selector
322
- */
323
- #map(method, callback, selector) {
324
- return selector === undefined || typeof selector === 'string'
325
- ? new TokenCollection(this.array.map(callback).filter(ele => ele.matches(selector)), this)
326
- : this.typeError(method, 'String');
327
- }
328
-
329
- /**
330
- * 带选择器筛选的flatMap
331
- * @param {string} method
332
- * @param {(ele: AstNode) => Token|Token[]} callback
333
- * @param {string} selector
334
- */
335
- #flatMap(method, callback, selector) {
336
- return selector === undefined || typeof selector === 'string'
337
- ? new TokenCollection(this.array.flatMap(callback).filter(ele => ele.matches(selector)), this)
338
- : this.typeError(method, 'String');
339
- }
340
-
341
- /**
342
- * 父元素
343
- * @param {string} selector
344
- */
345
- parent(selector) {
346
- return this.#map('parent', ele => ele.parentNode, selector);
347
- }
348
-
349
- /**
350
- * 祖先
351
- * @param {string} selector
352
- */
353
- parents(selector) {
354
- return this.#flatMap('parents', ele => ele.getAncestors(), selector);
355
- }
356
-
357
- /**
358
- * nextElementSibling
359
- * @param {string} selector
360
- */
361
- next(selector) {
362
- return this.#map('next', ele => ele.nextElementSibling, selector);
363
- }
364
-
365
- /**
366
- * previousElementSibling
367
- * @param {string} selector
368
- */
369
- prev(selector) {
370
- return this.#map('prev', ele => ele.previousElementSibling, selector);
371
- }
372
-
373
- /**
374
- * 筛选兄弟
375
- * @param {string} method
376
- * @param {(i: number) => number} start 起点
377
- * @param {(i: number) => number} count 数量
378
- * @param {string} selector
379
- */
380
- #siblings(method, start, count, selector) {
381
- if (selector !== undefined && typeof selector !== 'string') {
382
- this.typeError(method, 'String');
383
- }
384
- return new TokenCollection(this.array.flatMap(ele => {
385
- const {parentNode} = ele;
386
- if (!parentNode) {
387
- return [];
388
- }
389
- const {childNodes} = parentNode,
390
- i = childNodes.indexOf(ele);
391
- childNodes.splice(start(i), count(i));
392
- return childNodes;
393
- }).filter(ele => ele instanceof Token && ele.matches(selector)), this);
394
- }
395
-
396
- /**
397
- * 所有在后的兄弟
398
- * @param {string} selector
399
- */
400
- nextAll(selector) {
401
- return this.#siblings('nextAll', () => 0, i => i + 1, selector);
402
- }
403
-
404
- /**
405
- * 所有在前的兄弟
406
- * @param {string} selector
407
- */
408
- prevAll(selector) {
409
- return this.#siblings('prevAll', i => i, () => Infinity, selector);
410
- }
411
-
412
- /**
413
- * 所有在后的兄弟
414
- * @param {string} selector
415
- */
416
- siblings(selector) {
417
- return this.#siblings('siblings', i => i, () => 1, selector);
418
- }
419
-
420
- /**
421
- * 直到选择器被满足
422
- * @param {'parents'|'nextAll'|'prevAll'} method
423
- * @param {string|Token|Iterable<Token>} selector
424
- * @param {string} filter 额外的筛选选择器
425
- */
426
- #until(method, selector, filter) {
427
- const originalMethod = `${method.replace(/All$/u, '')}Until`;
428
- if (filter !== undefined && typeof filter !== 'string') {
429
- this.typeError(originalMethod, 'String');
430
- }
431
- const matches = TokenCollection.#matchesGenerator(originalMethod, selector);
432
- return new TokenCollection(this.array.flatMap(ele => {
433
- const /** @type {{array: Token[]}} */ {array} = $(ele)[method](),
434
- until = array[method === 'nextAll' ? 'findIndex' : 'findLastIndex'](end => matches(end));
435
- return method === 'nextAll' ? array.slice(0, until) : array.slice(until + 1);
436
- }).filter(ele => ele.matches(filter)), this);
437
- }
438
-
439
- /**
440
- * 直到选择器被满足的祖先
441
- * @param {string|Token|Iterable<Token>} selector
442
- * @param {string} filter 额外的筛选选择器
443
- */
444
- parentsUntil(selector, filter) {
445
- return this.#until('parents', selector, filter);
446
- }
447
-
448
- /**
449
- * 直到选择器被满足的后方兄弟
450
- * @param {string|Token|Iterable<Token>} selector
451
- * @param {string} filter 额外的筛选选择器
452
- */
453
- nextUntil(selector, filter) {
454
- return this.#until('nextAll', selector, filter);
455
- }
456
-
457
- /**
458
- * 直到选择器被满足的前方兄弟
459
- * @param {string|Token|Iterable<Token>} selector
460
- * @param {string} filter 额外的筛选选择器
461
- */
462
- prevUntil(selector, filter) {
463
- return this.#until('prevAll', selector, filter);
464
- }
465
-
466
- /**
467
- * Token子节点
468
- * @param {string} selector
469
- */
470
- children(selector) {
471
- return selector === undefined || typeof selector === 'string'
472
- ? new TokenCollection(this.#tokens.flatMap(ele => ele.children).filter(ele => ele.matches(selector)), this)
473
- : this.typeError('children', 'String');
474
- }
475
-
476
- /** 所有子节点 */
477
- contents() {
478
- return new TokenCollection(this.array.flatMap(ele => ele.childNodes), this);
479
- }
480
-
481
- /**
482
- * 存取数据
483
- * @param {string|Record<string, *>} key 键
484
- * @param {*} value 值
485
- */
486
- data(key, value) {
487
- if (value !== undefined && typeof key !== 'string') {
488
- this.typeError('data', 'String');
489
- } else if (value === undefined && !isPlainObject(key)) {
490
- const data = cache.get(this.#firstToken);
491
- return key === undefined ? data : data?.[key];
492
- }
493
- for (const token of this.#tokens) {
494
- if (!cache.has(token)) {
495
- cache.set(token, {});
496
- }
497
- const data = cache.get(token);
498
- if (typeof key === 'string') {
499
- data[key] = value;
500
- } else {
501
- Object.assign(data, key);
502
- }
503
- }
504
- return this;
505
- }
506
-
507
- /**
508
- * 删除数据
509
- * @param {string|string[]} name 键
510
- */
511
- removeData(name) {
512
- if (name !== undefined && typeof name !== 'string' && !Array.isArray(name)) {
513
- this.typeError('removeData', 'String', 'Array');
514
- }
515
- name = typeof name === 'string' ? name.split(/\s/u) : name;
516
- for (const token of this.#tokens) {
517
- if (!cache.has(token)) {
518
- continue;
519
- } else if (name === undefined) {
520
- cache.delete(token);
521
- } else {
522
- const data = cache.get(token);
523
- for (const key of name) {
524
- delete data[key];
525
- }
526
- }
527
- }
528
- return this;
529
- }
530
-
531
- /**
532
- * 添加事件监听
533
- * @param {string|Record<string, AstListener>} events 事件名
534
- * @param {string|AstListener} selector
535
- * @param {AstListener} handler 事件处理
536
- * @param {boolean} once 是否一次性
537
- */
538
- #addEventListener(events, selector, handler, once = false) {
539
- if (typeof events !== 'string' && !isPlainObject(events)) {
540
- this.typeError(once ? 'once' : 'on', 'String', 'Object');
541
- } else if (typeof selector === 'function') {
542
- handler = selector;
543
- selector = undefined;
544
- }
545
- const eventPair = typeof events === 'string'
546
- ? events.split(/\s/u).map(/** @returns {[string, AstListener]} */ event => [event, handler])
547
- : Object.entries(events);
548
- for (const token of this.#tokens) {
549
- if (token.matches(selector)) {
550
- for (const [event, listener] of eventPair) {
551
- token.addEventListener(event, listener, {once});
552
- }
553
- }
554
- }
555
- return this;
556
- }
557
-
558
- /**
559
- * 添加事件监听
560
- * @param {string|Record<string, AstListener>} events 事件名
561
- * @param {string|AstListener} selector
562
- * @param {AstListener} handler 事件处理
563
- */
564
- on(events, selector, handler) {
565
- return this.#addEventListener(events, selector, handler);
566
- }
567
-
568
- /**
569
- * 添加一次性事件监听
570
- * @param {string|Record<string, AstListener>} events 事件名
571
- * @param {string|AstListener} selector
572
- * @param {AstListener} handler 事件处理
573
- */
574
- one(events, selector, handler) {
575
- return this.#addEventListener(events, selector, handler, true);
576
- }
577
-
578
- /**
579
- * 移除事件监听
580
- * @param {string|Record<string, AstListener|undefined>|undefined} events 事件名
581
- * @param {string|AstListener} selector
582
- * @param {AstListener} handler 事件处理
583
- */
584
- off(events, selector, handler) {
585
- if (events !== undefined && typeof events !== 'string' && !isPlainObject(events)) {
586
- this.typeError('off', 'String', 'Object');
587
- } else if (typeof selector === 'function') {
588
- handler = selector;
589
- selector = undefined;
590
- }
591
- let eventPair;
592
- if (events) {
593
- eventPair = typeof events === 'string'
594
- ? events.split(/\s/u).map(/** @returns {[string, AstListener]} */ event => [event, handler])
595
- : Object.entries(events);
596
- }
597
- for (const token of this.#tokens) {
598
- if (!token.matches(selector)) {
599
- continue;
600
- } else if (events === undefined) {
601
- token.removeAllEventListeners();
602
- } else {
603
- for (const [event, listener] of eventPair) {
604
- if (listener === undefined) {
605
- token.removeAllEventListeners(event);
606
- } else if (typeof listener === 'function') {
607
- token.removeEventListener(event, listener);
608
- } else {
609
- this.typeError('off', 'String', 'Function');
610
- }
611
- }
612
- }
613
- }
614
- return this;
615
- }
616
-
617
- /**
618
- * 触发事件
619
- * @param {string|Event} event 事件名
620
- * @param {*} data 事件数据
621
- */
622
- trigger(event, data) {
623
- for (const token of this) {
624
- const e = typeof event === 'string' ? new Event(event, {bubbles: true}) : new Event(event.type, event);
625
- token.dispatchEvent(e, data);
626
- }
627
- return this;
628
- }
629
-
630
- /**
631
- * 伪装触发事件
632
- * @param {string|Event} event 事件名
633
- * @param {*} data 事件数据
634
- */
635
- triggerHandler(event, data) {
636
- const {array: [firstNode]} = this;
637
- if (!firstNode) {
638
- return undefined;
639
- }
640
- const e = typeof event === 'string' ? new Event(event) : event;
641
- let result;
642
- for (const listener of firstNode.listEventListeners(e.type)) {
643
- result = listener(e, data);
644
- }
645
- return result;
646
- }
647
-
648
- /**
649
- * 插入文档
650
- * @param {'append'|'prepend'|'before'|'after'|'replaceChildren'|'replaceWith'} method
651
- * @param {AstNode|Iterable<AstNode>|CollectionCallback<AstNode|Iterable<AstNode>, string>} content 插入内容
652
- * @param {...AstNode|Iterable<AstNode>} additional 更多插入内容
653
- */
654
- #insert(method, content, ...additional) {
655
- if (typeof content === 'function') {
656
- for (let i = 0; i < this.length; i++) {
657
- const token = this.array[i];
658
- if (token instanceof Token) {
659
- const result = content.call(token, i, token.toString());
660
- if (typeof result === 'string' || result instanceof Token) {
661
- token[method](result);
662
- } else if (result?.[Symbol.iterator]) {
663
- token[method](...result);
664
- } else {
665
- this.typeError(method, 'String', 'Token');
666
- }
667
- }
668
- }
669
- } else {
670
- for (const token of this) {
671
- if (token instanceof Token) {
672
- token[method](
673
- ...Symbol.iterator in content ? content : [content],
674
- ...additional.flatMap(ele => Symbol.iterator in ele ? [...ele] : ele),
675
- );
676
- }
677
- }
678
- }
679
- return this;
680
- }
681
-
682
- /**
683
- * 在末尾插入
684
- * @param {AstNode|Iterable<AstNode>|CollectionCallback<AstNode|Iterable<AstNode>, string>} content 插入内容
685
- * @param {...AstNode|Iterable<AstNode>} additional 更多插入内容
686
- */
687
- append(content, ...additional) {
688
- return this.#insert('append', content, ...additional);
689
- }
690
-
691
- /**
692
- * 在开头插入
693
- * @param {AstNode|Iterable<AstNode>|CollectionCallback<AstNode|Iterable<AstNode>, string>} content 插入内容
694
- * @param {...AstNode|Iterable<AstNode>} additional 更多插入内容
695
- */
696
- prepend(content, ...additional) {
697
- return this.#insert('prepend', content, ...additional);
698
- }
699
-
700
- /**
701
- * 在后方插入
702
- * @param {AstNode|Iterable<AstNode>|CollectionCallback<AstNode|Iterable<AstNode>, string>} content 插入内容
703
- * @param {...AstNode|Iterable<AstNode>} additional 更多插入内容
704
- */
705
- before(content, ...additional) {
706
- return this.#insert('before', content, ...additional);
707
- }
708
-
709
- /**
710
- * 在前方插入
711
- * @param {AstNode|Iterable<AstNode>|CollectionCallback<AstNode|Iterable<AstNode>, string>} content 插入内容
712
- * @param {...AstNode|Iterable<AstNode>} additional 更多插入内容
713
- */
714
- after(content, ...additional) {
715
- return this.#insert('after', content, ...additional);
716
- }
717
-
718
- /**
719
- * 替换所有子节点
720
- * @param {AstNode|Iterable<AstNode>|CollectionCallback<AstNode|Iterable<AstNode>, string>} content 插入内容
721
- * @param {...AstNode|Iterable<AstNode>} additional 更多插入内容
722
- */
723
- html(content) {
724
- if (content === undefined) {
725
- return this.toString();
726
- }
727
- return this.#insert('replaceChildren', content);
728
- }
729
-
730
- /**
731
- * 替换自身
732
- * @param {AstNode|Iterable<AstNode>|CollectionCallback<AstNode|Iterable<AstNode>, string>} content 插入内容
733
- * @param {...AstNode|Iterable<AstNode>} additional 更多插入内容
734
- */
735
- replaceWith(content) {
736
- return this.#insert('replaceWith', content);
737
- }
738
-
739
- /**
740
- * 移除自身
741
- * @param {string} selector
742
- */
743
- remove(selector) {
744
- this.removeData();
745
- for (const token of this) {
746
- if (token instanceof Token && token.matches(selector)) {
747
- token.remove();
748
- token.removeAllEventListeners();
749
- }
750
- }
751
- return this;
752
- }
753
-
754
- /**
755
- * 移除自身
756
- * @param {string} selector
757
- */
758
- detach(selector) {
759
- for (const token of this) {
760
- if (token instanceof Token && token.matches(selector)) {
761
- token.remove();
762
- }
763
- }
764
- return this;
765
- }
766
-
767
- /** 清空子节点 */
768
- empty() {
769
- for (const token of this.#tokens) {
770
- token.replaceChildren();
771
- }
772
- return this;
773
- }
774
-
775
- /**
776
- * 深拷贝
777
- * @param {boolean} withData 是否复制数据
778
- */
779
- clone(withData) {
780
- return new TokenCollection(this.#tokens.map(ele => {
781
- const cloned = ele.cloneNode();
782
- if (withData && cache.has(ele)) {
783
- cache.set(cloned, structuredClone(cache.get(ele)));
784
- }
785
- return cloned;
786
- }), this);
787
- }
788
-
789
- /**
790
- * 插入到
791
- * @param {string} method
792
- * @param {'append'|'prepend'|'before'|'after'|'replaceWith'} elementMethod 对应的AstElement方法
793
- * @param {Token|Iterable<Token>} target 目标位置
794
- */
795
- #insertAdjacent(method, elementMethod, target) {
796
- if (target instanceof Token) {
797
- target[elementMethod](...this);
798
- } else if (target?.[Symbol.iterator]) {
799
- for (const token of target) {
800
- if (token instanceof Token) {
801
- token[elementMethod](...this);
802
- }
803
- }
804
- } else {
805
- this.typeError(method, 'Token', 'Iterable');
806
- }
807
- return this;
808
- }
809
-
810
- /**
811
- * 插入到末尾
812
- * @param {Token|Iterable<Token>} target 目标位置
813
- */
814
- appendTo(target) {
815
- return this.#insertAdjacent('appendTo', 'append', target);
816
- }
817
-
818
- /**
819
- * 插入到开头
820
- * @param {Token|Iterable<Token>} target 目标位置
821
- */
822
- prependTo(target) {
823
- return this.#insertAdjacent('prependTo', 'prepend', target);
824
- }
825
-
826
- /**
827
- * 插入到前方
828
- * @param {Token|Iterable<Token>} target 目标位置
829
- */
830
- insertBefore(target) {
831
- return this.#insertAdjacent('insertBefore', 'before', target);
832
- }
833
-
834
- /**
835
- * 插入到后方
836
- * @param {Token|Iterable<Token>} target 目标位置
837
- */
838
- insertAfter(target) {
839
- return this.#insertAdjacent('insertAfter', 'after', target);
840
- }
841
-
842
- /**
843
- * 替换全部
844
- * @param {Token|Iterable<Token>} target 目标位置
845
- */
846
- replaceAll(target) {
847
- return this.#insertAdjacent('replaceAll', 'replaceWith', target);
848
- }
849
-
850
- /**
851
- * 获取几何属性
852
- * @param {string|string[]} key 属性键
853
- */
854
- css(key) {
855
- const /** @type {Record<string, number>} */ style = this.#firstToken?.style;
856
- if (typeof key === 'string') {
857
- return style?.[key];
858
- }
859
- return Array.isArray(key)
860
- ? Object.fromEntries(key.map(k => [k, style?.[k]]))
861
- : this.typeError('css', 'String', 'Array');
862
- }
863
-
864
- /**
865
- * 查询或修改值
866
- * @param {string|boolean|(string|boolean)[]|CollectionCallback<string, string|boolean>} value 值
867
- */
868
- val(value) {
869
- if (value === undefined) {
870
- const /** @type {{getValue: () => string|booleand}} */ firstToken = this.#firstToken;
871
- return firstToken?.getValue && firstToken.getValue();
872
- }
873
- let /** @type {(i: number, token: {getValue: () => string|boolean}) => string|boolean} */ getValue;
874
- if (typeof value === 'string' || typeof value === 'boolean') {
875
- getValue = /** @implements */ () => value;
876
- } else if (typeof value === 'function') {
877
- getValue = /** @implements */ (i, token) => value.call(token, i, token.getValue && token.getValue());
878
- } else if (Array.isArray(value)) {
879
- getValue = /** @implements */ i => value[i];
880
- } else {
881
- this.typeError('val', 'String', 'Array', 'Function');
882
- }
883
- for (let i = 0; i < this.length; i++) {
884
- const /** @type {{setValue: (value: string|boolean) => void}} */ token = this.array[i];
885
- if (token instanceof Token && typeof token.setValue === 'function' && token.setValue.length === 1) {
886
- token.setValue(getValue(i, token));
887
- }
888
- }
889
- return this;
890
- }
891
-
892
- /**
893
- * 查询或修改属性
894
- * @param {'getAttr'|'getAttribute'} getter 属性getter
895
- * @param {'setAttr'|'setAttribute'} setter 属性setter
896
- * @param {string|Record<string, string|boolean>} name 属性名
897
- * @param {string|boolean|CollectionCallback<string|boolean, string|boolean>} value 属性值
898
- */
899
- #attr(getter, setter, name, value) {
900
- if (typeof name === 'string' && value === undefined) {
901
- const firstToken = this.#firstToken;
902
- return firstToken?.[getter] && firstToken[getter](name);
903
- }
904
- for (let i = 0; i < this.length; i++) {
905
- const token = this.array[i];
906
- if (token instanceof Token && token[setter]) {
907
- if (typeof value === 'function') {
908
- token[setter](name, value.call(token, i, token[getter] && token[getter](name)));
909
- } else if (isPlainObject(name)) {
910
- for (const [k, v] of Object.entries(name)) {
911
- token[setter](k, v);
912
- }
913
- } else {
914
- token[setter](name, value);
915
- }
916
- }
917
- }
918
- return this;
919
- }
920
-
921
- /**
922
- * 标签属性
923
- * @param {string|Record<string, string|boolean>} name 属性名
924
- * @param {string|boolean|CollectionCallback<string|boolean, string|boolean>} value 属性值
925
- */
926
- attr(name, value) {
927
- return this.#attr('getAttr', 'setAttr', name, value);
928
- }
929
-
930
- /**
931
- * 节点属性
932
- * @param {string|Record<string, string|boolean>} name 属性名
933
- * @param {string|boolean|CollectionCallback<string|boolean, string|boolean>} value 属性值
934
- */
935
- prop(name, value) {
936
- return this.#attr('getAttribute', 'setAttribute', name, value);
937
- }
938
-
939
- /**
940
- * 移除属性
941
- * @param {'removeAttr'|'removeAttribute'} method
942
- * @param {string} name 属性名
943
- */
944
- #removeAttr(method, name) {
945
- for (const token of this) {
946
- if (token instanceof Token && token[method]) {
947
- token[method](name);
948
- }
949
- }
950
- return this;
951
- }
952
-
953
- /**
954
- * 标签属性
955
- * @param {string} name 属性名
956
- */
957
- removeAttr(name) {
958
- return this.#removeAttr('removeAttr', name);
959
- }
960
-
961
- /**
962
- * 节点属性
963
- * @param {string} name 属性名
964
- */
965
- removeProp(name) {
966
- return this.#removeAttr('removeAttribute', name);
967
- }
968
-
969
- /**
970
- * 添加class
971
- * @this {TokenCollection & {array: AttributeToken[]}}
972
- * @param {string|string[]|CollectionCallback<string|string[], string>} className 类名
973
- */
974
- addClass(className) {
975
- /** @type {CollectionCallback<string|string[], string>} */
976
- const callback = typeof className === 'function' ? className : () => className;
977
- for (let i = 0; i < this.length; i++) {
978
- const token = this.array[i],
979
- {classList} = token;
980
- if (classList) {
981
- const newClasses = callback.call(token, i, token.className);
982
- for (const newClass of Array.isArray(newClasses) ? newClasses : [newClasses]) {
983
- classList.add(newClass);
984
- }
985
- token.className = [...classList].join(' ');
986
- }
987
- }
988
- return this;
989
- }
990
-
991
- /**
992
- * 移除class
993
- * @this {TokenCollection & {array: AttributeToken[]}}
994
- * @param {undefined|string|string[]|CollectionCallback<undefined|string|string[], string>} className 类名
995
- */
996
- removeClass(className) {
997
- /** @type {CollectionCallback<undefined|string|string[], string>} */
998
- const callback = typeof className === 'function' ? className : () => className;
999
- for (let i = 0; i < this.length; i++) {
1000
- const token = this.array[i],
1001
- {classList} = token;
1002
- if (classList) {
1003
- const newClasses = callback.call(token, i, token.className);
1004
- if (newClasses === undefined) {
1005
- classList.clear();
1006
- } else {
1007
- for (const newClass of Array.isArray(newClasses) ? newClasses : [newClasses]) {
1008
- classList.delete(newClass);
1009
- }
1010
- }
1011
- token.className = [...classList].join(' ');
1012
- }
1013
- }
1014
- return this;
1015
- }
1016
-
1017
- /**
1018
- * 增减class
1019
- * @this {TokenCollection & {array: AttributeToken[]}}
1020
- * @param {string|string[]|CollectionCallback<string|string[], string>} className 类名
1021
- * @param {boolean} state 是否增删
1022
- */
1023
- toggleClass(className, state) {
1024
- if (typeof state === 'boolean') {
1025
- return this[state ? 'addClass' : 'removeClass'](className);
1026
- }
1027
- /** @type {CollectionCallback<string|string[], string>} */
1028
- const callback = typeof className === 'function' ? className : () => className;
1029
- for (let i = 0; i < this.length; i++) {
1030
- const token = this.array[i],
1031
- {classList} = token;
1032
- if (classList) {
1033
- const newClasses = callback.call(token, i, token.className);
1034
- for (const newClass of Array.isArray(newClasses) ? new Set(newClasses) : [newClasses]) {
1035
- classList[classList.has(newClass) ? 'delete' : 'add'](newClass);
1036
- }
1037
- token.className = [...classList].join(' ');
1038
- }
1039
- }
1040
- return this;
1041
- }
1042
-
1043
- /**
1044
- * 是否带有某class
1045
- * @this {{array: AttributeToken[]}}
1046
- * @param {string} className 类名
1047
- */
1048
- hasClass(className) {
1049
- return this.array.some(token => token.classList?.has(className));
1050
- }
1051
-
1052
- /**
1053
- * 全包围
1054
- * @param {string[]|CollectionCallback<string[], undefined>} wrapper 包裹
1055
- * @throws `Error` 不是连续的兄弟节点
1056
- */
1057
- wrapAll(wrapper) {
1058
- if (typeof wrapper !== 'function' && !Array.isArray(wrapper)) {
1059
- this.typeError('wrapAll', 'Array', 'Function');
1060
- }
1061
- const {array} = this,
1062
- [firstNode] = array,
1063
- /** @type {Token} */ ancestor = firstNode?.parentNode,
1064
- error = new Error('wrapAll 的主体应为普通Token的连续子节点!');
1065
- if (ancestor?.constructor !== Token) {
1066
- throw error;
1067
- }
1068
- const {childNodes} = ancestor,
1069
- i = childNodes.indexOf(firstNode),
1070
- j = childNodes.findIndex(node => node.contains(array.at(-1)));
1071
- if (j === -1 || childNodes.slice(i, j + 1).some(node => !array.includes(node))) {
1072
- throw error;
1073
- }
1074
- const [pre, post] = typeof wrapper === 'function' ? wrapper.call(firstNode) : wrapper,
1075
- config = ancestor.getAttribute('config'),
1076
- include = ancestor.getAttribute('include'),
1077
- token = new Token(`${pre}${String(childNodes.slice(i, j + 1))}${post}`, config).parse(undefined, include);
1078
- this.detach();
1079
- (childNodes[i - 1]?.after ?? ancestor.prepend)(...token.childNodes);
1080
- return this;
1081
- }
1082
-
1083
- /**
1084
- * 局部包裹
1085
- * @param {'html'|'replaceWith'} method
1086
- * @param {string} originalMethod 原方法
1087
- * @param {string[]|CollectionCallback<string[], undefined>} wrapper 包裹
1088
- * @returns {this}
1089
- */
1090
- #wrap(method, originalMethod, wrapper) {
1091
- if (typeof wrapper !== 'function' && !Array.isArray(wrapper)) {
1092
- this.typeError(originalMethod, 'Array', 'Function');
1093
- }
1094
-
1095
- /**
1096
- * @implements {CollectionCallback<AstNode|Iterable<AstNode>, string>}
1097
- * @this {Token}
1098
- * @param {number} i 序号
1099
- * @param {string} string 原文本
1100
- */
1101
- const callback = function(i, string) {
1102
- const [pre, post] = typeof wrapper === 'function' ? wrapper.call(this, i) : wrapper,
1103
- config = this.getAttribute('config'),
1104
- include = this.getAttribute('include');
1105
- return new Token(`${pre}${string}${post}`, config).parse(undefined, include).childNodes;
1106
- };
1107
- return this[method](callback);
1108
- }
1109
-
1110
- /**
1111
- * 包裹内部
1112
- * @param {string[]|CollectionCallback<string[], undefined>} wrapper 包裹
1113
- */
1114
- wrapInner(wrapper) {
1115
- return this.#wrap('html', 'wrapInner', wrapper);
1116
- }
1117
-
1118
- /**
1119
- * 包裹自身
1120
- * @param {string[]|CollectionCallback<string[], undefined>} wrapper 包裹
1121
- */
1122
- wrap(wrapper) {
1123
- return this.#wrap('replaceWith', 'wrap', wrapper);
1124
- }
1125
-
1126
- /** 相对于文档的位置 */
1127
- offset() {
1128
- const offset = this.#firstToken?.getBoundingClientRect();
1129
- return offset && {top: offset.top, left: offset.left};
1130
- }
1131
-
1132
- /** 相对于父容器的位置 */
1133
- position() {
1134
- const style = this.#firstToken?.style;
1135
- return style && {top: style.top, left: style.left};
1136
- }
1137
-
1138
- /** 高度 */
1139
- height() {
1140
- return this.#firstToken?.offsetHeight;
1141
- }
1142
-
1143
- /** 宽度 */
1144
- width() {
1145
- return this.#firstToken?.offsetWidth;
1146
- }
1147
-
1148
- /** 内部高度 */
1149
- innerHeight() {
1150
- return this.#firstToken?.clientHeight;
1151
- }
1152
-
1153
- /** 内部宽度 */
1154
- innerWidth() {
1155
- return this.#firstToken?.clientWidth;
1156
- }
1157
- }
1158
-
1159
- /**
1160
- * 代替TokenCollection构造器
1161
- * @param {AstNode|Iterable<AstNode>} arr 节点数组
1162
- */
1163
- const $ = arr => new TokenCollection(arr instanceof AstNode ? [arr] : arr);
1164
- /* eslint-disable func-names */
1165
- $.hasData = /** @param {Token} element */ function hasData(element) {
1166
- return element instanceof Token ? cache.has(element) : typeError(this, 'hasData', 'Token');
1167
- };
1168
- $.data = /** @type {function(Token, string, *): *} */ function data(element, key, value) {
1169
- if (!(element instanceof Token)) {
1170
- typeError(this, 'data', 'Token');
1171
- } else if (key === undefined) {
1172
- return cache.get(element);
1173
- } else if (typeof key !== 'string') {
1174
- typeError(this, 'data', 'String');
1175
- } else if (value === undefined) {
1176
- return cache.get(element)?.[key];
1177
- } else if (!cache.has(element)) {
1178
- cache.set(element, {});
1179
- }
1180
- cache.get(element)[key] = value;
1181
- return value;
1182
- };
1183
- $.removeData = /** @type {function(Token, string): void} */ function removeData(element, name) {
1184
- if (!(element instanceof Token)) {
1185
- typeError(this, 'removeData', 'Token');
1186
- } else if (name === undefined) {
1187
- cache.delete(element);
1188
- } else if (typeof name !== 'string') {
1189
- typeError(this, 'removeData', 'String');
1190
- } else if (cache.has(element)) {
1191
- const data = cache.get(element);
1192
- delete data[name];
1193
- }
1194
- };
1195
- /* eslint-enable func-names */
1196
- Object.defineProperty($, 'cache', {value: cache, enumerable: false, writable: false, configurable: false});
1197
-
1198
- Parser.tool.$ = __filename;
1199
- module.exports = $;