wikiparser-node 1.2.0-b → 1.2.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.
- package/config/.schema.json +172 -0
- package/config/llwiki.json +35 -1
- package/config/moegirl.json +44 -1
- package/config/zhwiki.json +466 -1
- package/dist/addon/table.d.ts +6 -0
- package/dist/addon/table.js +564 -0
- package/dist/base.d.ts +46 -0
- package/dist/bin/toc.js +18 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +209 -0
- package/dist/internal.d.ts +44 -0
- package/dist/lib/element.d.ts +155 -0
- package/dist/lib/element.js +654 -0
- package/dist/lib/node.d.ts +146 -0
- package/dist/lib/node.js +419 -0
- package/dist/lib/range.d.ts +104 -0
- package/dist/lib/range.js +385 -0
- package/dist/lib/ranges.d.ts +26 -0
- package/dist/lib/ranges.js +117 -0
- package/dist/lib/text.d.ts +62 -0
- package/dist/lib/text.js +235 -0
- package/dist/lib/title.d.ts +38 -0
- package/dist/lib/title.js +162 -0
- package/dist/mixin/attributesParent.js +94 -0
- package/dist/mixin/fixed.js +32 -0
- package/dist/mixin/flagsParent.js +70 -0
- package/dist/mixin/hidden.js +22 -0
- package/dist/mixin/magicLinkParent.js +41 -0
- package/dist/mixin/singleLine.js +25 -0
- package/dist/mixin/sol.js +43 -0
- package/dist/mixin/syntax.js +56 -0
- package/dist/parser/braces.js +123 -0
- package/dist/parser/commentAndExt.js +69 -0
- package/dist/parser/converter.js +40 -0
- package/dist/parser/externalLinks.js +28 -0
- package/dist/parser/hrAndDoubleUnderscore.js +38 -0
- package/dist/parser/html.js +36 -0
- package/dist/parser/links.js +94 -0
- package/dist/parser/list.js +59 -0
- package/dist/parser/magicLinks.js +40 -0
- package/dist/parser/quotes.js +67 -0
- package/dist/parser/selector.js +162 -0
- package/dist/parser/table.js +112 -0
- package/dist/src/arg.d.ts +58 -0
- package/dist/src/arg.js +190 -0
- package/dist/src/atom.d.ts +12 -0
- package/dist/src/atom.js +27 -0
- package/dist/src/attribute.d.ts +86 -0
- package/dist/src/attribute.js +392 -0
- package/dist/src/attributes.d.ts +107 -0
- package/dist/src/attributes.js +344 -0
- package/dist/src/converter.d.ts +51 -0
- package/dist/src/converter.js +79 -0
- package/dist/src/converterFlags.d.ts +87 -0
- package/dist/src/converterFlags.js +223 -0
- package/dist/src/converterRule.d.ts +81 -0
- package/dist/src/converterRule.js +212 -0
- package/dist/src/extLink.d.ts +53 -0
- package/dist/src/extLink.js +121 -0
- package/dist/src/gallery.d.ts +55 -0
- package/dist/src/gallery.js +123 -0
- package/dist/src/heading.d.ts +72 -0
- package/dist/src/heading.js +128 -0
- package/dist/src/hidden.d.ts +21 -0
- package/dist/src/hidden.js +23 -0
- package/dist/src/html.d.ts +103 -0
- package/dist/src/html.js +230 -0
- package/dist/src/imageParameter.d.ts +66 -0
- package/dist/src/imageParameter.js +247 -0
- package/dist/src/imagemap.d.ts +57 -0
- package/dist/src/imagemap.js +148 -0
- package/dist/src/imagemapLink.d.ts +57 -0
- package/dist/src/imagemapLink.js +40 -0
- package/dist/src/index.d.ts +146 -0
- package/dist/src/index.js +777 -0
- package/dist/src/link/base.d.ts +52 -0
- package/dist/src/link/base.js +207 -0
- package/dist/src/link/category.d.ts +13 -0
- package/dist/src/link/category.js +29 -0
- package/dist/src/link/file.d.ts +96 -0
- package/dist/src/link/file.js +253 -0
- package/dist/src/link/galleryImage.d.ts +43 -0
- package/dist/src/link/galleryImage.js +106 -0
- package/dist/src/link/index.d.ts +53 -0
- package/dist/src/link/index.js +123 -0
- package/dist/src/magicLink.d.ts +69 -0
- package/dist/src/magicLink.js +148 -0
- package/dist/src/nested.d.ts +44 -0
- package/dist/src/nested.js +87 -0
- package/dist/src/nowiki/base.d.ts +46 -0
- package/dist/src/nowiki/base.js +42 -0
- package/dist/src/nowiki/comment.d.ts +39 -0
- package/dist/src/nowiki/comment.js +67 -0
- package/dist/src/nowiki/dd.d.ts +8 -0
- package/dist/src/nowiki/dd.js +25 -0
- package/dist/src/nowiki/doubleUnderscore.d.ts +43 -0
- package/dist/src/nowiki/doubleUnderscore.js +50 -0
- package/dist/src/nowiki/hr.d.ts +29 -0
- package/dist/src/nowiki/hr.js +14 -0
- package/dist/src/nowiki/index.d.ts +16 -0
- package/dist/src/nowiki/index.js +21 -0
- package/dist/src/nowiki/list.d.ts +29 -0
- package/dist/src/nowiki/list.js +48 -0
- package/dist/src/nowiki/listBase.d.ts +18 -0
- package/dist/src/nowiki/listBase.js +12 -0
- package/dist/src/nowiki/noinclude.d.ts +25 -0
- package/dist/src/nowiki/noinclude.js +25 -0
- package/dist/src/nowiki/quote.d.ts +22 -0
- package/dist/src/nowiki/quote.js +50 -0
- package/dist/src/onlyinclude.d.ts +16 -0
- package/dist/src/onlyinclude.js +57 -0
- package/dist/src/paramTag/index.d.ts +37 -0
- package/dist/src/paramTag/index.js +68 -0
- package/dist/src/paramTag/inputbox.d.ts +8 -0
- package/dist/src/paramTag/inputbox.js +23 -0
- package/dist/src/parameter.d.ts +84 -0
- package/dist/src/parameter.js +206 -0
- package/dist/src/pre.d.ts +28 -0
- package/dist/src/pre.js +51 -0
- package/dist/src/syntax.d.ts +24 -0
- package/dist/src/syntax.js +30 -0
- package/dist/src/table/base.d.ts +52 -0
- package/dist/src/table/base.js +78 -0
- package/dist/src/table/index.d.ts +229 -0
- package/dist/src/table/index.js +380 -0
- package/dist/src/table/td.d.ts +103 -0
- package/dist/src/table/td.js +261 -0
- package/dist/src/table/tr.d.ts +32 -0
- package/dist/src/table/tr.js +56 -0
- package/dist/src/table/trBase.d.ts +54 -0
- package/dist/src/table/trBase.js +159 -0
- package/dist/src/tagPair/ext.d.ts +58 -0
- package/dist/src/tagPair/ext.js +142 -0
- package/dist/src/tagPair/include.d.ts +52 -0
- package/dist/src/tagPair/include.js +58 -0
- package/dist/src/tagPair/index.d.ts +47 -0
- package/dist/src/tagPair/index.js +93 -0
- package/dist/src/transclude.d.ts +167 -0
- package/dist/src/transclude.js +689 -0
- package/dist/util/constants.js +108 -0
- package/dist/util/debug.js +64 -0
- package/dist/util/diff.js +72 -0
- package/dist/util/lint.js +48 -0
- package/dist/util/string.js +61 -0
- package/errors/README +1 -0
- package/package.json +12 -32
- package/printed/README +1 -0
- package/bundle/bundle.min.js +0 -36
- package/extensions/dist/base.js +0 -68
- package/extensions/dist/editor.js +0 -159
- package/extensions/dist/highlight.js +0 -30
- package/extensions/dist/lint.js +0 -48
- package/extensions/editor.css +0 -63
- package/extensions/ui.css +0 -117
|
@@ -0,0 +1,103 @@
|
|
|
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 {};
|
|
@@ -0,0 +1,261 @@
|
|
|
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;
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
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;
|
|
@@ -0,0 +1,54 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
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;
|