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