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