wikiparser-node 0.10.0 → 0.11.0-m
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/README.md +44 -32
- package/config/.schema.json +1 -8
- package/config/default.json +1 -2
- package/config/llwiki.json +1 -36
- package/config/moegirl.json +1 -45
- package/config/zhwiki.json +1 -467
- package/dist/index.d.ts +110 -0
- package/dist/lib/element.d.ts +48 -0
- package/dist/lib/node.d.ts +108 -0
- package/dist/lib/text.d.ts +30 -0
- package/dist/lib/title.d.ts +15 -0
- package/dist/mixin/hidden.d.ts +8 -0
- package/dist/parser/brackets.d.ts +12 -0
- package/dist/parser/commentAndExt.d.ts +8 -0
- package/dist/parser/converter.d.ts +7 -0
- package/dist/parser/externalLinks.d.ts +7 -0
- package/dist/parser/hrAndDoubleUnderscore.d.ts +11 -0
- package/dist/parser/html.d.ts +7 -0
- package/dist/parser/links.d.ts +7 -0
- package/dist/parser/list.d.ts +7 -0
- package/dist/parser/magicLinks.d.ts +7 -0
- package/dist/parser/quotes.d.ts +7 -0
- package/dist/parser/table.d.ts +11 -0
- package/dist/src/arg.d.ts +25 -0
- package/dist/src/atom/hidden.d.ts +5 -0
- package/dist/src/atom/index.d.ts +14 -0
- package/dist/src/attribute.d.ts +42 -0
- package/dist/src/attributes.d.ts +33 -0
- package/dist/src/converter.d.ts +29 -0
- package/dist/src/converterFlags.d.ts +27 -0
- package/dist/src/converterRule.d.ts +29 -0
- package/dist/src/extLink.d.ts +22 -0
- package/dist/src/gallery.d.ts +19 -0
- package/dist/src/hasNowiki/index.d.ts +14 -0
- package/dist/src/hasNowiki/pre.d.ts +13 -0
- package/dist/src/heading.d.ts +24 -0
- package/dist/src/html.d.ts +33 -0
- package/dist/src/imageParameter.d.ts +23 -0
- package/dist/src/imagemap.d.ts +26 -0
- package/dist/src/imagemapLink.d.ts +16 -0
- package/dist/src/index.d.ts +53 -0
- package/dist/src/link/category.d.ts +8 -0
- package/dist/src/link/file.d.ts +27 -0
- package/dist/src/link/galleryImage.d.ts +15 -0
- package/dist/src/link/index.d.ts +29 -0
- package/dist/src/magicLink.d.ts +14 -0
- package/dist/src/nested/choose.d.ts +13 -0
- package/dist/src/nested/combobox.d.ts +13 -0
- package/dist/src/nested/index.d.ts +20 -0
- package/dist/src/nested/references.d.ts +13 -0
- package/dist/src/nowiki/comment.d.ts +19 -0
- package/dist/src/nowiki/dd.d.ts +8 -0
- package/dist/src/nowiki/doubleUnderscore.d.ts +12 -0
- package/dist/src/nowiki/hr.d.ts +13 -0
- package/dist/src/nowiki/index.d.ts +18 -0
- package/dist/src/nowiki/list.d.ts +8 -0
- package/dist/src/nowiki/noinclude.d.ts +8 -0
- package/dist/src/nowiki/quote.d.ts +20 -0
- package/dist/src/onlyinclude.d.ts +17 -0
- package/dist/src/paramTag/index.d.ts +24 -0
- package/dist/src/paramTag/inputbox.d.ts +8 -0
- package/dist/src/parameter.d.ts +28 -0
- package/dist/src/syntax.d.ts +15 -0
- package/dist/src/table/index.d.ts +16 -0
- package/dist/src/table/td.d.ts +39 -0
- package/dist/src/table/tr.d.ts +17 -0
- package/dist/src/tagPair/ext.d.ts +17 -0
- package/dist/src/tagPair/include.d.ts +16 -0
- package/dist/src/tagPair/index.d.ts +28 -0
- package/dist/src/transclude.d.ts +75 -0
- package/dist/util/lint.d.ts +28 -0
- package/dist/util/string.d.ts +31 -0
- package/i18n/zh-hans.json +1 -1
- package/i18n/zh-hant.json +1 -1
- package/index.js +5 -257
- package/lib/element.js +7 -482
- package/lib/node.js +11 -540
- package/lib/text.js +3 -96
- package/lib/title.js +1 -28
- package/mixin/hidden.js +0 -3
- package/package.json +11 -5
- package/parser/brackets.js +3 -2
- package/parser/commentAndExt.js +8 -6
- package/parser/converter.js +1 -2
- package/parser/externalLinks.js +1 -2
- package/parser/hrAndDoubleUnderscore.js +1 -2
- package/parser/html.js +1 -2
- package/parser/links.js +5 -6
- package/parser/list.js +1 -2
- package/parser/magicLinks.js +1 -2
- package/parser/quotes.js +1 -2
- package/parser/table.js +1 -2
- package/src/arg.js +4 -118
- package/src/atom/hidden.js +0 -2
- package/src/atom/index.js +1 -18
- package/src/attribute.js +6 -190
- package/src/attributes.js +5 -308
- package/src/converter.js +3 -109
- package/src/converterFlags.js +1 -188
- package/src/converterRule.js +2 -184
- package/src/extLink.js +2 -122
- package/src/gallery.js +2 -56
- package/src/hasNowiki/index.js +1 -13
- package/src/hasNowiki/pre.js +1 -13
- package/src/heading.js +4 -55
- package/src/html.js +5 -120
- package/src/imageParameter.js +3 -165
- package/src/imagemap.js +3 -62
- package/src/imagemapLink.js +2 -14
- package/src/index.js +9 -530
- package/src/link/category.js +1 -32
- package/src/link/file.js +3 -158
- package/src/link/galleryImage.js +3 -61
- package/src/link/index.js +4 -273
- package/src/magicLink.js +6 -87
- package/src/nested/choose.js +1 -2
- package/src/nested/combobox.js +1 -2
- package/src/nested/index.js +8 -32
- package/src/nested/references.js +1 -2
- package/src/nowiki/comment.js +2 -26
- package/src/nowiki/dd.js +1 -47
- package/src/nowiki/doubleUnderscore.js +1 -31
- package/src/nowiki/hr.js +2 -21
- package/src/nowiki/index.js +2 -24
- package/src/nowiki/list.js +2 -5
- package/src/nowiki/noinclude.js +0 -14
- package/src/nowiki/quote.js +2 -16
- package/src/onlyinclude.js +2 -27
- package/src/paramTag/index.js +2 -25
- package/src/paramTag/inputbox.js +2 -5
- package/src/parameter.js +6 -148
- package/src/syntax.js +1 -69
- package/src/table/index.js +2 -939
- package/src/table/td.js +6 -226
- package/src/table/tr.js +3 -248
- package/src/tagPair/ext.js +4 -23
- package/src/tagPair/include.js +1 -25
- package/src/tagPair/index.js +3 -52
- package/src/transclude.js +6 -513
- package/typings/api.d.ts +9 -0
- package/typings/index.d.ts +51 -0
- package/typings/node.d.ts +16 -0
- package/typings/parser.d.ts +5 -0
- package/typings/token.d.ts +28 -0
- package/util/lint.js +2 -0
- package/util/string.js +0 -51
- package/lib/ranges.js +0 -130
- package/mixin/attributeParent.js +0 -117
- package/mixin/fixedToken.js +0 -40
- package/mixin/singleLine.js +0 -31
- package/mixin/sol.js +0 -54
- package/parser/selector.js +0 -177
- package/src/charinsert.js +0 -97
- package/tool/index.js +0 -1202
- package/util/debug.js +0 -73
package/src/paramTag/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const {generateForChild} = require('../../util/lint'),
|
|
4
|
-
singleLine = require('../../mixin/singleLine'),
|
|
5
4
|
Parser = require('../..'),
|
|
6
5
|
Token = require('..'),
|
|
7
6
|
AtomToken = require('../atom');
|
|
@@ -15,17 +14,15 @@ class ParamTagToken extends Token {
|
|
|
15
14
|
|
|
16
15
|
/**
|
|
17
16
|
* @param {string} wikitext wikitext
|
|
18
|
-
* @param {accum} accum
|
|
17
|
+
* @param {import('../../typings/token').accum} accum
|
|
19
18
|
*/
|
|
20
19
|
constructor(wikitext, config = Parser.getConfig(), accum = []) {
|
|
21
20
|
super(undefined, config, true, accum, {
|
|
22
|
-
SingleLineAtomToken: ':',
|
|
23
21
|
});
|
|
24
22
|
if (wikitext) {
|
|
25
|
-
const SingleLineAtomToken =
|
|
23
|
+
const SingleLineAtomToken = AtomToken;
|
|
26
24
|
this.append(
|
|
27
25
|
...wikitext.split('\n').map(line => new SingleLineAtomToken(line, 'param-line', config, accum, {
|
|
28
|
-
AstText: ':',
|
|
29
26
|
})),
|
|
30
27
|
);
|
|
31
28
|
}
|
|
@@ -33,7 +30,6 @@ class ParamTagToken extends Token {
|
|
|
33
30
|
|
|
34
31
|
/**
|
|
35
32
|
* @override
|
|
36
|
-
* @param {string} selector
|
|
37
33
|
*/
|
|
38
34
|
toString(selector) {
|
|
39
35
|
return super.toString(selector, '\n');
|
|
@@ -49,11 +45,6 @@ class ParamTagToken extends Token {
|
|
|
49
45
|
return 1;
|
|
50
46
|
}
|
|
51
47
|
|
|
52
|
-
/** @override */
|
|
53
|
-
print() {
|
|
54
|
-
return super.print({sep: '\n'});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
48
|
/**
|
|
58
49
|
* @override
|
|
59
50
|
* @param {number} start 起始位置
|
|
@@ -70,20 +61,6 @@ class ParamTagToken extends Token {
|
|
|
70
61
|
return generateForChild(child, rect, Parser.msg('invalid parameter of $1', this.name));
|
|
71
62
|
});
|
|
72
63
|
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* @override
|
|
76
|
-
* @this {ParamTagToken & {constructor: typeof ParamTagToken}}
|
|
77
|
-
*/
|
|
78
|
-
cloneNode() {
|
|
79
|
-
const cloned = this.cloneChildNodes();
|
|
80
|
-
return Parser.run(() => {
|
|
81
|
-
const token = new this.constructor(undefined, this.getAttribute('config'));
|
|
82
|
-
token.append(...cloned);
|
|
83
|
-
return token;
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
64
|
}
|
|
87
65
|
|
|
88
|
-
Parser.classes.ParamTagToken = __filename;
|
|
89
66
|
module.exports = ParamTagToken;
|
package/src/paramTag/inputbox.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const parseBrackets = require('../../parser/brackets'),
|
|
4
|
-
singleLine = require('../../mixin/singleLine'),
|
|
5
4
|
Parser = require('../..'),
|
|
6
5
|
ParamTagToken = require('.'),
|
|
7
6
|
AtomToken = require('../atom');
|
|
@@ -15,7 +14,7 @@ class InputboxToken extends ParamTagToken {
|
|
|
15
14
|
|
|
16
15
|
/**
|
|
17
16
|
* @param {string} wikitext wikitext
|
|
18
|
-
* @param {accum} accum
|
|
17
|
+
* @param {import('../../typings/token').accum} accum
|
|
19
18
|
*/
|
|
20
19
|
constructor(wikitext, config = Parser.getConfig(), accum = []) {
|
|
21
20
|
super(undefined, config, accum);
|
|
@@ -23,13 +22,11 @@ class InputboxToken extends ParamTagToken {
|
|
|
23
22
|
accum.splice(accum.indexOf(this), 1);
|
|
24
23
|
accum.push(this);
|
|
25
24
|
if (wikitext) {
|
|
26
|
-
const SingleLineAtomToken =
|
|
25
|
+
const SingleLineAtomToken = AtomToken;
|
|
27
26
|
this.append(...wikitext.split('\n').map(line => new SingleLineAtomToken(line, 'param-line', config, accum, {
|
|
28
|
-
AstText: ':', ArgToken: ':', TranscludeToken: ':',
|
|
29
27
|
})));
|
|
30
28
|
}
|
|
31
29
|
}
|
|
32
30
|
}
|
|
33
31
|
|
|
34
|
-
Parser.classes.InputboxToken = __filename;
|
|
35
32
|
module.exports = InputboxToken;
|
package/src/parameter.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const {extUrlChar, extUrlCharFirst} = require('../util/string'),
|
|
4
4
|
{generateForChild} = require('../util/lint'),
|
|
5
|
-
fixedToken = require('../mixin/fixedToken'),
|
|
6
5
|
Parser = require('..'),
|
|
7
6
|
Token = require('.');
|
|
8
7
|
|
|
@@ -10,7 +9,7 @@ const {noWrap, extUrlChar, extUrlCharFirst} = require('../util/string'),
|
|
|
10
9
|
* 模板或魔术字参数
|
|
11
10
|
* @classdesc `{childNodes: [Token, Token]}`
|
|
12
11
|
*/
|
|
13
|
-
class ParameterToken extends
|
|
12
|
+
class ParameterToken extends Token {
|
|
14
13
|
type = 'parameter';
|
|
15
14
|
|
|
16
15
|
/** 是否是匿名参数 */
|
|
@@ -18,37 +17,14 @@ class ParameterToken extends fixedToken(Token) {
|
|
|
18
17
|
return this.firstChild.length === 0;
|
|
19
18
|
}
|
|
20
19
|
|
|
21
|
-
/** getValue()的getter */
|
|
22
|
-
get value() {
|
|
23
|
-
return this.getValue();
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
set value(value) {
|
|
27
|
-
this.setValue(value);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* 是否是重复参数
|
|
32
|
-
* @this {ParameterToken & {parentNode: TranscludeToken}}
|
|
33
|
-
*/
|
|
34
|
-
get duplicated() {
|
|
35
|
-
const TranscludeToken = require('./transclude');
|
|
36
|
-
try {
|
|
37
|
-
return Boolean(this.parentNode?.getDuplicatedArgs()?.some(([key]) => key === this.name));
|
|
38
|
-
} catch {
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
20
|
/**
|
|
44
21
|
* @param {string|number} key 参数名
|
|
45
22
|
* @param {string} value 参数值
|
|
46
|
-
* @param {accum} accum
|
|
23
|
+
* @param {import('../typings/token').accum} accum
|
|
47
24
|
*/
|
|
48
25
|
constructor(key, value, config = Parser.getConfig(), accum = []) {
|
|
49
26
|
super(undefined, config, true, accum);
|
|
50
27
|
const keyToken = new Token(typeof key === 'number' ? undefined : key, config, true, accum, {
|
|
51
|
-
'Stage-11': ':', '!HeadingToken': '',
|
|
52
28
|
}),
|
|
53
29
|
token = new Token(value, config, true, accum);
|
|
54
30
|
keyToken.type = 'parameter-key';
|
|
@@ -60,37 +36,22 @@ class ParameterToken extends fixedToken(Token) {
|
|
|
60
36
|
afterBuild() {
|
|
61
37
|
if (!this.anon) {
|
|
62
38
|
const TranscludeToken = require('./transclude');
|
|
63
|
-
const name = this.firstChild.
|
|
39
|
+
const name = String(this.firstChild).replace(/<!--.*?-->/gsu, '')
|
|
64
40
|
.replace(/^[ \t\n\0\v]+|(?<=[^ \t\n\0\v])[ \t\n\0\v]+$/gu, ''),
|
|
65
41
|
{parentNode} = this;
|
|
66
42
|
this.setAttribute('name', name);
|
|
67
43
|
if (parentNode instanceof TranscludeToken) {
|
|
68
|
-
parentNode.getAttribute('keys').add(name);
|
|
69
44
|
parentNode.getArgs(name, false, false).add(this);
|
|
70
45
|
}
|
|
71
46
|
}
|
|
72
|
-
const /** @type {AstListener} */ parameterListener = ({prevTarget}, data) => {
|
|
73
|
-
if (!this.anon) { // 匿名参数不管怎么变动还是匿名
|
|
74
|
-
const {firstChild, name} = this;
|
|
75
|
-
if (prevTarget === firstChild) {
|
|
76
|
-
const newKey = firstChild.toString('comment, noinclude, include')
|
|
77
|
-
.replace(/^[ \t\n\0\v]+|(?<=[^ \t\n\0\v])[ \t\n\0\v]+$/gu, '');
|
|
78
|
-
data.oldKey = name;
|
|
79
|
-
data.newKey = newKey;
|
|
80
|
-
this.setAttribute('name', newKey);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
this.addEventListener(['remove', 'insert', 'replace', 'text'], parameterListener);
|
|
85
47
|
}
|
|
86
48
|
|
|
87
49
|
/**
|
|
88
50
|
* @override
|
|
89
|
-
* @param {string} selector
|
|
90
51
|
* @returns {string}
|
|
91
52
|
*/
|
|
92
53
|
toString(selector) {
|
|
93
|
-
return this.anon
|
|
54
|
+
return this.anon
|
|
94
55
|
? this.lastChild.toString(selector)
|
|
95
56
|
: super.toString(selector, '=');
|
|
96
57
|
}
|
|
@@ -108,11 +69,6 @@ class ParameterToken extends fixedToken(Token) {
|
|
|
108
69
|
return this.anon ? 0 : 1;
|
|
109
70
|
}
|
|
110
71
|
|
|
111
|
-
/** @override */
|
|
112
|
-
print() {
|
|
113
|
-
return super.print({sep: this.anon ? '' : '='});
|
|
114
|
-
}
|
|
115
|
-
|
|
116
72
|
/**
|
|
117
73
|
* @override
|
|
118
74
|
* @param {number} start 起始位置
|
|
@@ -121,7 +77,7 @@ class ParameterToken extends fixedToken(Token) {
|
|
|
121
77
|
const errors = super.lint(start),
|
|
122
78
|
{firstChild, lastChild} = this,
|
|
123
79
|
link = new RegExp(`https?://${extUrlCharFirst}${extUrlChar}$`, 'iu')
|
|
124
|
-
.exec(firstChild.
|
|
80
|
+
.exec(String(firstChild).replace(/<!--.*?-->/gsu, ''))?.[0];
|
|
125
81
|
if (link && new URL(link).search) {
|
|
126
82
|
const e = generateForChild(firstChild, {start}, 'unescaped query string in an anonymous parameter');
|
|
127
83
|
errors.push({
|
|
@@ -136,104 +92,6 @@ class ParameterToken extends fixedToken(Token) {
|
|
|
136
92
|
}
|
|
137
93
|
return errors;
|
|
138
94
|
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* 获取参数值
|
|
142
|
-
* @this {ParameterToken & {parentNode: TranscludeToken}}
|
|
143
|
-
*/
|
|
144
|
-
getValue() {
|
|
145
|
-
const TranscludeToken = require('./transclude');
|
|
146
|
-
const value = this.lastChild.text();
|
|
147
|
-
return this.anon && this.parentNode?.isTemplate() ? value : value.trim();
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/** @override */
|
|
151
|
-
cloneNode() {
|
|
152
|
-
const [key, value] = this.cloneChildNodes(),
|
|
153
|
-
config = this.getAttribute('config');
|
|
154
|
-
return Parser.run(() => {
|
|
155
|
-
const token = new ParameterToken(this.anon ? Number(this.name) : undefined, undefined, config);
|
|
156
|
-
token.firstChild.safeReplaceWith(key);
|
|
157
|
-
token.lastChild.safeReplaceWith(value);
|
|
158
|
-
token.afterBuild();
|
|
159
|
-
return token;
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* @override
|
|
165
|
-
* @param {ParameterToken} token 待替换的节点
|
|
166
|
-
* @complexity `n`
|
|
167
|
-
*/
|
|
168
|
-
safeReplaceWith(token) {
|
|
169
|
-
Parser.warn(`${this.constructor.name}.safeReplaceWith 方法退化到 replaceWith。`);
|
|
170
|
-
return this.replaceWith(token);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* 设置参数值
|
|
175
|
-
* @this {ParameterToken & {parentNode: TranscludeToken}}
|
|
176
|
-
* @param {string} value 参数值
|
|
177
|
-
* @throws `SyntaxError` 非法的模板参数
|
|
178
|
-
*/
|
|
179
|
-
setValue(value) {
|
|
180
|
-
value = String(value);
|
|
181
|
-
const TranscludeToken = require('./transclude');
|
|
182
|
-
const templateLike = this.parentNode?.isTemplate(),
|
|
183
|
-
wikitext = `{{${templateLike ? ':T|' : 'lc:'}${this.anon ? '' : '1='}${value}}}`,
|
|
184
|
-
root = Parser.parse(wikitext, this.getAttribute('include'), 2, this.getAttribute('config')),
|
|
185
|
-
{length, firstChild: transclude} = root,
|
|
186
|
-
/** @type {Token & {lastChild: ParameterToken}} */
|
|
187
|
-
{lastChild: parameter, type, name, length: transcludeLength} = transclude,
|
|
188
|
-
targetType = templateLike ? 'template' : 'magic-word',
|
|
189
|
-
targetName = templateLike ? 'T' : 'lc';
|
|
190
|
-
if (length !== 1 || type !== targetType || name !== targetName || transcludeLength !== 2
|
|
191
|
-
|| parameter.anon !== this.anon || parameter.name !== '1'
|
|
192
|
-
) {
|
|
193
|
-
throw new SyntaxError(`非法的模板参数:${noWrap(value)}`);
|
|
194
|
-
}
|
|
195
|
-
const {lastChild} = parameter;
|
|
196
|
-
parameter.destroy(true);
|
|
197
|
-
this.lastChild.safeReplaceWith(lastChild);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* 修改参数名
|
|
202
|
-
* @this {ParameterToken & {parentNode: TranscludeToken}}
|
|
203
|
-
* @param {string} key 新参数名
|
|
204
|
-
* @param {boolean} force 是否无视冲突命名
|
|
205
|
-
* @throws `Error` 仅用于模板参数
|
|
206
|
-
* @throws `SyntaxError` 非法的模板参数名
|
|
207
|
-
* @throws `RangeError` 更名造成重复参数
|
|
208
|
-
*/
|
|
209
|
-
rename(key, force) {
|
|
210
|
-
key = String(key);
|
|
211
|
-
const TranscludeToken = require('./transclude');
|
|
212
|
-
const {parentNode} = this;
|
|
213
|
-
// 必须检测是否是TranscludeToken
|
|
214
|
-
if (!parentNode?.isTemplate() || !(parentNode instanceof TranscludeToken)) {
|
|
215
|
-
throw new Error(`${this.constructor.name}.rename 方法仅用于模板参数!`);
|
|
216
|
-
}
|
|
217
|
-
const root = Parser.parse(`{{:T|${key}=}}`, this.getAttribute('include'), 2, this.getAttribute('config')),
|
|
218
|
-
{length, firstChild: template} = root,
|
|
219
|
-
{type, name, lastChild: parameter, length: templateLength} = template;
|
|
220
|
-
if (length !== 1 || type !== 'template' || name !== 'T' || templateLength !== 2) {
|
|
221
|
-
throw new SyntaxError(`非法的模板参数名:${key}`);
|
|
222
|
-
}
|
|
223
|
-
const {name: parameterName, firstChild} = parameter;
|
|
224
|
-
if (this.name === parameterName) {
|
|
225
|
-
Parser.warn('未改变实际参数名', parameterName);
|
|
226
|
-
} else if (parentNode.hasArg(parameterName)) {
|
|
227
|
-
if (force) {
|
|
228
|
-
Parser.warn('参数更名造成重复参数', parameterName);
|
|
229
|
-
} else {
|
|
230
|
-
throw new RangeError(`参数更名造成重复参数:${parameterName}`);
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
parameter.destroy(true);
|
|
234
|
-
this.firstChild.safeReplaceWith(firstChild);
|
|
235
|
-
}
|
|
236
95
|
}
|
|
237
96
|
|
|
238
|
-
Parser.classes.ParameterToken = __filename;
|
|
239
97
|
module.exports = ParameterToken;
|
package/src/syntax.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const Parser = require('..'),
|
|
4
|
-
{undo} = require('../util/debug'),
|
|
5
|
-
{text} = require('../util/string'),
|
|
6
4
|
Token = require('.');
|
|
7
5
|
|
|
8
6
|
/**
|
|
@@ -10,82 +8,16 @@ const Parser = require('..'),
|
|
|
10
8
|
* @classdesc `{childNodes: ...AstText|Token}`
|
|
11
9
|
*/
|
|
12
10
|
class SyntaxToken extends Token {
|
|
13
|
-
#pattern;
|
|
14
|
-
|
|
15
11
|
/**
|
|
16
12
|
* @param {string} wikitext 语法wikitext
|
|
17
13
|
* @param {RegExp} pattern 语法正则
|
|
18
14
|
* @param {string} type Token.type
|
|
19
|
-
* @param {accum} accum
|
|
20
|
-
* @param {acceptable} acceptable 可接受的子节点设置
|
|
21
|
-
* @throws `RangeError` 含有g修饰符的语法正则
|
|
15
|
+
* @param {import('../typings/token').accum} accum
|
|
22
16
|
*/
|
|
23
17
|
constructor(wikitext, pattern, type = 'plain', config = Parser.getConfig(), accum = [], acceptable = undefined) {
|
|
24
|
-
if (pattern.global) {
|
|
25
|
-
throw new RangeError(`SyntaxToken 的语法正则不能含有 g 修饰符:${pattern}`);
|
|
26
|
-
}
|
|
27
18
|
super(wikitext, config, true, accum, acceptable);
|
|
28
19
|
this.type = type;
|
|
29
|
-
this.#pattern = pattern;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** @override */
|
|
33
|
-
cloneNode() {
|
|
34
|
-
const cloned = this.cloneChildNodes(),
|
|
35
|
-
config = this.getAttribute('config'),
|
|
36
|
-
acceptable = this.getAttribute('acceptable');
|
|
37
|
-
return Parser.run(() => {
|
|
38
|
-
const token = new SyntaxToken(undefined, this.#pattern, this.type, config, [], acceptable);
|
|
39
|
-
token.append(...cloned);
|
|
40
|
-
token.afterBuild();
|
|
41
|
-
return token;
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** @override */
|
|
46
|
-
afterBuild() {
|
|
47
|
-
const /** @type {AstListener} */ syntaxListener = (e, data) => {
|
|
48
|
-
const pattern = this.#pattern;
|
|
49
|
-
if (!Parser.running && !pattern.test(this.text())) {
|
|
50
|
-
undo(e, data);
|
|
51
|
-
Parser.error(`不可修改 ${this.constructor.name} 的语法!`, pattern);
|
|
52
|
-
throw new Error(`不可修改 ${this.constructor.name} 的语法!`);
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
this.addEventListener(['remove', 'insert', 'replace', 'text'], syntaxListener);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* @override
|
|
60
|
-
* @template {string} T
|
|
61
|
-
* @param {T} key 属性键
|
|
62
|
-
* @returns {TokenAttribute<T>}
|
|
63
|
-
*/
|
|
64
|
-
getAttribute(key) {
|
|
65
|
-
return key === 'pattern' ? this.#pattern : super.getAttribute(key);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* @override
|
|
70
|
-
* @param {PropertyKey} key 属性键
|
|
71
|
-
*/
|
|
72
|
-
hasAttribute(key) {
|
|
73
|
-
return key === 'pattern' || super.hasAttribute(key);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* @override
|
|
78
|
-
* @param {...Token} elements 待替换的子节点
|
|
79
|
-
* @complexity `n`
|
|
80
|
-
*/
|
|
81
|
-
replaceChildren(...elements) {
|
|
82
|
-
if (this.#pattern.test(text(elements))) {
|
|
83
|
-
Parser.run(() => {
|
|
84
|
-
super.replaceChildren(...elements);
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
20
|
}
|
|
88
21
|
}
|
|
89
22
|
|
|
90
|
-
Parser.classes.SyntaxToken = __filename;
|
|
91
23
|
module.exports = SyntaxToken;
|