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