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