wikiparser-node 1.1.6 → 1.2.0-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 (154) hide show
  1. package/bundle/bundle.min.js +36 -0
  2. package/config/llwiki.json +1 -35
  3. package/config/moegirl.json +1 -44
  4. package/config/zhwiki.json +1 -466
  5. package/extensions/dist/base.js +68 -0
  6. package/extensions/dist/editor.js +159 -0
  7. package/extensions/dist/highlight.js +30 -0
  8. package/extensions/dist/lint.js +48 -0
  9. package/extensions/editor.css +63 -0
  10. package/extensions/ui.css +117 -0
  11. package/package.json +32 -12
  12. package/config/.schema.json +0 -172
  13. package/dist/addon/table.d.ts +0 -6
  14. package/dist/addon/table.js +0 -564
  15. package/dist/base.d.ts +0 -46
  16. package/dist/bin/toc.js +0 -18
  17. package/dist/index.d.ts +0 -31
  18. package/dist/index.js +0 -209
  19. package/dist/internal.d.ts +0 -44
  20. package/dist/lib/element.d.ts +0 -155
  21. package/dist/lib/element.js +0 -658
  22. package/dist/lib/node.d.ts +0 -146
  23. package/dist/lib/node.js +0 -419
  24. package/dist/lib/range.d.ts +0 -104
  25. package/dist/lib/range.js +0 -385
  26. package/dist/lib/ranges.d.ts +0 -26
  27. package/dist/lib/ranges.js +0 -117
  28. package/dist/lib/text.d.ts +0 -62
  29. package/dist/lib/text.js +0 -235
  30. package/dist/lib/title.d.ts +0 -38
  31. package/dist/lib/title.js +0 -162
  32. package/dist/mixin/attributesParent.js +0 -94
  33. package/dist/mixin/fixed.js +0 -32
  34. package/dist/mixin/flagsParent.js +0 -70
  35. package/dist/mixin/hidden.js +0 -22
  36. package/dist/mixin/magicLinkParent.js +0 -41
  37. package/dist/mixin/singleLine.js +0 -25
  38. package/dist/mixin/sol.js +0 -43
  39. package/dist/mixin/syntax.js +0 -56
  40. package/dist/parser/braces.js +0 -123
  41. package/dist/parser/commentAndExt.js +0 -69
  42. package/dist/parser/converter.js +0 -40
  43. package/dist/parser/externalLinks.js +0 -28
  44. package/dist/parser/hrAndDoubleUnderscore.js +0 -38
  45. package/dist/parser/html.js +0 -36
  46. package/dist/parser/links.js +0 -94
  47. package/dist/parser/list.js +0 -59
  48. package/dist/parser/magicLinks.js +0 -40
  49. package/dist/parser/quotes.js +0 -67
  50. package/dist/parser/selector.js +0 -163
  51. package/dist/parser/table.js +0 -112
  52. package/dist/src/arg.d.ts +0 -58
  53. package/dist/src/arg.js +0 -190
  54. package/dist/src/atom.d.ts +0 -12
  55. package/dist/src/atom.js +0 -27
  56. package/dist/src/attribute.d.ts +0 -86
  57. package/dist/src/attribute.js +0 -392
  58. package/dist/src/attributes.d.ts +0 -107
  59. package/dist/src/attributes.js +0 -344
  60. package/dist/src/converter.d.ts +0 -51
  61. package/dist/src/converter.js +0 -79
  62. package/dist/src/converterFlags.d.ts +0 -87
  63. package/dist/src/converterFlags.js +0 -223
  64. package/dist/src/converterRule.d.ts +0 -81
  65. package/dist/src/converterRule.js +0 -212
  66. package/dist/src/extLink.d.ts +0 -53
  67. package/dist/src/extLink.js +0 -121
  68. package/dist/src/gallery.d.ts +0 -55
  69. package/dist/src/gallery.js +0 -129
  70. package/dist/src/heading.d.ts +0 -72
  71. package/dist/src/heading.js +0 -128
  72. package/dist/src/hidden.d.ts +0 -21
  73. package/dist/src/hidden.js +0 -23
  74. package/dist/src/html.d.ts +0 -103
  75. package/dist/src/html.js +0 -230
  76. package/dist/src/imageParameter.d.ts +0 -66
  77. package/dist/src/imageParameter.js +0 -244
  78. package/dist/src/imagemap.d.ts +0 -57
  79. package/dist/src/imagemap.js +0 -148
  80. package/dist/src/imagemapLink.d.ts +0 -57
  81. package/dist/src/imagemapLink.js +0 -40
  82. package/dist/src/index.d.ts +0 -146
  83. package/dist/src/index.js +0 -777
  84. package/dist/src/link/base.d.ts +0 -52
  85. package/dist/src/link/base.js +0 -207
  86. package/dist/src/link/category.d.ts +0 -13
  87. package/dist/src/link/category.js +0 -29
  88. package/dist/src/link/file.d.ts +0 -96
  89. package/dist/src/link/file.js +0 -253
  90. package/dist/src/link/galleryImage.d.ts +0 -43
  91. package/dist/src/link/galleryImage.js +0 -106
  92. package/dist/src/link/index.d.ts +0 -53
  93. package/dist/src/link/index.js +0 -123
  94. package/dist/src/magicLink.d.ts +0 -69
  95. package/dist/src/magicLink.js +0 -148
  96. package/dist/src/nested.d.ts +0 -44
  97. package/dist/src/nested.js +0 -87
  98. package/dist/src/nowiki/base.d.ts +0 -46
  99. package/dist/src/nowiki/base.js +0 -42
  100. package/dist/src/nowiki/comment.d.ts +0 -39
  101. package/dist/src/nowiki/comment.js +0 -67
  102. package/dist/src/nowiki/dd.d.ts +0 -8
  103. package/dist/src/nowiki/dd.js +0 -25
  104. package/dist/src/nowiki/doubleUnderscore.d.ts +0 -43
  105. package/dist/src/nowiki/doubleUnderscore.js +0 -50
  106. package/dist/src/nowiki/hr.d.ts +0 -29
  107. package/dist/src/nowiki/hr.js +0 -14
  108. package/dist/src/nowiki/index.d.ts +0 -16
  109. package/dist/src/nowiki/index.js +0 -21
  110. package/dist/src/nowiki/list.d.ts +0 -29
  111. package/dist/src/nowiki/list.js +0 -48
  112. package/dist/src/nowiki/listBase.d.ts +0 -18
  113. package/dist/src/nowiki/listBase.js +0 -12
  114. package/dist/src/nowiki/noinclude.d.ts +0 -25
  115. package/dist/src/nowiki/noinclude.js +0 -25
  116. package/dist/src/nowiki/quote.d.ts +0 -22
  117. package/dist/src/nowiki/quote.js +0 -50
  118. package/dist/src/onlyinclude.d.ts +0 -16
  119. package/dist/src/onlyinclude.js +0 -57
  120. package/dist/src/paramTag/index.d.ts +0 -37
  121. package/dist/src/paramTag/index.js +0 -68
  122. package/dist/src/paramTag/inputbox.d.ts +0 -8
  123. package/dist/src/paramTag/inputbox.js +0 -23
  124. package/dist/src/parameter.d.ts +0 -84
  125. package/dist/src/parameter.js +0 -206
  126. package/dist/src/pre.d.ts +0 -28
  127. package/dist/src/pre.js +0 -51
  128. package/dist/src/syntax.d.ts +0 -24
  129. package/dist/src/syntax.js +0 -30
  130. package/dist/src/table/base.d.ts +0 -52
  131. package/dist/src/table/base.js +0 -78
  132. package/dist/src/table/index.d.ts +0 -229
  133. package/dist/src/table/index.js +0 -380
  134. package/dist/src/table/td.d.ts +0 -103
  135. package/dist/src/table/td.js +0 -261
  136. package/dist/src/table/tr.d.ts +0 -32
  137. package/dist/src/table/tr.js +0 -56
  138. package/dist/src/table/trBase.d.ts +0 -54
  139. package/dist/src/table/trBase.js +0 -159
  140. package/dist/src/tagPair/ext.d.ts +0 -58
  141. package/dist/src/tagPair/ext.js +0 -142
  142. package/dist/src/tagPair/include.d.ts +0 -52
  143. package/dist/src/tagPair/include.js +0 -58
  144. package/dist/src/tagPair/index.d.ts +0 -47
  145. package/dist/src/tagPair/index.js +0 -93
  146. package/dist/src/transclude.d.ts +0 -167
  147. package/dist/src/transclude.js +0 -689
  148. package/dist/util/constants.js +0 -108
  149. package/dist/util/debug.js +0 -64
  150. package/dist/util/diff.js +0 -72
  151. package/dist/util/lint.js +0 -48
  152. package/dist/util/string.js +0 -61
  153. package/errors/README +0 -1
  154. package/printed/README +0 -1
@@ -1,103 +0,0 @@
1
- import * as Parser from '../../index';
2
- import { Token } from '../index';
3
- import { TableBaseToken } from './base';
4
- import type { LintError } from '../../base';
5
- import type { SyntaxToken, AttributesToken, TrToken, TableToken } from '../../internal';
6
- export type TdSubtypes = 'td' | 'th' | 'caption';
7
- export interface TdSpanAttrs {
8
- rowspan?: number;
9
- colspan?: number;
10
- }
11
- declare type TdAttrGetter<T extends string> = T extends keyof TdSpanAttrs ? number : string | true | undefined;
12
- declare type TdAttrSetter<T extends string> = T extends keyof TdSpanAttrs ? number : string | boolean;
13
- export type TdAttrs = Record<string, string | true> & TdSpanAttrs;
14
- declare const TdToken_base: ((abstract new (...args: any[]) => {
15
- removeAt(): never;
16
- insertAt(token: string, i?: number | undefined): Parser.AstText;
17
- insertAt<T extends Parser.AstNodes>(token: T, i?: number | undefined): T;
18
- readonly length: number;
19
- toString(omit?: Set<string> | undefined, separator?: string | undefined): string;
20
- text(separator?: string | undefined): string;
21
- afterBuild(): void;
22
- getAttribute<T_1 extends string>(key: T_1): TokenAttributeGetter<T_1>;
23
- setAttribute<T_2 extends string>(key: T_2, value: TokenAttributeSetter<T_2>): void;
24
- addEventListener(events: string | string[], listener: AstListener): void;
25
- replaceChildren(...elements: (string | Parser.AstNodes)[]): void;
26
- }) & {
27
- readonly fixed: true;
28
- }) & typeof TableBaseToken;
29
- /**
30
- * `<td>`、`<th>`和`<caption>`
31
- * @classdesc `{childNodes: [SyntaxToken, AttributesToken, Token]}`
32
- */
33
- export declare class TdToken extends TdToken_base {
34
- #private;
35
- readonly type = "td";
36
- readonly childNodes: [SyntaxToken, AttributesToken, Token];
37
- abstract get children(): [SyntaxToken, AttributesToken, Token];
38
- abstract get parentNode(): TrToken | TableToken | undefined;
39
- abstract get parentElement(): TrToken | TableToken | undefined;
40
- abstract get nextSibling(): this | TrToken | SyntaxToken | undefined;
41
- abstract get nextElementSibling(): this | TrToken | SyntaxToken | undefined;
42
- abstract get previousSibling(): Token | undefined;
43
- /** 单元格类型 */
44
- get subtype(): TdSubtypes;
45
- set subtype(subtype: TdSubtypes);
46
- /** rowspan */
47
- get rowspan(): number;
48
- set rowspan(rowspan: number);
49
- /** colspan */
50
- get colspan(): number;
51
- set colspan(colspan: number);
52
- /** 内部wikitext */
53
- get innerText(): string;
54
- set innerText(text: string);
55
- /**
56
- * @param syntax 单元格语法
57
- * @param inner 内部wikitext
58
- */
59
- constructor(syntax: string, inner?: string, config?: Parser.Config, accum?: Token[]);
60
- /** @override */
61
- text(): string;
62
- /** @override */
63
- lint(start?: number): LintError[];
64
- /** @override */
65
- print(): string;
66
- /** 是否位于行首 */
67
- isIndependent(): boolean;
68
- /** @override */
69
- cloneNode(): this;
70
- /**
71
- * @override
72
- * @param syntax 表格语法
73
- * @param esc 是否需要转义
74
- */
75
- setSyntax(syntax: string, esc?: boolean): void;
76
- /** 改为独占一行 */
77
- independence(): void;
78
- /**
79
- * @override
80
- * @param key 属性键
81
- */
82
- getAttr<T extends string>(key: T): TdAttrGetter<T>;
83
- /** @override */
84
- getAttrs(): TdAttrs;
85
- /**
86
- * @override
87
- * @param key 属性键
88
- * @param value 属性值
89
- */
90
- setAttr<T extends string>(key: T, value: TdAttrSetter<T>): void;
91
- /** @override */
92
- escape(): void;
93
- }
94
- /**
95
- * 创建新的单元格
96
- * @param inner 内部wikitext
97
- * @param subtype 单元格类型
98
- * @param attr 单元格属性
99
- * @param include 是否嵌入
100
- * @param config
101
- */
102
- export declare const createTd: (inner: string | Token, subtype?: TdSubtypes, attr?: TdAttrs, include?: boolean, config?: Parser.Config) => TdToken;
103
- export {};
@@ -1,261 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createTd = exports.TdToken = void 0;
4
- const lint_1 = require("../../util/lint");
5
- const debug_1 = require("../../util/debug");
6
- const constants_1 = require("../../util/constants");
7
- const fixed_1 = require("../../mixin/fixed");
8
- const Parser = require("../../index");
9
- const index_1 = require("../index");
10
- const base_1 = require("./base");
11
- /**
12
- * `<td>`、`<th>`和`<caption>`
13
- * @classdesc `{childNodes: [SyntaxToken, AttributesToken, Token]}`
14
- */
15
- // @ts-expect-error not implementing all abstract methods
16
- class TdToken extends (0, fixed_1.fixed)(base_1.TableBaseToken) {
17
- type = 'td';
18
- #innerSyntax = '';
19
- /** 单元格类型 */
20
- get subtype() {
21
- return this.#getSyntax().subtype;
22
- }
23
- /* NOT FOR BROWSER */
24
- set subtype(subtype) {
25
- this.setSyntax(subtype);
26
- }
27
- /** rowspan */
28
- get rowspan() {
29
- return this.getAttr('rowspan');
30
- }
31
- set rowspan(rowspan) {
32
- this.setAttr('rowspan', rowspan);
33
- }
34
- /** colspan */
35
- get colspan() {
36
- return this.getAttr('colspan');
37
- }
38
- set colspan(colspan) {
39
- this.setAttr('colspan', colspan);
40
- }
41
- /** 内部wikitext */
42
- get innerText() {
43
- return this.lastChild.text();
44
- }
45
- set innerText(text) {
46
- this.lastChild.replaceChildren(...Parser.parse(text, true, undefined, this.getAttribute('config')).childNodes);
47
- }
48
- /* NOT FOR BROWSER END */
49
- /**
50
- * @param syntax 单元格语法
51
- * @param inner 内部wikitext
52
- */
53
- constructor(syntax, inner, config = Parser.getConfig(), accum = []) {
54
- let innerSyntax = /\||\0\d+!\x7F/u.exec(inner ?? ''), attr = innerSyntax ? inner.slice(0, innerSyntax.index) : '';
55
- if (/\[\[|-\{/u.test(attr)) {
56
- innerSyntax = null;
57
- attr = '';
58
- }
59
- super(/^(?:\n[^\S\n]*(?:[|!]|\|\+|\{\{\s*!\s*\}\}\+?)|(?:\||\{\{\s*!\s*\}\}){2}|!!|\{\{\s*!!\s*\}\})$/u, syntax, attr, config, accum, { SyntaxToken: 0, AttributesToken: 1, Token: 2 });
60
- if (innerSyntax) {
61
- [this.#innerSyntax] = innerSyntax;
62
- }
63
- const innerToken = new index_1.Token(inner?.slice((innerSyntax?.index ?? NaN) + this.#innerSyntax.length), config, accum);
64
- innerToken.type = 'td-inner';
65
- innerToken.setAttribute('stage', 4);
66
- this.insertAt(innerToken);
67
- }
68
- /** 表格语法信息 */
69
- #getSyntax() {
70
- const syntax = this.firstChild.text(), esc = syntax.includes('{{'), char = syntax.at(-1);
71
- let subtype = 'td';
72
- if (char === '!') {
73
- subtype = 'th';
74
- }
75
- else if (char === '+') {
76
- subtype = 'caption';
77
- }
78
- if (this.isIndependent()) {
79
- return { subtype, escape: esc, correction: false };
80
- }
81
- const { previousSibling } = this;
82
- if (!(previousSibling instanceof TdToken)) {
83
- return { subtype, escape: esc, correction: true };
84
- }
85
- const result = previousSibling.#getSyntax();
86
- result.escape ||= esc;
87
- result.correction = previousSibling.lastChild
88
- .toString(new Set(['comment', 'ext', 'include', 'noinclude', 'arg', 'template', 'magic-word']))
89
- .includes('\n');
90
- if (subtype === 'th' && result.subtype !== 'th') {
91
- result.subtype = 'th';
92
- result.correction = true;
93
- }
94
- return result;
95
- }
96
- /** @private */
97
- afterBuild() {
98
- if (this.#innerSyntax.includes('\0')) {
99
- this.#innerSyntax = this.buildFromStr(this.#innerSyntax, 'string');
100
- }
101
- }
102
- /** @private */
103
- toString(omit) {
104
- this.#correct();
105
- const { childNodes: [syntax, attr, inner] } = this;
106
- return omit && this.matchesTypes(omit)
107
- ? ''
108
- : `${syntax.toString(omit)}${attr.toString(omit)}${this.#innerSyntax}${inner.toString(omit)}`;
109
- }
110
- /** @override */
111
- text() {
112
- this.#correct();
113
- const { childNodes: [syntax, attr, inner] } = this;
114
- return `${syntax.text()}${attr.text()}${this.#innerSyntax}${inner.text()}`;
115
- }
116
- /** @private */
117
- getGaps(i) {
118
- if (i === 1) {
119
- this.#correct();
120
- return this.#innerSyntax.length;
121
- }
122
- return 0;
123
- }
124
- /** @override */
125
- lint(start = this.getAbsoluteIndex()) {
126
- const errors = super.lint(start);
127
- start += this.getRelativeIndex(this.length - 1);
128
- for (const child of this.lastChild.childNodes) {
129
- if (child.type === 'text' && child.data.includes('|')) {
130
- errors.push((0, lint_1.generateForChild)(child, { start }, 'additional "|" in a table cell', 'warning'));
131
- }
132
- }
133
- return errors;
134
- }
135
- /** @override */
136
- print() {
137
- const { childNodes: [syntax, attr, inner] } = this;
138
- return `<span class="wpb-td">${syntax.print()}${attr.print()}${this.#innerSyntax}${inner.print()}</span>`;
139
- }
140
- /* NOT FOR BROWSER */
141
- /** 是否位于行首 */
142
- isIndependent() {
143
- return this.firstChild.text().startsWith('\n');
144
- }
145
- /** @override */
146
- cloneNode() {
147
- const token = super.cloneNode();
148
- token.setAttribute('innerSyntax', this.#innerSyntax);
149
- return token;
150
- }
151
- /** @private */
152
- getAttribute(key) {
153
- return key === 'innerSyntax' ? this.#innerSyntax : super.getAttribute(key);
154
- }
155
- /** @private */
156
- setAttribute(key, value) {
157
- if (key === 'innerSyntax') {
158
- this.#innerSyntax = value ?? '';
159
- }
160
- else {
161
- super.setAttribute(key, value);
162
- }
163
- }
164
- /**
165
- * @override
166
- * @param syntax 表格语法
167
- * @param esc 是否需要转义
168
- */
169
- setSyntax(syntax, esc = false) {
170
- const aliases = { td: '\n|', th: '\n!', caption: '\n|+' };
171
- super.setSyntax(aliases[syntax] ?? syntax, esc);
172
- }
173
- /** 修复\<td\>语法 */
174
- #correct() {
175
- if (String(this.childNodes[1])) {
176
- this.#innerSyntax ||= '|';
177
- }
178
- const { subtype, escape, correction } = this.#getSyntax();
179
- if (correction) {
180
- this.setSyntax(subtype, escape);
181
- }
182
- }
183
- /** 改为独占一行 */
184
- independence() {
185
- if (!this.isIndependent()) {
186
- const { subtype, escape } = this.#getSyntax();
187
- this.setSyntax(subtype, escape);
188
- }
189
- }
190
- /**
191
- * @override
192
- * @param key 属性键
193
- */
194
- getAttr(key) {
195
- const value = super.getAttr(key);
196
- key = key.toLowerCase().trim();
197
- return (key === 'rowspan' || key === 'colspan' ? Number(value) || 1 : value);
198
- }
199
- /** @override */
200
- getAttrs() {
201
- const attr = super.getAttrs();
202
- if ('rowspan' in attr) {
203
- attr.rowspan = Number(attr.rowspan);
204
- }
205
- if ('colspan' in attr) {
206
- attr.colspan = Number(attr.colspan);
207
- }
208
- return attr;
209
- }
210
- /**
211
- * @override
212
- * @param key 属性键
213
- * @param value 属性值
214
- */
215
- setAttr(key, value) {
216
- key = key.toLowerCase().trim();
217
- let v;
218
- if (typeof value === 'number' && (key === 'rowspan' || key === 'colspan')) {
219
- v = value === 1 ? false : String(value);
220
- }
221
- else {
222
- v = value;
223
- }
224
- super.setAttr(key, v);
225
- if (!String(this.childNodes[1])) {
226
- this.#innerSyntax = '';
227
- }
228
- }
229
- /** @override */
230
- escape() {
231
- super.escape();
232
- if (String(this.childNodes[1])) {
233
- this.#innerSyntax ||= '{{!}}';
234
- }
235
- if (this.#innerSyntax === '|') {
236
- this.#innerSyntax = '{{!}}';
237
- }
238
- }
239
- }
240
- exports.TdToken = TdToken;
241
- /* NOT FOR BROWSER */
242
- /**
243
- * 创建新的单元格
244
- * @param inner 内部wikitext
245
- * @param subtype 单元格类型
246
- * @param attr 单元格属性
247
- * @param include 是否嵌入
248
- * @param config
249
- */
250
- const createTd = (inner, subtype = 'td', attr = {}, include = false, config = Parser.getConfig()) => {
251
- const innerToken = typeof inner === 'string' ? Parser.parse(inner, include, undefined, config) : inner, token = debug_1.Shadow.run(() => new TdToken('\n|', undefined, config));
252
- token.setSyntax(subtype);
253
- token.lastChild.safeReplaceWith(innerToken);
254
- for (const [k, v] of Object.entries(attr)) {
255
- token.setAttr(k, v);
256
- }
257
- return token;
258
- };
259
- exports.createTd = createTd;
260
- /* NOT FOR BROWSER END */
261
- constants_1.classes['TdToken'] = __filename;
@@ -1,32 +0,0 @@
1
- import * as Parser from '../../index';
2
- import { TrBaseToken } from './trBase';
3
- import type { Token, TdToken, TableToken, SyntaxToken, AttributesToken } from '../../internal';
4
- /**
5
- * 表格行,含开头的换行,不含结尾的换行
6
- * @classdesc `{childNodes: [SyntaxToken, AttributesToken, ?Token, ...TdToken]}`
7
- */
8
- export declare class TrToken extends TrBaseToken {
9
- #private;
10
- readonly type = "tr";
11
- readonly childNodes: [SyntaxToken, AttributesToken, ...TdToken[]];
12
- abstract get children(): [SyntaxToken, AttributesToken, ...TdToken[]];
13
- abstract get lastChild(): AttributesToken | TdToken;
14
- abstract get lastElementChild(): AttributesToken | TdToken;
15
- abstract get parentNode(): TableToken | undefined;
16
- abstract get parentElement(): TableToken | undefined;
17
- abstract get nextSibling(): this | SyntaxToken | undefined;
18
- abstract get nextElementSibling(): this | SyntaxToken | undefined;
19
- abstract get previousSibling(): Token;
20
- abstract get previousElementSibling(): Token;
21
- /**
22
- * @param syntax 表格语法
23
- * @param attr 表格属性
24
- */
25
- constructor(syntax: string, attr?: string, config?: Parser.Config, accum?: Token[]);
26
- /** @override */
27
- text(): string;
28
- /** 获取下一行 */
29
- getNextRow(): TrToken | undefined;
30
- /** 获取前一行 */
31
- getPreviousRow(): TrToken | undefined;
32
- }
@@ -1,56 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TrToken = void 0;
4
- const constants_1 = require("../../util/constants");
5
- const Parser = require("../../index");
6
- const trBase_1 = require("./trBase");
7
- /**
8
- * 表格行,含开头的换行,不含结尾的换行
9
- * @classdesc `{childNodes: [SyntaxToken, AttributesToken, ?Token, ...TdToken]}`
10
- */
11
- // @ts-expect-error not implementing all abstract methods
12
- class TrToken extends trBase_1.TrBaseToken {
13
- type = 'tr';
14
- /**
15
- * @param syntax 表格语法
16
- * @param attr 表格属性
17
- */
18
- constructor(syntax, attr, config = Parser.getConfig(), accum = []) {
19
- super(/^\n[^\S\n]*(?:\|-+|\{\{\s*!\s*\}\}-+|\{\{\s*!-\s*\}\}-*)$/u, syntax, attr, config, accum, {
20
- Token: 2, SyntaxToken: 0, AttributesToken: 1, TdToken: '2:',
21
- });
22
- }
23
- /* NOT FOR BROWSER */
24
- /** @override */
25
- text() {
26
- const str = super.text();
27
- return str.trim().includes('\n') ? str : '';
28
- }
29
- /**
30
- * 获取相邻行
31
- * @param subset 筛选兄弟节点的方法
32
- */
33
- #getSiblingRow(subset) {
34
- const { parentNode } = this;
35
- if (!parentNode) {
36
- return undefined;
37
- }
38
- const { childNodes } = parentNode, index = childNodes.indexOf(this);
39
- for (const child of subset(childNodes, index)) {
40
- if (child instanceof TrToken && child.getRowCount()) {
41
- return child;
42
- }
43
- }
44
- return undefined;
45
- }
46
- /** 获取下一行 */
47
- getNextRow() {
48
- return this.#getSiblingRow((childNodes, index) => childNodes.slice(index + 1));
49
- }
50
- /** 获取前一行 */
51
- getPreviousRow() {
52
- return this.#getSiblingRow((childNodes, index) => childNodes.slice(0, index).reverse());
53
- }
54
- }
55
- exports.TrToken = TrToken;
56
- constants_1.classes['TrToken'] = __filename;
@@ -1,54 +0,0 @@
1
- import { Token } from '..';
2
- import { TableBaseToken } from './base';
3
- import { TdToken } from './td';
4
- import type { LintError } from '../../base';
5
- import type { AstNodes, SyntaxToken, TrToken } from '../../internal';
6
- import type { TdAttrs, TdSubtypes } from './td';
7
- export interface TableCoords {
8
- readonly row: number;
9
- readonly column: number;
10
- readonly x?: undefined;
11
- readonly y?: undefined;
12
- readonly start?: boolean;
13
- }
14
- /** 表格行或表格 */
15
- export declare abstract class TrBaseToken extends TableBaseToken {
16
- #private;
17
- type: 'table' | 'tr';
18
- /** @override */
19
- lint(start?: number): LintError[];
20
- /** @override */
21
- text(): string;
22
- /**
23
- * @override
24
- * @param i 移除位置
25
- */
26
- removeAt(i: number): AstNodes;
27
- /**
28
- * @override
29
- * @param token 待插入的子节点
30
- * @param i 插入位置
31
- */
32
- insertAt<T extends Token>(token: T, i?: number): T;
33
- /** 获取行数 */
34
- getRowCount(): number;
35
- /** 获取列数 */
36
- getColCount(): number;
37
- /**
38
- * 获取第n列
39
- * @param n 列号
40
- * @param insert 是否用于判断插入新列的位置
41
- * @throws `RangeError` 不存在对应单元格
42
- */
43
- getNthCol(n: number, insert?: false): TdToken | undefined;
44
- /** @ignore */
45
- getNthCol(n: number, insert: true): TdToken | TrToken | SyntaxToken | undefined;
46
- /**
47
- * 插入新的单元格
48
- * @param inner 单元格内部wikitext
49
- * @param {TableCoords} coord 单元格坐标
50
- * @param subtype 单元格类型
51
- * @param attr 单元格属性
52
- */
53
- insertTableCell(inner: string | Token, { column }: TableCoords, subtype?: TdSubtypes, attr?: TdAttrs): TdToken;
54
- }
@@ -1,159 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TrBaseToken = void 0;
4
- const lint_1 = require("../../util/lint");
5
- const debug_1 = require("../../util/debug");
6
- const constants_1 = require("../../util/constants");
7
- const __1 = require("..");
8
- const base_1 = require("./base");
9
- const td_1 = require("./td");
10
- /** 表格行或表格 */
11
- class TrBaseToken extends base_1.TableBaseToken {
12
- /** @override */
13
- lint(start = this.getAbsoluteIndex()) {
14
- const errors = super.lint(start), inter = this.childNodes.find(({ type }) => type === 'table-inter');
15
- if (!inter) {
16
- return errors;
17
- }
18
- const first = inter.childNodes.find(child => child.text().trim()), tdPattern = /^\s*(?:!|\{\{\s*![!-]?\s*\}\})/u,
19
- /** @ignore */
20
- isArg = (token) => token.type === 'arg',
21
- /** @ignore */
22
- isTransclude = (token) => token.type === 'magic-word';
23
- if (!first || tdPattern.test(String(first))
24
- || isArg(first) && tdPattern.test(first.default || '')) {
25
- return errors;
26
- }
27
- else if (isTransclude(first)) {
28
- try {
29
- if (first.getPossibleValues().every(token => tdPattern.test(token.text()))) {
30
- return errors;
31
- }
32
- }
33
- catch { }
34
- }
35
- const error = (0, lint_1.generateForChild)(inter, { start }, 'content to be moved out from the table');
36
- errors.push({
37
- ...error,
38
- startIndex: error.startIndex + 1,
39
- startLine: error.startLine + 1,
40
- startCol: 0,
41
- excerpt: error.excerpt.slice(1),
42
- });
43
- return errors;
44
- }
45
- /* NOT FOR BROWSER */
46
- /** 修复简单的表格语法错误 */
47
- #correct() {
48
- const { childNodes: [, , child] } = this;
49
- if (child?.constructor === __1.Token) {
50
- const { firstChild } = child;
51
- if (firstChild?.type !== 'text') {
52
- child.prepend('\n');
53
- }
54
- else if (!firstChild.data.startsWith('\n')) {
55
- firstChild.insertData(0, '\n');
56
- }
57
- }
58
- }
59
- /** @override */
60
- text() {
61
- this.#correct();
62
- return super.text();
63
- }
64
- /** @private */
65
- toString(omit) {
66
- this.#correct();
67
- return super.toString(omit);
68
- }
69
- /**
70
- * @override
71
- * @param i 移除位置
72
- */
73
- removeAt(i) {
74
- const child = this.childNodes.at(i);
75
- if (child instanceof td_1.TdToken && child.isIndependent()) {
76
- const { nextSibling } = child;
77
- if (nextSibling?.type === 'td') {
78
- nextSibling.independence();
79
- }
80
- }
81
- return super.removeAt(i);
82
- }
83
- /**
84
- * @override
85
- * @param token 待插入的子节点
86
- * @param i 插入位置
87
- */
88
- insertAt(token, i = this.length) {
89
- if (!debug_1.Shadow.running && token.type !== 'td') {
90
- if (this.type === 'tr') {
91
- this.typeError('insertAt', 'TdToken');
92
- }
93
- else if (token.type !== 'tr') {
94
- this.typeError('insertAt', 'TrToken', 'TdToken');
95
- }
96
- }
97
- const child = this.childNodes.at(i);
98
- if (token instanceof td_1.TdToken && token.isIndependent() && child instanceof td_1.TdToken) {
99
- child.independence();
100
- }
101
- return super.insertAt(token, i);
102
- }
103
- /** 获取行数 */
104
- getRowCount() {
105
- return Number(this.childNodes.some(child => child instanceof td_1.TdToken && child.isIndependent() && !child.firstChild.text().endsWith('+')));
106
- }
107
- /** 获取列数 */
108
- getColCount() {
109
- let count = 0, last = 0;
110
- for (const child of this.childNodes) {
111
- if (child instanceof td_1.TdToken) {
112
- last = child.isIndependent() ? Number(child.subtype !== 'caption') : last;
113
- count += last;
114
- }
115
- }
116
- return count;
117
- }
118
- /** @ignore */
119
- getNthCol(n, insert = false) {
120
- const nCols = this.getColCount();
121
- n += n < 0 ? nCols : 0;
122
- if (n < 0 || n > nCols || n === nCols && !insert) {
123
- throw new RangeError(`不存在第 ${n} 个单元格!`);
124
- }
125
- let last = 0;
126
- /** @ignore */
127
- const isTr = (token) => token.type === 'tr',
128
- /** @ignore */
129
- isSyntax = (token) => token.type === 'table-syntax';
130
- for (const child of this.childNodes.slice(2)) {
131
- if (child instanceof td_1.TdToken) {
132
- if (child.isIndependent()) {
133
- last = Number(child.subtype !== 'caption');
134
- }
135
- n -= last;
136
- if (n < 0) {
137
- return child;
138
- }
139
- }
140
- else if (isTr(child) || isSyntax(child)) {
141
- return child;
142
- }
143
- }
144
- return undefined;
145
- }
146
- /**
147
- * 插入新的单元格
148
- * @param inner 单元格内部wikitext
149
- * @param {TableCoords} coord 单元格坐标
150
- * @param subtype 单元格类型
151
- * @param attr 单元格属性
152
- */
153
- insertTableCell(inner, { column }, subtype = 'td', attr = {}) {
154
- const token = (0, td_1.createTd)(inner, subtype, attr, this.getAttribute('include'), this.getAttribute('config'));
155
- return this.insertBefore(token, this.getNthCol(column, true));
156
- }
157
- }
158
- exports.TrBaseToken = TrBaseToken;
159
- constants_1.classes['TrBaseToken'] = __filename;