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.
Files changed (155) hide show
  1. package/README.md +44 -32
  2. package/config/.schema.json +1 -8
  3. package/config/default.json +1 -2
  4. package/config/llwiki.json +1 -36
  5. package/config/moegirl.json +1 -45
  6. package/config/zhwiki.json +1 -467
  7. package/dist/index.d.ts +110 -0
  8. package/dist/lib/element.d.ts +48 -0
  9. package/dist/lib/node.d.ts +108 -0
  10. package/dist/lib/text.d.ts +30 -0
  11. package/dist/lib/title.d.ts +15 -0
  12. package/dist/mixin/hidden.d.ts +8 -0
  13. package/dist/parser/brackets.d.ts +12 -0
  14. package/dist/parser/commentAndExt.d.ts +8 -0
  15. package/dist/parser/converter.d.ts +7 -0
  16. package/dist/parser/externalLinks.d.ts +7 -0
  17. package/dist/parser/hrAndDoubleUnderscore.d.ts +11 -0
  18. package/dist/parser/html.d.ts +7 -0
  19. package/dist/parser/links.d.ts +7 -0
  20. package/dist/parser/list.d.ts +7 -0
  21. package/dist/parser/magicLinks.d.ts +7 -0
  22. package/dist/parser/quotes.d.ts +7 -0
  23. package/dist/parser/table.d.ts +11 -0
  24. package/dist/src/arg.d.ts +25 -0
  25. package/dist/src/atom/hidden.d.ts +5 -0
  26. package/dist/src/atom/index.d.ts +14 -0
  27. package/dist/src/attribute.d.ts +42 -0
  28. package/dist/src/attributes.d.ts +33 -0
  29. package/dist/src/converter.d.ts +29 -0
  30. package/dist/src/converterFlags.d.ts +27 -0
  31. package/dist/src/converterRule.d.ts +29 -0
  32. package/dist/src/extLink.d.ts +22 -0
  33. package/dist/src/gallery.d.ts +19 -0
  34. package/dist/src/hasNowiki/index.d.ts +14 -0
  35. package/dist/src/hasNowiki/pre.d.ts +13 -0
  36. package/dist/src/heading.d.ts +24 -0
  37. package/dist/src/html.d.ts +33 -0
  38. package/dist/src/imageParameter.d.ts +23 -0
  39. package/dist/src/imagemap.d.ts +26 -0
  40. package/dist/src/imagemapLink.d.ts +16 -0
  41. package/dist/src/index.d.ts +53 -0
  42. package/dist/src/link/category.d.ts +8 -0
  43. package/dist/src/link/file.d.ts +27 -0
  44. package/dist/src/link/galleryImage.d.ts +15 -0
  45. package/dist/src/link/index.d.ts +29 -0
  46. package/dist/src/magicLink.d.ts +14 -0
  47. package/dist/src/nested/choose.d.ts +13 -0
  48. package/dist/src/nested/combobox.d.ts +13 -0
  49. package/dist/src/nested/index.d.ts +20 -0
  50. package/dist/src/nested/references.d.ts +13 -0
  51. package/dist/src/nowiki/comment.d.ts +19 -0
  52. package/dist/src/nowiki/dd.d.ts +8 -0
  53. package/dist/src/nowiki/doubleUnderscore.d.ts +12 -0
  54. package/dist/src/nowiki/hr.d.ts +13 -0
  55. package/dist/src/nowiki/index.d.ts +18 -0
  56. package/dist/src/nowiki/list.d.ts +8 -0
  57. package/dist/src/nowiki/noinclude.d.ts +8 -0
  58. package/dist/src/nowiki/quote.d.ts +20 -0
  59. package/dist/src/onlyinclude.d.ts +17 -0
  60. package/dist/src/paramTag/index.d.ts +24 -0
  61. package/dist/src/paramTag/inputbox.d.ts +8 -0
  62. package/dist/src/parameter.d.ts +28 -0
  63. package/dist/src/syntax.d.ts +15 -0
  64. package/dist/src/table/index.d.ts +16 -0
  65. package/dist/src/table/td.d.ts +39 -0
  66. package/dist/src/table/tr.d.ts +17 -0
  67. package/dist/src/tagPair/ext.d.ts +17 -0
  68. package/dist/src/tagPair/include.d.ts +16 -0
  69. package/dist/src/tagPair/index.d.ts +28 -0
  70. package/dist/src/transclude.d.ts +75 -0
  71. package/dist/util/lint.d.ts +28 -0
  72. package/dist/util/string.d.ts +31 -0
  73. package/i18n/zh-hans.json +1 -1
  74. package/i18n/zh-hant.json +1 -1
  75. package/index.js +5 -257
  76. package/lib/element.js +7 -482
  77. package/lib/node.js +11 -540
  78. package/lib/text.js +3 -96
  79. package/lib/title.js +1 -28
  80. package/mixin/hidden.js +0 -3
  81. package/package.json +11 -5
  82. package/parser/brackets.js +3 -2
  83. package/parser/commentAndExt.js +8 -6
  84. package/parser/converter.js +1 -2
  85. package/parser/externalLinks.js +1 -2
  86. package/parser/hrAndDoubleUnderscore.js +1 -2
  87. package/parser/html.js +1 -2
  88. package/parser/links.js +5 -6
  89. package/parser/list.js +1 -2
  90. package/parser/magicLinks.js +1 -2
  91. package/parser/quotes.js +1 -2
  92. package/parser/table.js +1 -2
  93. package/src/arg.js +4 -118
  94. package/src/atom/hidden.js +0 -2
  95. package/src/atom/index.js +1 -18
  96. package/src/attribute.js +6 -190
  97. package/src/attributes.js +5 -308
  98. package/src/converter.js +3 -109
  99. package/src/converterFlags.js +1 -188
  100. package/src/converterRule.js +2 -184
  101. package/src/extLink.js +2 -122
  102. package/src/gallery.js +2 -56
  103. package/src/hasNowiki/index.js +1 -13
  104. package/src/hasNowiki/pre.js +1 -13
  105. package/src/heading.js +4 -55
  106. package/src/html.js +5 -120
  107. package/src/imageParameter.js +3 -165
  108. package/src/imagemap.js +3 -62
  109. package/src/imagemapLink.js +2 -14
  110. package/src/index.js +9 -530
  111. package/src/link/category.js +1 -32
  112. package/src/link/file.js +3 -158
  113. package/src/link/galleryImage.js +3 -61
  114. package/src/link/index.js +4 -273
  115. package/src/magicLink.js +6 -87
  116. package/src/nested/choose.js +1 -2
  117. package/src/nested/combobox.js +1 -2
  118. package/src/nested/index.js +8 -32
  119. package/src/nested/references.js +1 -2
  120. package/src/nowiki/comment.js +2 -26
  121. package/src/nowiki/dd.js +1 -47
  122. package/src/nowiki/doubleUnderscore.js +1 -31
  123. package/src/nowiki/hr.js +2 -21
  124. package/src/nowiki/index.js +2 -24
  125. package/src/nowiki/list.js +2 -5
  126. package/src/nowiki/noinclude.js +0 -14
  127. package/src/nowiki/quote.js +2 -16
  128. package/src/onlyinclude.js +2 -27
  129. package/src/paramTag/index.js +2 -25
  130. package/src/paramTag/inputbox.js +2 -5
  131. package/src/parameter.js +6 -148
  132. package/src/syntax.js +1 -69
  133. package/src/table/index.js +2 -939
  134. package/src/table/td.js +6 -226
  135. package/src/table/tr.js +3 -248
  136. package/src/tagPair/ext.js +4 -23
  137. package/src/tagPair/include.js +1 -25
  138. package/src/tagPair/index.js +3 -52
  139. package/src/transclude.js +6 -513
  140. package/typings/api.d.ts +9 -0
  141. package/typings/index.d.ts +51 -0
  142. package/typings/node.d.ts +16 -0
  143. package/typings/parser.d.ts +5 -0
  144. package/typings/token.d.ts +28 -0
  145. package/util/lint.js +2 -0
  146. package/util/string.js +0 -51
  147. package/lib/ranges.js +0 -130
  148. package/mixin/attributeParent.js +0 -117
  149. package/mixin/fixedToken.js +0 -40
  150. package/mixin/singleLine.js +0 -31
  151. package/mixin/sol.js +0 -54
  152. package/parser/selector.js +0 -177
  153. package/src/charinsert.js +0 -97
  154. package/tool/index.js +0 -1202
  155. package/util/debug.js +0 -73
package/src/converter.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {text, print} = require('../util/string'),
3
+ const {text} = require('../util/string'),
4
4
  Parser = require('..'),
5
5
  Token = require('.'),
6
6
  ConverterFlagsToken = require('./converterFlags'),
@@ -13,23 +13,10 @@ const {text, print} = require('../util/string'),
13
13
  class ConverterToken extends Token {
14
14
  type = 'converter';
15
15
 
16
- /**
17
- * 是否无转换
18
- * @this {ConverterToken & {lastChild: ConverterRuleToken}}
19
- */
20
- get noConvert() {
21
- return this.hasFlag('R') || this.length === 2 && !this.lastChild.variant;
22
- }
23
-
24
- /** flags */
25
- get flags() {
26
- return this.getAllFlags();
27
- }
28
-
29
16
  /**
30
17
  * @param {string[]} flags 转换类型标记
31
18
  * @param {string[]} rules 转换规则
32
- * @param {accum} accum
19
+ * @param {import('../typings/token').accum} accum
33
20
  */
34
21
  constructor(flags, rules, config = Parser.getConfig(), accum = []) {
35
22
  super(undefined, config, true, accum);
@@ -45,19 +32,15 @@ class ConverterToken extends Token {
45
32
  ...rules.slice(1).map(rule => new ConverterRuleToken(rule, true, config, accum)),
46
33
  );
47
34
  }
48
- this.getAttribute('protectChildren')(0);
49
35
  }
50
36
 
51
37
  /**
52
38
  * @override
53
- * @param {string} selector
54
39
  * @returns {string}
55
40
  */
56
41
  toString(selector) {
57
42
  const {childNodes: [flags, ...rules]} = this;
58
- return selector && this.matches(selector)
59
- ? ''
60
- : `-{${flags.toString(selector)}${flags.length > 0 ? '|' : ''}${rules.map(String).join(';')}}-`;
43
+ return `-{${flags.toString()}${flags.length > 0 ? '|' : ''}${rules.map(String).join(';')}}-`;
61
44
  }
62
45
 
63
46
  /**
@@ -82,95 +65,6 @@ class ConverterToken extends Token {
82
65
  i = i < 0 ? i + this.length : i;
83
66
  return i || this.firstChild.length > 0 ? 1 : 0;
84
67
  }
85
-
86
- /** @override */
87
- print() {
88
- const {childNodes: [flags, ...rules]} = this;
89
- return `<span class="wpb-converter">-{${flags.print()}${
90
- flags.length > 0 ? '|' : ''
91
- }${print(rules, {sep: ';'})}}-</span>`;
92
- }
93
-
94
- /** @override */
95
- cloneNode() {
96
- const [flags, ...rules] = this.cloneChildNodes();
97
- return Parser.run(() => {
98
- const token = new ConverterToken([], [], this.getAttribute('config'));
99
- token.firstChild.safeReplaceWith(flags);
100
- token.append(...rules);
101
- return token;
102
- });
103
- }
104
-
105
- /**
106
- * 获取所有转换类型标记
107
- * @this {{firstChild: ConverterFlagsToken}}
108
- */
109
- getAllFlags() {
110
- return this.firstChild.getAllFlags();
111
- }
112
-
113
- /**
114
- * 获取有效的转换类型标记
115
- * @this {{firstChild: ConverterFlagsToken}}
116
- */
117
- getEffectiveFlags() {
118
- return this.firstChild.getEffectiveFlags();
119
- }
120
-
121
- /**
122
- * 获取未知的转换类型标记
123
- * @this {{firstChild: ConverterFlagsToken}}
124
- */
125
- getUnknownFlags() {
126
- return this.firstChild.getUnknownFlags();
127
- }
128
-
129
- /**
130
- * 是否具有某转换类型标记
131
- * @this {{firstChild: ConverterFlagsToken}}
132
- * @param {string} flag 转换类型标记
133
- */
134
- hasFlag(flag) {
135
- return this.firstChild.hasFlag(flag);
136
- }
137
-
138
- /**
139
- * 是否具有某有效的转换类型标记
140
- * @this {{firstChild: ConverterFlagsToken}}
141
- * @param {string} flag 转换类型标记
142
- */
143
- hasEffectiveFlag(flag) {
144
- return this.firstChild.hasEffectiveFlag(flag);
145
- }
146
-
147
- /**
148
- * 移除转换类型标记
149
- * @this {{firstChild: ConverterFlagsToken}}
150
- * @param {string} flag 转换类型标记
151
- */
152
- removeFlag(flag) {
153
- this.firstChild.removeFlag(flag);
154
- }
155
-
156
- /**
157
- * 设置转换类型标记
158
- * @this {{firstChild: ConverterFlagsToken}}
159
- * @param {string} flag 转换类型标记
160
- */
161
- setFlag(flag) {
162
- this.firstChild.setFlag(flag);
163
- }
164
-
165
- /**
166
- * 开关某转换类型标记
167
- * @this {{firstChild: ConverterFlagsToken}}
168
- * @param {string} flag 转换类型标记
169
- */
170
- toggleFlag(flag) {
171
- this.firstChild.toggleFlag(flag);
172
- }
173
68
  }
174
69
 
175
- Parser.classes.ConverterToken = __filename;
176
70
  module.exports = ConverterToken;
@@ -17,11 +17,10 @@ class ConverterFlagsToken extends Token {
17
17
 
18
18
  /**
19
19
  * @param {string[]} flags 转换类型标记
20
- * @param {accum} accum
20
+ * @param {import('../typings/token').accum} accum
21
21
  */
22
22
  constructor(flags, config = Parser.getConfig(), accum = []) {
23
23
  super(undefined, config, true, accum, {
24
- AtomToken: ':',
25
24
  });
26
25
  this.append(...flags.map(flag => new AtomToken(flag, 'converter-flag', config, accum)));
27
26
  }
@@ -32,17 +31,10 @@ class ConverterFlagsToken extends Token {
32
31
  */
33
32
  afterBuild() {
34
33
  this.#flags = this.childNodes.map(child => child.text().trim());
35
- const /** @type {AstListener} */ converterFlagsListener = ({prevTarget}) => {
36
- if (prevTarget) {
37
- this.#flags[this.childNodes.indexOf(prevTarget)] = prevTarget.text().trim();
38
- }
39
- };
40
- this.addEventListener(['remove', 'insert', 'text', 'replace'], converterFlagsListener);
41
34
  }
42
35
 
43
36
  /**
44
37
  * @override
45
- * @param {string} selector
46
38
  */
47
39
  toString(selector) {
48
40
  return super.toString(selector, ';');
@@ -58,11 +50,6 @@ class ConverterFlagsToken extends Token {
58
50
  return 1;
59
51
  }
60
52
 
61
- /** @override */
62
- print() {
63
- return super.print({sep: ';'});
64
- }
65
-
66
53
  /**
67
54
  * @override
68
55
  * @param {number} start 起始位置
@@ -105,180 +92,6 @@ class ConverterFlagsToken extends Token {
105
92
  const variants = new Set(this.getAttribute('config').variants);
106
93
  return new Set(this.#flags.filter(flag => variants.has(flag)));
107
94
  }
108
-
109
- /** @override */
110
- cloneNode() {
111
- const cloned = this.cloneChildNodes();
112
- return Parser.run(() => {
113
- const token = new ConverterFlagsToken([], this.getAttribute('config'));
114
- token.append(...cloned);
115
- token.afterBuild();
116
- return token;
117
- });
118
- }
119
-
120
- /**
121
- * @override
122
- * @template {string} T
123
- * @param {T} key 属性键
124
- * @returns {TokenAttribute<T>}
125
- */
126
- getAttribute(key) {
127
- if (key === 'flags') {
128
- return Parser.debugging ? this.#flags : [...this.#flags];
129
- }
130
- return super.getAttribute(key);
131
- }
132
-
133
- /**
134
- * @override
135
- * @param {PropertyKey} key 属性键
136
- */
137
- hasAttribute(key) {
138
- return key === 'flags' || super.hasAttribute(key);
139
- }
140
-
141
- /**
142
- * @override
143
- * @param {number} i 移除位置
144
- * @complexity `n`
145
- */
146
- removeAt(i) {
147
- const /** @type {AtomToken} */ token = super.removeAt(i);
148
- this.#flags?.splice(i, 1);
149
- return token;
150
- }
151
-
152
- /**
153
- * @override
154
- * @param {AtomToken} token 待插入的子节点
155
- * @param {number} i 插入位置
156
- * @complexity `n`
157
- */
158
- insertAt(token, i = this.length) {
159
- super.insertAt(token, i);
160
- this.#flags?.splice(i, 0, token.text());
161
- return token;
162
- }
163
-
164
- /** 获取所有转换类型标记 */
165
- getAllFlags() {
166
- return new Set(this.#flags);
167
- }
168
-
169
- /**
170
- * 获取转换类型标记节点
171
- * @param {string} flag 转换类型标记
172
- * @returns {AtomToken[]}
173
- * @complexity `n`
174
- */
175
- getFlagToken(flag) {
176
- return this.#flags.includes(flag) ? this.childNodes.filter(child => child.text().trim() === flag) : [];
177
- }
178
-
179
- /**
180
- * 获取有效转换类型标记
181
- * @complexity `n`
182
- */
183
- getEffectiveFlags() {
184
- const variantFlags = this.getVariantFlags(),
185
- unknownFlags = this.getUnknownFlags();
186
- if (variantFlags.size > 0) {
187
- return new Set([...variantFlags, ...unknownFlags]);
188
- }
189
- const flags = new Set([...this.#flags.filter(flag => definedFlags.has(flag)), ...unknownFlags]);
190
- if (flags.size === 0) {
191
- return new Set('S');
192
- } else if (flags.has('R')) {
193
- return new Set('R');
194
- } else if (flags.has('N')) {
195
- return new Set('N');
196
- } else if (flags.has('-')) {
197
- return new Set('-');
198
- } else if (flags.has('H')) {
199
- const hasT = flags.has('T'),
200
- hasD = flags.has('D');
201
- return hasT && hasD
202
- ? new Set(['+', 'H', 'T', 'D'])
203
- : new Set(['+', 'H', ...hasT ? ['T'] : [], ...hasD ? ['D'] : [], ...unknownFlags]);
204
- }
205
- if (flags.size === 1 && flags.has('T')) {
206
- flags.add('H');
207
- }
208
- if (flags.has('A')) {
209
- flags.add('+');
210
- flags.add('S');
211
- }
212
- if (flags.has('D')) {
213
- flags.delete('S');
214
- }
215
- return flags;
216
- }
217
-
218
- /**
219
- * 是否具有某转换类型标记
220
- * @param {string} flag 转换类型标记
221
- */
222
- hasFlag(flag) {
223
- return typeof flag === 'string' ? this.#flags.includes(flag) : this.typeError('hasFlag', 'String');
224
- }
225
-
226
- /**
227
- * 是否具有某有效转换类型标记
228
- * @param {string} flag 转换类型标记
229
- * @complexity `n`
230
- */
231
- hasEffectiveFlag(flag) {
232
- return typeof flag === 'string' ? this.getEffectiveFlags().has(flag) : this.typeError('hasFlag', 'String');
233
- }
234
-
235
- /**
236
- * 移除某转换类型标记
237
- * @param {string} flag 转换类型标记
238
- * @complexity `n²`
239
- */
240
- removeFlag(flag) {
241
- for (const token of this.getFlagToken(flag)) {
242
- token.remove();
243
- }
244
- }
245
-
246
- /**
247
- * 添加转换类型标记
248
- * @param {string} flag 转换类型标记
249
- * @complexity `n`
250
- */
251
- #newFlag(flag) {
252
- const token = Parser.run(() => new AtomToken(flag, 'converter-flag', this.getAttribute('config')));
253
- this.insertAt(token);
254
- }
255
-
256
- /**
257
- * 设置转换类型标记
258
- * @param {string} flag 转换类型标记
259
- * @complexity `n`
260
- */
261
- setFlag(flag) {
262
- if (!this.#flags.includes(flag)) {
263
- this.#newFlag(flag);
264
- } else if (!this.getEffectiveFlags().has(flag)) {
265
- Parser.error('此 flag 不会生效', flag);
266
- }
267
- }
268
-
269
- /**
270
- * 开关转换类型标记
271
- * @param {string} flag 转换类型标记
272
- * @complexity `n²`
273
- */
274
- toggleFlag(flag) {
275
- if (this.#flags.includes(flag)) {
276
- this.removeFlag(flag);
277
- } else {
278
- this.#newFlag(flag);
279
- }
280
- }
281
95
  }
282
96
 
283
- Parser.classes.ConverterFlagsToken = __filename;
284
97
  module.exports = ConverterFlagsToken;
@@ -1,8 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  const Parser = require('..'),
4
- {undo} = require('../util/debug'),
5
- {noWrap} = require('../util/string'),
6
4
  Token = require('.'),
7
5
  AtomToken = require('./atom');
8
6
 
@@ -13,35 +11,10 @@ const Parser = require('..'),
13
11
  class ConverterRuleToken extends Token {
14
12
  type = 'converter-rule';
15
13
 
16
- /** 语言变体 */
17
- get variant() {
18
- return this.childNodes.at(-2)?.text()?.trim() ?? '';
19
- }
20
-
21
- set variant(variant) {
22
- this.setVariant(variant);
23
- }
24
-
25
- /** 是否是单向转换 */
26
- get unidirectional() {
27
- return this.length === 3;
28
- }
29
-
30
- set unidirectional(unidirectional) {
31
- if (unidirectional === false) {
32
- this.makeBidirectional();
33
- }
34
- }
35
-
36
- /** 是否是双向转换 */
37
- get bidirectional() {
38
- return this.length === 2;
39
- }
40
-
41
14
  /**
42
15
  * @param {string} rule 转换规则
43
16
  * @param {boolean} hasColon 是否带有":"
44
- * @param {accum} accum
17
+ * @param {import('../typings/token').accum} accum
45
18
  */
46
19
  constructor(rule, hasColon = true, config = Parser.getConfig(), accum = []) {
47
20
  super(undefined, config, true, accum);
@@ -61,16 +34,14 @@ class ConverterRuleToken extends Token {
61
34
  } else {
62
35
  super.insertAt(new AtomToken(rule, 'converter-rule-noconvert', config, accum));
63
36
  }
64
- this.getAttribute('protectChildren')('1:');
65
37
  }
66
38
 
67
39
  /**
68
40
  * @override
69
- * @param {string} selector
70
41
  * @returns {string}
71
42
  */
72
43
  toString(selector) {
73
- if (this.length === 3 && !(selector && this.matches(selector))) {
44
+ if (this.length === 3) {
74
45
  const {childNodes: [from, variant, to]} = this;
75
46
  return `${from.toString(selector)}=>${variant.toString(selector)}:${to.toString(selector)}`;
76
47
  }
@@ -98,159 +69,6 @@ class ConverterRuleToken extends Token {
98
69
  i = i < 0 ? i + length : i;
99
70
  return i === 0 && length === 3 ? 2 : 1;
100
71
  }
101
-
102
- /** @override */
103
- print() {
104
- if (this.length === 3) {
105
- const {childNodes: [from, variant, to]} = this;
106
- return `<span class="wpb-converter-rule">${from.print()}=>${variant.print()}:${to.print()}</span>`;
107
- }
108
- return super.print({sep: ':'});
109
- }
110
-
111
- /** @override */
112
- cloneNode() {
113
- const cloned = this.cloneChildNodes(),
114
- placeholders = ['', 'zh:', '=>zh:'],
115
- placeholder = placeholders[cloned.length - 1];
116
- return Parser.run(() => {
117
- const token = new ConverterRuleToken(placeholder, placeholder, this.getAttribute('config'));
118
- for (let i = 0; i < cloned.length; i++) {
119
- token.childNodes[i].safeReplaceWith(cloned[i]);
120
- }
121
- token.afterBuild();
122
- return token;
123
- });
124
- }
125
-
126
- /** @override */
127
- afterBuild() {
128
- const /** @type {AstListener} */ converterRuleListener = (e, data) => {
129
- const {prevTarget} = e;
130
- if (this.length > 1 && this.childNodes.at(-2) === prevTarget) {
131
- const v = prevTarget.text().trim(),
132
- {variants} = this.getAttribute('config');
133
- if (!variants.includes(v)) {
134
- undo(e, data);
135
- throw new Error(`无效的语言变体:${v}`);
136
- }
137
- }
138
- };
139
- this.addEventListener(['remove', 'insert', 'text', 'replace'], converterRuleListener);
140
- }
141
-
142
- /**
143
- * @override
144
- * @param {number} i 移除位置
145
- * @returns {AtomToken}
146
- * @throws `Error` 至少保留1个子节点
147
- */
148
- removeAt(i) {
149
- if (this.length === 1) {
150
- throw new Error(`${this.constructor.name} 需至少保留 1 个子节点!`);
151
- }
152
- const removed = super.removeAt(i);
153
- if (this.length === 1) {
154
- this.firstChild.type = 'converter-rule-noconvert';
155
- }
156
- return removed;
157
- }
158
-
159
- /**
160
- * @override
161
- * @throws `Error` 请勿手动插入子节点
162
- */
163
- insertAt() {
164
- throw new Error(`转换规则语法复杂,请勿尝试对 ${this.constructor.name} 手动插入子节点!`);
165
- }
166
-
167
- /** 修改为不转换 */
168
- noConvert() {
169
- const {length} = this;
170
- for (let i = 0; i < length - 1; i++) { // ConverterRuleToken只能从前往后删除子节点
171
- this.removeAt(0);
172
- }
173
- }
174
-
175
- /**
176
- * 设置转换目标
177
- * @param {string} to 转换目标
178
- * @throws `SyntaxError` 非法的转换目标
179
- */
180
- setTo(to) {
181
- to = String(to);
182
- const config = this.getAttribute('config'),
183
- root = Parser.parse(`-{|${config.variants[0]}:${to}}-`, this.getAttribute('include'), undefined, config),
184
- {length, firstChild: converter} = root,
185
- {lastChild: converterRule, type, length: converterLength} = converter;
186
- if (length !== 1 || type !== 'converter' || converterLength !== 2 || converterRule.length !== 2) {
187
- throw new SyntaxError(`非法的转换目标:${noWrap(to)}`);
188
- }
189
- const {lastChild} = converterRule;
190
- converterRule.destroy(true);
191
- this.lastChild.safeReplaceWith(lastChild);
192
- }
193
-
194
- /**
195
- * 设置语言变体
196
- * @param {string} variant 语言变体
197
- * @throws `RangeError` 无效的语言变体
198
- */
199
- setVariant(variant) {
200
- if (typeof variant !== 'string') {
201
- this.typeError('setVariant', 'String');
202
- }
203
- const config = this.getAttribute('config'),
204
- v = variant.trim();
205
- if (!config.variants.includes(v)) {
206
- throw new RangeError(`无效的语言变体:${v}`);
207
- } else if (this.length === 1) {
208
- super.insertAt(Parser.run(() => new AtomToken(variant, 'converter-rule-variant', config)), 0);
209
- } else {
210
- this.childNodes.at(-2).setText(variant);
211
- }
212
- }
213
-
214
- /**
215
- * 设置转换原文
216
- * @param {string} from 转换原文
217
- * @throws `Error` 尚未指定语言变体
218
- * @throws `SyntaxError` 非法的转换原文
219
- */
220
- setFrom(from) {
221
- const {variant, unidirectional} = this;
222
- if (!variant) {
223
- throw new Error('请先指定语言变体!');
224
- }
225
- from = String(from);
226
- const config = this.getAttribute('config'),
227
- root = Parser.parse(`-{|${from}=>${variant}:}-`, this.getAttribute('include'), undefined, config),
228
- {length, firstChild: converter} = root,
229
- {type, length: converterLength, lastChild: converterRule} = converter;
230
- if (length !== 1 || type !== 'converter' || converterLength !== 2 || converterRule.length !== 3) {
231
- throw new SyntaxError(`非法的转换原文:${noWrap(from)}`);
232
- } else if (unidirectional) {
233
- this.firstChild.safeReplaceWith(converterRule.firstChild);
234
- } else {
235
- super.insertAt(converterRule.firstChild, 0);
236
- }
237
- }
238
-
239
- /**
240
- * 修改为单向转换
241
- * @param {string} from 转换来源
242
- */
243
- makeUnidirectional(from) {
244
- this.setFrom(from);
245
- }
246
-
247
- /** 修改为双向转换 */
248
- makeBidirectional() {
249
- if (this.unidirectional) {
250
- this.removeAt(0);
251
- }
252
- }
253
72
  }
254
73
 
255
- Parser.classes.ConverterRuleToken = __filename;
256
74
  module.exports = ConverterRuleToken;