wikiparser-node 1.13.1-b → 1.13.1

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 (174) hide show
  1. package/config/.schema.json +203 -0
  2. package/config/default.json +32 -31
  3. package/config/enwiki.json +829 -15
  4. package/config/llwiki.json +56 -21
  5. package/config/moegirl.json +65 -21
  6. package/config/zhwiki.json +498 -32
  7. package/dist/addon/table.js +494 -0
  8. package/dist/addon/token.js +392 -0
  9. package/dist/addon/transclude.js +184 -0
  10. package/dist/base.d.ts +110 -0
  11. package/dist/base.js +67 -0
  12. package/dist/index.d.ts +33 -0
  13. package/dist/index.js +279 -0
  14. package/dist/internal.d.ts +47 -0
  15. package/dist/lib/element.d.ts +125 -0
  16. package/dist/lib/element.js +364 -0
  17. package/dist/lib/node.d.ts +173 -0
  18. package/dist/lib/node.js +478 -0
  19. package/dist/lib/range.d.ts +105 -0
  20. package/dist/lib/range.js +406 -0
  21. package/dist/lib/ranges.d.ts +28 -0
  22. package/dist/lib/ranges.js +126 -0
  23. package/dist/lib/rect.d.ts +18 -0
  24. package/dist/lib/rect.js +36 -0
  25. package/dist/lib/text.d.ts +58 -0
  26. package/dist/lib/text.js +414 -0
  27. package/dist/lib/title.d.ts +49 -0
  28. package/dist/lib/title.js +251 -0
  29. package/dist/mixin/attributesParent.d.ts +49 -0
  30. package/dist/mixin/attributesParent.js +80 -0
  31. package/dist/mixin/fixed.d.ts +5 -0
  32. package/dist/mixin/fixed.js +32 -0
  33. package/dist/mixin/flagsParent.d.ts +43 -0
  34. package/dist/mixin/flagsParent.js +64 -0
  35. package/dist/mixin/hidden.d.ts +7 -0
  36. package/dist/mixin/hidden.js +39 -0
  37. package/dist/mixin/magicLinkParent.d.ts +19 -0
  38. package/dist/mixin/magicLinkParent.js +43 -0
  39. package/dist/mixin/singleLine.d.ts +5 -0
  40. package/dist/mixin/singleLine.js +25 -0
  41. package/dist/mixin/sol.d.ts +6 -0
  42. package/dist/mixin/sol.js +45 -0
  43. package/dist/mixin/syntax.d.ts +8 -0
  44. package/dist/mixin/syntax.js +46 -0
  45. package/dist/parser/braces.js +152 -0
  46. package/dist/parser/commentAndExt.js +84 -0
  47. package/dist/parser/converter.js +41 -0
  48. package/dist/parser/externalLinks.js +39 -0
  49. package/dist/parser/hrAndDoubleUnderscore.js +44 -0
  50. package/dist/parser/html.js +40 -0
  51. package/dist/parser/links.js +103 -0
  52. package/dist/parser/list.js +116 -0
  53. package/dist/parser/magicLinks.js +55 -0
  54. package/dist/parser/quotes.js +69 -0
  55. package/dist/parser/redirect.js +28 -0
  56. package/dist/parser/selector.js +443 -0
  57. package/dist/parser/table.js +125 -0
  58. package/dist/src/arg.d.ts +49 -0
  59. package/dist/src/arg.js +220 -0
  60. package/dist/src/atom.d.ts +14 -0
  61. package/dist/src/atom.js +54 -0
  62. package/dist/src/attribute.d.ts +64 -0
  63. package/dist/src/attribute.js +482 -0
  64. package/dist/src/attributes.d.ts +103 -0
  65. package/dist/src/attributes.js +383 -0
  66. package/dist/src/converter.d.ts +28 -0
  67. package/dist/src/converter.js +153 -0
  68. package/dist/src/converterFlags.d.ts +80 -0
  69. package/dist/src/converterFlags.js +241 -0
  70. package/dist/src/converterRule.d.ts +71 -0
  71. package/dist/src/converterRule.js +218 -0
  72. package/dist/src/extLink.d.ts +36 -0
  73. package/dist/src/extLink.js +223 -0
  74. package/dist/src/gallery.d.ts +51 -0
  75. package/dist/src/gallery.js +166 -0
  76. package/dist/src/heading.d.ts +44 -0
  77. package/dist/src/heading.js +222 -0
  78. package/dist/src/hidden.d.ts +9 -0
  79. package/dist/src/hidden.js +82 -0
  80. package/dist/src/html.d.ts +61 -0
  81. package/dist/src/html.js +344 -0
  82. package/dist/src/imageParameter.d.ts +60 -0
  83. package/dist/src/imageParameter.js +262 -0
  84. package/dist/src/imagemap.d.ts +47 -0
  85. package/dist/src/imagemap.js +148 -0
  86. package/dist/src/imagemapLink.d.ts +35 -0
  87. package/dist/src/imagemapLink.js +99 -0
  88. package/dist/src/index.d.ts +136 -0
  89. package/dist/src/index.js +790 -0
  90. package/dist/src/link/base.d.ts +52 -0
  91. package/dist/src/link/base.js +258 -0
  92. package/dist/src/link/category.d.ts +22 -0
  93. package/dist/src/link/category.js +36 -0
  94. package/dist/src/link/file.d.ts +102 -0
  95. package/dist/src/link/file.js +345 -0
  96. package/dist/src/link/galleryImage.d.ts +29 -0
  97. package/dist/src/link/galleryImage.js +133 -0
  98. package/dist/src/link/index.d.ts +39 -0
  99. package/dist/src/link/index.js +100 -0
  100. package/dist/src/link/redirectTarget.d.ts +27 -0
  101. package/dist/src/link/redirectTarget.js +71 -0
  102. package/dist/src/magicLink.d.ts +57 -0
  103. package/dist/src/magicLink.js +261 -0
  104. package/dist/src/nested.d.ts +40 -0
  105. package/dist/src/nested.js +108 -0
  106. package/dist/src/nowiki/base.d.ts +28 -0
  107. package/dist/src/nowiki/base.js +90 -0
  108. package/dist/src/nowiki/comment.d.ts +14 -0
  109. package/dist/src/nowiki/comment.js +123 -0
  110. package/dist/src/nowiki/dd.d.ts +8 -0
  111. package/dist/src/nowiki/dd.js +74 -0
  112. package/dist/src/nowiki/doubleUnderscore.d.ts +15 -0
  113. package/dist/src/nowiki/doubleUnderscore.js +101 -0
  114. package/dist/src/nowiki/hr.d.ts +5 -0
  115. package/dist/src/nowiki/hr.js +72 -0
  116. package/dist/src/nowiki/index.d.ts +14 -0
  117. package/dist/src/nowiki/index.js +30 -0
  118. package/dist/src/nowiki/list.d.ts +5 -0
  119. package/dist/src/nowiki/list.js +67 -0
  120. package/dist/src/nowiki/listBase.d.ts +23 -0
  121. package/dist/src/nowiki/listBase.js +100 -0
  122. package/dist/src/nowiki/noinclude.d.ts +6 -0
  123. package/dist/src/nowiki/noinclude.js +77 -0
  124. package/dist/src/nowiki/quote.d.ts +14 -0
  125. package/dist/src/nowiki/quote.js +149 -0
  126. package/dist/src/onlyinclude.d.ts +13 -0
  127. package/dist/src/onlyinclude.js +60 -0
  128. package/dist/src/paramTag/index.d.ts +28 -0
  129. package/dist/src/paramTag/index.js +80 -0
  130. package/dist/src/paramTag/inputbox.d.ts +8 -0
  131. package/dist/src/paramTag/inputbox.js +38 -0
  132. package/dist/src/parameter.d.ts +60 -0
  133. package/dist/src/parameter.js +267 -0
  134. package/dist/src/pre.d.ts +28 -0
  135. package/dist/src/pre.js +70 -0
  136. package/dist/src/redirect.d.ts +30 -0
  137. package/dist/src/redirect.js +128 -0
  138. package/dist/src/syntax.d.ts +15 -0
  139. package/dist/src/syntax.js +87 -0
  140. package/dist/src/table/base.d.ts +28 -0
  141. package/dist/src/table/base.js +81 -0
  142. package/dist/src/table/index.d.ts +230 -0
  143. package/dist/src/table/index.js +506 -0
  144. package/dist/src/table/td.d.ts +72 -0
  145. package/dist/src/table/td.js +375 -0
  146. package/dist/src/table/tr.d.ts +30 -0
  147. package/dist/src/table/tr.js +61 -0
  148. package/dist/src/table/trBase.d.ts +49 -0
  149. package/dist/src/table/trBase.js +165 -0
  150. package/dist/src/tagPair/ext.d.ts +29 -0
  151. package/dist/src/tagPair/ext.js +229 -0
  152. package/dist/src/tagPair/include.d.ts +33 -0
  153. package/dist/src/tagPair/include.js +145 -0
  154. package/dist/src/tagPair/index.d.ts +23 -0
  155. package/dist/src/tagPair/index.js +130 -0
  156. package/dist/src/transclude.d.ts +159 -0
  157. package/dist/src/transclude.js +598 -0
  158. package/dist/util/constants.js +26 -0
  159. package/dist/util/debug.js +95 -0
  160. package/dist/util/diff.js +83 -0
  161. package/dist/util/html.js +146 -0
  162. package/dist/util/lint.js +32 -0
  163. package/dist/util/string.js +107 -0
  164. package/errors/README +3 -0
  165. package/package.json +21 -28
  166. package/printed/README +3 -0
  167. package/bundle/bundle.min.js +0 -37
  168. package/extensions/dist/base.js +0 -163
  169. package/extensions/dist/codejar.js +0 -53
  170. package/extensions/dist/editor.js +0 -159
  171. package/extensions/dist/highlight.js +0 -30
  172. package/extensions/dist/lint.js +0 -72
  173. package/extensions/editor.css +0 -59
  174. package/extensions/ui.css +0 -162
@@ -0,0 +1,790 @@
1
+ "use strict";
2
+ // PHP解析器的步骤:
3
+ // -1. 替换签名和`{{subst:}}`,参见Parser::preSaveTransform;这在revision中不可能保留,可以跳过
4
+ // 0. 移除特定字符`\0`和`\x7F`,参见Parser::parse
5
+ // 1. 注释/扩展标签('<'相关),参见Preprocessor_Hash::buildDomTreeArrayFromText和Sanitizer::decodeTagAttributes
6
+ // 2. 模板/模板变量/标题,注意rightmost法则,以及`-{`和`[[`可以破坏`{{`或`{{{`语法,
7
+ // 参见Preprocessor_Hash::buildDomTreeArrayFromText
8
+ // 3. HTML标签(允许不匹配),参见Sanitizer::internalRemoveHtmlTags
9
+ // 4. 表格,参见Parser::handleTables
10
+ // 5. 水平线、状态开关和余下的标题,参见Parser::internalParse
11
+ // 6. 内链,含文件和分类,参见Parser::handleInternalLinks2
12
+ // 7. `'`,参见Parser::doQuotes
13
+ // 8. 外链,参见Parser::handleExternalLinks
14
+ // 9. ISBN、RFC(未来将废弃,不予支持)和自由外链,参见Parser::handleMagicLinks
15
+ // 10. 段落和列表,参见BlockLevelPass::execute
16
+ // 11. 转换,参见LanguageConverter::recursiveConvertTopLevel
17
+ // \0\d+.\x7F标记Token:
18
+ // !: `{{!}}`专用
19
+ // {: `{{(!}}`专用
20
+ // }: `{{!)}}`专用
21
+ // -: `{{!-}}`专用
22
+ // +: `{{!!}}`专用
23
+ // ~: `{{=}}`专用
24
+ // a: AttributeToken
25
+ // b: TableToken
26
+ // c: CommentToke
27
+ // d: ListToken
28
+ // e: ExtToken
29
+ // f: MagicLinkToken inside ImageParameterToken
30
+ // h: HeadingToken
31
+ // i: RFC/PMID/ISBN
32
+ // l: LinkToken
33
+ // m: `{{fullurl:}}`、`{{canonicalurl:}}`或`{{filepath:}}`
34
+ // n: NoIncludeToken和IncludeToken
35
+ // o: RedirectToken
36
+ // q: QuoteToken
37
+ // r: HrToken
38
+ // s: `{{{|subst:}}}`
39
+ // t: ArgToken或TranscludeToken
40
+ // u: `__toc__`
41
+ // v: ConverterToken
42
+ // w: ExtLinkToken
43
+ // x: HtmlToken
44
+ Object.defineProperty(exports, "__esModule", { value: true });
45
+ exports.Token = void 0;
46
+ const string_1 = require("../util/string");
47
+ const constants_1 = require("../util/constants");
48
+ const debug_1 = require("../util/debug");
49
+ const lint_1 = require("../util/lint");
50
+ const index_1 = require("../index");
51
+ const element_1 = require("../lib/element");
52
+ const text_1 = require("../lib/text");
53
+ /* NOT FOR BROWSER */
54
+ const assert = require("assert/strict");
55
+ const html_1 = require("../util/html");
56
+ const ranges_1 = require("../lib/ranges");
57
+ const range_1 = require("../lib/range");
58
+ /**
59
+ * 可接受的Token类型
60
+ * @param value 可接受的Token类型
61
+ */
62
+ const getAcceptable = (value) => {
63
+ const acceptable = {};
64
+ for (const [k, v] of Object.entries(value)) {
65
+ if (k.startsWith('Stage-')) {
66
+ for (let i = 0; i <= Number(k.slice(6)); i++) {
67
+ for (const type of constants_1.aliases[i]) {
68
+ acceptable[type] = new ranges_1.Ranges(v);
69
+ }
70
+ }
71
+ }
72
+ else if (k.startsWith('!')) { // `!`项必须放在最后
73
+ delete acceptable[k.slice(1)];
74
+ }
75
+ else {
76
+ acceptable[k] = new ranges_1.Ranges(v);
77
+ }
78
+ }
79
+ return acceptable;
80
+ };
81
+ /* NOT FOR BROWSER END */
82
+ /**
83
+ * 所有节点的基类
84
+ * @classdesc `{childNodes: ...(AstText|Token)}`
85
+ */
86
+ class Token extends element_1.AstElement {
87
+ #type = 'plain';
88
+ /** 解析阶段,参见顶部注释。只对plain Token有意义。 */
89
+ #stage = 0;
90
+ #config;
91
+ /** 这个数组起两个作用:1. 数组中的Token会在build时替换`/\0\d+.\x7F/`标记;2. 数组中的Token会依次执行parseOnce和build方法。 */
92
+ #accum;
93
+ #include;
94
+ #built = false;
95
+ #string;
96
+ /* NOT FOR BROWSER */
97
+ #acceptable;
98
+ #protectedChildren = new ranges_1.Ranges();
99
+ /* NOT FOR BROWSER END */
100
+ get type() {
101
+ return this.#type;
102
+ }
103
+ set type(value) {
104
+ /* NOT FOR BROWSER */
105
+ const plainTypes = [
106
+ 'plain',
107
+ 'root',
108
+ 'table-inter',
109
+ 'arg-default',
110
+ 'attr-value',
111
+ 'ext-link-text',
112
+ 'heading-title',
113
+ 'parameter-key',
114
+ 'parameter-value',
115
+ 'link-text',
116
+ 'td-inner',
117
+ 'ext-inner',
118
+ 'list-range',
119
+ ];
120
+ if (!plainTypes.includes(value)) {
121
+ throw new RangeError(`"${value}" is not a valid type for ${this.constructor.name}!`);
122
+ }
123
+ /* NOT FOR BROWSER END */
124
+ this.#type = value;
125
+ }
126
+ /* NOT FOR BROWSER */
127
+ /** 所有图片,包括图库 */
128
+ get images() {
129
+ return this.querySelectorAll('file,gallery-image,imagemap-image');
130
+ }
131
+ /** 所有内链、外链和自由外链 */
132
+ get links() {
133
+ return this.querySelectorAll('link,redirect-target,ext-link,free-ext-link,magic-link,image-parameter#link');
134
+ }
135
+ /** 所有模板和模块 */
136
+ get embeds() {
137
+ return this.querySelectorAll('template,magic-word#invoke');
138
+ }
139
+ /* NOT FOR BROWSER END */
140
+ /** @class */
141
+ constructor(wikitext, config = index_1.default.getConfig(), accum = [], acceptable) {
142
+ super();
143
+ if (typeof wikitext === 'string') {
144
+ this.insertAt(wikitext);
145
+ }
146
+ this.#config = config;
147
+ this.#accum = accum;
148
+ accum.push(this);
149
+ /* NOT FOR BROWSER */
150
+ this.setAttribute('acceptable', acceptable);
151
+ }
152
+ /** @private */
153
+ parseOnce(n = this.#stage, include = false) {
154
+ if (n < this.#stage || this.length === 0 || !this.getAttribute('plain')) {
155
+ return this;
156
+ }
157
+ else if (this.#stage >= constants_1.MAX_STAGE) {
158
+ /* NOt FOR BROWSER */
159
+ if (this.type === 'root') {
160
+ index_1.default.error('Fully parsed!');
161
+ }
162
+ /* NOT FOR BROWSER END */
163
+ return this;
164
+ }
165
+ switch (n) {
166
+ case 0:
167
+ if (this.type === 'root') {
168
+ this.#accum.pop();
169
+ const isRedirect = this.#parseRedirect();
170
+ include &&= !isRedirect;
171
+ }
172
+ this.#include = include;
173
+ this.#parseCommentAndExt(include);
174
+ break;
175
+ case 1:
176
+ this.#parseBraces();
177
+ break;
178
+ case 2:
179
+ this.#parseHtml();
180
+ break;
181
+ case 3:
182
+ this.#parseTable();
183
+ break;
184
+ case 4:
185
+ this.#parseHrAndDoubleUnderscore();
186
+ break;
187
+ case 5:
188
+ this.#parseLinks();
189
+ break;
190
+ case 6:
191
+ this.#parseQuotes();
192
+ break;
193
+ case 7:
194
+ this.#parseExternalLinks();
195
+ break;
196
+ case 8:
197
+ this.#parseMagicLinks();
198
+ break;
199
+ case 9:
200
+ this.#parseList();
201
+ break;
202
+ case 10:
203
+ this.#parseConverter();
204
+ // no default
205
+ }
206
+ if (this.type === 'root') {
207
+ for (const token of this.#accum) {
208
+ token?.parseOnce(n, include); // eslint-disable-line @typescript-eslint/no-unnecessary-condition
209
+ }
210
+ }
211
+ this.#stage++;
212
+ return this;
213
+ }
214
+ /** @private */
215
+ buildFromStr(str, type) {
216
+ const nodes = str.split(/[\0\x7F]/u).map((s, i) => {
217
+ if (i % 2 === 0) {
218
+ return new text_1.AstText(s);
219
+ }
220
+ else if (isNaN(s.slice(-1))) {
221
+ return this.#accum[Number(s.slice(0, -1))];
222
+ }
223
+ throw new Error(`Failed to build! Unrecognized token: ${s}`);
224
+ });
225
+ if (type === constants_1.BuildMethod.String) {
226
+ return nodes.map(String).join('');
227
+ }
228
+ else if (type === constants_1.BuildMethod.Text) {
229
+ return (0, string_1.text)(nodes);
230
+ }
231
+ return nodes;
232
+ }
233
+ /** @private */
234
+ build() {
235
+ this.#stage = constants_1.MAX_STAGE;
236
+ const { length, firstChild } = this, str = firstChild?.toString();
237
+ if (length === 1 && firstChild.type === 'text' && str.includes('\0')) {
238
+ this.replaceChildren(...this.buildFromStr(str));
239
+ this.normalize();
240
+ if (this.type === 'root') {
241
+ for (const token of this.#accum) {
242
+ token?.build(); // eslint-disable-line @typescript-eslint/no-unnecessary-condition
243
+ }
244
+ }
245
+ }
246
+ }
247
+ /** @private */
248
+ afterBuild() {
249
+ if (this.type === 'root') {
250
+ for (const token of this.#accum) {
251
+ token?.afterBuild(); // eslint-disable-line @typescript-eslint/no-unnecessary-condition
252
+ }
253
+ }
254
+ this.#built = true;
255
+ }
256
+ /** @private */
257
+ parse(n = constants_1.MAX_STAGE, include) {
258
+ n = Math.min(n, constants_1.MAX_STAGE);
259
+ while (this.#stage < n) {
260
+ this.parseOnce(this.#stage, include);
261
+ }
262
+ if (n) {
263
+ this.build();
264
+ this.afterBuild();
265
+ }
266
+ return this;
267
+ }
268
+ /** 解析重定向 */
269
+ #parseRedirect() {
270
+ const { parseRedirect } = require('../parser/redirect');
271
+ const wikitext = this.firstChild.toString(), parsed = parseRedirect(wikitext, this.#config, this.#accum);
272
+ if (parsed) {
273
+ this.setText(parsed);
274
+ }
275
+ return Boolean(parsed);
276
+ }
277
+ /**
278
+ * 解析HTML注释和扩展标签
279
+ * @param includeOnly 是否嵌入
280
+ */
281
+ #parseCommentAndExt(includeOnly) {
282
+ const { parseCommentAndExt } = require('../parser/commentAndExt');
283
+ this.setText(parseCommentAndExt(this.firstChild.toString(), this.#config, this.#accum, includeOnly));
284
+ }
285
+ /** 解析花括号 */
286
+ #parseBraces() {
287
+ const { parseBraces } = require('../parser/braces');
288
+ const str = this.type === 'root' ? this.firstChild.toString() : `\0${this.firstChild.toString()}`, parsed = parseBraces(str, this.#config, this.#accum);
289
+ this.setText(this.type === 'root' ? parsed : parsed.slice(1));
290
+ }
291
+ /** 解析HTML标签 */
292
+ #parseHtml() {
293
+ if (this.#config.excludes?.includes('html')) {
294
+ return;
295
+ }
296
+ const { parseHtml } = require('../parser/html');
297
+ this.setText(parseHtml(this.firstChild.toString(), this.#config, this.#accum));
298
+ }
299
+ /** 解析表格 */
300
+ #parseTable() {
301
+ if (this.#config.excludes?.includes('table')) {
302
+ return;
303
+ }
304
+ const { parseTable } = require('../parser/table');
305
+ this.setText(parseTable(this, this.#config, this.#accum));
306
+ }
307
+ /** 解析`<hr>`和状态开关 */
308
+ #parseHrAndDoubleUnderscore() {
309
+ if (this.#config.excludes?.includes('hr')) {
310
+ return;
311
+ }
312
+ const { parseHrAndDoubleUnderscore } = require('../parser/hrAndDoubleUnderscore');
313
+ this.setText(parseHrAndDoubleUnderscore(this, this.#config, this.#accum));
314
+ }
315
+ /** 解析内部链接 */
316
+ #parseLinks() {
317
+ const { parseLinks } = require('../parser/links');
318
+ this.setText(parseLinks(this.firstChild.toString(), this.#config, this.#accum));
319
+ }
320
+ /** 解析单引号 */
321
+ #parseQuotes() {
322
+ if (this.#config.excludes?.includes('quote')) {
323
+ return;
324
+ }
325
+ const { parseQuotes } = require('../parser/quotes');
326
+ const lines = this.firstChild.toString().split('\n');
327
+ for (let i = 0; i < lines.length; i++) {
328
+ lines[i] = parseQuotes(lines[i], this.#config, this.#accum);
329
+ }
330
+ this.setText(lines.join('\n'));
331
+ }
332
+ /** 解析外部链接 */
333
+ #parseExternalLinks() {
334
+ if (this.#config.excludes?.includes('extLink')) {
335
+ return;
336
+ }
337
+ const { parseExternalLinks } = require('../parser/externalLinks');
338
+ this.setText(parseExternalLinks(this.firstChild.toString(), this.#config, this.#accum));
339
+ }
340
+ /** 解析自由外链 */
341
+ #parseMagicLinks() {
342
+ if (this.#config.excludes?.includes('magicLink')) {
343
+ return;
344
+ }
345
+ const { parseMagicLinks } = require('../parser/magicLinks');
346
+ this.setText(parseMagicLinks(this.firstChild.toString(), this.#config, this.#accum));
347
+ }
348
+ /** 解析列表 */
349
+ #parseList() {
350
+ if (this.#config.excludes?.includes('list')) {
351
+ return;
352
+ }
353
+ const { parseList } = require('../parser/list');
354
+ const lines = this.firstChild.toString().split('\n'), state = { lastPrefix: '' };
355
+ let i = this.type === 'root' || this.type === 'ext-inner' && this.name === 'poem' ? 0 : 1;
356
+ for (; i < lines.length; i++) {
357
+ lines[i] = parseList(lines[i], state, this.#config, this.#accum);
358
+ }
359
+ this.setText(lines.join('\n'));
360
+ }
361
+ /** 解析语言变体转换 */
362
+ #parseConverter() {
363
+ if (this.#config.variants.length > 0) {
364
+ const { parseConverter } = require('../parser/converter');
365
+ this.setText(parseConverter(this.firstChild.toString(), this.#config, this.#accum));
366
+ }
367
+ }
368
+ /** @private */
369
+ getAttribute(key) {
370
+ switch (key) {
371
+ case 'plain':
372
+ return (this.constructor === Token);
373
+ case 'config':
374
+ return this.#config;
375
+ case 'include':
376
+ return (this.#include ?? Boolean(this.getRootNode().#include));
377
+ case 'accum':
378
+ return this.#accum;
379
+ case 'built':
380
+ return this.#built;
381
+ /* NOT FOR BROWSER */
382
+ case 'stage':
383
+ return this.#stage;
384
+ case 'protectedChildren':
385
+ return this.#protectedChildren;
386
+ /* NOT FOR BROWSER END */
387
+ default:
388
+ return super.getAttribute(key);
389
+ }
390
+ }
391
+ /** @private */
392
+ setAttribute(key, value) {
393
+ switch (key) {
394
+ case 'stage':
395
+ if (this.#stage === 0 && this.type === 'root') {
396
+ this.#accum.shift();
397
+ }
398
+ this.#stage = value;
399
+ break;
400
+ /* NOT FOR BROWSER */
401
+ case 'acceptable':
402
+ this.#acceptable = value && (() => getAcceptable(value));
403
+ break;
404
+ /* NOT FOR BROWSER END */
405
+ default:
406
+ super.setAttribute(key, value);
407
+ }
408
+ }
409
+ insertAt(child, i = this.length) {
410
+ const token = typeof child === 'string' ? new text_1.AstText(child) : child;
411
+ /* NOT FOR BROWSER */
412
+ const acceptable = this.getAcceptable();
413
+ if (!debug_1.Shadow.running && acceptable) {
414
+ const acceptableIndices = Object.fromEntries(Object.entries(acceptable).map(([str, ranges]) => [str, ranges.applyTo(this.length + 1)])), nodesAfter = this.childNodes.slice(i), insertedName = token.constructor.name;
415
+ i += i < 0 ? this.length : 0;
416
+ if (!acceptableIndices[insertedName]?.includes(i)) {
417
+ this.constructorError(`cannot insert a ${insertedName} at position ${i}`);
418
+ }
419
+ else if (nodesAfter.some(({ constructor: { name } }, j) => !acceptableIndices[name]?.includes(i + j + 1))) {
420
+ this.constructorError(`violates the order of acceptable nodes by inserting a child node at position ${i}`);
421
+ }
422
+ }
423
+ /* NOT FOR BROWSER END */
424
+ super.insertAt(token, i);
425
+ /* NOT FOR BROWSER */
426
+ const e = new Event('insert', { bubbles: true });
427
+ this.dispatchEvent(e, { type: 'insert', position: i < 0 ? i + this.length - 1 : i });
428
+ if (token.type !== 'list-range' && token.constructor === Token && this.getAttribute('plain')) {
429
+ index_1.default.warn('You are inserting a plain token as a child of another plain token. '
430
+ + 'Consider calling Token.flatten method afterwards.');
431
+ }
432
+ /* NOT FOR BROWSER END */
433
+ if (token.type === 'root') {
434
+ token.type = 'plain';
435
+ }
436
+ return token;
437
+ }
438
+ /** @private */
439
+ normalizeTitle(title, defaultNs = 0, halfParsed, decode, selfLink) {
440
+ return index_1.default.normalizeTitle(title, defaultNs, this.#include, this.#config, halfParsed, decode, selfLink);
441
+ }
442
+ /** @private */
443
+ lint(start = this.getAbsoluteIndex(), re) {
444
+ const { viewOnly } = index_1.default;
445
+ index_1.default.viewOnly = true;
446
+ let errors = super.lint(start, re);
447
+ if (this.type === 'root') {
448
+ const record = {}, selector = 'category,html-attr#id,ext-attr#id,table-attr#id';
449
+ for (const cat of this.querySelectorAll(selector)) {
450
+ let key;
451
+ if (cat.type === 'category') {
452
+ key = cat.name;
453
+ }
454
+ else {
455
+ const value = cat.getValue();
456
+ key = `#${value === true ? '' : value}`;
457
+ }
458
+ const thisCat = record[key];
459
+ if (thisCat) {
460
+ thisCat.add(cat);
461
+ }
462
+ else {
463
+ record[key] = new Set([cat]);
464
+ }
465
+ }
466
+ for (const [key, value] of Object.entries(record)) {
467
+ if (value.size > 1 && !key.startsWith('#mw-customcollapsible-')) {
468
+ const isCat = !key.startsWith('#'), msg = `duplicated ${isCat ? 'category' : 'id'}`, severity = isCat ? 'error' : 'warning';
469
+ errors.push(...[...value].map(cat => {
470
+ const e = (0, lint_1.generateForSelf)(cat, { start: cat.getAbsoluteIndex() }, 'no-duplicate', msg, severity);
471
+ if (isCat) {
472
+ e.suggestions = [
473
+ {
474
+ desc: 'remove',
475
+ range: [e.startIndex, e.endIndex],
476
+ text: '',
477
+ },
478
+ ];
479
+ }
480
+ return e;
481
+ }));
482
+ }
483
+ }
484
+ const regex = /<!--\s*lint-(disable(?:(?:-next)?-line)?|enable)(\s[\sa-z,-]*)?-->/gu, wikitext = this.toString(), ignores = [];
485
+ let mt = regex.exec(wikitext), last = 0, curLine = 0;
486
+ while (mt) {
487
+ const { 1: type, index } = mt, detail = mt[2]?.trim();
488
+ curLine += wikitext.slice(last, index).split('\n').length - 1;
489
+ last = index;
490
+ ignores.push({
491
+ line: curLine + (type === 'disable-line' ? 0 : 1),
492
+ from: type === 'disable' ? regex.lastIndex : undefined,
493
+ to: type === 'enable' ? regex.lastIndex : undefined,
494
+ rules: detail ? new Set(detail.split(',').map(r => r.trim())) : undefined,
495
+ });
496
+ mt = regex.exec(wikitext);
497
+ }
498
+ errors = errors.filter(({ rule, startLine, startIndex }) => {
499
+ const nearest = { pos: 0 };
500
+ for (const { line, from, to, rules } of ignores) {
501
+ if (line > startLine + 1) {
502
+ break;
503
+ }
504
+ else if (rules && !rules.has(rule)) {
505
+ continue;
506
+ }
507
+ else if (line === startLine && from === undefined && to === undefined) {
508
+ return false;
509
+ }
510
+ else if (from <= startIndex && from > nearest.pos) {
511
+ nearest.pos = from;
512
+ nearest.type = 'from';
513
+ }
514
+ else if (to <= startIndex && to > nearest.pos) {
515
+ nearest.pos = to;
516
+ nearest.type = 'to';
517
+ }
518
+ }
519
+ return nearest.type !== 'from';
520
+ });
521
+ }
522
+ index_1.default.viewOnly = viewOnly;
523
+ return errors;
524
+ }
525
+ /** @private */
526
+ toString(skip, separator) {
527
+ const { rev } = debug_1.Shadow, root = this.getRootNode();
528
+ if (this.#string && this.#string[0] !== rev) {
529
+ this.#string = undefined;
530
+ }
531
+ if (!skip
532
+ && root.type === 'root'
533
+ && root.#built
534
+ && index_1.default.viewOnly) {
535
+ this.#string ??= [rev, super.toString(false, separator)];
536
+ return this.#string[1];
537
+ }
538
+ return super.toString(skip, separator);
539
+ }
540
+ /* NOT FOR BROWSER */
541
+ /** @private */
542
+ getAcceptable() {
543
+ if (typeof this.#acceptable === 'function') {
544
+ this.#acceptable = this.#acceptable();
545
+ }
546
+ return this.#acceptable;
547
+ }
548
+ /** @private */
549
+ dispatchEvent(e, data) {
550
+ if (this.#built) {
551
+ super.dispatchEvent(e, data);
552
+ }
553
+ }
554
+ /** @private */
555
+ protectChildren(...args) {
556
+ this.#protectedChildren.push(...new ranges_1.Ranges(args));
557
+ }
558
+ /**
559
+ * @override
560
+ * @param i 移除位置
561
+ */
562
+ removeAt(i) {
563
+ i += i < 0 ? this.length : 0;
564
+ if (!debug_1.Shadow.running) {
565
+ const protectedIndices = this.#protectedChildren.applyTo(this.childNodes);
566
+ if (protectedIndices.includes(i)) {
567
+ this.constructorError(`cannot remove the child node at position ${i}`);
568
+ }
569
+ const acceptable = this.getAcceptable();
570
+ if (acceptable) {
571
+ const acceptableIndices = Object.fromEntries(Object.entries(acceptable).map(([str, ranges]) => [str, ranges.applyTo(this.length - 1)])), nodesAfter = this.childNodes.slice(i + 1);
572
+ if (nodesAfter.some(({ constructor: { name } }, j) => !acceptableIndices[name]?.includes(i + j))) {
573
+ this.constructorError(`violates the order of acceptable nodes by removing the child node at position ${i}`);
574
+ }
575
+ }
576
+ }
577
+ const node = super.removeAt(i);
578
+ const e = new Event('remove', { bubbles: true });
579
+ this.dispatchEvent(e, { type: 'remove', position: i, removed: node });
580
+ return node;
581
+ }
582
+ /**
583
+ * 替换为同类节点
584
+ * @param token 待替换的节点
585
+ * @throws `Error` 不存在父节点
586
+ */
587
+ safeReplaceWith(token) {
588
+ const { parentNode } = this;
589
+ if (!parentNode) {
590
+ throw new Error('The node does not have a parent node!');
591
+ }
592
+ else if (token.constructor !== this.constructor) {
593
+ this.typeError('safeReplaceWith', this.constructor.name);
594
+ }
595
+ try {
596
+ assert.deepEqual(token.getAcceptable(), this.getAcceptable());
597
+ }
598
+ catch (e) {
599
+ if (e instanceof assert.AssertionError) {
600
+ this.constructorError('has a different #acceptable property');
601
+ }
602
+ throw e;
603
+ }
604
+ const i = parentNode.childNodes.indexOf(this);
605
+ super.removeAt.call(parentNode, i);
606
+ super.insertAt.call(parentNode, token, i);
607
+ if (token.type === 'root') {
608
+ token.type = 'plain';
609
+ }
610
+ const e = new Event('replace', { bubbles: true });
611
+ token.dispatchEvent(e, { type: 'replace', position: i, oldToken: this });
612
+ }
613
+ /**
614
+ * 创建HTML注释
615
+ * @param data 注释内容
616
+ */
617
+ createComment(data) {
618
+ require('../addon/token');
619
+ return this.createComment(data);
620
+ }
621
+ /**
622
+ * 创建标签
623
+ * @param tagName 标签名
624
+ * @param options 选项
625
+ * @param options.selfClosing 是否自封闭
626
+ * @param options.closing 是否是闭合标签
627
+ * @throws `RangeError` 非法的标签名
628
+ */
629
+ createElement(tagName, options) {
630
+ require('../addon/token');
631
+ return this.createElement(tagName, options);
632
+ }
633
+ /**
634
+ * 创建纯文本节点
635
+ * @param data 文本内容
636
+ */
637
+ createTextNode(data = '') {
638
+ return new text_1.AstText(data);
639
+ }
640
+ /** 创建AstRange对象 */
641
+ createRange() {
642
+ return new range_1.AstRange();
643
+ }
644
+ /**
645
+ * 找到给定位置
646
+ * @param index 位置
647
+ */
648
+ caretPositionFromIndex(index) {
649
+ require('../addon/token');
650
+ return this.caretPositionFromIndex(index);
651
+ }
652
+ /**
653
+ * 找到给定位置
654
+ * @param x 列数
655
+ * @param y 行数
656
+ */
657
+ caretPositionFromPoint(x, y) {
658
+ return this.caretPositionFromIndex(this.indexFromPos(y, x));
659
+ }
660
+ /**
661
+ * 找到给定位置所在的最外层节点
662
+ * @param index 位置
663
+ */
664
+ elementFromIndex(index) {
665
+ return this.caretPositionFromIndex(index)?.offsetNode;
666
+ }
667
+ /**
668
+ * 找到给定位置所在的最外层节点
669
+ * @param x 列数
670
+ * @param y 行数
671
+ */
672
+ elementFromPoint(x, y) {
673
+ return this.elementFromIndex(this.indexFromPos(y, x));
674
+ }
675
+ /**
676
+ * 找到给定位置所在的所有节点
677
+ * @param index 位置
678
+ */
679
+ elementsFromIndex(index) {
680
+ const offsetNode = this.caretPositionFromIndex(index)?.offsetNode;
681
+ return offsetNode ? [...offsetNode.getAncestors().reverse(), offsetNode] : [];
682
+ }
683
+ /**
684
+ * 找到给定位置所在的所有节点
685
+ * @param x 列数
686
+ * @param y 行数
687
+ */
688
+ elementsFromPoint(x, y) {
689
+ return this.elementsFromIndex(this.indexFromPos(y, x));
690
+ }
691
+ /**
692
+ * 判断标题是否是跨维基链接
693
+ * @param title 标题
694
+ */
695
+ isInterwiki(title) {
696
+ return index_1.default.isInterwiki(title, this.#config);
697
+ }
698
+ /** @private */
699
+ cloneChildNodes() {
700
+ return this.childNodes.map(child => child.cloneNode());
701
+ }
702
+ /** 深拷贝节点 */
703
+ cloneNode() {
704
+ if (this.constructor !== Token) {
705
+ this.constructorError('does not specify a cloneNode method');
706
+ }
707
+ const cloned = this.cloneChildNodes();
708
+ return debug_1.Shadow.run(() => {
709
+ const token = new Token(undefined, this.#config, [], this.getAcceptable());
710
+ token.type = this.type;
711
+ token.setAttribute('stage', this.#stage);
712
+ token.setAttribute('name', this.name);
713
+ token.append(...cloned);
714
+ token.protectChildren(...this.#protectedChildren);
715
+ return token;
716
+ });
717
+ }
718
+ /** 获取全部章节 */
719
+ sections() {
720
+ require('../addon/token');
721
+ return this.sections();
722
+ }
723
+ /**
724
+ * 获取指定章节
725
+ * @param n 章节序号
726
+ */
727
+ section(n) {
728
+ return this.sections()?.[n];
729
+ }
730
+ /**
731
+ * 获取指定的外层HTML标签
732
+ * @param tag HTML标签名
733
+ * @throws `RangeError` 非法的标签或空标签
734
+ */
735
+ findEnclosingHtml(tag) {
736
+ require('../addon/token');
737
+ return this.findEnclosingHtml(tag);
738
+ }
739
+ /** 获取全部分类 */
740
+ getCategories() {
741
+ const categories = this.querySelectorAll('category');
742
+ return categories.map(({ name, sortkey }) => [name, sortkey]);
743
+ }
744
+ /**
745
+ * 展开模板
746
+ * @param context 模板调用环境
747
+ */
748
+ expand() {
749
+ require('../addon/token');
750
+ return this.expand();
751
+ }
752
+ /** 解析部分魔术字 */
753
+ solveConst() {
754
+ require('../addon/token');
755
+ return this.solveConst();
756
+ }
757
+ /** 合并普通节点的普通子节点 */
758
+ flatten() {
759
+ if (this.getAttribute('plain')) {
760
+ for (const child of this.childNodes) {
761
+ if (child.type !== 'text' && child.getAttribute('plain')) {
762
+ child.replaceWith(...child.childNodes);
763
+ }
764
+ }
765
+ }
766
+ }
767
+ /** 生成HTML */
768
+ toHtml() {
769
+ require('../addon/token');
770
+ return this.toHtml();
771
+ }
772
+ /** @private */
773
+ toHtmlInternal(nowrap, nocc) {
774
+ for (const child of this.childNodes) {
775
+ if (child.type === 'text') {
776
+ child.removeBlankLines();
777
+ }
778
+ }
779
+ for (let i = 0; i < this.length; i++) {
780
+ const child = this.childNodes[i];
781
+ if (child.is('list') || child.is('dd')) {
782
+ child.getRange();
783
+ }
784
+ }
785
+ this.normalize();
786
+ return (0, html_1.font)(this, (0, html_1.html)(this.childNodes, '', nowrap, nocc));
787
+ }
788
+ }
789
+ exports.Token = Token;
790
+ constants_1.classes['Token'] = __filename;