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