wikiparser-node 0.4.0 → 0.6.2-b

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/bundle/bundle.min.js +34 -0
  2. package/package.json +12 -5
  3. package/.eslintrc.json +0 -714
  4. package/README.md +0 -39
  5. package/config/default.json +0 -769
  6. package/config/llwiki.json +0 -630
  7. package/config/moegirl.json +0 -727
  8. package/config/zhwiki.json +0 -1269
  9. package/errors/README +0 -1
  10. package/index.js +0 -245
  11. package/jsconfig.json +0 -7
  12. package/lib/element.js +0 -755
  13. package/lib/node.js +0 -585
  14. package/lib/ranges.js +0 -131
  15. package/lib/text.js +0 -146
  16. package/lib/title.js +0 -69
  17. package/mixin/attributeParent.js +0 -113
  18. package/mixin/fixedToken.js +0 -40
  19. package/mixin/hidden.js +0 -19
  20. package/mixin/sol.js +0 -59
  21. package/parser/brackets.js +0 -112
  22. package/parser/commentAndExt.js +0 -63
  23. package/parser/converter.js +0 -45
  24. package/parser/externalLinks.js +0 -31
  25. package/parser/hrAndDoubleUnderscore.js +0 -35
  26. package/parser/html.js +0 -42
  27. package/parser/links.js +0 -98
  28. package/parser/list.js +0 -59
  29. package/parser/magicLinks.js +0 -41
  30. package/parser/quotes.js +0 -64
  31. package/parser/selector.js +0 -175
  32. package/parser/table.js +0 -112
  33. package/printed/README +0 -1
  34. package/printed/example.json +0 -120
  35. package/src/arg.js +0 -169
  36. package/src/atom/hidden.js +0 -13
  37. package/src/atom/index.js +0 -41
  38. package/src/attribute.js +0 -422
  39. package/src/converter.js +0 -157
  40. package/src/converterFlags.js +0 -232
  41. package/src/converterRule.js +0 -253
  42. package/src/extLink.js +0 -167
  43. package/src/gallery.js +0 -91
  44. package/src/heading.js +0 -100
  45. package/src/html.js +0 -202
  46. package/src/imageParameter.js +0 -254
  47. package/src/index.js +0 -737
  48. package/src/link/category.js +0 -53
  49. package/src/link/file.js +0 -265
  50. package/src/link/galleryImage.js +0 -61
  51. package/src/link/index.js +0 -322
  52. package/src/magicLink.js +0 -108
  53. package/src/nowiki/comment.js +0 -57
  54. package/src/nowiki/dd.js +0 -59
  55. package/src/nowiki/doubleUnderscore.js +0 -51
  56. package/src/nowiki/hr.js +0 -41
  57. package/src/nowiki/index.js +0 -44
  58. package/src/nowiki/list.js +0 -16
  59. package/src/nowiki/noinclude.js +0 -28
  60. package/src/nowiki/quote.js +0 -36
  61. package/src/onlyinclude.js +0 -54
  62. package/src/parameter.js +0 -187
  63. package/src/syntax.js +0 -83
  64. package/src/table/index.js +0 -967
  65. package/src/table/td.js +0 -308
  66. package/src/table/tr.js +0 -282
  67. package/src/tagPair/ext.js +0 -105
  68. package/src/tagPair/include.js +0 -50
  69. package/src/tagPair/index.js +0 -117
  70. package/src/transclude.js +0 -703
  71. package/test/api.js +0 -83
  72. package/test/real.js +0 -133
  73. package/test/test.js +0 -28
  74. package/test/util.js +0 -80
  75. package/tool/index.js +0 -918
  76. package/typings/api.d.ts +0 -13
  77. package/typings/array.d.ts +0 -28
  78. package/typings/event.d.ts +0 -24
  79. package/typings/index.d.ts +0 -94
  80. package/typings/node.d.ts +0 -29
  81. package/typings/parser.d.ts +0 -16
  82. package/typings/table.d.ts +0 -14
  83. package/typings/token.d.ts +0 -22
  84. package/typings/tool.d.ts +0 -11
  85. package/util/debug.js +0 -73
  86. package/util/string.js +0 -88
package/src/magicLink.js DELETED
@@ -1,108 +0,0 @@
1
- 'use strict';
2
-
3
- const Parser = require('..'),
4
- Token = require('.');
5
-
6
- /**
7
- * 自由外链
8
- * @classdesc `{childNodes: [...AstText|CommentToken|IncludeToken|NoincludeToken]}`
9
- */
10
- class MagicLinkToken extends Token {
11
- type = 'free-ext-link';
12
- #protocolRegex;
13
-
14
- /** 协议 */
15
- get protocol() {
16
- return this.#protocolRegex.exec(this.text())?.[0];
17
- }
18
-
19
- set protocol(value) {
20
- if (typeof value !== 'string') {
21
- this.typeError('protocol', 'String');
22
- }
23
- if (!new RegExp(`${this.#protocolRegex.source}$`, 'iu').test(value)) {
24
- throw new RangeError(`非法的外链协议:${value}`);
25
- }
26
- this.replaceChildren(this.text().replace(this.#protocolRegex, value));
27
- }
28
-
29
- /** 和内链保持一致 */
30
- get link() {
31
- return this.text();
32
- }
33
-
34
- set link(url) {
35
- this.setTarget(url);
36
- }
37
-
38
- /**
39
- * @param {string} url 网址
40
- * @param {boolean} doubleSlash 是否接受"//"作为协议
41
- * @param {accum} accum
42
- */
43
- constructor(url, doubleSlash, config = Parser.getConfig(), accum = []) {
44
- super(url, config, true, accum, {'Stage-1': ':', '!ExtToken': ''});
45
- if (doubleSlash) {
46
- this.type = 'ext-link-url';
47
- }
48
- this.#protocolRegex = new RegExp(`^(?:${config.protocol}${doubleSlash ? '|//' : ''})`, 'iu');
49
- }
50
-
51
- /** @override */
52
- afterBuild() {
53
- const ParameterToken = require('./parameter');
54
- const /** @type {ParameterToken} */ parameter = this.closest('parameter');
55
- if (parameter?.getValue() === this.text()) {
56
- this.replaceWith(...this.childNodes);
57
- }
58
- return this;
59
- }
60
-
61
- /** @override */
62
- cloneNode() {
63
- const cloned = this.cloneChildNodes(),
64
- token = Parser.run(() => new MagicLinkToken(
65
- undefined, this.type === 'ext-link-url', this.getAttribute('config'),
66
- ));
67
- token.append(...cloned);
68
- token.afterBuild();
69
- return token;
70
- }
71
-
72
- /**
73
- * 获取网址
74
- * @throws `Error` 非标准协议
75
- */
76
- getUrl() {
77
- let url = this.text();
78
- if (url.startsWith('//')) {
79
- url = `https:${url}`;
80
- }
81
- try {
82
- return new URL(url);
83
- } catch (e) {
84
- if (e instanceof TypeError && e.message === 'Invalid URL') {
85
- throw new Error(`非标准协议的外部链接:${url}`);
86
- }
87
- throw e;
88
- }
89
- }
90
-
91
- /**
92
- * 设置外链目标
93
- * @param {string|URL} url 含协议的网址
94
- * @throws `SyntaxError` 非法的自由外链目标
95
- */
96
- setTarget(url) {
97
- url = String(url);
98
- const root = Parser.parse(url, this.getAttribute('include'), 9, this.getAttribute('config')),
99
- {childNodes: {length}, firstElementChild} = root;
100
- if (length !== 1 || firstElementChild?.type !== 'free-ext-link') {
101
- throw new SyntaxError(`非法的自由外链目标:${url}`);
102
- }
103
- this.replaceChildren(...firstElementChild.childNodes);
104
- }
105
- }
106
-
107
- Parser.classes.MagicLinkToken = __filename;
108
- module.exports = MagicLinkToken;
@@ -1,57 +0,0 @@
1
- 'use strict';
2
-
3
- const hidden = require('../../mixin/hidden'),
4
- Parser = require('../..'),
5
- NowikiToken = require('.');
6
-
7
- /**
8
- * HTML注释,不可见
9
- * @classdesc `{childNodes: [AstText]}`
10
- */
11
- class CommentToken extends hidden(NowikiToken) {
12
- type = 'comment';
13
- closed;
14
-
15
- /** 内部wikitext */
16
- get innerText() {
17
- return String(this.firstChild);
18
- }
19
-
20
- /**
21
- * @param {string} wikitext wikitext
22
- * @param {boolean} closed 是否闭合
23
- * @param {accum} accum
24
- */
25
- constructor(wikitext, closed = true, config = Parser.getConfig(), accum = []) {
26
- super(wikitext, config, accum);
27
- this.closed = closed;
28
- Object.defineProperty(this, 'closed', {enumerable: false});
29
- }
30
-
31
- /** @override */
32
- cloneNode() {
33
- return Parser.run(() => new CommentToken(String(this.firstChild), this.closed, this.getAttribute('config')));
34
- }
35
-
36
- /**
37
- * @override
38
- * @param {string} selector
39
- */
40
- toString(selector) {
41
- if (!this.closed && this.nextSibling) {
42
- Parser.error('自动闭合HTML注释', this);
43
- this.closed = true;
44
- }
45
- return selector && this.matches(selector)
46
- ? ''
47
- : `<!--${String(this.firstChild)}${this.closed ? '-->' : ''}`;
48
- }
49
-
50
- /** @override */
51
- getPadding() {
52
- return 4;
53
- }
54
- }
55
-
56
- Parser.classes.CommentToken = __filename;
57
- module.exports = CommentToken;
package/src/nowiki/dd.js DELETED
@@ -1,59 +0,0 @@
1
- 'use strict';
2
-
3
- const Parser = require('../..'),
4
- NowikiToken = require('.');
5
-
6
- /**
7
- * :
8
- * @classdesc `{childNodes: [AstText]}`
9
- */
10
- class DdToken extends NowikiToken {
11
- type = 'dd';
12
-
13
- /** 是否包含<dt> */
14
- get dt() {
15
- return String(this).includes(';');
16
- }
17
-
18
- /** 是否包含<ul> */
19
- get ul() {
20
- return String(this).includes('*');
21
- }
22
-
23
- /** 是否包含<ol> */
24
- get ol() {
25
- return String(this).includes('#');
26
- }
27
-
28
- /** 缩进数 */
29
- get indent() {
30
- return String(this).split(':').length - 1;
31
- }
32
-
33
- set indent(indent) {
34
- if (this.type === 'dd') {
35
- if (typeof indent !== 'number') {
36
- this.typeError('set indent', 'Number');
37
- } else if (!Number.isInteger(indent) || indent < 0) {
38
- throw new RangeError(`indent 应为自然数!${indent}`);
39
- }
40
- this.setText(':'.repeat(indent));
41
- }
42
- }
43
-
44
- /**
45
- * @override
46
- * @param {string} str 新文本
47
- * @throws `RangeError` 错误的列表语法
48
- */
49
- setText(str) {
50
- const src = this.type === 'dd' ? ':' : ';:*#';
51
- if (new RegExp(`[^${src}]`, 'u').test(str)) {
52
- throw new RangeError(`${this.constructor.name} 仅能包含${[...src].map(c => `"${c}"`).join('、')}!`);
53
- }
54
- return super.setText(str);
55
- }
56
- }
57
-
58
- Parser.classes.DdToken = __filename;
59
- module.exports = DdToken;
@@ -1,51 +0,0 @@
1
- 'use strict';
2
-
3
- const hidden = require('../../mixin/hidden'),
4
- Parser = require('../..'),
5
- NowikiToken = require('.');
6
-
7
- /**
8
- * 状态开关
9
- * @classdesc `{childNodes: [AstText]}`
10
- */
11
- class DoubleUnderscoreToken extends hidden(NowikiToken) {
12
- type = 'double-underscore';
13
-
14
- /**
15
- * @param {string} word 状态开关名
16
- * @param {accum} accum
17
- */
18
- constructor(word, config = Parser.getConfig(), accum = []) {
19
- super(word, config, accum);
20
- this.setAttribute('name', word.toLowerCase());
21
- }
22
-
23
- /** @override */
24
- cloneNode() {
25
- return Parser.run(() => new DoubleUnderscoreToken(String(this.firstChild), this.getAttribute('config')));
26
- }
27
-
28
- /**
29
- * @override
30
- * @param {string} selector
31
- */
32
- toString(selector) {
33
- return selector && this.matches(selector) ? '' : `__${String(this.firstChild)}__`;
34
- }
35
-
36
- /** @override */
37
- getPadding() {
38
- return 2;
39
- }
40
-
41
- /**
42
- * @override
43
- * @throws `Error` 禁止修改
44
- */
45
- setText() {
46
- throw new Error(`禁止修改 ${this.constructor.name}!`);
47
- }
48
- }
49
-
50
- Parser.classes.DoubleUnderscoreToken = __filename;
51
- module.exports = DoubleUnderscoreToken;
package/src/nowiki/hr.js DELETED
@@ -1,41 +0,0 @@
1
- 'use strict';
2
-
3
- const sol = require('../../mixin/sol'),
4
- Parser = require('../..'),
5
- NowikiToken = require('.');
6
-
7
- /**
8
- * `<hr>`
9
- * @classdesc `{childNodes: [AstText]}`
10
- */
11
- class HrToken extends sol(NowikiToken) {
12
- type = 'hr';
13
-
14
- /**
15
- * @param {number} n 字符串长度
16
- * @param {accum} accum
17
- */
18
- constructor(n, config = Parser.getConfig(), accum = []) {
19
- super('-'.repeat(n), config, accum);
20
- }
21
-
22
- /** @override */
23
- cloneNode() {
24
- return Parser.run(() => new HrToken(String(this).length, this.getAttribute('config')));
25
- }
26
-
27
- /**
28
- * @override
29
- * @param {string} str 新文本
30
- * @throws `RangeError` 错误的\<hr\>语法
31
- */
32
- setText(str) {
33
- if (str.length < 4 || /[^-]/u.test(str)) {
34
- throw new RangeError('<hr>总是写作不少于4个的连续"-"!');
35
- }
36
- return super.setText(str);
37
- }
38
- }
39
-
40
- Parser.classes.HrToken = __filename;
41
- module.exports = HrToken;
@@ -1,44 +0,0 @@
1
- 'use strict';
2
-
3
- const fixedToken = require('../../mixin/fixedToken'),
4
- Parser = require('../..'),
5
- Token = require('..'),
6
- AstText = require('../../lib/text');
7
-
8
- /**
9
- * 纯文字Token,不会被解析
10
- * @classdesc `{childNodes: [AstText]}`
11
- */
12
- class NowikiToken extends fixedToken(Token) {
13
- type = 'ext-inner';
14
-
15
- /**
16
- * @param {string} wikitext wikitext
17
- * @param {accum} accum
18
- */
19
- constructor(wikitext, config = Parser.getConfig(), accum = []) {
20
- super(wikitext, config, true, accum);
21
- }
22
-
23
- /**
24
- * @override
25
- * @this {NowikiToken & {firstChild: AstText, constructor: typeof NowikiToken}}
26
- */
27
- cloneNode() {
28
- const {constructor, firstChild: {data}, type} = this,
29
- token = Parser.run(() => new constructor(data, this.getAttribute('config')));
30
- token.type = type;
31
- return token;
32
- }
33
-
34
- /**
35
- * @override
36
- * @param {string} str 新文本
37
- */
38
- setText(str) {
39
- return super.setText(str, 0);
40
- }
41
- }
42
-
43
- Parser.classes.NowikiToken = __filename;
44
- module.exports = NowikiToken;
@@ -1,16 +0,0 @@
1
- 'use strict';
2
-
3
- const sol = require('../../mixin/sol'),
4
- Parser = require('../..'),
5
- DdToken = require('./dd');
6
-
7
- /**
8
- * ;:*#
9
- * @classdesc `{childNodes: [AstText]}`
10
- */
11
- class ListToken extends sol(DdToken) {
12
- type = 'list';
13
- }
14
-
15
- Parser.classes.ListToken = __filename;
16
- module.exports = ListToken;
@@ -1,28 +0,0 @@
1
- 'use strict';
2
-
3
- const hidden = require('../../mixin/hidden'),
4
- Parser = require('../..'),
5
- NowikiToken = require('.');
6
-
7
- /**
8
- * `<noinclude>`和`</noinclude>`,不可进行任何更改
9
- * @classdesc `{childNodes: [AstText]}`
10
- */
11
- class NoincludeToken extends hidden(NowikiToken) {
12
- type = 'noinclude';
13
-
14
- /**
15
- * @override
16
- * @param {string} str 新文本
17
- * @throws `Error` 不可更改
18
- */
19
- setText(str) {
20
- if (/^<\/?(?:(?:no|only)include|includeonly)(?:\s.*)?\/?>$/isu.test(String(this))) {
21
- throw new Error(`${this.constructor.name} 不可更改文字内容!`);
22
- }
23
- return super.setText(str);
24
- }
25
- }
26
-
27
- Parser.classes.NoincludeToken = __filename;
28
- module.exports = NoincludeToken;
@@ -1,36 +0,0 @@
1
- 'use strict';
2
-
3
- const Parser = require('../..'),
4
- NowikiToken = require('.');
5
-
6
- /**
7
- * `<hr>`
8
- * @classdesc `{childNodes: [AstText]}`
9
- */
10
- class QuoteToken extends NowikiToken {
11
- type = 'quote';
12
-
13
- /**
14
- * @param {number} n 字符串长度
15
- * @param {accum} accum
16
- */
17
- constructor(n, config = Parser.getConfig(), accum = []) {
18
- super("'".repeat(n), config, accum);
19
- this.setAttribute('name', String(n));
20
- }
21
-
22
- /**
23
- * @override
24
- * @param {string} str 新文本
25
- * @throws `RangeError` 错误的单引号语法
26
- */
27
- setText(str) {
28
- if (str === "''" || str === "'''" || str === "'''''") {
29
- return super.setText(str);
30
- }
31
- throw new RangeError(`${this.constructor.name} 的内部文本只能为连续 2/3/5 个"'"!`);
32
- }
33
- }
34
-
35
- Parser.classes.QuoteToken = __filename;
36
- module.exports = QuoteToken;
@@ -1,54 +0,0 @@
1
- 'use strict';
2
-
3
- const Parser = require('..'),
4
- Token = require('.');
5
-
6
- /**
7
- * 嵌入时的`<onlyinclude>`
8
- * @classdesc `{childNodes: ...AstText|Token}`
9
- */
10
- class OnlyincludeToken extends Token {
11
- type = 'onlyinclude';
12
-
13
- /** 内部wikitext */
14
- get innerText() {
15
- return this.text();
16
- }
17
-
18
- /**
19
- * @param {string} inner 标签内部wikitext
20
- * @param {accum} accum
21
- */
22
- constructor(inner, config = Parser.getConfig(), accum = []) {
23
- super(inner, config, true, accum);
24
- }
25
-
26
- /** @override */
27
- cloneNode() {
28
- const cloned = this.cloneChildNodes(),
29
- token = Parser.run(() => new OnlyincludeToken(undefined, this.getAttribute('config')));
30
- token.append(...cloned);
31
- return token;
32
- }
33
-
34
- /**
35
- * @override
36
- * @param {string} selector
37
- */
38
- toString(selector) {
39
- return selector && this.matches(selector) ? '' : `<onlyinclude>${super.toString(selector)}</onlyinclude>`;
40
- }
41
-
42
- /** @override */
43
- getPadding() {
44
- return 13;
45
- }
46
-
47
- /** @override */
48
- isPlain() {
49
- return true;
50
- }
51
- }
52
-
53
- Parser.classes.OnlyincludeToken = __filename;
54
- module.exports = OnlyincludeToken;
package/src/parameter.js DELETED
@@ -1,187 +0,0 @@
1
- 'use strict';
2
-
3
- const {noWrap} = require('../util/string'),
4
- fixedToken = require('../mixin/fixedToken'),
5
- Parser = require('..'),
6
- Token = require('.');
7
-
8
- /**
9
- * 模板或魔术字参数
10
- * @classdesc `{childNodes: [AtomToken, Token]}`
11
- */
12
- class ParameterToken extends fixedToken(Token) {
13
- type = 'parameter';
14
-
15
- /** 是否是匿名参数 */
16
- get anon() {
17
- return this.firstElementChild.childNodes.length === 0;
18
- }
19
-
20
- /** getValue()的getter */
21
- get value() {
22
- return this.getValue();
23
- }
24
-
25
- set value(value) {
26
- this.setValue(value);
27
- }
28
-
29
- /**
30
- * @param {string|number} key 参数名
31
- * @param {string} value 参数值
32
- * @param {accum} accum
33
- */
34
- constructor(key, value, config = Parser.getConfig(), accum = []) {
35
- super(undefined, config, true, accum);
36
- const AtomToken = require('./atom');
37
- const keyToken = new AtomToken(typeof key === 'number' ? undefined : key, 'parameter-key', config, accum, {
38
- 'Stage-2': ':', '!HeadingToken': '',
39
- }),
40
- token = new Token(value, config, true, accum);
41
- token.type = 'parameter-value';
42
- this.append(keyToken, token.setAttribute('stage', 2));
43
- }
44
-
45
- /** @override */
46
- cloneNode() {
47
- const [key, value] = this.cloneChildNodes(),
48
- config = this.getAttribute('config');
49
- return Parser.run(() => {
50
- const token = new ParameterToken(this.anon ? Number(this.name) : undefined, undefined, config);
51
- token.firstElementChild.safeReplaceWith(key);
52
- token.lastElementChild.safeReplaceWith(value);
53
- return token.afterBuild();
54
- });
55
- }
56
-
57
- /** @override */
58
- afterBuild() {
59
- if (!this.anon) {
60
- const name = this.firstElementChild.text().trim(),
61
- {parentNode} = this;
62
- this.setAttribute('name', name);
63
- if (parentNode && parentNode instanceof require('./transclude')) {
64
- parentNode.getAttribute('keys').add(name);
65
- parentNode.getArgs(name, false, false).add(this);
66
- }
67
- }
68
- const /** @type {AstListener} */ parameterListener = ({prevTarget}, data) => {
69
- if (!this.anon) { // 匿名参数不管怎么变动还是匿名
70
- const {firstElementChild, name} = this;
71
- if (prevTarget === firstElementChild) {
72
- const newKey = firstElementChild.text().trim();
73
- data.oldKey = name;
74
- data.newKey = newKey;
75
- this.setAttribute('name', newKey);
76
- }
77
- }
78
- };
79
- this.addEventListener(['remove', 'insert', 'replace', 'text'], parameterListener);
80
- return this;
81
- }
82
-
83
- /**
84
- * @override
85
- * @param {string} selector
86
- * @returns {string}
87
- */
88
- toString(selector) {
89
- return this.anon && !(selector && this.matches(selector))
90
- ? this.lastElementChild.toString(selector)
91
- : super.toString(selector, '=');
92
- }
93
-
94
- /** @override */
95
- getGaps() {
96
- return this.anon ? 0 : 1;
97
- }
98
-
99
- /**
100
- * @override
101
- * @returns {string}
102
- */
103
- text() {
104
- return this.anon ? this.lastElementChild.text() : super.text('=');
105
- }
106
-
107
- /**
108
- * @override
109
- * @param {ParameterToken} token 待替换的节点
110
- * @complexity `n`
111
- */
112
- safeReplaceWith(token) {
113
- Parser.warn(`${this.constructor.name}.safeReplaceWith 方法退化到 replaceWith。`);
114
- return this.replaceWith(token);
115
- }
116
-
117
- /** 获取参数值 */
118
- getValue() {
119
- const value = this.lastElementChild.text();
120
- return this.anon && this.parentNode?.matches('template, magic-word#invoke') ? value : value.trim();
121
- }
122
-
123
- /**
124
- * 设置参数值
125
- * @param {string} value 参数值
126
- * @throws `SyntaxError` 非法的模板参数
127
- */
128
- setValue(value) {
129
- value = String(value);
130
- const templateLike = this.parentNode?.matches('template, magic-word#invoke'),
131
- wikitext = `{{${templateLike ? ':T|' : 'lc:'}${this.anon ? '' : '1='}${value}}}`,
132
- root = Parser.parse(wikitext, this.getAttribute('include'), 2, this.getAttribute('config')),
133
- {childNodes: {length}, firstElementChild} = root,
134
- /** @type {ParameterToken} */ lastElementChild = firstElementChild?.lastElementChild;
135
- if (length !== 1 || !firstElementChild?.matches(templateLike ? 'template#T' : 'magic-word#lc')
136
- || firstElementChild.childNodes.length !== 2
137
- || lastElementChild.anon !== this.anon || lastElementChild.name !== '1'
138
- ) {
139
- throw new SyntaxError(`非法的模板参数:${noWrap(value)}`);
140
- }
141
- const {lastChild} = lastElementChild;
142
- lastElementChild.destroy(true);
143
- this.lastElementChild.safeReplaceWith(lastChild);
144
- }
145
-
146
- /**
147
- * 修改参数名
148
- * @param {string} key 新参数名
149
- * @param {boolean} force 是否无视冲突命名
150
- * @throws `Error` 仅用于模板参数
151
- * @throws `SyntaxError` 非法的模板参数名
152
- * @throws `RangeError` 更名造成重复参数
153
- */
154
- rename(key, force) {
155
- if (typeof key !== 'string') {
156
- this.typeError('rename', 'String');
157
- }
158
- const {parentNode} = this;
159
- // 必须检测是否是TranscludeToken
160
- if (!parentNode || !parentNode.matches('template, magic-word#invoke')
161
- || !(parentNode instanceof require('./transclude'))
162
- ) {
163
- throw new Error(`${this.constructor.name}.rename 方法仅用于模板参数!`);
164
- }
165
- const root = Parser.parse(`{{:T|${key}=}}`, this.getAttribute('include'), 2, this.getAttribute('config')),
166
- {childNodes: {length}, firstElementChild} = root;
167
- if (length !== 1 || !firstElementChild?.matches('template#T') || firstElementChild.childNodes.length !== 2) {
168
- throw new SyntaxError(`非法的模板参数名:${key}`);
169
- }
170
- const {lastElementChild} = firstElementChild,
171
- {name, firstChild} = lastElementChild;
172
- if (this.name === name) {
173
- Parser.warn('未改变实际参数名', name);
174
- } else if (parentNode.hasArg(name)) {
175
- if (force) {
176
- Parser.warn('参数更名造成重复参数', name);
177
- } else {
178
- throw new RangeError(`参数更名造成重复参数:${name}`);
179
- }
180
- }
181
- lastElementChild.destroy(true);
182
- this.firstElementChild.safeReplaceWith(firstChild);
183
- }
184
- }
185
-
186
- Parser.classes.ParameterToken = __filename;
187
- module.exports = ParameterToken;