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