wikiparser-node 0.11.0-m → 0.11.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 (141) hide show
  1. package/config/.schema.json +7 -0
  2. package/config/default.json +1 -0
  3. package/config/llwiki.json +35 -0
  4. package/config/moegirl.json +44 -0
  5. package/config/zhwiki.json +466 -0
  6. package/dist/index.d.ts +4 -0
  7. package/dist/lib/element.d.ts +116 -2
  8. package/dist/lib/node.d.ts +193 -10
  9. package/dist/lib/ranges.d.ts +37 -0
  10. package/dist/lib/text.d.ts +35 -1
  11. package/dist/lib/title.d.ts +6 -0
  12. package/dist/mixin/attributeParent.d.ts +9 -0
  13. package/dist/mixin/fixedToken.d.ts +8 -0
  14. package/dist/mixin/singleLine.d.ts +8 -0
  15. package/dist/mixin/sol.d.ts +8 -0
  16. package/dist/parser/selector.d.ts +12 -0
  17. package/dist/src/arg.d.ts +30 -1
  18. package/dist/src/atom/index.d.ts +2 -1
  19. package/dist/src/attribute.d.ts +24 -1
  20. package/dist/src/attributes.d.ts +79 -0
  21. package/dist/src/charinsert.d.ts +32 -0
  22. package/dist/src/converter.d.ts +75 -1
  23. package/dist/src/converterFlags.d.ts +57 -1
  24. package/dist/src/converterRule.d.ts +47 -1
  25. package/dist/src/extLink.d.ts +41 -1
  26. package/dist/src/gallery.d.ts +15 -1
  27. package/dist/src/heading.d.ts +22 -2
  28. package/dist/src/html.d.ts +25 -2
  29. package/dist/src/imageParameter.d.ts +43 -1
  30. package/dist/src/imagemap.d.ts +12 -1
  31. package/dist/src/imagemapLink.d.ts +5 -0
  32. package/dist/src/index.d.ts +136 -3
  33. package/dist/src/link/category.d.ts +8 -0
  34. package/dist/src/link/file.d.ts +58 -0
  35. package/dist/src/link/index.d.ts +61 -2
  36. package/dist/src/magicLink.d.ts +22 -0
  37. package/dist/src/nested/index.d.ts +3 -5
  38. package/dist/src/nowiki/comment.d.ts +13 -1
  39. package/dist/src/nowiki/dd.d.ts +9 -0
  40. package/dist/src/nowiki/doubleUnderscore.d.ts +11 -1
  41. package/dist/src/nowiki/index.d.ts +11 -2
  42. package/dist/src/nowiki/quote.d.ts +0 -7
  43. package/dist/src/onlyinclude.d.ts +8 -1
  44. package/dist/src/paramTag/index.d.ts +8 -3
  45. package/dist/src/parameter.d.ts +48 -1
  46. package/dist/src/syntax.d.ts +6 -1
  47. package/dist/src/table/index.d.ts +257 -0
  48. package/dist/src/table/td.d.ts +67 -6
  49. package/dist/src/table/tr.d.ts +74 -0
  50. package/dist/src/tagPair/ext.d.ts +2 -1
  51. package/dist/src/tagPair/include.d.ts +9 -0
  52. package/dist/src/tagPair/index.d.ts +14 -1
  53. package/dist/src/transclude.d.ts +125 -1
  54. package/dist/tool/index.d.ts +420 -0
  55. package/dist/util/base.d.ts +10 -0
  56. package/dist/util/debug.d.ts +20 -0
  57. package/dist/util/diff.d.ts +8 -0
  58. package/dist/util/string.d.ts +24 -0
  59. package/index.js +256 -4
  60. package/lib/element.js +488 -6
  61. package/lib/node.js +495 -6
  62. package/lib/ranges.js +130 -0
  63. package/lib/text.js +96 -1
  64. package/lib/title.js +28 -1
  65. package/mixin/attributeParent.js +117 -0
  66. package/mixin/fixedToken.js +40 -0
  67. package/mixin/hidden.js +3 -0
  68. package/mixin/singleLine.js +31 -0
  69. package/mixin/sol.js +54 -0
  70. package/package.json +5 -2
  71. package/parser/brackets.js +1 -0
  72. package/parser/commentAndExt.js +1 -0
  73. package/parser/converter.js +1 -0
  74. package/parser/externalLinks.js +1 -0
  75. package/parser/hrAndDoubleUnderscore.js +1 -0
  76. package/parser/html.js +1 -0
  77. package/parser/links.js +5 -4
  78. package/parser/list.js +1 -0
  79. package/parser/magicLinks.js +1 -0
  80. package/parser/quotes.js +1 -0
  81. package/parser/selector.js +180 -0
  82. package/parser/table.js +1 -0
  83. package/src/arg.js +116 -2
  84. package/src/atom/hidden.js +2 -0
  85. package/src/atom/index.js +17 -0
  86. package/src/attribute.js +189 -3
  87. package/src/attributes.js +307 -4
  88. package/src/charinsert.js +97 -0
  89. package/src/converter.js +108 -2
  90. package/src/converterFlags.js +187 -0
  91. package/src/converterRule.js +183 -1
  92. package/src/extLink.js +121 -1
  93. package/src/gallery.js +54 -0
  94. package/src/hasNowiki/index.js +12 -0
  95. package/src/hasNowiki/pre.js +12 -0
  96. package/src/heading.js +54 -3
  97. package/src/html.js +118 -3
  98. package/src/imageParameter.js +164 -2
  99. package/src/imagemap.js +61 -2
  100. package/src/imagemapLink.js +13 -1
  101. package/src/index.js +530 -3
  102. package/src/link/category.js +32 -1
  103. package/src/link/file.js +157 -2
  104. package/src/link/galleryImage.js +60 -2
  105. package/src/link/index.js +270 -1
  106. package/src/magicLink.js +83 -0
  107. package/src/nested/choose.js +1 -0
  108. package/src/nested/combobox.js +1 -0
  109. package/src/nested/index.js +27 -0
  110. package/src/nested/references.js +1 -0
  111. package/src/nowiki/comment.js +25 -1
  112. package/src/nowiki/dd.js +47 -1
  113. package/src/nowiki/doubleUnderscore.js +31 -1
  114. package/src/nowiki/hr.js +20 -1
  115. package/src/nowiki/index.js +23 -1
  116. package/src/nowiki/list.js +5 -2
  117. package/src/nowiki/noinclude.js +14 -0
  118. package/src/nowiki/quote.js +14 -0
  119. package/src/onlyinclude.js +26 -1
  120. package/src/paramTag/index.js +24 -1
  121. package/src/paramTag/inputbox.js +4 -1
  122. package/src/parameter.js +147 -5
  123. package/src/syntax.js +68 -0
  124. package/src/table/index.js +941 -2
  125. package/src/table/td.js +230 -5
  126. package/src/table/tr.js +247 -2
  127. package/src/tagPair/ext.js +21 -1
  128. package/src/tagPair/include.js +24 -0
  129. package/src/tagPair/index.js +56 -2
  130. package/src/transclude.js +512 -5
  131. package/tool/index.js +1209 -0
  132. package/typings/array.d.ts +29 -0
  133. package/typings/event.d.ts +22 -0
  134. package/typings/index.d.ts +67 -0
  135. package/typings/node.d.ts +19 -0
  136. package/typings/parser.d.ts +7 -0
  137. package/typings/table.d.ts +10 -0
  138. package/typings/token.d.ts +3 -0
  139. package/typings/tool.d.ts +6 -0
  140. package/util/debug.js +73 -0
  141. package/util/string.js +51 -0
package/src/table/td.js CHANGED
@@ -1,15 +1,26 @@
1
1
  'use strict';
2
2
 
3
+ /**
4
+ * @template T
5
+ * @typedef {import('../../typings/node').TokenAttribute<T>} TokenAttribute
6
+ */
7
+
3
8
  const {generateForChild} = require('../../util/lint'),
9
+ fixedToken = require('../../mixin/fixedToken'),
10
+ {typeError} = require('../../util/debug'),
11
+ {isPlainObject} = require('../../util/base'),
4
12
  Parser = require('../..'),
5
13
  Token = require('..'),
6
14
  TrToken = require('./tr');
7
15
 
16
+ const aliases = {td: '\n|', th: '\n!', caption: '\n|+'},
17
+ openingPattern = /^(?:\n[^\S\n]*(?:[|!]|\|\+|\{\{\s*!\s*\}\}\+?)|(?:\||\{\{\s*!\s*\}\}){2}|!!|\{\{\s*!!\s*\}\})$/u;
18
+
8
19
  /**
9
20
  * `<td>`、`<th>`和`<caption>`
10
21
  * @classdesc `{childNodes: [SyntaxToken, AttributesToken, Token]}`
11
22
  */
12
- class TdToken extends TrToken {
23
+ class TdToken extends fixedToken(TrToken) {
13
24
  type = 'td';
14
25
  #innerSyntax = '';
15
26
 
@@ -21,13 +32,46 @@ class TdToken extends TrToken {
21
32
  return this.getSyntax().subtype;
22
33
  }
23
34
 
35
+ set subtype(subtype) {
36
+ this.setSyntax(subtype);
37
+ }
38
+
39
+ /** rowspan */
40
+ get rowspan() {
41
+ return this.getAttr('rowspan');
42
+ }
43
+
44
+ set rowspan(rowspan) {
45
+ this.setAttr('rowspan', rowspan);
46
+ }
47
+
48
+ /** colspan */
49
+ get colspan() {
50
+ return this.getAttr('colspan');
51
+ }
52
+
53
+ set colspan(colspan) {
54
+ this.setAttr('colspan', colspan);
55
+ }
56
+
57
+ /** 内部wikitext */
58
+ get innerText() {
59
+ return this.lastChild.text();
60
+ }
61
+
62
+ /** 是否位于行首 */
63
+ isIndependent() {
64
+ return this.firstChild.text()[0] === '\n';
65
+ }
66
+
24
67
  /**
25
68
  * 获取单元格语法信息
26
- * @returns {{subtype: 'td'|'th'|'caption'}}
69
+ * @returns {{subtype: 'td'|'th'|'caption', escape: boolean, correction: boolean}}
27
70
  * @complexity `n`
28
71
  */
29
72
  getSyntax() {
30
73
  const syntax = this.firstChild.text(),
74
+ esc = syntax.includes('{{'),
31
75
  char = syntax.at(-1);
32
76
  let subtype = 'td';
33
77
  if (char === '!') {
@@ -35,7 +79,23 @@ class TdToken extends TrToken {
35
79
  } else if (char === '+') {
36
80
  subtype = 'caption';
37
81
  }
38
- return {subtype};
82
+ if (this.isIndependent()) {
83
+ return {subtype, escape: esc, correction: false};
84
+ }
85
+ const {previousSibling} = this;
86
+ if (previousSibling?.type !== 'td') {
87
+ return {subtype, escape: esc, correction: true};
88
+ }
89
+ const result = previousSibling.getSyntax();
90
+ result.escape ||= esc;
91
+ result.correction = previousSibling.lastChild
92
+ .toString('comment, ext, include, noinclude, arg, template, magic-word')
93
+ .includes('\n');
94
+ if (subtype === 'th' && result.subtype !== 'th') {
95
+ result.subtype = 'th';
96
+ result.correction = true;
97
+ }
98
+ return result;
39
99
  }
40
100
 
41
101
  /**
@@ -50,7 +110,7 @@ class TdToken extends TrToken {
50
110
  innerSyntax = undefined;
51
111
  attr = '';
52
112
  }
53
- super(syntax, attr, config, accum);
113
+ super(syntax, attr, config, accum, openingPattern);
54
114
  if (innerSyntax) {
55
115
  [this.#innerSyntax] = innerSyntax;
56
116
  }
@@ -58,6 +118,8 @@ class TdToken extends TrToken {
58
118
  const innerToken = new Token(inner?.slice(innerSyntax?.index + this.#innerSyntax.length), config, true, accum);
59
119
  innerToken.type = 'td-inner';
60
120
  this.insertAt(innerToken.setAttribute('stage', 4));
121
+ this.setAttribute('acceptable', {SyntaxToken: 0, AttributesToken: 1, Token: 2})
122
+ .seal(['getRowCount', 'getNthCol', 'insertTableCell'], true);
61
123
  }
62
124
 
63
125
  /** @override */
@@ -69,12 +131,16 @@ class TdToken extends TrToken {
69
131
 
70
132
  /**
71
133
  * @override
134
+ * @param {string} selector
72
135
  * @returns {string}
73
136
  * @complexity `n`
74
137
  */
75
138
  toString(selector) {
139
+ this.#correct();
76
140
  const {childNodes: [syntax, attr, inner]} = this;
77
- return `${syntax.toString()}${attr.toString()}${this.#innerSyntax}${inner.toString()}`;
141
+ return selector && this.matches(selector)
142
+ ? ''
143
+ : `${syntax.toString(selector)}${attr.toString(selector)}${this.#innerSyntax}${inner.toString(selector)}`;
78
144
  }
79
145
 
80
146
  /**
@@ -83,6 +149,7 @@ class TdToken extends TrToken {
83
149
  * @complexity `n`
84
150
  */
85
151
  text() {
152
+ this.#correct();
86
153
  const {childNodes: [syntax, attr, inner]} = this;
87
154
  return `${syntax.text()}${attr.text()}${this.#innerSyntax}${inner.text()}`;
88
155
  }
@@ -94,6 +161,7 @@ class TdToken extends TrToken {
94
161
  getGaps(i = 0) {
95
162
  i = i < 0 ? i + this.length : i;
96
163
  if (i === 1) {
164
+ this.#correct();
97
165
  return this.#innerSyntax.length;
98
166
  }
99
167
  return 0;
@@ -113,6 +181,163 @@ class TdToken extends TrToken {
113
181
  }
114
182
  return errors;
115
183
  }
184
+
185
+ /** @override */
186
+ print() {
187
+ const {childNodes: [syntax, attr, inner]} = this;
188
+ return `<span class="wpb-td">${syntax.print()}${attr.print()}${this.#innerSyntax}${inner.print()}</span>`;
189
+ }
190
+
191
+ /** @override */
192
+ cloneNode() {
193
+ const /** @type {TdToken} */ token = super.cloneNode();
194
+ token.setAttribute('innerSyntax', this.#innerSyntax);
195
+ return token;
196
+ }
197
+
198
+ /**
199
+ * 创建新的单元格
200
+ * @param {string|Token} inner 内部wikitext
201
+ * @param {'td'|'th'|'caption'} subtype 单元格类型
202
+ * @param {Record<string, string>} attr 单元格属性
203
+ * @param {boolean} include 是否嵌入
204
+ * @throws `RangeError` 非法的单元格类型
205
+ */
206
+ static create(inner, subtype = 'td', attr = {}, include = false, config = Parser.getConfig()) {
207
+ if (typeof inner !== 'string' && inner?.constructor !== Token || !isPlainObject(attr)) {
208
+ typeError(this, 'create', 'String', 'Token', 'Object');
209
+ } else if (subtype !== 'td' && subtype !== 'th' && subtype !== 'caption') {
210
+ throw new RangeError('单元格的子类型只能为 "td"、"th" 或 "caption"!');
211
+ } else if (typeof inner === 'string') {
212
+ inner = Parser.parse(inner, include, undefined, config);
213
+ }
214
+ const token = Parser.run(() => new TdToken('\n|', undefined, config));
215
+ token.setSyntax(subtype);
216
+ token.lastChild.safeReplaceWith(inner);
217
+ for (const [k, v] of Object.entries(attr)) {
218
+ token.setAttr(k, v);
219
+ }
220
+ return token;
221
+ }
222
+
223
+ /**
224
+ * @override
225
+ * @template {string} T
226
+ * @param {T} key 属性键
227
+ * @returns {TokenAttribute<T>}
228
+ */
229
+ getAttribute(key) {
230
+ return key === 'innerSyntax' ? this.#innerSyntax : super.getAttribute(key);
231
+ }
232
+
233
+ /**
234
+ * @override
235
+ * @template {string} T
236
+ * @param {T} key 属性键
237
+ * @param {TokenAttribute<T>} value 属性值
238
+ * @returns {this}
239
+ */
240
+ setAttribute(key, value) {
241
+ if (key === 'innerSyntax') {
242
+ this.#innerSyntax = String(value);
243
+ return this;
244
+ }
245
+ return super.setAttribute(key, value);
246
+ }
247
+
248
+ /**
249
+ * @override
250
+ * @param {string} syntax 表格语法
251
+ * @param {boolean} esc 是否需要转义
252
+ */
253
+ setSyntax(syntax, esc) {
254
+ super.setSyntax(aliases[syntax] ?? syntax, esc);
255
+ }
256
+
257
+ /**
258
+ * 修复\<td\>语法
259
+ * @complexity `n`
260
+ */
261
+ #correct() {
262
+ if (String(this.childNodes[1])) {
263
+ this.#innerSyntax ||= '|';
264
+ }
265
+ const {subtype, escape, correction} = this.getSyntax();
266
+ if (correction) {
267
+ this.setSyntax(subtype, escape);
268
+ }
269
+ }
270
+
271
+ /**
272
+ * 改为独占一行
273
+ * @complexity `n`
274
+ */
275
+ independence() {
276
+ if (!this.isIndependent()) {
277
+ const {subtype, escape} = this.getSyntax();
278
+ this.setSyntax(subtype, escape);
279
+ }
280
+ }
281
+
282
+ /**
283
+ * 获取单元格属性
284
+ * @template {string} T
285
+ * @param {T} key 属性键
286
+ * @returns {T extends 'rowspan'|'colspan' ? number : string|true}
287
+ */
288
+ getAttr(key) {
289
+ const /** @type {string|true} */ value = super.getAttr(key);
290
+ key = key?.toLowerCase()?.trim();
291
+ return key === 'rowspan' || key === 'colspan' ? Number(value) || 1 : value;
292
+ }
293
+
294
+ /**
295
+ * 获取全部单元格属性
296
+ * @returns {{rowspan: number, colspan: number, [key: string]: string|true}}
297
+ */
298
+ getAttrs() {
299
+ const /** @type {record<string, string|true>} */ attr = super.getAttrs();
300
+ if ('rowspan' in attr) {
301
+ attr.rowspan = Number(attr.rowspan);
302
+ }
303
+ if ('colspan' in attr) {
304
+ attr.colspan = Number(attr.colspan);
305
+ }
306
+ return attr;
307
+ }
308
+
309
+ /**
310
+ * 设置单元格属性
311
+ * @template {string} T
312
+ * @param {T} key 属性键
313
+ * @param {T extends 'rowspan'|'colspan' ? number : string|boolean} value 属性值
314
+ */
315
+ setAttr(key, value) {
316
+ if (typeof key !== 'string') {
317
+ this.typeError('setAttr', 'String');
318
+ }
319
+ key = key.toLowerCase().trim();
320
+ if (typeof value === 'number' && (key === 'rowspan' || key === 'colspan')) {
321
+ value = value === 1 ? false : String(value);
322
+ }
323
+ const /** @type {boolean} */ result = super.setAttr(key, value);
324
+ if (!String(this.childNodes[1])) {
325
+ this.#innerSyntax = '';
326
+ }
327
+ return result;
328
+ }
329
+
330
+ /** @override */
331
+ escape() {
332
+ super.escape();
333
+ if (String(this.childNodes[1])) {
334
+ this.#innerSyntax ||= '{{!}}';
335
+ }
336
+ if (this.#innerSyntax === '|') {
337
+ this.#innerSyntax = '{{!}}';
338
+ }
339
+ }
116
340
  }
117
341
 
342
+ Parser.classes.TdToken = __filename;
118
343
  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,14 +38,17 @@ class TrToken extends Token {
19
38
  * @param {import('../../typings/token').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
  /**
@@ -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 {import('../../typings/table').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
 
@@ -59,6 +60,11 @@ class ExtToken extends TagPairToken {
59
60
  innerToken = new PreToken(inner, newConfig, accum);
60
61
  break;
61
62
  }
63
+ case 'charinsert': {
64
+ const CharinsertToken = require('../charinsert');
65
+ innerToken = new CharinsertToken(inner, newConfig, accum);
66
+ break;
67
+ }
62
68
  case 'references': {
63
69
  const ReferencesToken = require('../nested/references');
64
70
  innerToken = new ReferencesToken(inner, newConfig, accum);
@@ -121,6 +127,20 @@ class ExtToken extends TagPairToken {
121
127
  }
122
128
  return errors;
123
129
  }
130
+
131
+ /** @override */
132
+ cloneNode() {
133
+ const inner = this.lastChild.cloneNode(),
134
+ tags = this.getAttribute('tags'),
135
+ config = this.getAttribute('config'),
136
+ attr = String(this.firstChild);
137
+ return Parser.run(() => {
138
+ const token = new ExtToken(tags[0], attr, '', this.selfClosing ? undefined : tags[1], config);
139
+ token.lastChild.safeReplaceWith(inner);
140
+ return token;
141
+ });
142
+ }
124
143
  }
125
144
 
145
+ Parser.classes.ExtToken = __filename;
126
146
  module.exports = ExtToken;
@@ -21,6 +21,30 @@ class IncludeToken extends hidden(TagPairToken) {
21
21
  constructor(name, attr = '', inner = undefined, closed = undefined, config = Parser.getConfig(), accum = []) {
22
22
  super(name, attr, inner ?? '', inner === undefined ? closed : closed ?? '', config, accum);
23
23
  }
24
+
25
+ /** @override */
26
+ cloneNode() {
27
+ const tags = this.getAttribute('tags'),
28
+ config = this.getAttribute('config'),
29
+ inner = this.selfClosing ? undefined : String(this.lastChild),
30
+ closing = this.selfClosing || !this.closed ? undefined : tags[1],
31
+ token = Parser.run(() => new IncludeToken(tags[0], String(this.firstChild), inner, closing, config));
32
+ return token;
33
+ }
34
+
35
+ /**
36
+ * @override
37
+ * @param {string} str 新文本
38
+ */
39
+ setText(str) {
40
+ return super.setText(str, 1);
41
+ }
42
+
43
+ /** 清除标签属性 */
44
+ removeAttr() {
45
+ super.setText('', 0);
46
+ }
24
47
  }
25
48
 
49
+ Parser.classes.IncludeToken = __filename;
26
50
  module.exports = IncludeToken;