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