wikiparser-node 0.8.1-m → 0.9.0

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 (81) hide show
  1. package/README.md +39 -0
  2. package/config/moegirl.json +1 -0
  3. package/i18n/zh-hans.json +44 -0
  4. package/i18n/zh-hant.json +44 -0
  5. package/index.js +264 -10
  6. package/lib/element.js +507 -33
  7. package/lib/node.js +550 -6
  8. package/lib/ranges.js +130 -0
  9. package/lib/text.js +111 -41
  10. package/lib/title.js +28 -5
  11. package/mixin/attributeParent.js +117 -0
  12. package/mixin/fixedToken.js +40 -0
  13. package/mixin/hidden.js +3 -0
  14. package/mixin/singleLine.js +31 -0
  15. package/mixin/sol.js +54 -0
  16. package/package.json +9 -8
  17. package/parser/brackets.js +9 -2
  18. package/parser/commentAndExt.js +3 -5
  19. package/parser/converter.js +1 -0
  20. package/parser/externalLinks.js +1 -0
  21. package/parser/hrAndDoubleUnderscore.js +1 -0
  22. package/parser/html.js +1 -0
  23. package/parser/links.js +6 -5
  24. package/parser/list.js +1 -0
  25. package/parser/magicLinks.js +5 -4
  26. package/parser/quotes.js +1 -0
  27. package/parser/selector.js +177 -0
  28. package/parser/table.js +1 -0
  29. package/src/arg.js +123 -5
  30. package/src/atom/hidden.js +2 -0
  31. package/src/atom/index.js +17 -0
  32. package/src/attribute.js +191 -8
  33. package/src/attributes.js +311 -8
  34. package/src/charinsert.js +97 -0
  35. package/src/converter.js +108 -2
  36. package/src/converterFlags.js +190 -3
  37. package/src/converterRule.js +185 -4
  38. package/src/extLink.js +122 -2
  39. package/src/gallery.js +59 -11
  40. package/src/hasNowiki/index.js +12 -0
  41. package/src/hasNowiki/pre.js +12 -0
  42. package/src/heading.js +57 -6
  43. package/src/html.js +133 -12
  44. package/src/imageParameter.js +232 -38
  45. package/src/imagemap.js +65 -6
  46. package/src/imagemapLink.js +14 -2
  47. package/src/index.js +537 -8
  48. package/src/link/category.js +32 -1
  49. package/src/link/file.js +173 -11
  50. package/src/link/galleryImage.js +63 -5
  51. package/src/link/index.js +268 -9
  52. package/src/magicLink.js +92 -11
  53. package/src/nested/choose.js +1 -0
  54. package/src/nested/combobox.js +1 -0
  55. package/src/nested/index.js +31 -7
  56. package/src/nested/references.js +1 -0
  57. package/src/nowiki/comment.js +27 -3
  58. package/src/nowiki/dd.js +47 -1
  59. package/src/nowiki/doubleUnderscore.js +31 -1
  60. package/src/nowiki/hr.js +20 -1
  61. package/src/nowiki/index.js +25 -3
  62. package/src/nowiki/list.js +5 -2
  63. package/src/nowiki/noinclude.js +14 -0
  64. package/src/nowiki/quote.js +17 -3
  65. package/src/onlyinclude.js +26 -1
  66. package/src/paramTag/index.js +27 -4
  67. package/src/paramTag/inputbox.js +4 -1
  68. package/src/parameter.js +150 -8
  69. package/src/syntax.js +68 -0
  70. package/src/table/index.js +941 -4
  71. package/src/table/td.js +229 -10
  72. package/src/table/tr.js +249 -4
  73. package/src/tagPair/ext.js +36 -9
  74. package/src/tagPair/include.js +24 -0
  75. package/src/tagPair/index.js +51 -2
  76. package/src/transclude.js +547 -29
  77. package/tool/index.js +1202 -0
  78. package/util/debug.js +73 -0
  79. package/util/lint.js +8 -7
  80. package/util/string.js +67 -1
  81. package/config/minimum.json +0 -142
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wikiparser-node",
3
- "version": "0.8.1-m",
3
+ "version": "0.9.0",
4
4
  "description": "A Node.js parser for MediaWiki markup with AST",
5
5
  "keywords": [
6
6
  "mediawiki",
@@ -15,22 +15,23 @@
15
15
  "author": "Bhsd",
16
16
  "files": [
17
17
  "/index.js",
18
- "/parser/",
19
- "/util/",
18
+ "/config/",
19
+ "/i18n/",
20
20
  "/lib/",
21
+ "/mixin/",
22
+ "/parser/",
21
23
  "/src/",
22
- "/config/",
23
- "/mixin/"
24
+ "/tool/",
25
+ "/util/"
24
26
  ],
25
27
  "repository": {
26
28
  "type": "git",
27
29
  "url": "git+https://github.com/bhsd-harry/wikiparser-node.git"
28
30
  },
29
31
  "scripts": {
30
- "test": "eslint .",
32
+ "test": "eslint . && node test/test.js",
31
33
  "real": "node test/real.js",
32
- "single": "node test/single.js",
33
- "publish": "npm publish --tag mini"
34
+ "single": "node --prof test/single.js && node --prof-process isolate-0x*-v8.log > test/processed.txt && rm isolate-0x*-v8.log"
34
35
  },
35
36
  "devDependencies": {
36
37
  "@types/node": "^17.0.23",
@@ -14,6 +14,7 @@ const {removeComment} = require('../util/string'),
14
14
  */
15
15
  const parseBrackets = (text, config = Parser.getConfig(), accum = []) => {
16
16
  const source = `${config.excludes.includes('heading') ? '' : '^(\0\\d+c\x7F)*={1,6}|'}\\[\\[|\\{{2,}|-\\{(?!\\{)`,
17
+ {parserFunction: [,,, subst]} = config,
17
18
  /** @type {BracketExecArray[]} */ stack = [],
18
19
  closes = {'=': '\n', '{': '\\}{2,}|\\|', '-': '\\}-', '[': '\\]\\]'},
19
20
  /** @type {Record<string, string>} */ marks = {'!': '!', '!!': '+', '(!': '{', '!)': '}', '!-': '-', '=': '~'};
@@ -62,12 +63,17 @@ const parseBrackets = (text, config = Parser.getConfig(), accum = []) => {
62
63
  let skip = false,
63
64
  ch = 't';
64
65
  if (close.length === 3) {
65
- new ArgToken(parts.map(part => part.join('=')), config, accum);
66
+ const argParts = parts.map(part => part.join('=')),
67
+ str = argParts.length > 1 && removeComment(argParts[1]).trim();
68
+ new ArgToken(argParts, config, accum);
69
+ if (str && str.endsWith(':') && subst.includes(str.slice(0, -1).toLowerCase())) {
70
+ ch = 's';
71
+ }
66
72
  } else {
67
73
  const name = removeComment(parts[0][0]).trim();
68
74
  if (name in marks) {
69
75
  ch = marks[name]; // 标记{{!}}等
70
- } else if (/^(?:fullurl|canonicalurl|filepath):.|^server$/iu.test(name)) {
76
+ } else if (/^(?:filepath|(?:full|canonical)urle?):.|^server$/iu.test(name)) {
71
77
  ch = 'm';
72
78
  } else if (/^#vardefine:./iu.test(name)) {
73
79
  ch = 'c';
@@ -116,4 +122,5 @@ const parseBrackets = (text, config = Parser.getConfig(), accum = []) => {
116
122
  return text;
117
123
  };
118
124
 
125
+ Parser.parsers.parseBrackets = __filename;
119
126
  module.exports = parseBrackets;
@@ -20,12 +20,9 @@ const parseCommentAndExt = (text, config = Parser.getConfig(), accum = [], inclu
20
20
  const str = `\0${accum.length}e\x7F`;
21
21
  new OnlyincludeToken(inner, config, accum);
22
22
  return str;
23
- }).replace(/(^|\0\d+e\x7F)(.*?)(?=$|\0\d+e\x7F)/gsu, (_, lead, substr) => {
24
- if (substr === '') {
25
- return lead;
26
- }
23
+ }).replace(/(?<=^|\0\d+e\x7F)[^\0]+(?=$|\0\d+e\x7F)/gu, substr => {
27
24
  new NoincludeToken(substr, config, accum);
28
- return `${lead}\0${accum.length - 1}c\x7F`;
25
+ return `\0${accum.length - 1}c\x7F`;
29
26
  });
30
27
  }
31
28
  const ext = config.ext.join('|'),
@@ -58,4 +55,5 @@ const parseCommentAndExt = (text, config = Parser.getConfig(), accum = [], inclu
58
55
  );
59
56
  };
60
57
 
58
+ Parser.parsers.parseCommentAndExt = __filename;
61
59
  module.exports = parseCommentAndExt;
@@ -42,4 +42,5 @@ const parseConverter = (wikitext, config = Parser.getConfig(), accum = []) => {
42
42
  return wikitext;
43
43
  };
44
44
 
45
+ Parser.parsers.parseConverter = __filename;
45
46
  module.exports = parseConverter;
@@ -29,4 +29,5 @@ const parseExternalLinks = (wikitext, config = Parser.getConfig(), accum = []) =
29
29
  });
30
30
  };
31
31
 
32
+ Parser.parsers.parseExternalLinks = __filename;
32
33
  module.exports = parseExternalLinks;
@@ -34,4 +34,5 @@ const parseHrAndDoubleUnderscore = ({firstChild: {data}, type, name}, config = P
34
34
  return type === 'root' || type === 'ext-inner' && name === 'poem' ? data : data.slice(1);
35
35
  };
36
36
 
37
+ Parser.parsers.parseHrAndDoubleUnderscore = __filename;
37
38
  module.exports = parseHrAndDoubleUnderscore;
package/parser/html.js CHANGED
@@ -38,4 +38,5 @@ const parseHtml = (wikitext, config = Parser.getConfig(), accum = []) => {
38
38
  return text;
39
39
  };
40
40
 
41
+ Parser.parsers.parseHtml = __filename;
41
42
  module.exports = parseHtml;
package/parser/links.js CHANGED
@@ -44,12 +44,12 @@ const parseLinks = (wikitext, config = Parser.getConfig(), accum = []) => {
44
44
  continue;
45
45
  }
46
46
  const title = Parser.normalizeTitle(link, 0, false, config, true, true, true),
47
- {ns, valid} = title;
47
+ {ns, interwiki, valid} = title;
48
48
  if (!valid) {
49
49
  s += `[[${x}`;
50
50
  continue;
51
51
  } else if (mightBeImg) {
52
- if (ns !== 6) {
52
+ if (interwiki || ns !== 6) {
53
53
  s += `[[${x}`;
54
54
  continue;
55
55
  }
@@ -79,15 +79,16 @@ const parseLinks = (wikitext, config = Parser.getConfig(), accum = []) => {
79
79
  s += `\0${accum.length}l\x7F${after}`;
80
80
  let SomeLinkToken = LinkToken;
81
81
  if (!force) {
82
- if (ns === 6) {
82
+ if (!interwiki && ns === 6) {
83
83
  SomeLinkToken = FileToken;
84
- } else if (ns === 14) {
84
+ } else if (!interwiki && ns === 14) {
85
85
  SomeLinkToken = CategoryToken;
86
86
  }
87
87
  }
88
- new SomeLinkToken(link, text, title, config, accum, delimiter);
88
+ new SomeLinkToken(link, text, config, accum, delimiter);
89
89
  }
90
90
  return s;
91
91
  };
92
92
 
93
+ Parser.parsers.parseLinks = __filename;
93
94
  module.exports = parseLinks;
package/parser/list.js CHANGED
@@ -55,4 +55,5 @@ const parseList = (text, config = Parser.getConfig(), accum = []) => {
55
55
  return text;
56
56
  };
57
57
 
58
+ Parser.parsers.parseList = __filename;
58
59
  module.exports = parseList;
@@ -10,10 +10,10 @@ const {extUrlChar, extUrlCharFirst} = require('../util/string'),
10
10
  * @param {accum} accum
11
11
  */
12
12
  const parseMagicLinks = (wikitext, config = Parser.getConfig(), accum = []) => {
13
- const regex = new RegExp(`(^|[^\\p{L}\\d_])(?:${config.protocol})(${extUrlCharFirst}${extUrlChar})`, 'giu');
14
- return wikitext.replace(regex, /** @param {string} p1 */ (m, lead, p1) => {
13
+ const regex = new RegExp(`(?<![\\p{L}\\d_])(?:${config.protocol})(${extUrlCharFirst}${extUrlChar})`, 'giu');
14
+ return wikitext.replace(regex, /** @param {string} p1 */ (m, p1) => {
15
15
  let trail = '',
16
- url = lead ? m.slice(1) : m;
16
+ url = m;
17
17
  const m2 = /&(?:lt|gt|nbsp|#x0*(?:3[ce]|a0)|#0*(?:6[02]|160));/iu.exec(url);
18
18
  if (m2) {
19
19
  trail = url.slice(m2.index);
@@ -33,8 +33,9 @@ const parseMagicLinks = (wikitext, config = Parser.getConfig(), accum = []) => {
33
33
  return m;
34
34
  }
35
35
  new MagicLinkToken(url, false, config, accum);
36
- return `${lead}\0${accum.length - 1}w\x7F${trail}`;
36
+ return `\0${accum.length - 1}w\x7F${trail}`;
37
37
  });
38
38
  };
39
39
 
40
+ Parser.parsers.parseMagicLinks = __filename;
40
41
  module.exports = parseMagicLinks;
package/parser/quotes.js CHANGED
@@ -60,4 +60,5 @@ const parseQuotes = (text, config = Parser.getConfig(), accum = []) => {
60
60
  return arr.join('');
61
61
  };
62
62
 
63
+ Parser.parsers.parseQuotes = __filename;
63
64
  module.exports = parseQuotes;
@@ -0,0 +1,177 @@
1
+ 'use strict';
2
+
3
+ const Parser = require('..');
4
+
5
+ const /** @type {Set<pseudo>} */ simplePseudos = new Set([
6
+ 'root',
7
+ 'first-child',
8
+ 'first-of-type',
9
+ 'last-child',
10
+ 'last-of-type',
11
+ 'only-child',
12
+ 'only-of-type',
13
+ 'empty',
14
+ 'parent',
15
+ 'header',
16
+ 'hidden',
17
+ 'visible',
18
+ 'only-whitespace',
19
+ 'local-link',
20
+ 'read-only',
21
+ 'read-write',
22
+ 'invalid',
23
+ 'required',
24
+ 'optional',
25
+ ]),
26
+ /** @type {pseudo[]} */ complexPseudos = [
27
+ 'is',
28
+ 'not',
29
+ 'nth-child',
30
+ 'nth-of-type',
31
+ 'nth-last-child',
32
+ 'nth-last-of-type',
33
+ 'contains',
34
+ 'has',
35
+ 'lang',
36
+ ],
37
+ specialChars = [
38
+ ['[', '&lbrack;'],
39
+ [']', '&rbrack;'],
40
+ ['(', '&lpar;'],
41
+ [')', '&rpar;'],
42
+ ['"', '&quot;'],
43
+ [`'`, '&apos;'],
44
+ [':', '&colon;'],
45
+ ['\\', '&bsol;'],
46
+ ['&', '&amp;'],
47
+ ],
48
+ pseudoRegex = new RegExp(`:(${complexPseudos.join('|')})$`, 'u'),
49
+ regularRegex = /[[(,>+~]|\s+/u,
50
+ attributeRegex = /^\s*(\w+)\s*(?:([~|^$*!]?=)\s*("[^"]*"|'[^']*'|[^\s[\]]+)(?:\s+(i))?\s*)?\]/u,
51
+ functionRegex = /^(\s*"[^"]*"\s*|\s*'[^']*'\s*|[^()]*)\)/u,
52
+ grouping = new Set([',', '>', '+', '~']),
53
+ combinator = new Set(['>', '+', '~', '']);
54
+
55
+ /**
56
+ * 清理转义符号
57
+ * @param {string} selector
58
+ */
59
+ const sanitize = selector => {
60
+ for (const [c, escaped] of specialChars) {
61
+ selector = selector.replaceAll(`\\${c}`, escaped);
62
+ }
63
+ return selector;
64
+ };
65
+
66
+ /**
67
+ * 还原转义符号
68
+ * @param {string|undefined} selector
69
+ */
70
+ const desanitize = selector => {
71
+ if (selector === undefined) {
72
+ return undefined;
73
+ }
74
+ for (const [c, escaped] of specialChars) {
75
+ selector = selector.replaceAll(escaped, c);
76
+ }
77
+ return selector.trim();
78
+ };
79
+
80
+ /**
81
+ * 去除首尾的引号
82
+ * @param {string|undefined} val 属性值或伪选择器函数的参数
83
+ */
84
+ const deQuote = val => {
85
+ if (val === undefined) {
86
+ return undefined;
87
+ }
88
+ const quotes = /^(["']).*\1$/u.exec(val)?.[1];
89
+ return quotes ? val.slice(1, -1) : val;
90
+ };
91
+
92
+ /**
93
+ * 解析简单伪选择器
94
+ * @param {SelectorArray} step 当前顶部
95
+ * @param {string} str 不含属性和复杂伪选择器的语句
96
+ * @throws `SyntaxError` 非法的选择器
97
+ */
98
+ const pushSimple = (step, str) => {
99
+ const pieces = str.trim().split(':'),
100
+ // eslint-disable-next-line unicorn/explicit-length-check
101
+ i = pieces.slice(1).findIndex(pseudo => simplePseudos.has(pseudo)) + 1 || pieces.length;
102
+ if (pieces.slice(i).some(pseudo => !simplePseudos.has(pseudo))) {
103
+ throw new SyntaxError(`非法的选择器!\n${str}\n可能需要将':'转义为'\\:'。`);
104
+ }
105
+ step.push(desanitize(pieces.slice(0, i).join(':')), ...pieces.slice(i).map(piece => `:${piece}`));
106
+ };
107
+
108
+ /**
109
+ * 解析选择器
110
+ * @param {string} selector
111
+ * @throws `SyntaxError` 非法的选择器
112
+ */
113
+ const parseSelector = selector => {
114
+ selector = selector.trim();
115
+ const /** @type {SelectorArray[][]} */ stack = [[[]]];
116
+ let sanitized = sanitize(selector),
117
+ regex = regularRegex,
118
+ mt = regex.exec(sanitized),
119
+ [condition] = stack,
120
+ [step] = condition;
121
+ while (mt) {
122
+ let {0: syntax, index} = mt;
123
+ if (syntax.trim() === '') {
124
+ index += syntax.length;
125
+ const char = sanitized[index];
126
+ syntax = grouping.has(char) ? char : '';
127
+ }
128
+ if (syntax === ',') { // 情形1:并列
129
+ pushSimple(step, sanitized.slice(0, index));
130
+ condition = [[]];
131
+ [step] = condition;
132
+ stack.push(condition);
133
+ } else if (combinator.has(syntax)) { // 情形2:关系
134
+ pushSimple(step, sanitized.slice(0, index));
135
+ if (!step.some(Boolean)) {
136
+ throw new SyntaxError(`非法的选择器!\n${selector}\n可能需要通用选择器'*'。`);
137
+ }
138
+ step.relation = syntax;
139
+ step = [];
140
+ condition.push(step);
141
+ } else if (syntax === '[') { // 情形3:属性开启
142
+ pushSimple(step, sanitized.slice(0, index));
143
+ regex = attributeRegex;
144
+ } else if (syntax.endsWith(']')) { // 情形4:属性闭合
145
+ mt[3] = desanitize(deQuote(mt[3]));
146
+ step.push(mt.slice(1));
147
+ regex = regularRegex;
148
+ } else if (syntax === '(') { // 情形5:伪选择器开启
149
+ const pseudoExec = pseudoRegex.exec(sanitized.slice(0, index));
150
+ if (!pseudoExec) {
151
+ throw new SyntaxError(`非法的选择器!\n${desanitize(sanitized)}\n请检查伪选择器是否存在。`);
152
+ }
153
+ pushSimple(step, sanitized.slice(0, pseudoExec.index));
154
+ step.push(pseudoExec[1]); // 临时存放复杂伪选择器
155
+ regex = functionRegex;
156
+ } else { // 情形6:伪选择器闭合
157
+ const /** @type {pseudo} */ pseudo = step.pop();
158
+ mt.push(pseudo);
159
+ mt[1] = deQuote(mt[1]);
160
+ step.push(mt.slice(1));
161
+ regex = regularRegex;
162
+ }
163
+ sanitized = sanitized.slice(index + syntax.length);
164
+ if (grouping.has(syntax)) {
165
+ sanitized = sanitized.trim();
166
+ }
167
+ mt = regex.exec(sanitized);
168
+ }
169
+ if (regex === regularRegex) {
170
+ pushSimple(step, sanitized);
171
+ return stack;
172
+ }
173
+ throw new SyntaxError(`非法的选择器!\n${selector}\n检测到未闭合的'${regex === attributeRegex ? '[' : '('}'`);
174
+ };
175
+
176
+ Parser.parsers.parseSelector = __filename;
177
+ module.exports = parseSelector;
package/parser/table.js CHANGED
@@ -110,4 +110,5 @@ const parseTable = ({firstChild: {data}, type, name}, config = Parser.getConfig(
110
110
  return out.slice(1);
111
111
  };
112
112
 
113
+ Parser.parsers.parseTable = __filename;
113
114
  module.exports = parseTable;
package/src/arg.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {text} = require('../util/string'),
3
+ const {text, noWrap} = require('../util/string'),
4
4
  {generateForSelf, generateForChild} = require('../util/lint'),
5
5
  Parser = require('..'),
6
6
  Token = require('.');
@@ -24,11 +24,13 @@ class ArgToken extends Token {
24
24
  */
25
25
  constructor(parts, config = Parser.getConfig(), accum = []) {
26
26
  super(undefined, config, true, accum, {
27
+ AtomToken: 0, Token: 1, HiddenToken: '2:',
27
28
  });
28
29
  for (let i = 0; i < parts.length; i++) {
29
30
  if (i === 0 || i > 1) {
30
31
  const AtomToken = i === 0 ? require('./atom') : require('./atom/hidden');
31
32
  const token = new AtomToken(parts[i], i === 0 ? 'arg-name' : undefined, config, accum, {
33
+ 'Stage-2': ':', '!HeadingToken': '',
32
34
  });
33
35
  super.insertAt(token);
34
36
  } else {
@@ -37,13 +39,15 @@ class ArgToken extends Token {
37
39
  super.insertAt(token.setAttribute('stage', 2));
38
40
  }
39
41
  }
42
+ this.getAttribute('protectChildren')(0);
40
43
  }
41
44
 
42
45
  /**
43
46
  * @override
47
+ * @param {string} selector
44
48
  */
45
49
  toString(selector) {
46
- return `{{{${super.toString(selector, '|')}}}}`;
50
+ return selector && this.matches(selector) ? '' : `{{{${super.toString(selector, '|')}}}}`;
47
51
  }
48
52
 
49
53
  /**
@@ -64,14 +68,19 @@ class ArgToken extends Token {
64
68
  return 1;
65
69
  }
66
70
 
71
+ /** @override */
72
+ print() {
73
+ return super.print({pre: '{{{', post: '}}}', sep: '|'});
74
+ }
75
+
67
76
  /**
68
77
  * @override
69
78
  * @param {number} start 起始位置
70
79
  * @returns {LintError[]}
71
80
  */
72
- lint(start = 0) {
81
+ lint(start = this.getAbsoluteIndex()) {
73
82
  if (!this.getAttribute('include')) {
74
- return [generateForSelf(this, {start}, '未预期的模板参数')];
83
+ return [generateForSelf(this, {start}, 'unexpected template argument')];
75
84
  }
76
85
  const {childNodes: [argName, argDefault, ...rest]} = this,
77
86
  errors = argName.lint(start + 3);
@@ -80,10 +89,119 @@ class ArgToken extends Token {
80
89
  }
81
90
  if (rest.length > 0) {
82
91
  const rect = {start, ...this.getRootNode().posFromIndex(start)};
83
- errors.push(...rest.map(child => generateForChild(child, rect, '三重括号内的不可见部分')));
92
+ errors.push(...rest.map(child => {
93
+ const error = generateForChild(child, rect, 'invisible content inside triple brackets'),
94
+ {startIndex, startCol, excerpt} = error;
95
+ return {...error, startIndex: startIndex - 1, startCol: startCol - 1, excerpt: `|${excerpt}`};
96
+ }));
84
97
  }
85
98
  return errors;
86
99
  }
100
+
101
+ /** @override */
102
+ cloneNode() {
103
+ const [name, ...cloned] = this.cloneChildNodes();
104
+ return Parser.run(() => {
105
+ const token = new ArgToken([''], this.getAttribute('config'));
106
+ token.firstChild.safeReplaceWith(name);
107
+ token.append(...cloned);
108
+ token.afterBuild();
109
+ return token;
110
+ });
111
+ }
112
+
113
+ /** @override */
114
+ afterBuild() {
115
+ this.setAttribute('name', this.firstChild.text().trim());
116
+ const /** @type {AstListener} */ argListener = ({prevTarget}) => {
117
+ if (prevTarget === this.firstChild) {
118
+ this.setAttribute('name', prevTarget.text().trim());
119
+ }
120
+ };
121
+ this.addEventListener(['remove', 'insert', 'replace', 'text'], argListener);
122
+ }
123
+
124
+ /**
125
+ * 移除无效部分
126
+ * @complexity `n`
127
+ */
128
+ removeRedundant() {
129
+ Parser.run(() => {
130
+ for (let i = this.length - 1; i > 1; i--) {
131
+ super.removeAt(i);
132
+ }
133
+ });
134
+ }
135
+
136
+ /**
137
+ * 移除子节点,且在移除`arg-default`子节点时自动移除全部多余子节点
138
+ * @param {number} i 移除位置
139
+ * @returns {Token}
140
+ */
141
+ removeAt(i) {
142
+ if (i === 1) {
143
+ this.removeRedundant();
144
+ }
145
+ return super.removeAt(i);
146
+ }
147
+
148
+ /**
149
+ * @override
150
+ * @param {Token} token 待插入的子节点
151
+ * @param {number} i 插入位置
152
+ * @throws `RangeError` 不可插入多余子节点
153
+ */
154
+ insertAt(token, i = this.length) {
155
+ const j = i < 0 ? i + this.length : i;
156
+ if (j > 1) {
157
+ throw new RangeError(`${this.constructor.name} 不可插入多余的子节点!`);
158
+ }
159
+ super.insertAt(token, i);
160
+ if (j === 1) {
161
+ token.type = 'arg-default';
162
+ }
163
+ return token;
164
+ }
165
+
166
+ /**
167
+ * 设置参数名
168
+ * @param {string} name 新参数名
169
+ * @throws `SyntaxError` 非法的参数名
170
+ */
171
+ setName(name) {
172
+ name = String(name);
173
+ const root = Parser.parse(`{{{${name}}}}`, this.getAttribute('include'), 2, this.getAttribute('config')),
174
+ {length, firstChild: arg} = root;
175
+ if (length !== 1 || arg.type !== 'arg' || arg.length !== 1) {
176
+ throw new SyntaxError(`非法的参数名称:${noWrap(name)}`);
177
+ }
178
+ const {firstChild} = arg;
179
+ arg.destroy(true);
180
+ this.firstChild.safeReplaceWith(firstChild);
181
+ }
182
+
183
+ /**
184
+ * 设置预设值
185
+ * @param {string} value 预设值
186
+ * @throws `SyntaxError` 非法的参数预设值
187
+ */
188
+ setDefault(value) {
189
+ value = String(value);
190
+ const root = Parser.parse(`{{{|${value}}}}`, this.getAttribute('include'), 2, this.getAttribute('config')),
191
+ {length, firstChild: arg} = root;
192
+ if (length !== 1 || arg.type !== 'arg' || arg.length !== 2) {
193
+ throw new SyntaxError(`非法的参数预设值:${noWrap(value)}`);
194
+ }
195
+ const {childNodes: [, oldDefault]} = this,
196
+ {lastChild} = arg;
197
+ arg.destroy(true);
198
+ if (oldDefault) {
199
+ oldDefault.safeReplaceWith(lastChild);
200
+ } else {
201
+ this.insertAt(lastChild);
202
+ }
203
+ }
87
204
  }
88
205
 
206
+ Parser.classes.ArgToken = __filename;
89
207
  module.exports = ArgToken;
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const hidden = require('../../mixin/hidden'),
4
+ Parser = require('../..'),
4
5
  AtomToken = require('.');
5
6
 
6
7
  /** 不可见的节点 */
@@ -8,4 +9,5 @@ class HiddenToken extends hidden(AtomToken) {
8
9
  type = 'hidden';
9
10
  }
10
11
 
12
+ Parser.classes.HiddenToken = __filename;
11
13
  module.exports = HiddenToken;
package/src/atom/index.js CHANGED
@@ -14,6 +14,7 @@ class AtomToken extends Token {
14
14
  * @param {string} wikitext wikitext
15
15
  * @param {string|undefined} type Token.type
16
16
  * @param {accum} accum
17
+ * @param {acceptable} acceptable 可接受的子节点设置
17
18
  */
18
19
  constructor(wikitext, type, config = Parser.getConfig(), accum = [], acceptable = undefined) {
19
20
  super(wikitext, config, true, accum, acceptable);
@@ -21,6 +22,22 @@ class AtomToken extends Token {
21
22
  this.type = type;
22
23
  }
23
24
  }
25
+
26
+ /**
27
+ * @override
28
+ * @this {AtomToken & {constructor: typeof AtomToken}}
29
+ */
30
+ cloneNode() {
31
+ const cloned = this.cloneChildNodes(),
32
+ config = this.getAttribute('config'),
33
+ acceptable = this.getAttribute('acceptable');
34
+ return Parser.run(() => {
35
+ const token = new this.constructor(undefined, this.type, config, [], acceptable);
36
+ token.append(...cloned);
37
+ return token;
38
+ });
39
+ }
24
40
  }
25
41
 
42
+ Parser.classes.AtomToken = __filename;
26
43
  module.exports = AtomToken;