wikiparser-node 1.1.6 → 1.2.0-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 (154) hide show
  1. package/bundle/bundle.min.js +36 -0
  2. package/config/llwiki.json +1 -35
  3. package/config/moegirl.json +1 -44
  4. package/config/zhwiki.json +1 -466
  5. package/extensions/dist/base.js +68 -0
  6. package/extensions/dist/editor.js +159 -0
  7. package/extensions/dist/highlight.js +30 -0
  8. package/extensions/dist/lint.js +48 -0
  9. package/extensions/editor.css +63 -0
  10. package/extensions/ui.css +117 -0
  11. package/package.json +32 -12
  12. package/config/.schema.json +0 -172
  13. package/dist/addon/table.d.ts +0 -6
  14. package/dist/addon/table.js +0 -564
  15. package/dist/base.d.ts +0 -46
  16. package/dist/bin/toc.js +0 -18
  17. package/dist/index.d.ts +0 -31
  18. package/dist/index.js +0 -209
  19. package/dist/internal.d.ts +0 -44
  20. package/dist/lib/element.d.ts +0 -155
  21. package/dist/lib/element.js +0 -658
  22. package/dist/lib/node.d.ts +0 -146
  23. package/dist/lib/node.js +0 -419
  24. package/dist/lib/range.d.ts +0 -104
  25. package/dist/lib/range.js +0 -385
  26. package/dist/lib/ranges.d.ts +0 -26
  27. package/dist/lib/ranges.js +0 -117
  28. package/dist/lib/text.d.ts +0 -62
  29. package/dist/lib/text.js +0 -235
  30. package/dist/lib/title.d.ts +0 -38
  31. package/dist/lib/title.js +0 -162
  32. package/dist/mixin/attributesParent.js +0 -94
  33. package/dist/mixin/fixed.js +0 -32
  34. package/dist/mixin/flagsParent.js +0 -70
  35. package/dist/mixin/hidden.js +0 -22
  36. package/dist/mixin/magicLinkParent.js +0 -41
  37. package/dist/mixin/singleLine.js +0 -25
  38. package/dist/mixin/sol.js +0 -43
  39. package/dist/mixin/syntax.js +0 -56
  40. package/dist/parser/braces.js +0 -123
  41. package/dist/parser/commentAndExt.js +0 -69
  42. package/dist/parser/converter.js +0 -40
  43. package/dist/parser/externalLinks.js +0 -28
  44. package/dist/parser/hrAndDoubleUnderscore.js +0 -38
  45. package/dist/parser/html.js +0 -36
  46. package/dist/parser/links.js +0 -94
  47. package/dist/parser/list.js +0 -59
  48. package/dist/parser/magicLinks.js +0 -40
  49. package/dist/parser/quotes.js +0 -67
  50. package/dist/parser/selector.js +0 -163
  51. package/dist/parser/table.js +0 -112
  52. package/dist/src/arg.d.ts +0 -58
  53. package/dist/src/arg.js +0 -190
  54. package/dist/src/atom.d.ts +0 -12
  55. package/dist/src/atom.js +0 -27
  56. package/dist/src/attribute.d.ts +0 -86
  57. package/dist/src/attribute.js +0 -392
  58. package/dist/src/attributes.d.ts +0 -107
  59. package/dist/src/attributes.js +0 -344
  60. package/dist/src/converter.d.ts +0 -51
  61. package/dist/src/converter.js +0 -79
  62. package/dist/src/converterFlags.d.ts +0 -87
  63. package/dist/src/converterFlags.js +0 -223
  64. package/dist/src/converterRule.d.ts +0 -81
  65. package/dist/src/converterRule.js +0 -212
  66. package/dist/src/extLink.d.ts +0 -53
  67. package/dist/src/extLink.js +0 -121
  68. package/dist/src/gallery.d.ts +0 -55
  69. package/dist/src/gallery.js +0 -129
  70. package/dist/src/heading.d.ts +0 -72
  71. package/dist/src/heading.js +0 -128
  72. package/dist/src/hidden.d.ts +0 -21
  73. package/dist/src/hidden.js +0 -23
  74. package/dist/src/html.d.ts +0 -103
  75. package/dist/src/html.js +0 -230
  76. package/dist/src/imageParameter.d.ts +0 -66
  77. package/dist/src/imageParameter.js +0 -244
  78. package/dist/src/imagemap.d.ts +0 -57
  79. package/dist/src/imagemap.js +0 -148
  80. package/dist/src/imagemapLink.d.ts +0 -57
  81. package/dist/src/imagemapLink.js +0 -40
  82. package/dist/src/index.d.ts +0 -146
  83. package/dist/src/index.js +0 -777
  84. package/dist/src/link/base.d.ts +0 -52
  85. package/dist/src/link/base.js +0 -207
  86. package/dist/src/link/category.d.ts +0 -13
  87. package/dist/src/link/category.js +0 -29
  88. package/dist/src/link/file.d.ts +0 -96
  89. package/dist/src/link/file.js +0 -253
  90. package/dist/src/link/galleryImage.d.ts +0 -43
  91. package/dist/src/link/galleryImage.js +0 -106
  92. package/dist/src/link/index.d.ts +0 -53
  93. package/dist/src/link/index.js +0 -123
  94. package/dist/src/magicLink.d.ts +0 -69
  95. package/dist/src/magicLink.js +0 -148
  96. package/dist/src/nested.d.ts +0 -44
  97. package/dist/src/nested.js +0 -87
  98. package/dist/src/nowiki/base.d.ts +0 -46
  99. package/dist/src/nowiki/base.js +0 -42
  100. package/dist/src/nowiki/comment.d.ts +0 -39
  101. package/dist/src/nowiki/comment.js +0 -67
  102. package/dist/src/nowiki/dd.d.ts +0 -8
  103. package/dist/src/nowiki/dd.js +0 -25
  104. package/dist/src/nowiki/doubleUnderscore.d.ts +0 -43
  105. package/dist/src/nowiki/doubleUnderscore.js +0 -50
  106. package/dist/src/nowiki/hr.d.ts +0 -29
  107. package/dist/src/nowiki/hr.js +0 -14
  108. package/dist/src/nowiki/index.d.ts +0 -16
  109. package/dist/src/nowiki/index.js +0 -21
  110. package/dist/src/nowiki/list.d.ts +0 -29
  111. package/dist/src/nowiki/list.js +0 -48
  112. package/dist/src/nowiki/listBase.d.ts +0 -18
  113. package/dist/src/nowiki/listBase.js +0 -12
  114. package/dist/src/nowiki/noinclude.d.ts +0 -25
  115. package/dist/src/nowiki/noinclude.js +0 -25
  116. package/dist/src/nowiki/quote.d.ts +0 -22
  117. package/dist/src/nowiki/quote.js +0 -50
  118. package/dist/src/onlyinclude.d.ts +0 -16
  119. package/dist/src/onlyinclude.js +0 -57
  120. package/dist/src/paramTag/index.d.ts +0 -37
  121. package/dist/src/paramTag/index.js +0 -68
  122. package/dist/src/paramTag/inputbox.d.ts +0 -8
  123. package/dist/src/paramTag/inputbox.js +0 -23
  124. package/dist/src/parameter.d.ts +0 -84
  125. package/dist/src/parameter.js +0 -206
  126. package/dist/src/pre.d.ts +0 -28
  127. package/dist/src/pre.js +0 -51
  128. package/dist/src/syntax.d.ts +0 -24
  129. package/dist/src/syntax.js +0 -30
  130. package/dist/src/table/base.d.ts +0 -52
  131. package/dist/src/table/base.js +0 -78
  132. package/dist/src/table/index.d.ts +0 -229
  133. package/dist/src/table/index.js +0 -380
  134. package/dist/src/table/td.d.ts +0 -103
  135. package/dist/src/table/td.js +0 -261
  136. package/dist/src/table/tr.d.ts +0 -32
  137. package/dist/src/table/tr.js +0 -56
  138. package/dist/src/table/trBase.d.ts +0 -54
  139. package/dist/src/table/trBase.js +0 -159
  140. package/dist/src/tagPair/ext.d.ts +0 -58
  141. package/dist/src/tagPair/ext.js +0 -142
  142. package/dist/src/tagPair/include.d.ts +0 -52
  143. package/dist/src/tagPair/include.js +0 -58
  144. package/dist/src/tagPair/index.d.ts +0 -47
  145. package/dist/src/tagPair/index.js +0 -93
  146. package/dist/src/transclude.d.ts +0 -167
  147. package/dist/src/transclude.js +0 -689
  148. package/dist/util/constants.js +0 -108
  149. package/dist/util/debug.js +0 -64
  150. package/dist/util/diff.js +0 -72
  151. package/dist/util/lint.js +0 -48
  152. package/dist/util/string.js +0 -61
  153. package/errors/README +0 -1
  154. package/printed/README +0 -1
@@ -1,658 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AstElement = void 0;
4
- const fs = require("fs");
5
- const path = require("path");
6
- const string_1 = require("../util/string");
7
- const constants_1 = require("../util/constants");
8
- const selector_1 = require("../parser/selector");
9
- const ranges_1 = require("./ranges");
10
- const title_1 = require("./title");
11
- const Parser = require("../index");
12
- const node_1 = require("./node");
13
- const lintIgnoredExt = new Set([
14
- 'nowiki',
15
- 'pre',
16
- 'charinsert',
17
- 'score',
18
- 'syntaxhighlight',
19
- 'source',
20
- 'math',
21
- 'chem',
22
- 'ce',
23
- 'graph',
24
- 'mapframe',
25
- 'maplink',
26
- 'quiz',
27
- 'templatedata',
28
- 'timeline',
29
- ]);
30
- /* NOT FOR BROWSER */
31
- /**
32
- * optionally convert to lower cases
33
- * @param val 属性值
34
- * @param i 是否对大小写不敏感
35
- */
36
- const toCase = (val, i) => i ? val.toLowerCase() : val;
37
- /**
38
- * 检查某个下标是否符合表达式
39
- * @param str 表达式
40
- * @param i 待检查的下标
41
- */
42
- const nth = (str, i) => new ranges_1.Ranges(str.split(',')).applyTo(i + 1).includes(i);
43
- /**
44
- * 检测:lang()伪选择器
45
- * @param node 节点
46
- * @param node.attributes 节点属性
47
- * @param regex 语言正则
48
- */
49
- const matchesLang = ({ attributes }, regex) => {
50
- const lang = attributes?.['lang'];
51
- return typeof lang === 'string' && regex.test(lang);
52
- };
53
- /* NOT FOR BROWSER END */
54
- /** 类似HTMLElement */
55
- class AstElement extends node_1.AstNode {
56
- /** 子节点总数 */
57
- get length() {
58
- return this.childNodes.length;
59
- }
60
- /* NOT FOR BROWSER */
61
- /** 全部非文本子节点 */
62
- get children() {
63
- return this.childNodes.filter((child) => child.type !== 'text');
64
- }
65
- /** 首位非文本子节点 */
66
- get firstElementChild() {
67
- return this.childNodes.find((child) => child.type !== 'text');
68
- }
69
- /** 末位非文本子节点 */
70
- get lastElementChild() {
71
- return this.children.at(-1);
72
- }
73
- /** 非文本子节点总数 */
74
- get childElementCount() {
75
- return this.children.length;
76
- }
77
- /** 父节点 */
78
- get parentElement() {
79
- return this.parentNode;
80
- }
81
- /** AstElement.prototype.text()的getter写法 */
82
- get outerText() {
83
- return this.text();
84
- }
85
- /** 不可见 */
86
- get hidden() {
87
- return this.text() === '';
88
- }
89
- /** 后一个可见的兄弟节点 */
90
- get nextVisibleSibling() {
91
- let { nextSibling } = this;
92
- while (nextSibling?.text() === '') {
93
- ({ nextSibling } = nextSibling);
94
- }
95
- return nextSibling;
96
- }
97
- /** 前一个可见的兄弟节点 */
98
- get previousVisibleSibling() {
99
- let { previousSibling } = this;
100
- while (previousSibling?.text() === '') {
101
- ({ previousSibling } = previousSibling);
102
- }
103
- return previousSibling;
104
- }
105
- /** 内部高度 */
106
- get clientHeight() {
107
- const { innerText } = this;
108
- return typeof innerText === 'string' ? innerText.split('\n').length : undefined;
109
- }
110
- /** 内部宽度 */
111
- get clientWidth() {
112
- const { innerText } = this;
113
- return typeof innerText === 'string' ? innerText.split('\n').at(-1).length : undefined;
114
- }
115
- constructor() {
116
- super();
117
- this.seal('name');
118
- }
119
- /* NOT FOR BROWSER END */
120
- /**
121
- * 可见部分
122
- * @param separator 子节点间的连接符
123
- */
124
- text(separator) {
125
- return (0, string_1.text)(this.childNodes, separator);
126
- }
127
- /** 合并相邻的文本子节点 */
128
- normalize() {
129
- const childNodes = [...this.childNodes];
130
- for (let i = childNodes.length - 1; i >= 0; i--) {
131
- const { type, data } = childNodes[i], prev = childNodes[i - 1];
132
- if (type !== 'text' || this.getGaps(i - 1)) {
133
- //
134
- }
135
- else if (data === '') {
136
- childNodes.splice(i, 1);
137
- }
138
- else if (prev?.type === 'text') {
139
- prev.setAttribute('data', prev.data + data);
140
- childNodes.splice(i, 1);
141
- }
142
- }
143
- this.setAttribute('childNodes', childNodes);
144
- }
145
- /**
146
- * 移除子节点
147
- * @param i 移除位置
148
- */
149
- removeAt(i) {
150
- this.verifyChild(i);
151
- const childNodes = [...this.childNodes], [node] = childNodes.splice(i, 1);
152
- node.setAttribute('parentNode', undefined);
153
- this.setAttribute('childNodes', childNodes);
154
- return node;
155
- }
156
- /**
157
- * 插入子节点
158
- * @param node 待插入的子节点
159
- * @param i 插入位置
160
- * @throws `RangeError` 不能插入祖先或子节点
161
- */
162
- insertAt(node, i = this.length) {
163
- if (node.contains(this)) {
164
- Parser.error('不能插入祖先节点!', node);
165
- throw new RangeError('不能插入祖先节点!');
166
- }
167
- const childNodes = [...this.childNodes];
168
- if (childNodes.includes(node)) {
169
- Parser.error('不能插入子节点!', node);
170
- throw new RangeError('不能插入子节点!');
171
- }
172
- this.verifyChild(i, 1);
173
- node.parentNode?.removeChild(node);
174
- node.setAttribute('parentNode', this);
175
- childNodes.splice(i, 0, node);
176
- this.setAttribute('childNodes', childNodes);
177
- return node;
178
- }
179
- /**
180
- * 最近的祖先节点
181
- * @param selector 选择器
182
- */
183
- closest(selector) {
184
- let { parentNode } = this, condition;
185
- if (/[^a-z\-,\s]/u.test(selector)) {
186
- const stack = (0, selector_1.parseSelector)(selector);
187
- condition = /** @implements */ (token) => token.#matchesStack(stack);
188
- }
189
- else {
190
- const types = new Set(selector.split(',').map(str => str.trim()));
191
- condition = /** @implements */ (token) => types.has(token.type);
192
- }
193
- while (parentNode) {
194
- if (condition(parentNode)) {
195
- return parentNode;
196
- }
197
- ({ parentNode } = parentNode);
198
- }
199
- return undefined;
200
- }
201
- /**
202
- * 在末尾批量插入子节点
203
- * @param elements 插入节点
204
- */
205
- append(...elements) {
206
- for (const element of elements) {
207
- this.insertAt(element);
208
- }
209
- }
210
- /**
211
- * 批量替换子节点
212
- * @param elements 新的子节点
213
- */
214
- replaceChildren(...elements) {
215
- for (let i = this.length - 1; i >= 0; i--) {
216
- this.removeAt(i);
217
- }
218
- this.append(...elements);
219
- }
220
- /**
221
- * 修改文本子节点
222
- * @param str 新文本
223
- * @param i 子节点位置
224
- * @throws `RangeError` 对应位置的子节点不是文本节点
225
- */
226
- setText(str, i = 0) {
227
- this.verifyChild(i);
228
- const oldText = this.childNodes.at(i), { type, constructor: { name } } = oldText;
229
- if (type === 'text') {
230
- const { data } = oldText;
231
- oldText.replaceData(str);
232
- return data;
233
- }
234
- throw new RangeError(`第 ${i} 个子节点是 ${name}!`);
235
- }
236
- /** @private */
237
- toString(omit, separator = '') {
238
- return omit && this.matchesTypes(omit)
239
- ? ''
240
- : this.childNodes.map(child => child.toString(omit)).join(separator);
241
- }
242
- /**
243
- * @override
244
- * @param start
245
- */
246
- lint(start = this.getAbsoluteIndex()) {
247
- const { SyntaxToken } = require('../src/syntax');
248
- if (this instanceof SyntaxToken || this.constructor.hidden
249
- || this.type === 'ext-inner' && lintIgnoredExt.has(this.name)) {
250
- return [];
251
- }
252
- const errors = [];
253
- for (let i = 0, cur = start + this.getAttribute('padding'); i < this.length; i++) {
254
- const child = this.childNodes[i];
255
- errors.push(...child.lint(cur));
256
- cur += String(child).length + this.getGaps(i);
257
- }
258
- return errors;
259
- }
260
- /**
261
- * @override
262
- * @param opt 选项
263
- */
264
- print(opt = {}) {
265
- return String(this) ? `<span class="wpb-${opt.class ?? this.type}">${(0, string_1.print)(this.childNodes, opt)}</span>` : '';
266
- }
267
- /* NOT FOR BROWSER */
268
- /** @private */
269
- matchesTypes(types) {
270
- return types.has(this.type);
271
- }
272
- /**
273
- * 保存为JSON
274
- * @param file 文件名
275
- */
276
- json(file) {
277
- const json = {
278
- ...this,
279
- childNodes: this.childNodes.map(child => child.type === 'text' ? child.data : child.json()),
280
- };
281
- if (typeof file === 'string') {
282
- fs.writeFileSync(path.join(__dirname.slice(0, -4), '..', 'printed', `${file}${file.endsWith('.json') ? '' : '.json'}`), JSON.stringify(json, null, 2));
283
- }
284
- return json;
285
- }
286
- /** 销毁 */
287
- destroy() {
288
- this.parentNode?.destroy();
289
- for (const child of this.childNodes) {
290
- child.setAttribute('parentNode', undefined);
291
- }
292
- Object.setPrototypeOf(this, null);
293
- }
294
- /** 是否受保护。保护条件来自Token,这里仅提前用于:required和:optional伪选择器。 */
295
- #isProtected() {
296
- const { parentNode } = this;
297
- if (!parentNode) {
298
- return undefined;
299
- }
300
- const { childNodes, fixed } = parentNode, protectedIndices = parentNode.getAttribute('protectedChildren').applyTo(childNodes);
301
- return fixed || protectedIndices.includes(childNodes.indexOf(this));
302
- }
303
- /**
304
- * 检查是否符合属性选择器
305
- * @param key 属性键
306
- * @param equal 比较符
307
- * @param val 属性值
308
- * @param i 是否对大小写不敏感
309
- * @throws `RangeError` 复杂属性不能用于选择器
310
- */
311
- #matchesAttr(key, equal, val = '', i) {
312
- const isAttr = typeof this.hasAttr === 'function' && typeof this.getAttr === 'function';
313
- if (!(key in this) && (!isAttr || !this.hasAttr(key))) {
314
- return equal === '!=';
315
- }
316
- else if (!equal) {
317
- return true;
318
- }
319
- const v = toCase(val, i);
320
- let thisVal = this.getAttribute(key);
321
- if (isAttr) {
322
- const attr = this.getAttr(key);
323
- if (attr !== undefined) {
324
- thisVal = attr === true ? '' : attr;
325
- }
326
- }
327
- if (thisVal instanceof RegExp) {
328
- thisVal = thisVal.source;
329
- }
330
- if (equal === '~=') {
331
- const thisVals = typeof thisVal === 'string' ? thisVal.split(/\s/u) : thisVal;
332
- return Boolean(thisVals?.[Symbol.iterator])
333
- && [...thisVals].some(w => typeof w === 'string' && toCase(w, i) === v);
334
- }
335
- else if (typeof thisVal !== 'string') {
336
- throw new RangeError(`复杂属性 ${key} 不能用于选择器!`);
337
- }
338
- const stringVal = toCase(thisVal, i);
339
- switch (equal) {
340
- case '|=':
341
- return stringVal === v || stringVal.startsWith(`${v}-`);
342
- case '^=':
343
- return stringVal.startsWith(v);
344
- case '$=':
345
- return stringVal.endsWith(v);
346
- case '*=':
347
- return stringVal.includes(v);
348
- case '!=':
349
- return stringVal !== v;
350
- default: // `=`
351
- return stringVal === v;
352
- }
353
- }
354
- /**
355
- * 检查是否符合解析后的选择器,不含节点关系
356
- * @param step 解析后的选择器
357
- * @throws `SyntaxError` 错误的正则伪选择器
358
- * @throws `SyntaxError` 未定义的伪选择器
359
- */
360
- #matches(step) {
361
- const { parentNode, type, name, childNodes, link, fixed, constructor: { name: tokenName }, } = this, children = parentNode?.children, childrenOfType = children?.filter(({ type: t }) => t === type), siblingsCount = children?.length ?? 1, siblingsCountOfType = childrenOfType?.length ?? 1, index = (children?.indexOf(this) ?? 0) + 1, indexOfType = (childrenOfType?.indexOf(this) ?? 0) + 1, lastIndex = siblingsCount - index + 1, lastIndexOfType = siblingsCountOfType - indexOfType + 1;
362
- return step.every(selector => {
363
- if (typeof selector === 'string') {
364
- switch (selector) { // 情形1:简单伪选择器、type和name
365
- case '*':
366
- return true;
367
- case ':root':
368
- return !parentNode;
369
- case ':first-child':
370
- return index === 1;
371
- case ':first-of-type':
372
- return indexOfType === 1;
373
- case ':last-child':
374
- return lastIndex === 1;
375
- case ':last-of-type':
376
- return lastIndexOfType === 1;
377
- case ':only-child':
378
- return siblingsCount === 1;
379
- case ':only-of-type':
380
- return siblingsCountOfType === 1;
381
- case ':empty':
382
- return !childNodes.some(({ type: t, data }) => t !== 'text' || data);
383
- case ':parent':
384
- return childNodes.some(({ type: t, data }) => t !== 'text' || data);
385
- case ':header':
386
- return type === 'heading';
387
- case ':hidden':
388
- return this.text() === '';
389
- case ':visible':
390
- return this.text() !== '';
391
- case ':only-whitespace':
392
- return this.text().trim() === '';
393
- case ':any-link':
394
- return type === 'link' || type === 'free-ext-link' || type === 'ext-link'
395
- || (type === 'file' || type === 'gallery-image' && link);
396
- case ':local-link':
397
- return (type === 'link' || type === 'file' || type === 'gallery-image')
398
- && link instanceof title_1.Title && link.title === '';
399
- case ':read-only':
400
- return fixed;
401
- case ':read-write':
402
- return !fixed;
403
- case ':invalid':
404
- return type === 'table-inter' || tokenName === 'HiddenToken';
405
- case ':required':
406
- return this.#isProtected() === true;
407
- case ':optional':
408
- return this.#isProtected() === false;
409
- default: {
410
- const [t, n] = selector.split('#');
411
- return (!t || t === type || Boolean(constants_1.typeAliases[type]?.includes(t))) && (!n || n === name);
412
- }
413
- }
414
- }
415
- else if (selector.length === 4) { // 情形2:属性选择器
416
- return this.#matchesAttr(...selector);
417
- }
418
- const [s, pseudo] = selector; // 情形3:复杂伪选择器
419
- switch (pseudo) {
420
- case 'is':
421
- return this.matches(s);
422
- case 'not':
423
- return !this.matches(s);
424
- case 'nth-child':
425
- return nth(s, index);
426
- case 'nth-of-type':
427
- return nth(s, indexOfType);
428
- case 'nth-last-child':
429
- return nth(s, lastIndex);
430
- case 'nth-last-of-type':
431
- return nth(s, lastIndexOfType);
432
- case 'contains':
433
- return this.text().includes(s);
434
- case 'has':
435
- return Boolean(this.querySelector(s));
436
- case 'lang': {
437
- const regex = new RegExp(`^${s}(?:-|$)`, 'u');
438
- return matchesLang(this, regex)
439
- || this.getAncestors().some(ancestor => matchesLang(ancestor, regex));
440
- }
441
- case 'regex': {
442
- const mt = /^([^,]+),\s*\/(.+)\/([a-z]*)$/u.exec(s);
443
- if (!mt) {
444
- throw new SyntaxError('错误的伪选择器用法。请使用形如 ":regex(\'attr, /re/i\')" 的格式。');
445
- }
446
- try {
447
- return new RegExp(mt[2], mt[3]).test(String(this.getAttribute(mt[1].trim())));
448
- }
449
- catch {
450
- throw new SyntaxError(`错误的正则表达式:/${mt[2]}/${mt[3]}`);
451
- }
452
- }
453
- default:
454
- throw new SyntaxError(`未定义的伪选择器:${pseudo}`);
455
- }
456
- });
457
- }
458
- /**
459
- * 检查是否符合解析后的选择器
460
- * @param copy 解析后的选择器
461
- */
462
- #matchesArray(copy) {
463
- const condition = [...copy], step = condition.pop();
464
- if (this.#matches(step)) {
465
- const { parentNode, previousElementSibling } = this;
466
- switch (condition.at(-1)?.relation) {
467
- case undefined:
468
- return true;
469
- case '>':
470
- return Boolean(parentNode && parentNode.#matchesArray(condition));
471
- case '+':
472
- return Boolean(previousElementSibling && previousElementSibling.#matchesArray(condition));
473
- case '~': {
474
- if (!parentNode) {
475
- return false;
476
- }
477
- const { children } = parentNode;
478
- return children.slice(0, children.indexOf(this))
479
- .some(child => child.#matchesArray(condition));
480
- }
481
- default: // ' '
482
- return this.getAncestors().some(ancestor => ancestor.#matchesArray(condition));
483
- }
484
- }
485
- return false;
486
- }
487
- /**
488
- * 检查是否符合解析后的组合选择器
489
- * @param stack 解析后的一组选择器
490
- */
491
- #matchesStack(stack) {
492
- return stack.some(condition => this.#matchesArray(condition));
493
- }
494
- /**
495
- * 检查是否符合选择器
496
- * @param selector 选择器
497
- */
498
- matches(selector) {
499
- return selector === undefined || this.#matchesStack((0, selector_1.parseSelector)(selector));
500
- }
501
- /**
502
- * 符合条件的第一个后代节点
503
- * @param condition 条件
504
- */
505
- #getElementBy(condition) {
506
- for (const child of this.children) {
507
- if (condition(child)) {
508
- return child;
509
- }
510
- const descendant = child.#getElementBy(condition);
511
- if (descendant) {
512
- return descendant;
513
- }
514
- }
515
- return undefined;
516
- }
517
- /**
518
- * 符合选择器的第一个后代节点
519
- * @param selector 选择器
520
- */
521
- querySelector(selector) {
522
- const stack = (0, selector_1.parseSelector)(selector);
523
- return this.#getElementBy(token => token.#matchesStack(stack));
524
- }
525
- /**
526
- * 类型选择器
527
- * @param types
528
- */
529
- getElementByTypes(types) {
530
- const typeSet = new Set(types.split(',').map(str => str.trim()));
531
- return this.#getElementBy(({ type }) => typeSet.has(type));
532
- }
533
- /**
534
- * id选择器
535
- * @param id id名
536
- */
537
- getElementById(id) {
538
- return this.#getElementBy(token => 'id' in token && token.id === id);
539
- }
540
- /**
541
- * 符合条件的所有后代节点
542
- * @param condition 条件
543
- */
544
- #getElementsBy(condition) {
545
- const descendants = [];
546
- for (const child of this.children) {
547
- if (condition(child)) {
548
- descendants.push(child);
549
- }
550
- descendants.push(...child.#getElementsBy(condition));
551
- }
552
- return descendants;
553
- }
554
- /**
555
- * 符合选择器的所有后代节点
556
- * @param selector 选择器
557
- */
558
- querySelectorAll(selector) {
559
- const stack = (0, selector_1.parseSelector)(selector);
560
- return this.#getElementsBy(token => token.#matchesStack(stack));
561
- }
562
- /**
563
- * 类选择器
564
- * @param className 类名之一
565
- */
566
- getElementsByClassName(className) {
567
- return this.#getElementsBy(token => 'classList' in token && token.classList.has(className));
568
- }
569
- /**
570
- * 标签名选择器
571
- * @param tag 标签名
572
- */
573
- getElementsByTagName(tag) {
574
- return this.#getElementsBy(({ type, name }) => name === tag && (type === 'html' || type === 'ext'));
575
- }
576
- /**
577
- * 获取某一行的wikitext
578
- * @param n 行号
579
- */
580
- getLine(n) {
581
- return String(this).split('\n', n + 1)[n];
582
- }
583
- /**
584
- * 在开头批量插入子节点
585
- * @param elements 插入节点
586
- */
587
- prepend(...elements) {
588
- for (let i = 0; i < elements.length; i++) {
589
- this.insertAt(elements[i], i);
590
- }
591
- }
592
- /**
593
- * 获取子节点的位置
594
- * @param node 子节点
595
- * @throws `RangeError` 找不到子节点
596
- */
597
- #getChildIndex(node) {
598
- const i = this.childNodes.indexOf(node);
599
- if (i === -1) {
600
- Parser.error('找不到子节点!', node);
601
- throw new RangeError('找不到子节点!');
602
- }
603
- return i;
604
- }
605
- /**
606
- * 移除子节点
607
- * @param node 子节点
608
- */
609
- removeChild(node) {
610
- this.removeAt(this.#getChildIndex(node));
611
- return node;
612
- }
613
- /** @ignore */
614
- insertBefore(child, reference) {
615
- return reference === undefined
616
- ? this.insertAt(child)
617
- : this.insertAt(child, this.#getChildIndex(reference));
618
- }
619
- /**
620
- * 输出AST
621
- * @param depth 当前深度
622
- */
623
- echo(depth = 0) {
624
- const indent = ' '.repeat(depth), str = String(this), { childNodes, type, length } = this;
625
- if (childNodes.every(child => child.type === 'text' || !String(child))) {
626
- console.log(`${indent}\x1B[32m<%s>\x1B[0m${(0, string_1.noWrap)(str)}\x1B[32m</%s>\x1B[0m`, type, type);
627
- return;
628
- }
629
- Parser.info(`${indent}<${type}>`);
630
- let i = this.getAttribute('padding');
631
- if (i) {
632
- console.log(`${indent} ${(0, string_1.noWrap)(str.slice(0, i))}`);
633
- }
634
- for (let j = 0; j < length; j++) {
635
- const child = childNodes[j], childStr = String(child), gap = j === length - 1 ? 0 : this.getGaps(j);
636
- if (!childStr) {
637
- //
638
- }
639
- else if (child.type === 'text') {
640
- console.log(`${indent} ${(0, string_1.noWrap)(child.data)}`);
641
- }
642
- else {
643
- child.echo(depth + 1);
644
- }
645
- i += childStr.length;
646
- if (gap) {
647
- console.log(`${indent} ${(0, string_1.noWrap)(str.slice(i, i + gap))}`);
648
- i += gap;
649
- }
650
- }
651
- if (i < str.length) {
652
- console.log(`${indent} ${(0, string_1.noWrap)(str.slice(i))}`);
653
- }
654
- Parser.info(`${indent}</${type}>`);
655
- }
656
- }
657
- exports.AstElement = AstElement;
658
- constants_1.classes['AstElement'] = __filename;