wikiparser-node 1.3.4-b → 1.3.6

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