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