wikiparser-node 0.11.0-m → 0.11.0

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