wikiparser-node 1.0.0-beta.3 → 1.0.1-b

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (146) hide show
  1. package/README.md +10 -9
  2. package/bundle/bundle.min.js +37 -0
  3. package/config/llwiki.json +1 -35
  4. package/config/minimum.json +136 -0
  5. package/config/moegirl.json +1 -44
  6. package/config/zhwiki.json +1 -466
  7. package/extensions/dist/base.js +105 -0
  8. package/extensions/dist/editor.js +172 -0
  9. package/extensions/dist/highlight.js +46 -0
  10. package/extensions/dist/lint.js +43 -0
  11. package/extensions/editor.css +63 -0
  12. package/extensions/ui.css +119 -0
  13. package/package.json +35 -20
  14. package/config/.schema.json +0 -160
  15. package/dist/index.d.ts +0 -62
  16. package/dist/index.js +0 -305
  17. package/dist/internal.d.ts +0 -44
  18. package/dist/lib/element.d.ts +0 -186
  19. package/dist/lib/element.js +0 -659
  20. package/dist/lib/node.d.ts +0 -178
  21. package/dist/lib/node.js +0 -458
  22. package/dist/lib/range.d.ts +0 -132
  23. package/dist/lib/range.js +0 -387
  24. package/dist/lib/ranges.d.ts +0 -28
  25. package/dist/lib/ranges.js +0 -125
  26. package/dist/lib/text.d.ts +0 -78
  27. package/dist/lib/text.js +0 -227
  28. package/dist/lib/title.d.ts +0 -33
  29. package/dist/lib/title.js +0 -110
  30. package/dist/mixin/attributesParent.js +0 -93
  31. package/dist/mixin/fixed.js +0 -32
  32. package/dist/mixin/hidden.js +0 -23
  33. package/dist/mixin/singleLine.js +0 -26
  34. package/dist/mixin/sol.js +0 -44
  35. package/dist/parser/braces.js +0 -125
  36. package/dist/parser/commentAndExt.js +0 -58
  37. package/dist/parser/converter.js +0 -38
  38. package/dist/parser/externalLinks.js +0 -28
  39. package/dist/parser/hrAndDoubleUnderscore.js +0 -40
  40. package/dist/parser/html.js +0 -38
  41. package/dist/parser/links.js +0 -94
  42. package/dist/parser/list.js +0 -61
  43. package/dist/parser/magicLinks.js +0 -39
  44. package/dist/parser/quotes.js +0 -67
  45. package/dist/parser/selector.js +0 -165
  46. package/dist/parser/table.js +0 -117
  47. package/dist/src/arg.d.ts +0 -77
  48. package/dist/src/arg.js +0 -199
  49. package/dist/src/atom.d.ts +0 -11
  50. package/dist/src/atom.js +0 -24
  51. package/dist/src/attribute.d.ts +0 -106
  52. package/dist/src/attribute.js +0 -436
  53. package/dist/src/attributes.d.ts +0 -119
  54. package/dist/src/attributes.js +0 -366
  55. package/dist/src/converter.d.ts +0 -75
  56. package/dist/src/converter.js +0 -141
  57. package/dist/src/converterFlags.d.ts +0 -107
  58. package/dist/src/converterFlags.js +0 -238
  59. package/dist/src/converterRule.d.ts +0 -96
  60. package/dist/src/converterRule.js +0 -255
  61. package/dist/src/extLink.d.ts +0 -63
  62. package/dist/src/extLink.js +0 -157
  63. package/dist/src/gallery.d.ts +0 -70
  64. package/dist/src/gallery.js +0 -137
  65. package/dist/src/heading.d.ts +0 -82
  66. package/dist/src/heading.js +0 -135
  67. package/dist/src/hidden.d.ts +0 -19
  68. package/dist/src/hidden.js +0 -26
  69. package/dist/src/html.d.ts +0 -113
  70. package/dist/src/html.js +0 -245
  71. package/dist/src/imageParameter.d.ts +0 -82
  72. package/dist/src/imageParameter.js +0 -256
  73. package/dist/src/imagemap.d.ts +0 -77
  74. package/dist/src/imagemap.js +0 -169
  75. package/dist/src/imagemapLink.d.ts +0 -48
  76. package/dist/src/imagemapLink.js +0 -43
  77. package/dist/src/index.d.ts +0 -156
  78. package/dist/src/index.js +0 -819
  79. package/dist/src/link/base.d.ts +0 -69
  80. package/dist/src/link/base.js +0 -231
  81. package/dist/src/link/category.d.ts +0 -14
  82. package/dist/src/link/category.js +0 -28
  83. package/dist/src/link/file.d.ts +0 -105
  84. package/dist/src/link/file.js +0 -269
  85. package/dist/src/link/galleryImage.d.ts +0 -37
  86. package/dist/src/link/galleryImage.js +0 -100
  87. package/dist/src/link/index.d.ts +0 -50
  88. package/dist/src/link/index.js +0 -134
  89. package/dist/src/magicLink.d.ts +0 -46
  90. package/dist/src/magicLink.js +0 -131
  91. package/dist/src/nested.d.ts +0 -47
  92. package/dist/src/nested.js +0 -92
  93. package/dist/src/nowiki/base.d.ts +0 -38
  94. package/dist/src/nowiki/base.js +0 -31
  95. package/dist/src/nowiki/comment.d.ts +0 -42
  96. package/dist/src/nowiki/comment.js +0 -64
  97. package/dist/src/nowiki/dd.d.ts +0 -22
  98. package/dist/src/nowiki/dd.js +0 -46
  99. package/dist/src/nowiki/doubleUnderscore.d.ts +0 -35
  100. package/dist/src/nowiki/doubleUnderscore.js +0 -48
  101. package/dist/src/nowiki/hr.d.ts +0 -30
  102. package/dist/src/nowiki/hr.js +0 -36
  103. package/dist/src/nowiki/index.d.ts +0 -19
  104. package/dist/src/nowiki/index.js +0 -23
  105. package/dist/src/nowiki/list.d.ts +0 -16
  106. package/dist/src/nowiki/list.js +0 -13
  107. package/dist/src/nowiki/noinclude.d.ts +0 -21
  108. package/dist/src/nowiki/noinclude.js +0 -24
  109. package/dist/src/nowiki/quote.d.ts +0 -27
  110. package/dist/src/nowiki/quote.js +0 -58
  111. package/dist/src/onlyinclude.d.ts +0 -38
  112. package/dist/src/onlyinclude.js +0 -66
  113. package/dist/src/paramTag/index.d.ts +0 -50
  114. package/dist/src/paramTag/index.js +0 -78
  115. package/dist/src/paramTag/inputbox.d.ts +0 -7
  116. package/dist/src/paramTag/inputbox.js +0 -21
  117. package/dist/src/parameter.d.ts +0 -95
  118. package/dist/src/parameter.js +0 -204
  119. package/dist/src/pre.d.ts +0 -27
  120. package/dist/src/pre.js +0 -44
  121. package/dist/src/syntax.d.ts +0 -22
  122. package/dist/src/syntax.js +0 -67
  123. package/dist/src/table/base.d.ts +0 -60
  124. package/dist/src/table/base.js +0 -80
  125. package/dist/src/table/index.d.ts +0 -247
  126. package/dist/src/table/index.js +0 -783
  127. package/dist/src/table/td.d.ts +0 -119
  128. package/dist/src/table/td.js +0 -268
  129. package/dist/src/table/tr.d.ts +0 -31
  130. package/dist/src/table/tr.js +0 -50
  131. package/dist/src/table/trBase.d.ts +0 -62
  132. package/dist/src/table/trBase.js +0 -157
  133. package/dist/src/tagPair/ext.d.ts +0 -54
  134. package/dist/src/tagPair/ext.js +0 -150
  135. package/dist/src/tagPair/include.d.ts +0 -43
  136. package/dist/src/tagPair/include.js +0 -43
  137. package/dist/src/tagPair/index.d.ts +0 -57
  138. package/dist/src/tagPair/index.js +0 -113
  139. package/dist/src/transclude.d.ts +0 -200
  140. package/dist/src/transclude.js +0 -732
  141. package/dist/util/debug.js +0 -42
  142. package/dist/util/diff.js +0 -71
  143. package/dist/util/lint.js +0 -38
  144. package/dist/util/string.js +0 -74
  145. package/errors/README +0 -1
  146. package/printed/README +0 -1
@@ -1,783 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TableToken = void 0;
4
- const assert = require("assert/strict");
5
- const lint_1 = require("../../util/lint");
6
- const string_1 = require("../../util/string");
7
- const index_1 = require("../../index");
8
- const __1 = require("..");
9
- const tr_1 = require("./tr");
10
- const trBase_1 = require("./trBase");
11
- const td_1 = require("./td");
12
- const syntax_1 = require("../syntax");
13
- const closingPattern = /^\n[^\S\n]*(?:\|\}|\{\{\s*!\s*\}\}\}|\{\{\s*!\)\s*\}\})$/u;
14
- /**
15
- * 比较两个表格坐标
16
- * @param coords1 坐标1
17
- * @param coords2 坐标2
18
- */
19
- const cmpCoords = (coords1, coords2) => {
20
- const diff = coords1.row - coords2.row;
21
- return diff === 0 ? coords1.column - coords2.column : diff;
22
- };
23
- /**
24
- * 是否是行尾
25
- * @param {Token} cell 表格单元格
26
- */
27
- const isRowEnd = ({ type }) => type === 'tr' || type === 'table-syntax';
28
- /**
29
- * 是否是合并单元格的第一列
30
- * @param rowLayout 行布局
31
- * @param i 单元格序号
32
- * @param oneCol 是否仅有一列
33
- */
34
- const isStartCol = (rowLayout, i, oneCol = false) => {
35
- const coords = rowLayout[i];
36
- return rowLayout[i - 1] !== coords && (!oneCol || rowLayout[i + 1] !== coords);
37
- };
38
- /**
39
- * 设置表格格式
40
- * @param cells 单元格
41
- * @param attr 属性
42
- * @param multi 是否对所有单元格设置,或是仅对行首单元格设置
43
- */
44
- const format = (cells, attr = {}, multi = false) => {
45
- for (const [token, start] of cells) {
46
- if (multi || start) {
47
- if (typeof attr === 'string') {
48
- token.setSyntax(attr);
49
- }
50
- else {
51
- for (const [k, v] of Object.entries(attr)) {
52
- token.setAttr(k, v);
53
- }
54
- }
55
- }
56
- }
57
- };
58
- /**
59
- * 填补缺失单元格
60
- * @param y 行号
61
- * @param rowToken 表格行
62
- * @param layout 表格布局
63
- * @param maxCol 最大列数
64
- * @param token 待填充的单元格
65
- */
66
- const fill = (y, rowToken, layout, maxCol, token) => {
67
- const rowLayout = layout[y], lastIndex = rowToken.childNodes.findLastIndex(child => child instanceof td_1.TdToken && child.subtype !== 'caption'), pos = lastIndex + 1 || undefined;
68
- index_1.default.run(() => {
69
- for (let i = 0; i < maxCol; i++) {
70
- if (!rowLayout[i]) {
71
- rowToken.insertAt(token.cloneNode(), pos);
72
- }
73
- }
74
- });
75
- };
76
- /** @extends {Array<TableCoords[]>} */
77
- class Layout extends Array {
78
- /** 打印表格布局 */
79
- print() {
80
- const hBorders = new Array(this.length + 1).fill(undefined).map((_, i) => {
81
- const prev = this[i - 1] ?? [], next = this[i] ?? [];
82
- return new Array(Math.max(prev.length, next.length)).fill(undefined)
83
- .map((__, j) => prev[j] !== next[j]);
84
- }), vBorders = this.map(cur => new Array(cur.length + 1).fill(undefined).map((_, j) => cur[j - 1] !== cur[j]));
85
- let out = '';
86
- for (let i = 0; i <= this.length; i++) {
87
- const hBorder = hBorders[i].map(Number), vBorderTop = (vBorders[i - 1] ?? []).map(Number), vBorderBottom = (vBorders[i] ?? []).map(Number),
88
- // eslint-disable-next-line no-sparse-arrays
89
- border = [' ', , , '┌', , '┐', '─', '┬', , '│', '└', '├', '┘', '┤', '┴', '┼'];
90
- for (let j = 0; j <= hBorder.length; j++) {
91
- const bit = (vBorderTop[j] << 3) + (vBorderBottom[j] << 0)
92
- + (hBorder[j - 1] << 2) + (hBorder[j] << 1);
93
- out += `${border[bit]}${hBorder[j] ? '─' : ' '}`;
94
- }
95
- out += '\n';
96
- }
97
- console.log(out.slice(0, -1));
98
- }
99
- }
100
- /**
101
- * 表格
102
- * @classdesc `{childNodes: [SyntaxToken, AttributesToken, ?Token, ...TdToken, ...TrToken, ?SyntaxToken]}`
103
- */
104
- class TableToken extends trBase_1.TrBaseToken {
105
- /** @browser */
106
- type = 'table';
107
- /**
108
- * 表格是否闭合
109
- * @browser
110
- */
111
- get closed() {
112
- return this.lastChild.type === 'table-syntax';
113
- }
114
- set closed(closed) {
115
- if (closed && !this.closed) {
116
- this.close(this.closest('parameter') ? '\n{{!)}}' : '\n|}');
117
- }
118
- }
119
- /**
120
- * @browser
121
- * @param syntax 表格语法
122
- * @param attr 表格属性
123
- */
124
- constructor(syntax, attr = '', config = index_1.default.getConfig(), accum = []) {
125
- super(/^(?:\{\||\{\{\{\s*!\s*\}\}|\{\{\s*\(!\s*\}\})$/u, syntax, attr, config, accum, {
126
- Token: 2, SyntaxToken: [0, -1], AttributesToken: 1, TdToken: '2:', TrToken: '2:',
127
- });
128
- }
129
- /**
130
- * @override
131
- * @browser
132
- */
133
- lint(start = this.getAbsoluteIndex()) {
134
- const errors = super.lint(start);
135
- if (!this.closed) {
136
- const { firstChild, lastChild: tr } = this, { lastChild: td } = tr, error = (0, lint_1.generateForChild)(firstChild, { start }, 'unclosed table');
137
- errors.push({ ...error, excerpt: String(td?.type === 'td' ? td : tr).slice(0, 50) });
138
- }
139
- return errors;
140
- }
141
- /**
142
- * 闭合表格语法
143
- * @browser
144
- * @param syntax 表格结尾语法
145
- * @param halfParsed
146
- * @throws `SyntaxError` 表格的闭合部分不符合语法
147
- */
148
- close(syntax = '\n|}', halfParsed = false) {
149
- const isHalfParsed = halfParsed && index_1.default.running, config = this.getAttribute('config'), accum = this.getAttribute('accum'), inner = !isHalfParsed && index_1.default.parse(syntax, this.getAttribute('include'), 2, config), { lastChild } = this;
150
- if (inner && !closingPattern.test(inner.text())) {
151
- throw new SyntaxError(`表格的闭合部分不符合语法:${(0, string_1.noWrap)(syntax)}`);
152
- }
153
- else if (lastChild instanceof syntax_1.SyntaxToken) {
154
- lastChild.replaceChildren(...inner.childNodes);
155
- }
156
- else {
157
- super.insertAt(index_1.default.run(() => {
158
- const token = new syntax_1.SyntaxToken(syntax, closingPattern, 'table-syntax', config, accum, {
159
- 'Stage-1': ':', '!ExtToken': '', TranscludeToken: ':',
160
- });
161
- if (inner) {
162
- token.replaceChildren(...inner.childNodes);
163
- }
164
- return token;
165
- }));
166
- }
167
- }
168
- /**
169
- * @override
170
- * @param token 待插入的子节点
171
- * @param i 插入位置
172
- * @throws `SyntaxError` 表格的闭合部分非法
173
- */
174
- insertAt(token, i = this.length) {
175
- const previous = this.childNodes.at(i - 1);
176
- if (typeof token !== 'string' && token.type === 'td' && previous?.type === 'tr') {
177
- index_1.default.warn('改为将单元格插入当前行。');
178
- return previous.insertAt(token);
179
- }
180
- else if (i > 0 && i === this.length && token instanceof syntax_1.SyntaxToken
181
- && (token.getAttribute('pattern') !== closingPattern || !closingPattern.test(token.text()))) {
182
- throw new SyntaxError(`表格的闭合部分不符合语法:${(0, string_1.noWrap)(String(token))}`);
183
- }
184
- return super.insertAt(token, i);
185
- }
186
- /** @override */
187
- getRowCount() {
188
- return super.getRowCount() + this.childNodes.filter(child => child.type === 'tr' && child.getRowCount()).length;
189
- }
190
- /** 获取下一行 */
191
- getNextRow() {
192
- return this.getNthRow(super.getRowCount() ? 1 : 0, false, false);
193
- }
194
- /** @ignore */
195
- getNthRow(n, force = false, insert = false) {
196
- const nRows = this.getRowCount(), isRow = super.getRowCount();
197
- let m = n < 0 ? n + nRows : n;
198
- if (m === 0 && (isRow || force && nRows === 0)) {
199
- return this;
200
- }
201
- else if (m < 0 || m > nRows || m === nRows && !insert) {
202
- throw new RangeError(`不存在第 ${m} 行!`);
203
- }
204
- else if (isRow) {
205
- m--;
206
- }
207
- for (const child of this.childNodes.slice(2)) {
208
- if (child.type === 'tr' && child.getRowCount()) {
209
- m--;
210
- if (m < 0) {
211
- return child;
212
- }
213
- }
214
- else if (child.type === 'table-syntax') {
215
- return child;
216
- }
217
- }
218
- return undefined;
219
- }
220
- /** 获取所有行 */
221
- getAllRows() {
222
- const { childNodes: [, ...childNodes] } = this;
223
- return [
224
- ...super.getRowCount() ? [this] : [],
225
- ...childNodes.filter(child => child.type === 'tr' && child.getRowCount()),
226
- ];
227
- }
228
- /**
229
- * 获取指定坐标的单元格
230
- * @param coords 表格坐标
231
- */
232
- getNthCell(coords) {
233
- const rawCoords = coords.row === undefined ? this.toRawCoords(coords) : coords;
234
- return rawCoords && this.getNthRow(rawCoords.row, false, false)?.getNthCol(rawCoords.column);
235
- }
236
- /**
237
- * 获取表格布局
238
- * @param stop 中止条件
239
- * @param stop.row 中止行
240
- * @param stop.column 中止列
241
- * @param stop.x 中止行
242
- * @param stop.y 中止列
243
- */
244
- getLayout(stop) {
245
- const rows = this.getAllRows(), { length } = rows, layout = new Layout(...new Array(length).fill(undefined).map(() => []));
246
- for (let i = 0; i < length; i++) {
247
- if (i > (stop?.row ?? stop?.y ?? NaN)) {
248
- break;
249
- }
250
- const rowLayout = layout[i];
251
- let j = 0, k = 0, last;
252
- for (const cell of rows[i].childNodes.slice(2)) {
253
- if (cell instanceof td_1.TdToken) {
254
- if (cell.isIndependent()) {
255
- last = cell.subtype !== 'caption';
256
- }
257
- if (last) {
258
- const coords = { row: i, column: j }, { rowspan, colspan } = cell;
259
- j++;
260
- while (rowLayout[k]) {
261
- k++;
262
- }
263
- if (i === stop?.row && j > (stop.column ?? NaN)) {
264
- layout[i][k] = coords;
265
- return layout;
266
- }
267
- for (let y = i; y < Math.min(i + rowspan, length); y++) {
268
- for (let x = k; x < k + colspan; x++) {
269
- layout[y][x] = coords;
270
- }
271
- }
272
- k += colspan;
273
- if (i === stop?.y && k > (stop.x ?? NaN)) {
274
- return layout;
275
- }
276
- }
277
- }
278
- else if (isRowEnd(cell)) {
279
- break;
280
- }
281
- }
282
- }
283
- return layout;
284
- }
285
- /** 打印表格布局 */
286
- printLayout() {
287
- this.getLayout().print();
288
- }
289
- /**
290
- * 转换为渲染后的表格坐标
291
- * @param {TableCoords} coord wikitext中的表格坐标
292
- */
293
- toRenderedCoords({ row, column }) {
294
- const rowLayout = this.getLayout({ row, column })[row], x = rowLayout?.findIndex(coords => cmpCoords(coords, { row, column }) === 0);
295
- return rowLayout && (x === -1 ? undefined : { y: row, x: x });
296
- }
297
- /**
298
- * 转换为wikitext中的表格坐标
299
- * @param {TableRenderedCoords} coord 渲染后的表格坐标
300
- */
301
- toRawCoords({ x, y }) {
302
- const rowLayout = this.getLayout({ x, y })[y], coords = rowLayout?.[x];
303
- if (coords) {
304
- return { ...coords, start: coords.row === y && rowLayout[x - 1] !== coords };
305
- }
306
- else if (rowLayout || y > 0) {
307
- return x === rowLayout?.length
308
- ? { row: y, column: (rowLayout.findLast(({ row }) => row === y)?.column ?? -1) + 1, start: true }
309
- : undefined;
310
- }
311
- return { row: 0, column: 0, start: true };
312
- }
313
- /**
314
- * 获取完整行
315
- * @param y 行号
316
- */
317
- getFullRow(y) {
318
- const rows = this.getAllRows();
319
- return new Map(this.getLayout({ y })[y]?.map(({ row, column }) => [rows[row].getNthCol(column), row === y]));
320
- }
321
- /**
322
- * 获取完整列
323
- * @param x 列号
324
- */
325
- getFullCol(x) {
326
- const layout = this.getLayout(), colLayout = layout.map(row => row[x]).filter(Boolean), rows = this.getAllRows();
327
- return new Map(colLayout.map(coords => [rows[coords.row].getNthCol(coords.column), layout[coords.row][x - 1] !== coords]));
328
- }
329
- /**
330
- * 设置行格式
331
- * @param y 行号
332
- * @param attr 表格属性
333
- * @param multiRow 是否对所有单元格设置,或是仅对行首单元格设置
334
- */
335
- formatTableRow(y, attr = {}, multiRow = false) {
336
- format(this.getFullRow(y), attr, multiRow);
337
- }
338
- /**
339
- * 设置列格式
340
- * @param x 列号
341
- * @param attr 表格属性
342
- * @param multiCol 是否对所有单元格设置,或是仅对行首单元格设置
343
- */
344
- formatTableCol(x, attr = {}, multiCol = false) {
345
- format(this.getFullCol(x), attr, multiCol);
346
- }
347
- /**
348
- * 填补表格行
349
- * @param y 行号
350
- * @param inner 填充内容
351
- * @param subtype 单元格类型
352
- * @param attr 表格属性
353
- */
354
- fillTableRow(y, inner, subtype = 'td', attr = {}) {
355
- const rowToken = this.getNthRow(y), layout = this.getLayout({ y }), maxCol = Math.max(...layout.map(({ length }) => length)), token = td_1.TdToken.create(inner, subtype, attr, this.getAttribute('include'), this.getAttribute('config'));
356
- fill(y, rowToken, layout, maxCol, token);
357
- }
358
- /**
359
- * 填补表格
360
- * @param inner 填充内容
361
- * @param subtype 单元格类型
362
- * @param attr 表格属性
363
- */
364
- fillTable(inner, subtype = 'td', attr = {}) {
365
- const rowTokens = this.getAllRows(), layout = this.getLayout(), maxCol = Math.max(...layout.map(({ length }) => length)), token = td_1.TdToken.create(inner, subtype, attr, this.getAttribute('include'), this.getAttribute('config'));
366
- for (let y = 0; y < rowTokens.length; y++) {
367
- fill(y, rowTokens[y], layout, maxCol, token);
368
- }
369
- }
370
- /**
371
- * @override
372
- * @param inner 单元格内部wikitext
373
- * @param coords 单元格坐标
374
- * @param subtype 单元格类型
375
- * @param attr 单元格属性
376
- * @throws `RangeError` 指定的坐标不是单元格起始点
377
- */
378
- insertTableCell(inner, coords, subtype = 'td', attr = {}) {
379
- let rawCoords;
380
- if (coords.column === undefined) {
381
- const { x, y } = coords;
382
- rawCoords = this.toRawCoords(coords);
383
- if (!rawCoords?.start) {
384
- throw new RangeError(`指定的坐标不是单元格起始点:(${x}, ${y})`);
385
- }
386
- }
387
- else {
388
- rawCoords = coords;
389
- }
390
- const rowToken = this.getNthRow(rawCoords.row, true);
391
- return rowToken === this
392
- ? super.insertTableCell(inner, rawCoords, subtype, attr)
393
- : rowToken.insertTableCell(inner, rawCoords, subtype, attr);
394
- }
395
- /** 在开头插入一行 */
396
- #prependTableRow() {
397
- // @ts-expect-error abstract class
398
- const row = index_1.default.run(() => new tr_1.TrToken('\n|-', undefined, this.getAttribute('config'))), { childNodes } = this, [, , plain] = childNodes,
399
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
400
- start = plain?.constructor === __1.Token ? 3 : 2, tdChildren = childNodes.slice(start), index = tdChildren.findIndex(({ type }) => type !== 'td');
401
- this.insertAt(row, index === -1 ? -1 : index + start);
402
- index_1.default.run(() => {
403
- for (const cell of tdChildren.slice(0, index === -1 ? undefined : index)) {
404
- if (cell.subtype !== 'caption') {
405
- row.insertAt(cell);
406
- }
407
- }
408
- });
409
- return row;
410
- }
411
- /**
412
- * 插入表格行
413
- * @param y 行号
414
- * @param attr 表格行属性
415
- * @param inner 内部wikitext
416
- * @param subtype 单元格类型
417
- * @param innerAttr 单元格属性
418
- */
419
- insertTableRow(y, attr = {}, inner, subtype = 'td', innerAttr = {}) {
420
- let reference = this.getNthRow(y, false, true);
421
- // @ts-expect-error abstract class
422
- const token = index_1.default.run(() => new tr_1.TrToken('\n|-', undefined, this.getAttribute('config')));
423
- for (const [k, v] of Object.entries(attr)) {
424
- token.setAttr(k, v);
425
- }
426
- if (reference?.type === 'table') { // `row === 0`且表格自身是有效行
427
- reference = this.#prependTableRow();
428
- }
429
- this.insertBefore(token, reference);
430
- if (inner !== undefined) {
431
- const td = token.insertTableCell(inner, { row: 0, column: 0 }, subtype, innerAttr), set = new WeakSet(), layout = this.getLayout({ y }), maxCol = Math.max(...layout.map(({ length }) => length)), rowLayout = layout[y];
432
- index_1.default.run(() => {
433
- for (let i = 0; i < maxCol; i++) {
434
- const coords = rowLayout[i];
435
- if (!coords) {
436
- token.insertAt(td.cloneNode());
437
- }
438
- else if (!set.has(coords)) {
439
- set.add(coords);
440
- if (coords.row < y) {
441
- this.getNthCell(coords).rowspan++;
442
- }
443
- }
444
- }
445
- });
446
- }
447
- return token;
448
- }
449
- /**
450
- * 插入表格列
451
- * @param x 列号
452
- * @param inner 内部wikitext
453
- * @param subtype 单元格类型
454
- * @param attr 单元格属性
455
- * @throws `RangeError` 列号过大
456
- */
457
- insertTableCol(x, inner, subtype = 'td', attr = {}) {
458
- const layout = this.getLayout(), rowLength = layout.map(({ length }) => length), minCol = Math.min(...rowLength);
459
- if (x > minCol) {
460
- throw new RangeError(`表格第 ${rowLength.indexOf(minCol)} 行仅有 ${minCol} 列!`);
461
- }
462
- const token = td_1.TdToken.create(inner, subtype, attr, this.getAttribute('include'), this.getAttribute('config'));
463
- for (let i = 0; i < layout.length; i++) {
464
- const coords = layout[i][x], prevCoords = x === 0 ? true : layout[i][x - 1];
465
- if (!prevCoords) {
466
- continue;
467
- }
468
- else if (prevCoords !== coords) {
469
- const rowToken = this.getNthRow(i);
470
- rowToken.insertBefore(token.cloneNode(), rowToken.getNthCol(coords.column, true));
471
- }
472
- else if (coords.row === i) {
473
- this.getNthCell(coords).colspan++;
474
- }
475
- }
476
- }
477
- /**
478
- * 移除表格行
479
- * @param y 行号
480
- */
481
- removeTableRow(y) {
482
- const rows = this.getAllRows(), layout = this.getLayout(), rowLayout = layout[y], set = new WeakSet();
483
- for (let x = rowLayout.length - 1; x >= 0; x--) {
484
- const coords = rowLayout[x];
485
- if (set.has(coords)) {
486
- continue;
487
- }
488
- set.add(coords);
489
- const token = rows[coords.row].getNthCol(coords.column);
490
- let { rowspan } = token;
491
- if (rowspan > 1) {
492
- token.rowspan = --rowspan;
493
- if (coords.row === y) {
494
- const { colspan, subtype } = token, attr = token.getAttrs();
495
- for (let i = y + 1; rowspan && i < rows.length; i++, rowspan--) {
496
- const { column } = layout[i].slice(x + colspan).find(({ row }) => row === i) ?? {};
497
- if (column !== undefined) {
498
- rows[i].insertTableCell('', { row: 0, column }, subtype, { ...attr, rowspan });
499
- break;
500
- }
501
- }
502
- }
503
- }
504
- }
505
- const rowToken = rows[y].type === 'table' ? this.#prependTableRow() : rows[y];
506
- rowToken.remove();
507
- return rowToken;
508
- }
509
- /**
510
- * 移除表格列
511
- * @param x 列号
512
- */
513
- removeTableCol(x) {
514
- for (const [token, start] of this.getFullCol(x)) {
515
- const { colspan, lastChild } = token;
516
- if (colspan > 1) {
517
- token.colspan = colspan - 1;
518
- if (start) {
519
- lastChild.replaceChildren();
520
- }
521
- }
522
- else {
523
- token.remove();
524
- }
525
- }
526
- }
527
- /**
528
- * 合并单元格
529
- * @param xlim 列范围
530
- * @param ylim 行范围
531
- * @throws `RangeError` 待合并区域与外侧区域有重叠
532
- */
533
- mergeCells(xlim, ylim) {
534
- const layout = this.getLayout(), maxCol = Math.max(...layout.map(({ length }) => length)), posXlim = xlim.map(x => x < 0 ? x + maxCol : x), posYlim = ylim.map(y => y < 0 ? y + layout.length : y), [xmin, xmax] = posXlim.sort(), [ymin, ymax] = posYlim.sort(), set = new Set(layout.slice(ymin, ymax).flatMap(rowLayout => rowLayout.slice(xmin, xmax)));
535
- if ([...layout[ymin - 1] ?? [], ...layout[ymax] ?? []].some(coords => set.has(coords))
536
- || layout.some(rowLayout => set.has(rowLayout[xmin - 1]) || set.has(rowLayout[xmax]))) {
537
- throw new RangeError('待合并区域与外侧区域有重叠!');
538
- }
539
- const corner = layout[ymin][xmin], rows = this.getAllRows(), cornerCell = rows[corner.row].getNthCol(corner.column);
540
- cornerCell.rowspan = ymax - ymin;
541
- cornerCell.colspan = xmax - xmin;
542
- set.delete(corner);
543
- for (const token of [...set].map(({ row, column }) => rows[row].getNthCol(column))) {
544
- token.remove();
545
- }
546
- return cornerCell;
547
- }
548
- /**
549
- * 分裂单元格
550
- * @param coords 单元格坐标
551
- * @param dirs 分裂方向
552
- * @throws `RangeError` 指定的坐标不是单元格起始点
553
- */
554
- #split(coords, dirs) {
555
- const cell = this.getNthCell(coords), attr = cell.getAttrs(), { subtype } = cell;
556
- attr.rowspan ||= 1;
557
- attr.colspan ||= 1;
558
- for (const dir of dirs) {
559
- if (attr[dir] === 1) {
560
- dirs.delete(dir);
561
- }
562
- }
563
- if (dirs.size === 0) {
564
- return;
565
- }
566
- let { x, y } = coords;
567
- const rawCoords = x === undefined ? coords : this.toRawCoords(coords);
568
- if (rawCoords.start === false || x === undefined) {
569
- ({ x, y } = this.toRenderedCoords(rawCoords));
570
- }
571
- const splitting = { rowspan: 1, colspan: 1 };
572
- for (const dir of dirs) {
573
- cell.setAttr(dir, 1);
574
- splitting[dir] = attr[dir];
575
- delete attr[dir];
576
- }
577
- for (let j = y; j < y + splitting.rowspan; j++) {
578
- for (let i = x; i < x + splitting.colspan; i++) {
579
- if (i > x || j > y) {
580
- try {
581
- this.insertTableCell('', { x: i, y: j }, subtype, attr);
582
- }
583
- catch (e) {
584
- if (e instanceof RangeError && e.message.startsWith('指定的坐标不是单元格起始点:')) {
585
- break;
586
- }
587
- throw e;
588
- }
589
- }
590
- }
591
- }
592
- }
593
- /**
594
- * 分裂成多行
595
- * @param coords 单元格坐标
596
- */
597
- splitIntoRows(coords) {
598
- this.#split(coords, new Set(['rowspan']));
599
- }
600
- /**
601
- * 分裂成多列
602
- * @param coords 单元格坐标
603
- */
604
- splitIntoCols(coords) {
605
- this.#split(coords, new Set(['colspan']));
606
- }
607
- /**
608
- * 分裂成单元格
609
- * @param coords 单元格坐标
610
- */
611
- splitIntoCells(coords) {
612
- this.#split(coords, new Set(['rowspan', 'colspan']));
613
- }
614
- /**
615
- * 复制一行并插入该行之前
616
- * @param row 行号
617
- */
618
- replicateTableRow(row) {
619
- let rowToken = this.getNthRow(row);
620
- if (rowToken.type === 'table') {
621
- rowToken = this.#prependTableRow();
622
- }
623
- const replicated = this.insertBefore(rowToken.cloneNode(), rowToken);
624
- for (const [token, start] of this.getFullRow(row)) {
625
- if (start) {
626
- token.rowspan = 1;
627
- }
628
- else {
629
- token.rowspan++;
630
- }
631
- }
632
- return replicated;
633
- }
634
- /**
635
- * 复制一列并插入该列之前
636
- * @param x 列号
637
- */
638
- replicateTableCol(x) {
639
- const replicated = [];
640
- for (const [token, start] of this.getFullCol(x)) {
641
- if (start) {
642
- const newToken = token.cloneNode();
643
- newToken.colspan = 1;
644
- token.before(newToken);
645
- replicated.push(newToken);
646
- }
647
- else {
648
- token.colspan++;
649
- }
650
- }
651
- return replicated;
652
- }
653
- /**
654
- * 移动表格行
655
- * @param y 行号
656
- * @param before 新位置
657
- * @throws `RangeError` 无法移动
658
- */
659
- moveTableRowBefore(y, before) {
660
- const layout = this.getLayout(),
661
- /** @ignore */
662
- occupied = (i) => layout[i].map(({ row }, j) => row === i ? j : undefined).filter(j => j !== undefined);
663
- try {
664
- assert.deepStrictEqual(occupied(y), occupied(before));
665
- }
666
- catch (e) {
667
- if (e instanceof assert.AssertionError) {
668
- throw new RangeError(`第 ${y} 行与第 ${before} 行的构造不同,无法移动!`);
669
- }
670
- throw e;
671
- }
672
- const rowToken = this.removeTableRow(y);
673
- for (const coords of layout[before]) {
674
- if (coords.row < before) {
675
- this.getNthCell(coords).rowspan++;
676
- }
677
- }
678
- let beforeToken = this.getNthRow(before);
679
- if (beforeToken.type === 'table') {
680
- beforeToken = this.#prependTableRow();
681
- }
682
- this.insertBefore(rowToken, beforeToken);
683
- return rowToken;
684
- }
685
- /**
686
- * 移动表格行
687
- * @param y 行号
688
- * @param after 新位置
689
- * @throws `RangeError` 无法移动
690
- */
691
- moveTableRowAfter(y, after) {
692
- const layout = this.getLayout(), afterToken = this.getNthRow(after), cells = afterToken.childNodes.filter(child => child instanceof td_1.TdToken && child.subtype !== 'caption'),
693
- /** @ignore */
694
- occupied = (i, oneRow = false) => layout[i].map(({ row, column }, j) => row === i && (!oneRow || cells[column].rowspan === 1) ? j : undefined).filter(j => j !== undefined);
695
- try {
696
- assert.deepStrictEqual(occupied(y), occupied(after, true));
697
- }
698
- catch (e) {
699
- if (e instanceof assert.AssertionError) {
700
- throw new RangeError(`第 ${y} 行与第 ${after} 行的构造不同,无法移动!`);
701
- }
702
- throw e;
703
- }
704
- const rowToken = this.removeTableRow(y);
705
- for (const coords of layout[after]) {
706
- if (coords.row < after) {
707
- this.getNthCell(coords).rowspan++;
708
- }
709
- else {
710
- const cell = cells[coords.column], { rowspan } = cell;
711
- if (rowspan > 1) {
712
- cell.rowspan = rowspan + 1;
713
- }
714
- }
715
- }
716
- if (afterToken === this) {
717
- const index = this.childNodes.slice(2).findIndex(isRowEnd);
718
- this.insertAt(rowToken, index + 2);
719
- }
720
- else {
721
- this.insertBefore(rowToken, afterToken);
722
- }
723
- return rowToken;
724
- }
725
- /**
726
- * 移动表格列
727
- * @param x 列号
728
- * @param reference 新位置
729
- * @param after 在新位置之后或之前
730
- * @throws `RangeError` 无法移动
731
- */
732
- #moveCol(x, reference, after = false) {
733
- const layout = this.getLayout();
734
- if (layout.some(rowLayout => isStartCol(rowLayout, x) !== isStartCol(rowLayout, reference, after))) {
735
- throw new RangeError(`第 ${x} 列与第 ${reference} 列的构造不同,无法移动!`);
736
- }
737
- const setX = new WeakSet(), setRef = new WeakSet(), rows = this.getAllRows();
738
- for (let i = 0; i < layout.length; i++) {
739
- const rowLayout = layout[i], coords = rowLayout[x], refCoords = rowLayout[reference], start = isStartCol(rowLayout, x);
740
- if (refCoords && !start && !setRef.has(refCoords)) {
741
- setRef.add(refCoords);
742
- rows[refCoords.row].getNthCol(refCoords.column).colspan++;
743
- }
744
- if (coords && !setX.has(coords)) {
745
- setX.add(coords);
746
- const rowToken = rows[i];
747
- let token = rowToken.getNthCol(coords.column);
748
- const { colspan } = token;
749
- if (colspan > 1) {
750
- token.colspan = colspan - 1;
751
- if (start) {
752
- const original = token;
753
- token = token.cloneNode();
754
- original.lastChild.replaceChildren();
755
- token.colspan = 1;
756
- }
757
- }
758
- if (start) {
759
- const col = rowLayout.slice(reference + Number(after)).find(({ row }) => row === i)?.column;
760
- rowToken.insertBefore(token, col === undefined ? rowToken.childNodes.slice(2).find(isRowEnd) : rowToken.getNthCol(col));
761
- }
762
- }
763
- }
764
- }
765
- /**
766
- * 移动表格列
767
- * @param x 列号
768
- * @param before 新位置
769
- */
770
- moveTableColBefore(x, before) {
771
- this.#moveCol(x, before);
772
- }
773
- /**
774
- * 移动表格列
775
- * @param x 列号
776
- * @param after 新位置
777
- */
778
- moveTableColAfter(x, after) {
779
- this.#moveCol(x, after, true);
780
- }
781
- }
782
- exports.TableToken = TableToken;
783
- index_1.default.classes['TableToken'] = __filename;