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/src/table/td.js CHANGED
@@ -1,15 +1,21 @@
1
1
  'use strict';
2
2
 
3
3
  const {generateForChild} = require('../../util/lint'),
4
+ fixedToken = require('../../mixin/fixedToken'),
5
+ {typeError} = require('../../util/debug'),
6
+ {isPlainObject} = require('../../util/base'),
4
7
  Parser = require('../..'),
5
8
  Token = require('..'),
6
9
  TrToken = require('./tr');
7
10
 
11
+ const aliases = {td: '\n|', th: '\n!', caption: '\n|+'},
12
+ openingPattern = /^(?:\n[^\S\n]*(?:[|!]|\|\+|\{\{\s*!\s*\}\}\+?)|(?:\||\{\{\s*!\s*\}\}){2}|!!|\{\{\s*!!\s*\}\})$/u;
13
+
8
14
  /**
9
15
  * `<td>`、`<th>`和`<caption>`
10
16
  * @classdesc `{childNodes: [SyntaxToken, AttributesToken, Token]}`
11
17
  */
12
- class TdToken extends TrToken {
18
+ class TdToken extends fixedToken(TrToken) {
13
19
  type = 'td';
14
20
  #innerSyntax = '';
15
21
 
@@ -21,13 +27,46 @@ class TdToken extends TrToken {
21
27
  return this.getSyntax().subtype;
22
28
  }
23
29
 
30
+ set subtype(subtype) {
31
+ this.setSyntax(subtype);
32
+ }
33
+
34
+ /** rowspan */
35
+ get rowspan() {
36
+ return this.getAttr('rowspan');
37
+ }
38
+
39
+ set rowspan(rowspan) {
40
+ this.setAttr('rowspan', rowspan);
41
+ }
42
+
43
+ /** colspan */
44
+ get colspan() {
45
+ return this.getAttr('colspan');
46
+ }
47
+
48
+ set colspan(colspan) {
49
+ this.setAttr('colspan', colspan);
50
+ }
51
+
52
+ /** 内部wikitext */
53
+ get innerText() {
54
+ return this.lastChild.text();
55
+ }
56
+
57
+ /** 是否位于行首 */
58
+ isIndependent() {
59
+ return this.firstChild.text()[0] === '\n';
60
+ }
61
+
24
62
  /**
25
63
  * 获取单元格语法信息
26
- * @returns {{subtype: 'td'|'th'|'caption'}}
64
+ * @returns {{subtype: 'td'|'th'|'caption', escape: boolean, correction: boolean}}
27
65
  * @complexity `n`
28
66
  */
29
67
  getSyntax() {
30
68
  const syntax = this.firstChild.text(),
69
+ esc = syntax.includes('{{'),
31
70
  char = syntax.at(-1);
32
71
  let subtype = 'td';
33
72
  if (char === '!') {
@@ -35,7 +74,23 @@ class TdToken extends TrToken {
35
74
  } else if (char === '+') {
36
75
  subtype = 'caption';
37
76
  }
38
- return {subtype};
77
+ if (this.isIndependent()) {
78
+ return {subtype, escape: esc, correction: false};
79
+ }
80
+ const {previousSibling} = this;
81
+ if (previousSibling?.type !== 'td') {
82
+ return {subtype, escape: esc, correction: true};
83
+ }
84
+ const result = previousSibling.getSyntax();
85
+ result.escape ||= esc;
86
+ result.correction = previousSibling.lastChild
87
+ .toString('comment, ext, include, noinclude, arg, template, magic-word')
88
+ .includes('\n');
89
+ if (subtype === 'th' && result.subtype !== 'th') {
90
+ result.subtype = 'th';
91
+ result.correction = true;
92
+ }
93
+ return result;
39
94
  }
40
95
 
41
96
  /**
@@ -50,7 +105,7 @@ class TdToken extends TrToken {
50
105
  innerSyntax = undefined;
51
106
  attr = '';
52
107
  }
53
- super(syntax, attr, config, accum);
108
+ super(syntax, attr, config, accum, openingPattern);
54
109
  if (innerSyntax) {
55
110
  [this.#innerSyntax] = innerSyntax;
56
111
  }
@@ -58,6 +113,8 @@ class TdToken extends TrToken {
58
113
  const innerToken = new Token(inner?.slice(innerSyntax?.index + this.#innerSyntax.length), config, true, accum);
59
114
  innerToken.type = 'td-inner';
60
115
  this.insertAt(innerToken.setAttribute('stage', 4));
116
+ this.setAttribute('acceptable', {SyntaxToken: 0, AttributesToken: 1, Token: 2})
117
+ .seal(['getRowCount', 'getNthCol', 'insertTableCell'], true);
61
118
  }
62
119
 
63
120
  /** @override */
@@ -69,12 +126,16 @@ class TdToken extends TrToken {
69
126
 
70
127
  /**
71
128
  * @override
129
+ * @param {string} selector
72
130
  * @returns {string}
73
131
  * @complexity `n`
74
132
  */
75
133
  toString(selector) {
134
+ this.#correct();
76
135
  const {childNodes: [syntax, attr, inner]} = this;
77
- return `${syntax.toString()}${attr.toString()}${this.#innerSyntax}${inner.toString()}`;
136
+ return selector && this.matches(selector)
137
+ ? ''
138
+ : `${syntax.toString(selector)}${attr.toString(selector)}${this.#innerSyntax}${inner.toString(selector)}`;
78
139
  }
79
140
 
80
141
  /**
@@ -83,6 +144,7 @@ class TdToken extends TrToken {
83
144
  * @complexity `n`
84
145
  */
85
146
  text() {
147
+ this.#correct();
86
148
  const {childNodes: [syntax, attr, inner]} = this;
87
149
  return `${syntax.text()}${attr.text()}${this.#innerSyntax}${inner.text()}`;
88
150
  }
@@ -94,6 +156,7 @@ class TdToken extends TrToken {
94
156
  getGaps(i = 0) {
95
157
  i = i < 0 ? i + this.length : i;
96
158
  if (i === 1) {
159
+ this.#correct();
97
160
  return this.#innerSyntax.length;
98
161
  }
99
162
  return 0;
@@ -103,17 +166,173 @@ class TdToken extends TrToken {
103
166
  * @override
104
167
  * @param {number} start 起始位置
105
168
  */
106
- lint(start = 0) {
107
- const errors = super.lint(start),
108
- {lastChild} = this;
169
+ lint(start = this.getAbsoluteIndex()) {
170
+ const errors = super.lint(start);
109
171
  start += this.getRelativeIndex(-1);
110
- for (const child of lastChild.childNodes) {
172
+ for (const child of this.lastChild.childNodes) {
111
173
  if (child.type === 'text' && child.data.includes('|')) {
112
- errors.push(generateForChild(child, {start}, '表格单元格中多余的"|"', 'warning'));
174
+ errors.push(generateForChild(child, {start}, 'additional "|" in a table cell', 'warning'));
113
175
  }
114
176
  }
115
177
  return errors;
116
178
  }
179
+
180
+ /** @override */
181
+ print() {
182
+ const {childNodes: [syntax, attr, inner]} = this;
183
+ return `<span class="wpb-td">${syntax.print()}${attr.print()}${this.#innerSyntax}${inner.print()}</span>`;
184
+ }
185
+
186
+ /** @override */
187
+ cloneNode() {
188
+ const /** @type {TdToken} */ token = super.cloneNode();
189
+ token.setAttribute('innerSyntax', this.#innerSyntax);
190
+ return token;
191
+ }
192
+
193
+ /**
194
+ * 创建新的单元格
195
+ * @param {string|Token} inner 内部wikitext
196
+ * @param {'td'|'th'|'caption'} subtype 单元格类型
197
+ * @param {Record<string, string>} attr 单元格属性
198
+ * @param {boolean} include 是否嵌入
199
+ * @throws `RangeError` 非法的单元格类型
200
+ */
201
+ static create(inner, subtype = 'td', attr = {}, include = false, config = Parser.getConfig()) {
202
+ if (typeof inner !== 'string' && inner?.constructor !== Token || !isPlainObject(attr)) {
203
+ typeError(this, 'create', 'String', 'Token', 'Object');
204
+ } else if (subtype !== 'td' && subtype !== 'th' && subtype !== 'caption') {
205
+ throw new RangeError('单元格的子类型只能为 "td"、"th" 或 "caption"!');
206
+ } else if (typeof inner === 'string') {
207
+ inner = Parser.parse(inner, include, undefined, config);
208
+ }
209
+ const token = Parser.run(() => new TdToken('\n|', undefined, config));
210
+ token.setSyntax(subtype);
211
+ token.lastChild.safeReplaceWith(inner);
212
+ for (const [k, v] of Object.entries(attr)) {
213
+ token.setAttr(k, v);
214
+ }
215
+ return token;
216
+ }
217
+
218
+ /**
219
+ * @override
220
+ * @template {string} T
221
+ * @param {T} key 属性键
222
+ * @returns {TokenAttribute<T>}
223
+ */
224
+ getAttribute(key) {
225
+ return key === 'innerSyntax' ? this.#innerSyntax : super.getAttribute(key);
226
+ }
227
+
228
+ /**
229
+ * @override
230
+ * @template {string} T
231
+ * @param {T} key 属性键
232
+ * @param {TokenAttribute<T>} value 属性值
233
+ * @returns {this}
234
+ */
235
+ setAttribute(key, value) {
236
+ if (key === 'innerSyntax') {
237
+ this.#innerSyntax = String(value);
238
+ return this;
239
+ }
240
+ return super.setAttribute(key, value);
241
+ }
242
+
243
+ /**
244
+ * @override
245
+ * @param {string} syntax 表格语法
246
+ * @param {boolean} esc 是否需要转义
247
+ */
248
+ setSyntax(syntax, esc) {
249
+ super.setSyntax(aliases[syntax] ?? syntax, esc);
250
+ }
251
+
252
+ /**
253
+ * 修复\<td\>语法
254
+ * @complexity `n`
255
+ */
256
+ #correct() {
257
+ if (String(this.childNodes[1])) {
258
+ this.#innerSyntax ||= '|';
259
+ }
260
+ const {subtype, escape, correction} = this.getSyntax();
261
+ if (correction) {
262
+ this.setSyntax(subtype, escape);
263
+ }
264
+ }
265
+
266
+ /**
267
+ * 改为独占一行
268
+ * @complexity `n`
269
+ */
270
+ independence() {
271
+ if (!this.isIndependent()) {
272
+ const {subtype, escape} = this.getSyntax();
273
+ this.setSyntax(subtype, escape);
274
+ }
275
+ }
276
+
277
+ /**
278
+ * 获取单元格属性
279
+ * @template {string} T
280
+ * @param {T} key 属性键
281
+ * @returns {T extends 'rowspan'|'colspan' ? number : string|true}
282
+ */
283
+ getAttr(key) {
284
+ const /** @type {string|true} */ value = super.getAttr(key);
285
+ key = key?.toLowerCase()?.trim();
286
+ return key === 'rowspan' || key === 'colspan' ? Number(value) || 1 : value;
287
+ }
288
+
289
+ /**
290
+ * 获取全部单元格属性
291
+ * @returns {{rowspan: number, colspan: number, [key: string]: string|true}}
292
+ */
293
+ getAttrs() {
294
+ const /** @type {record<string, string|true>} */ attr = super.getAttrs();
295
+ if ('rowspan' in attr) {
296
+ attr.rowspan = Number(attr.rowspan);
297
+ }
298
+ if ('colspan' in attr) {
299
+ attr.colspan = Number(attr.colspan);
300
+ }
301
+ return attr;
302
+ }
303
+
304
+ /**
305
+ * 设置单元格属性
306
+ * @template {string} T
307
+ * @param {T} key 属性键
308
+ * @param {T extends 'rowspan'|'colspan' ? number : string|boolean} value 属性值
309
+ */
310
+ setAttr(key, value) {
311
+ if (typeof key !== 'string') {
312
+ this.typeError('setAttr', 'String');
313
+ }
314
+ key = key.toLowerCase().trim();
315
+ if (typeof value === 'number' && (key === 'rowspan' || key === 'colspan')) {
316
+ value = value === 1 ? false : String(value);
317
+ }
318
+ const /** @type {boolean} */ result = super.setAttr(key, value);
319
+ if (!String(this.childNodes[1])) {
320
+ this.#innerSyntax = '';
321
+ }
322
+ return result;
323
+ }
324
+
325
+ /** @override */
326
+ escape() {
327
+ super.escape();
328
+ if (String(this.childNodes[1])) {
329
+ this.#innerSyntax ||= '{{!}}';
330
+ }
331
+ if (this.#innerSyntax === '|') {
332
+ this.#innerSyntax = '{{!}}';
333
+ }
334
+ }
117
335
  }
118
336
 
337
+ Parser.classes.TdToken = __filename;
119
338
  module.exports = TdToken;
package/src/table/tr.js CHANGED
@@ -1,16 +1,35 @@
1
1
  'use strict';
2
2
 
3
3
  const {generateForChild} = require('../../util/lint'),
4
+ attributeParent = require('../../mixin/attributeParent'),
4
5
  Parser = require('../..'),
6
+ AstText = require('../../lib/text'),
5
7
  Token = require('..'),
6
8
  SyntaxToken = require('../syntax'),
7
9
  AttributesToken = require('../attributes');
8
10
 
11
+ const openingPattern = /^\n[^\S\n]*(?:\|-+|\{\{\s*!\s*\}\}-+|\{\{\s*!-\s*\}\}-*)$/u;
12
+
13
+ /**
14
+ * 转义表格语法
15
+ * @param {SyntaxToken} syntax 表格语法节点
16
+ */
17
+ const escapeTable = syntax => {
18
+ const templates = {'{|': '(!', '|}': '!)', '||': '!!', '|': '!'},
19
+ wikitext = syntax.childNodes.map(
20
+ child => child.type === 'text'
21
+ ? String(child).replace(/\{\||\|\}|\|{2}|\|/gu, p => `{{${templates[p]}}}`)
22
+ : String(child),
23
+ ).join(''),
24
+ token = Parser.parse(wikitext, syntax.getAttribute('include'), 2, syntax.getAttribute('config'));
25
+ syntax.replaceChildren(...token.childNodes);
26
+ };
27
+
9
28
  /**
10
29
  * 表格行,含开头的换行,不含结尾的换行
11
30
  * @classdesc `{childNodes: [SyntaxToken, AttributesToken, ?Token, ...TdToken]}`
12
31
  */
13
- class TrToken extends Token {
32
+ class TrToken extends attributeParent(Token, 1) {
14
33
  type = 'tr';
15
34
 
16
35
  /**
@@ -19,21 +38,24 @@ class TrToken extends Token {
19
38
  * @param {accum} accum
20
39
  * @param {RegExp} pattern 表格语法正则
21
40
  */
22
- constructor(syntax, attr = '', config = Parser.getConfig(), accum = [], pattern = undefined) {
41
+ constructor(syntax, attr = '', config = Parser.getConfig(), accum = [], pattern = openingPattern) {
23
42
  super(undefined, config, true, accum, {
43
+ Token: 2, SyntaxToken: 0, AttributesToken: 1, TdToken: '2:',
24
44
  });
25
45
  this.append(
26
46
  new SyntaxToken(syntax, pattern, 'table-syntax', config, accum, {
47
+ 'Stage-1': ':', '!ExtToken': '', TranscludeToken: ':',
27
48
  }),
28
49
  new AttributesToken(attr, 'table-attrs', this.type, config, accum),
29
50
  );
51
+ this.getAttribute('protectChildren')(0, 1);
30
52
  }
31
53
 
32
54
  /**
33
55
  * @override
34
56
  * @param {number} start 起始位置
35
57
  */
36
- lint(start = 0) {
58
+ lint(start = this.getAbsoluteIndex()) {
37
59
  const TranscludeToken = require('../transclude'),
38
60
  ArgToken = require('../arg');
39
61
  const errors = super.lint(start),
@@ -53,7 +75,7 @@ class TrToken extends Token {
53
75
  }
54
76
  } catch {}
55
77
  }
56
- const error = generateForChild(inter, {start}, '将被移出表格的内容');
78
+ const error = generateForChild(inter, {start}, 'content to be moved out from the table');
57
79
  errors.push({
58
80
  ...error,
59
81
  startIndex: error.startIndex + 1,
@@ -66,9 +88,232 @@ class TrToken extends Token {
66
88
 
67
89
  /** @override */
68
90
  text() {
91
+ this.#correct();
69
92
  const str = super.text();
70
93
  return this.type === 'tr' && !str.trim().includes('\n') ? '' : str;
71
94
  }
95
+
96
+ /**
97
+ * @override
98
+ * @this {TrToken & {constructor: typeof TrToken}}
99
+ */
100
+ cloneNode() {
101
+ const [syntax, attr, inner, ...cloned] = this.cloneChildNodes();
102
+ return Parser.run(() => {
103
+ const token = new this.constructor(undefined, undefined, this.getAttribute('config'));
104
+ token.firstChild.safeReplaceWith(syntax);
105
+ token.childNodes[1].safeReplaceWith(attr);
106
+ if (token.type === 'td') { // TdToken
107
+ token.childNodes[2].safeReplaceWith(inner);
108
+ } else if (inner !== undefined) {
109
+ token.insertAt(inner);
110
+ }
111
+ token.append(...cloned);
112
+ return token;
113
+ });
114
+ }
115
+
116
+ /** 修复简单的表格语法错误 */
117
+ #correct() {
118
+ const {childNodes: [,, child]} = this;
119
+ if (child?.constructor === Token) {
120
+ const /** @type {{firstChild: AstText}} */ {firstChild} = child;
121
+ if (firstChild.type !== 'text') {
122
+ child.prepend('\n');
123
+ } else if (firstChild.data[0] !== '\n') {
124
+ firstChild.insertData(0, '\n');
125
+ }
126
+ }
127
+ }
128
+
129
+ /**
130
+ * @override
131
+ * @param {string} selector
132
+ */
133
+ toString(selector) {
134
+ this.#correct();
135
+ return super.toString(selector);
136
+ }
137
+
138
+ /**
139
+ * 转义表格语法
140
+ * @complexity `n`
141
+ */
142
+ escape() {
143
+ for (const child of this.childNodes) {
144
+ if (child instanceof SyntaxToken) {
145
+ escapeTable(child);
146
+ } else if (child instanceof TrToken) {
147
+ child.escape();
148
+ }
149
+ }
150
+ }
151
+
152
+ /**
153
+ * 设置表格语法
154
+ * @param {string} syntax 表格语法
155
+ * @param {boolean} esc 是否需要转义
156
+ */
157
+ setSyntax(syntax, esc) {
158
+ const {firstChild} = this;
159
+ firstChild.replaceChildren(syntax);
160
+ if (esc) {
161
+ escapeTable(firstChild);
162
+ }
163
+ }
164
+
165
+ /**
166
+ * @override
167
+ * @param {number} i 移除位置
168
+ * @complexity `n`
169
+ */
170
+ removeAt(i) {
171
+ const TdToken = require('./td');
172
+ const child = this.childNodes.at(i);
173
+ if (child instanceof TdToken && child.isIndependent()) {
174
+ const {nextSibling} = child;
175
+ if (nextSibling?.type === 'td') {
176
+ nextSibling.independence();
177
+ }
178
+ }
179
+ return super.removeAt(i);
180
+ }
181
+
182
+ /**
183
+ * @override
184
+ * @template {AstText|Token} T
185
+ * @param {T} token 待插入的子节点
186
+ * @param {number} i 插入位置
187
+ * @returns {T}
188
+ * @complexity `n`
189
+ */
190
+ insertAt(token, i = this.length) {
191
+ if (!Parser.running && !(token instanceof TrToken)) {
192
+ this.typeError('insertAt', 'TrToken');
193
+ }
194
+ const TdToken = require('./td');
195
+ const child = this.childNodes.at(i);
196
+ if (token instanceof TdToken && token.isIndependent() && child instanceof TdToken) {
197
+ child.independence();
198
+ }
199
+ return super.insertAt(token, i);
200
+ }
201
+
202
+ /**
203
+ * 获取行数
204
+ * @returns {0|1}
205
+ * @complexity `n`
206
+ */
207
+ getRowCount() {
208
+ const TdToken = require('./td');
209
+ return Number(this.childNodes.some(
210
+ child => child instanceof TdToken && child.isIndependent() && !child.firstChild.text().endsWith('+'),
211
+ ));
212
+ }
213
+
214
+ /**
215
+ * 获取相邻行
216
+ * @param {(childNodes: Token[], index: number) => Token[]} subset 筛选兄弟节点的方法
217
+ * @complexity `n`
218
+ */
219
+ #getSiblingRow(subset) {
220
+ const {parentNode} = this;
221
+ if (!parentNode) {
222
+ return undefined;
223
+ }
224
+ const {childNodes} = parentNode,
225
+ index = childNodes.indexOf(this);
226
+ for (const child of subset(childNodes, index)) {
227
+ if (child instanceof TrToken && child.getRowCount()) {
228
+ return child;
229
+ }
230
+ }
231
+ return undefined;
232
+ }
233
+
234
+ /**
235
+ * 获取下一行
236
+ * @complexity `n`
237
+ */
238
+ getNextRow() {
239
+ return this.#getSiblingRow((childNodes, index) => childNodes.slice(index + 1));
240
+ }
241
+
242
+ /**
243
+ * 获取前一行
244
+ * @complexity `n`
245
+ */
246
+ getPreviousRow() {
247
+ return this.#getSiblingRow((childNodes, index) => childNodes.slice(0, index).reverse());
248
+ }
249
+
250
+ /**
251
+ * 获取列数
252
+ * @complexity `n`
253
+ */
254
+ getColCount() {
255
+ const TdToken = require('./td');
256
+ let count = 0,
257
+ last = 0;
258
+ for (const child of this.childNodes) {
259
+ if (child instanceof TdToken) {
260
+ last = child.isIndependent() ? Number(child.subtype !== 'caption') : last;
261
+ count += last;
262
+ }
263
+ }
264
+ return count;
265
+ }
266
+
267
+ /**
268
+ * 获取第n列
269
+ * @param {number} n 列号
270
+ * @param {boolean} insert 是否用于判断插入新列的位置
271
+ * @returns {TdToken}
272
+ * @complexity `n`
273
+ * @throws `RangeError` 不存在对应单元格
274
+ */
275
+ getNthCol(n, insert) {
276
+ if (!Number.isInteger(n)) {
277
+ this.typeError('getNthCol', 'Number');
278
+ }
279
+ const nCols = this.getColCount();
280
+ n = n < 0 ? n + nCols : n;
281
+ if (n < 0 || n > nCols || n === nCols && !insert) {
282
+ throw new RangeError(`不存在第 ${n} 个单元格!`);
283
+ }
284
+ const TdToken = require('./td');
285
+ let last = 0;
286
+ for (const child of this.childNodes.slice(2)) {
287
+ if (child instanceof TdToken) {
288
+ if (child.isIndependent()) {
289
+ last = Number(child.subtype !== 'caption');
290
+ }
291
+ n -= last;
292
+ if (n < 0) {
293
+ return child;
294
+ }
295
+ } else if (child.type === 'tr' || child.type === 'table-syntax') {
296
+ return child;
297
+ }
298
+ }
299
+ return undefined;
300
+ }
301
+
302
+ /**
303
+ * 插入新的单元格
304
+ * @param {string|Token} inner 单元格内部wikitext
305
+ * @param {TableCoords} coord 单元格坐标
306
+ * @param {'td'|'th'|'caption'} subtype 单元格类型
307
+ * @param {Record<string, string|boolean>} attr 单元格属性
308
+ * @returns {TdToken}
309
+ * @complexity `n`
310
+ */
311
+ insertTableCell(inner, {column}, subtype = 'td', attr = {}) {
312
+ const TdToken = require('./td');
313
+ const token = TdToken.create(inner, subtype, attr, this.getAttribute('include'), this.getAttribute('config'));
314
+ return this.insertBefore(token, this.getNthCol(column, true));
315
+ }
72
316
  }
73
317
 
318
+ Parser.classes.TrToken = __filename;
74
319
  module.exports = TrToken;
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const {generateForSelf} = require('../../util/lint'),
4
+ attributeParent = require('../../mixin/attributeParent'),
4
5
  Parser = require('../..'),
5
6
  Token = require('..'),
6
7
  TagPairToken = require('.'),
@@ -10,7 +11,7 @@ const {generateForSelf} = require('../../util/lint'),
10
11
  * 扩展标签
11
12
  * @classdesc `{childNodes: [AttributesToken, NowikiToken|Token]}`
12
13
  */
13
- class ExtToken extends TagPairToken {
14
+ class ExtToken extends attributeParent(TagPairToken) {
14
15
  type = 'ext';
15
16
  closed = true;
16
17
 
@@ -22,7 +23,7 @@ class ExtToken extends TagPairToken {
22
23
  * @param {accum} accum
23
24
  */
24
25
  constructor(name, attr = '', inner = '', closed = undefined, config = Parser.getConfig(), accum = []) {
25
- attr = !attr || /^\s/u.test(attr) ? attr : ` ${attr}`;
26
+ attr = !attr || attr.trimStart() !== attr ? attr : ` ${attr}`;
26
27
  const lcName = name.toLowerCase(),
27
28
  attrToken = new AttributesToken(attr, 'ext-attrs', lcName, config, accum),
28
29
  /** @type {ParserConfig} */ newConfig = {...config, excludes: [...config.excludes]},
@@ -58,12 +59,24 @@ class ExtToken extends TagPairToken {
58
59
  innerToken = new PreToken(inner, newConfig, accum);
59
60
  break;
60
61
  }
61
- case 'references':
62
- case 'choose':
62
+ case 'charinsert': {
63
+ const CharinsertToken = require('../charinsert');
64
+ innerToken = new CharinsertToken(inner, newConfig, accum);
65
+ break;
66
+ }
67
+ case 'references': {
68
+ const ReferencesToken = require('../nested/references');
69
+ innerToken = new ReferencesToken(inner, newConfig, accum);
70
+ break;
71
+ }
72
+ case 'choose': {
73
+ const ChooseToken = require('../nested/choose');
74
+ innerToken = new ChooseToken(inner, newConfig, accum);
75
+ break;
76
+ }
63
77
  case 'combobox': {
64
- const NestedToken = require('../nested'),
65
- /** @type {typeof NestedToken} */ NestedExtToken = require(`../nested/${lcName}`);
66
- innerToken = new NestedExtToken(inner, newConfig, accum);
78
+ const ComboboxToken = require('../nested/combobox');
79
+ innerToken = new ComboboxToken(inner, newConfig, accum);
67
80
  break;
68
81
  }
69
82
  case 'imagemap': {
@@ -106,16 +119,30 @@ class ExtToken extends TagPairToken {
106
119
  * @override
107
120
  * @param {number} start 起始位置
108
121
  */
109
- lint(start = 0) {
122
+ lint(start = this.getAbsoluteIndex()) {
110
123
  const errors = super.lint(start);
111
124
  if (this.name !== 'nowiki' && this.closest('html-attrs, table-attrs')) {
112
125
  const root = this.getRootNode(),
113
126
  excerpt = String(root).slice(Math.max(0, start - 25), start + 25),
114
127
  rect = {start, ...root.posFromIndex(start)};
115
- errors.push({...generateForSelf(this, rect, 'HTML标签属性中的扩展标签'), excerpt});
128
+ errors.push({...generateForSelf(this, rect, 'extension tag in HTML tag attributes'), excerpt});
116
129
  }
117
130
  return errors;
118
131
  }
132
+
133
+ /** @override */
134
+ cloneNode() {
135
+ const inner = this.lastChild.cloneNode(),
136
+ tags = this.getAttribute('tags'),
137
+ config = this.getAttribute('config'),
138
+ attr = String(this.firstChild);
139
+ return Parser.run(() => {
140
+ const token = new ExtToken(tags[0], attr, '', this.selfClosing ? undefined : tags[1], config);
141
+ token.lastChild.safeReplaceWith(inner);
142
+ return token;
143
+ });
144
+ }
119
145
  }
120
146
 
147
+ Parser.classes.ExtToken = __filename;
121
148
  module.exports = ExtToken;