wikiparser-node 1.13.2-b → 1.13.3
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/bundle/bundle.min.js +16 -16
- package/config/.schema.json +203 -0
- package/config/default.json +32 -31
- package/config/enwiki.json +829 -15
- package/config/llwiki.json +56 -21
- package/config/moegirl.json +65 -21
- package/config/zhwiki.json +498 -32
- package/dist/addon/table.js +494 -0
- package/dist/addon/token.js +392 -0
- package/dist/addon/transclude.js +177 -0
- package/dist/base.d.ts +109 -0
- package/dist/base.js +67 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +284 -0
- package/dist/internal.d.ts +47 -0
- package/dist/lib/element.d.ts +125 -0
- package/dist/lib/element.js +364 -0
- package/dist/lib/node.d.ts +173 -0
- package/dist/lib/node.js +478 -0
- package/dist/lib/range.d.ts +105 -0
- package/dist/lib/range.js +406 -0
- package/dist/lib/ranges.d.ts +28 -0
- package/dist/lib/ranges.js +126 -0
- package/dist/lib/rect.d.ts +18 -0
- package/dist/lib/rect.js +36 -0
- package/dist/lib/text.d.ts +58 -0
- package/dist/lib/text.js +414 -0
- package/dist/lib/title.d.ts +49 -0
- package/dist/lib/title.js +251 -0
- package/dist/mixin/attributesParent.d.ts +49 -0
- package/dist/mixin/attributesParent.js +80 -0
- package/dist/mixin/fixed.d.ts +5 -0
- package/dist/mixin/fixed.js +32 -0
- package/dist/mixin/flagsParent.d.ts +43 -0
- package/dist/mixin/flagsParent.js +64 -0
- package/dist/mixin/hidden.d.ts +7 -0
- package/dist/mixin/hidden.js +39 -0
- package/dist/mixin/magicLinkParent.d.ts +19 -0
- package/dist/mixin/magicLinkParent.js +43 -0
- package/dist/mixin/singleLine.d.ts +6 -0
- package/dist/mixin/singleLine.js +33 -0
- package/dist/mixin/sol.d.ts +6 -0
- package/dist/mixin/sol.js +45 -0
- package/dist/mixin/syntax.d.ts +8 -0
- package/dist/mixin/syntax.js +46 -0
- package/dist/parser/braces.js +152 -0
- package/dist/parser/commentAndExt.js +84 -0
- package/dist/parser/converter.js +43 -0
- package/dist/parser/externalLinks.js +39 -0
- package/dist/parser/hrAndDoubleUnderscore.js +45 -0
- package/dist/parser/html.js +40 -0
- package/dist/parser/links.js +105 -0
- package/dist/parser/list.js +116 -0
- package/dist/parser/magicLinks.js +55 -0
- package/dist/parser/quotes.js +69 -0
- package/dist/parser/redirect.js +29 -0
- package/dist/parser/selector.js +443 -0
- package/dist/parser/table.js +125 -0
- package/dist/src/arg.d.ts +49 -0
- package/dist/src/arg.js +220 -0
- package/dist/src/atom.d.ts +14 -0
- package/dist/src/atom.js +54 -0
- package/dist/src/attribute.d.ts +64 -0
- package/dist/src/attribute.js +326 -0
- package/dist/src/attributes.d.ts +105 -0
- package/dist/src/attributes.js +379 -0
- package/dist/src/converter.d.ts +28 -0
- package/dist/src/converter.js +153 -0
- package/dist/src/converterFlags.d.ts +80 -0
- package/dist/src/converterFlags.js +241 -0
- package/dist/src/converterRule.d.ts +71 -0
- package/dist/src/converterRule.js +218 -0
- package/dist/src/extLink.d.ts +36 -0
- package/dist/src/extLink.js +223 -0
- package/dist/src/gallery.d.ts +53 -0
- package/dist/src/gallery.js +165 -0
- package/dist/src/heading.d.ts +44 -0
- package/dist/src/heading.js +222 -0
- package/dist/src/hidden.d.ts +9 -0
- package/dist/src/hidden.js +82 -0
- package/dist/src/html.d.ts +61 -0
- package/dist/src/html.js +344 -0
- package/dist/src/imageParameter.d.ts +60 -0
- package/dist/src/imageParameter.js +262 -0
- package/dist/src/imagemap.d.ts +49 -0
- package/dist/src/imagemap.js +147 -0
- package/dist/src/imagemapLink.d.ts +35 -0
- package/dist/src/imagemapLink.js +99 -0
- package/dist/src/index.d.ts +135 -0
- package/dist/src/index.js +791 -0
- package/dist/src/link/base.d.ts +52 -0
- package/dist/src/link/base.js +258 -0
- package/dist/src/link/category.d.ts +22 -0
- package/dist/src/link/category.js +36 -0
- package/dist/src/link/file.d.ts +102 -0
- package/dist/src/link/file.js +345 -0
- package/dist/src/link/galleryImage.d.ts +29 -0
- package/dist/src/link/galleryImage.js +133 -0
- package/dist/src/link/index.d.ts +39 -0
- package/dist/src/link/index.js +100 -0
- package/dist/src/link/redirectTarget.d.ts +27 -0
- package/dist/src/link/redirectTarget.js +71 -0
- package/dist/src/magicLink.d.ts +57 -0
- package/dist/src/magicLink.js +261 -0
- package/dist/src/nested.d.ts +41 -0
- package/dist/src/nested.js +113 -0
- package/dist/src/nowiki/base.d.ts +28 -0
- package/dist/src/nowiki/base.js +90 -0
- package/dist/src/nowiki/comment.d.ts +14 -0
- package/dist/src/nowiki/comment.js +123 -0
- package/dist/src/nowiki/dd.d.ts +8 -0
- package/dist/src/nowiki/dd.js +74 -0
- package/dist/src/nowiki/doubleUnderscore.d.ts +15 -0
- package/dist/src/nowiki/doubleUnderscore.js +101 -0
- package/dist/src/nowiki/hr.d.ts +5 -0
- package/dist/src/nowiki/hr.js +72 -0
- package/dist/src/nowiki/index.d.ts +14 -0
- package/dist/src/nowiki/index.js +30 -0
- package/dist/src/nowiki/list.d.ts +5 -0
- package/dist/src/nowiki/list.js +67 -0
- package/dist/src/nowiki/listBase.d.ts +23 -0
- package/dist/src/nowiki/listBase.js +100 -0
- package/dist/src/nowiki/noinclude.d.ts +6 -0
- package/dist/src/nowiki/noinclude.js +77 -0
- package/dist/src/nowiki/quote.d.ts +14 -0
- package/dist/src/nowiki/quote.js +149 -0
- package/dist/src/onlyinclude.d.ts +13 -0
- package/dist/src/onlyinclude.js +60 -0
- package/dist/src/paramTag/index.d.ts +28 -0
- package/dist/src/paramTag/index.js +97 -0
- package/dist/src/paramTag/inputbox.d.ts +8 -0
- package/dist/src/paramTag/inputbox.js +26 -0
- package/dist/src/parameter.d.ts +60 -0
- package/dist/src/parameter.js +267 -0
- package/dist/src/pre.d.ts +30 -0
- package/dist/src/pre.js +69 -0
- package/dist/src/redirect.d.ts +30 -0
- package/dist/src/redirect.js +128 -0
- package/dist/src/syntax.d.ts +15 -0
- package/dist/src/syntax.js +87 -0
- package/dist/src/table/base.d.ts +28 -0
- package/dist/src/table/base.js +81 -0
- package/dist/src/table/index.d.ts +231 -0
- package/dist/src/table/index.js +505 -0
- package/dist/src/table/td.d.ts +72 -0
- package/dist/src/table/td.js +375 -0
- package/dist/src/table/tr.d.ts +30 -0
- package/dist/src/table/tr.js +61 -0
- package/dist/src/table/trBase.d.ts +49 -0
- package/dist/src/table/trBase.js +165 -0
- package/dist/src/tagPair/ext.d.ts +30 -0
- package/dist/src/tagPair/ext.js +231 -0
- package/dist/src/tagPair/include.d.ts +33 -0
- package/dist/src/tagPair/include.js +145 -0
- package/dist/src/tagPair/index.d.ts +23 -0
- package/dist/src/tagPair/index.js +130 -0
- package/dist/src/transclude.d.ts +161 -0
- package/dist/src/transclude.js +607 -0
- package/dist/util/constants.js +26 -0
- package/dist/util/debug.js +95 -0
- package/dist/util/diff.js +83 -0
- package/dist/util/html.js +146 -0
- package/dist/util/lint.js +32 -0
- package/dist/util/sharable.js +163 -0
- package/dist/util/string.js +107 -0
- package/errors/README +3 -0
- package/extensions/dist/base.js +7 -4
- package/package.json +24 -23
- package/printed/README +3 -0
package/dist/src/arg.js
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ArgToken = void 0;
|
|
4
|
+
const string_1 = require("../util/string");
|
|
5
|
+
const lint_1 = require("../util/lint");
|
|
6
|
+
const rect_1 = require("../lib/rect");
|
|
7
|
+
const index_1 = require("../index");
|
|
8
|
+
const index_2 = require("./index");
|
|
9
|
+
const atom_1 = require("./atom");
|
|
10
|
+
const hidden_1 = require("./hidden");
|
|
11
|
+
/* NOT FOR BROWSER */
|
|
12
|
+
const constants_1 = require("../util/constants");
|
|
13
|
+
const debug_1 = require("../util/debug");
|
|
14
|
+
const html_1 = require("../util/html");
|
|
15
|
+
/* NOT FOR BROWSER END */
|
|
16
|
+
/**
|
|
17
|
+
* `{{{}}}`包裹的参数
|
|
18
|
+
* @classdesc `{childNodes: [AtomToken, ?Token, ...HiddenToken]}`
|
|
19
|
+
*/
|
|
20
|
+
class ArgToken extends index_2.Token {
|
|
21
|
+
/* NOT FOR BROWSER END */
|
|
22
|
+
get type() {
|
|
23
|
+
return 'arg';
|
|
24
|
+
}
|
|
25
|
+
/** 预设值 */
|
|
26
|
+
get default() {
|
|
27
|
+
return this.childNodes[1]?.text() ?? false;
|
|
28
|
+
}
|
|
29
|
+
/* NOT FOR BROWSER */
|
|
30
|
+
set default(value) {
|
|
31
|
+
this.setDefault(value);
|
|
32
|
+
}
|
|
33
|
+
/* NOT FOR BROWSER END */
|
|
34
|
+
/** @param parts 以'|'分隔的各部分 */
|
|
35
|
+
constructor(parts, config = index_1.default.getConfig(), accum = []) {
|
|
36
|
+
super(undefined, config, accum, {
|
|
37
|
+
AtomToken: 0, Token: 1, HiddenToken: '2:',
|
|
38
|
+
});
|
|
39
|
+
for (const [i, part] of parts.entries()) {
|
|
40
|
+
if (i === 0) {
|
|
41
|
+
const token = new atom_1.AtomToken(part, 'arg-name', config, accum, {
|
|
42
|
+
'Stage-2': ':', '!HeadingToken': '',
|
|
43
|
+
});
|
|
44
|
+
super.insertAt(token);
|
|
45
|
+
}
|
|
46
|
+
else if (i > 1) {
|
|
47
|
+
const token = new hidden_1.HiddenToken(part, config, accum);
|
|
48
|
+
super.insertAt(token);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
const token = new index_2.Token(part, config, accum);
|
|
52
|
+
token.type = 'arg-default';
|
|
53
|
+
token.setAttribute('stage', 2);
|
|
54
|
+
super.insertAt(token);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/* NOT FOR BROWSER */
|
|
58
|
+
this.protectChildren(0);
|
|
59
|
+
}
|
|
60
|
+
/** @private */
|
|
61
|
+
toString(skip) {
|
|
62
|
+
return `{{{${super.toString(skip, '|')}}}}`;
|
|
63
|
+
}
|
|
64
|
+
/** @private */
|
|
65
|
+
text() {
|
|
66
|
+
return `{{{${(0, string_1.text)(this.childNodes.slice(0, 2), '|')}}}}`;
|
|
67
|
+
}
|
|
68
|
+
/** @private */
|
|
69
|
+
getAttribute(key) {
|
|
70
|
+
return key === 'padding' ? 3 : super.getAttribute(key);
|
|
71
|
+
}
|
|
72
|
+
/** @private */
|
|
73
|
+
getGaps() {
|
|
74
|
+
return 1;
|
|
75
|
+
}
|
|
76
|
+
/** 设置name */
|
|
77
|
+
#setName() {
|
|
78
|
+
this.setAttribute('name', this.firstChild.toString(true).trim());
|
|
79
|
+
}
|
|
80
|
+
/** @private */
|
|
81
|
+
afterBuild() {
|
|
82
|
+
this.#setName();
|
|
83
|
+
super.afterBuild();
|
|
84
|
+
/* NOT FOR BROWSER */
|
|
85
|
+
const /** @implements */ argListener = ({ prevTarget }) => {
|
|
86
|
+
if (prevTarget === this.firstChild) {
|
|
87
|
+
this.#setName();
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
this.addEventListener(['remove', 'insert', 'replace', 'text'], argListener);
|
|
91
|
+
}
|
|
92
|
+
/** @private */
|
|
93
|
+
lint(start = this.getAbsoluteIndex(), re) {
|
|
94
|
+
const { childNodes: [argName, argDefault, ...rest] } = this;
|
|
95
|
+
if (!this.getAttribute('include')) {
|
|
96
|
+
const e = (0, lint_1.generateForSelf)(this, { start }, 'no-arg', 'unexpected template argument');
|
|
97
|
+
if (argDefault) {
|
|
98
|
+
e.fix = { range: [start, e.endIndex], text: argDefault.text() };
|
|
99
|
+
}
|
|
100
|
+
return [e];
|
|
101
|
+
}
|
|
102
|
+
const errors = argName.lint(start + 3, re);
|
|
103
|
+
if (argDefault) {
|
|
104
|
+
errors.push(...argDefault.lint(start + 4 + argName.toString().length, re));
|
|
105
|
+
}
|
|
106
|
+
if (rest.length > 0) {
|
|
107
|
+
const rect = new rect_1.BoundingRect(this, start);
|
|
108
|
+
errors.push(...rest.map(child => {
|
|
109
|
+
const e = (0, lint_1.generateForChild)(child, rect, 'no-ignored', 'invisible content inside triple braces');
|
|
110
|
+
e.startIndex--;
|
|
111
|
+
e.startCol--;
|
|
112
|
+
e.suggestions = [
|
|
113
|
+
{
|
|
114
|
+
desc: 'remove',
|
|
115
|
+
range: [e.startIndex, e.endIndex],
|
|
116
|
+
text: '',
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
desc: 'escape',
|
|
120
|
+
range: [e.startIndex, e.startIndex + 1],
|
|
121
|
+
text: '{{!}}',
|
|
122
|
+
},
|
|
123
|
+
];
|
|
124
|
+
return e;
|
|
125
|
+
}));
|
|
126
|
+
}
|
|
127
|
+
return errors;
|
|
128
|
+
}
|
|
129
|
+
/** @private */
|
|
130
|
+
print() {
|
|
131
|
+
return super.print({ pre: '{{{', post: '}}}', sep: '|' });
|
|
132
|
+
}
|
|
133
|
+
/* NOT FOR BROWSER */
|
|
134
|
+
cloneNode() {
|
|
135
|
+
const [name, ...cloned] = this.cloneChildNodes();
|
|
136
|
+
return debug_1.Shadow.run(() => {
|
|
137
|
+
// @ts-expect-error abstract class
|
|
138
|
+
const token = new ArgToken([''], this.getAttribute('config'));
|
|
139
|
+
token.firstChild.safeReplaceWith(name);
|
|
140
|
+
token.append(...cloned);
|
|
141
|
+
return token;
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
/** 移除无效部分 */
|
|
145
|
+
removeRedundant() {
|
|
146
|
+
debug_1.Shadow.run(() => {
|
|
147
|
+
for (let i = this.length - 1; i > 1; i--) {
|
|
148
|
+
super.removeAt(i);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* @override
|
|
154
|
+
* @param i 移除位置
|
|
155
|
+
*/
|
|
156
|
+
removeAt(i) {
|
|
157
|
+
if (i === 1) {
|
|
158
|
+
this.removeRedundant();
|
|
159
|
+
}
|
|
160
|
+
return super.removeAt(i);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* @override
|
|
164
|
+
* @param token 待插入的子节点
|
|
165
|
+
* @param i 插入位置
|
|
166
|
+
*/
|
|
167
|
+
insertAt(token, i = this.length) {
|
|
168
|
+
i += i < 0 ? this.length : 0;
|
|
169
|
+
if (debug_1.Shadow.running) {
|
|
170
|
+
//
|
|
171
|
+
}
|
|
172
|
+
else if (i > 1) {
|
|
173
|
+
this.constructorError('cannot insert redundant child nodes');
|
|
174
|
+
}
|
|
175
|
+
else if (typeof token === 'string') {
|
|
176
|
+
this.constructorError('cannot insert text nodes');
|
|
177
|
+
}
|
|
178
|
+
super.insertAt(token, i);
|
|
179
|
+
if (i === 1) {
|
|
180
|
+
token.type = 'arg-default';
|
|
181
|
+
}
|
|
182
|
+
return token;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* 设置参数名
|
|
186
|
+
* @param name 新参数名
|
|
187
|
+
*/
|
|
188
|
+
setName(name) {
|
|
189
|
+
const { childNodes } = index_1.default.parse(name, this.getAttribute('include'), 2, this.getAttribute('config'));
|
|
190
|
+
this.firstChild.replaceChildren(...childNodes);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* 设置预设值
|
|
194
|
+
* @param value 预设值
|
|
195
|
+
*/
|
|
196
|
+
setDefault(value) {
|
|
197
|
+
if (value === false) {
|
|
198
|
+
this.removeAt(1);
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
const root = index_1.default.parse(value, this.getAttribute('include'), undefined, this.getAttribute('config')), { childNodes: [, oldDefault] } = this;
|
|
202
|
+
if (oldDefault) {
|
|
203
|
+
oldDefault.replaceChildren(...root.childNodes);
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
root.type = 'arg-default';
|
|
207
|
+
this.insertAt(root);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
/** @private */
|
|
211
|
+
toHtmlInternal(nowrap, nocc) {
|
|
212
|
+
if (this.length === 1) {
|
|
213
|
+
const html = (0, html_1.font)(this, this.toString());
|
|
214
|
+
return nowrap ? html.replaceAll('\n', ' ') : html;
|
|
215
|
+
}
|
|
216
|
+
return (0, html_1.font)(this, this.childNodes[1].toHtmlInternal(nowrap, nocc));
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
exports.ArgToken = ArgToken;
|
|
220
|
+
constants_1.classes['ArgToken'] = __filename;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Token } from './index';
|
|
2
|
+
import type { Config } from '../base';
|
|
3
|
+
declare const atomTypes: readonly ["arg-name", "attr-key", "attr-value", "ext-attr-dirty", "html-attr-dirty", "table-attr-dirty", "converter-flag", "converter-rule-variant", "converter-rule-to", "converter-rule-from", "invoke-function", "invoke-module", "template-name", "link-target", "param-line"];
|
|
4
|
+
declare type AtomTypes = typeof atomTypes[number];
|
|
5
|
+
/** 不会被继续解析的plain Token */
|
|
6
|
+
export declare class AtomToken extends Token {
|
|
7
|
+
#private;
|
|
8
|
+
get type(): AtomTypes;
|
|
9
|
+
set type(value: AtomTypes);
|
|
10
|
+
/** @class */
|
|
11
|
+
constructor(wikitext: string | undefined, type: AtomTypes, config?: Config, accum?: Token[], acceptable?: Acceptable);
|
|
12
|
+
cloneNode(): this;
|
|
13
|
+
}
|
|
14
|
+
export {};
|
package/dist/src/atom.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AtomToken = void 0;
|
|
4
|
+
const index_1 = require("./index");
|
|
5
|
+
/* NOT FOR BROWSER */
|
|
6
|
+
const debug_1 = require("../util/debug");
|
|
7
|
+
const constants_1 = require("../util/constants");
|
|
8
|
+
/* NOT FOR BROWSER END */
|
|
9
|
+
const atomTypes = [
|
|
10
|
+
'arg-name',
|
|
11
|
+
'attr-key',
|
|
12
|
+
'attr-value',
|
|
13
|
+
'ext-attr-dirty',
|
|
14
|
+
'html-attr-dirty',
|
|
15
|
+
'table-attr-dirty',
|
|
16
|
+
'converter-flag',
|
|
17
|
+
'converter-rule-variant',
|
|
18
|
+
'converter-rule-to',
|
|
19
|
+
'converter-rule-from',
|
|
20
|
+
'invoke-function',
|
|
21
|
+
'invoke-module',
|
|
22
|
+
'template-name',
|
|
23
|
+
'link-target',
|
|
24
|
+
'param-line',
|
|
25
|
+
];
|
|
26
|
+
/** 不会被继续解析的plain Token */
|
|
27
|
+
class AtomToken extends index_1.Token {
|
|
28
|
+
#type;
|
|
29
|
+
get type() {
|
|
30
|
+
return this.#type;
|
|
31
|
+
}
|
|
32
|
+
set type(value) {
|
|
33
|
+
if (!atomTypes.includes(value)) {
|
|
34
|
+
throw new RangeError(`"${value}" is not a valid type for AtomToken!`);
|
|
35
|
+
}
|
|
36
|
+
this.#type = value;
|
|
37
|
+
}
|
|
38
|
+
/** @class */
|
|
39
|
+
constructor(wikitext, type, config, accum, acceptable) {
|
|
40
|
+
super(wikitext, config, accum, acceptable);
|
|
41
|
+
this.#type = type;
|
|
42
|
+
}
|
|
43
|
+
/* NOT FOR BROWSER */
|
|
44
|
+
cloneNode() {
|
|
45
|
+
const cloned = this.cloneChildNodes(), config = this.getAttribute('config'), acceptable = this.getAcceptable();
|
|
46
|
+
return debug_1.Shadow.run(() => {
|
|
47
|
+
const token = new AtomToken(undefined, this.type, config, [], acceptable);
|
|
48
|
+
token.append(...cloned);
|
|
49
|
+
return token;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.AtomToken = AtomToken;
|
|
54
|
+
constants_1.classes['AtomToken'] = __filename;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import Parser from '../index';
|
|
2
|
+
import { Token } from './index';
|
|
3
|
+
import { AtomToken } from './atom';
|
|
4
|
+
import type { LintError } from '../base';
|
|
5
|
+
import type { AttributesToken } from '../internal';
|
|
6
|
+
export type AttributeTypes = 'ext-attr' | 'html-attr' | 'table-attr';
|
|
7
|
+
/**
|
|
8
|
+
* 扩展和HTML标签属性
|
|
9
|
+
* @classdesc `{childNodes: [AtomToken, Token|AtomToken]}`
|
|
10
|
+
*/
|
|
11
|
+
export declare abstract class AttributeToken extends Token {
|
|
12
|
+
#private;
|
|
13
|
+
readonly name: string;
|
|
14
|
+
readonly childNodes: readonly [AtomToken, Token];
|
|
15
|
+
abstract get firstChild(): AtomToken;
|
|
16
|
+
abstract get lastChild(): Token;
|
|
17
|
+
abstract get parentNode(): AttributesToken | undefined;
|
|
18
|
+
abstract get nextSibling(): AtomToken | this | undefined;
|
|
19
|
+
abstract get previousSibling(): AtomToken | this | undefined;
|
|
20
|
+
abstract get children(): [AtomToken, Token];
|
|
21
|
+
abstract get firstElementChild(): AtomToken;
|
|
22
|
+
abstract get lastElementChild(): Token;
|
|
23
|
+
abstract get parentElement(): AttributesToken | undefined;
|
|
24
|
+
abstract get nextElementSibling(): AtomToken | this | undefined;
|
|
25
|
+
abstract get previousElementSibling(): AtomToken | this | undefined;
|
|
26
|
+
get type(): AttributeTypes;
|
|
27
|
+
/** 标签名 */
|
|
28
|
+
get tag(): string;
|
|
29
|
+
/** 引号是否匹配 */
|
|
30
|
+
get balanced(): boolean;
|
|
31
|
+
set balanced(value: boolean);
|
|
32
|
+
/** getValue()的getter */
|
|
33
|
+
get value(): string | true;
|
|
34
|
+
set value(value: string | true);
|
|
35
|
+
/**
|
|
36
|
+
* @param type 标签类型
|
|
37
|
+
* @param tag 标签名
|
|
38
|
+
* @param key 属性名
|
|
39
|
+
* @param equal 等号
|
|
40
|
+
* @param value 属性值
|
|
41
|
+
* @param quotes 引号
|
|
42
|
+
*/
|
|
43
|
+
constructor(type: AttributeTypes, tag: string, key: string, equal?: string, value?: string, quotes?: readonly [string?, string?], config?: Parser.Config, accum?: Token[]);
|
|
44
|
+
/** 获取属性值 */
|
|
45
|
+
getValue(): string | true;
|
|
46
|
+
cloneNode(): this;
|
|
47
|
+
/** 转义等号 */
|
|
48
|
+
escape(): void;
|
|
49
|
+
/** 闭合引号 */
|
|
50
|
+
close(): void;
|
|
51
|
+
/**
|
|
52
|
+
* 设置属性值
|
|
53
|
+
* @param value 参数值
|
|
54
|
+
* @throws `RangeError` 扩展标签属性不能包含 ">"
|
|
55
|
+
* @throws `RangeError` 同时包含单引号和双引号
|
|
56
|
+
*/
|
|
57
|
+
setValue(value: string | boolean): void;
|
|
58
|
+
/**
|
|
59
|
+
* 修改属性名
|
|
60
|
+
* @param key 新属性名
|
|
61
|
+
* @throws `Error` title和alt属性不能更名
|
|
62
|
+
*/
|
|
63
|
+
rename(key: string): void;
|
|
64
|
+
}
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
3
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
4
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
5
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
6
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
7
|
+
var _, done = false;
|
|
8
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
9
|
+
var context = {};
|
|
10
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
11
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
12
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
13
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
14
|
+
if (kind === "accessor") {
|
|
15
|
+
if (result === void 0) continue;
|
|
16
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
17
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
18
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
19
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
20
|
+
}
|
|
21
|
+
else if (_ = accept(result)) {
|
|
22
|
+
if (kind === "field") initializers.unshift(_);
|
|
23
|
+
else descriptor[key] = _;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
27
|
+
done = true;
|
|
28
|
+
};
|
|
29
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
30
|
+
var useValue = arguments.length > 2;
|
|
31
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
32
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
33
|
+
}
|
|
34
|
+
return useValue ? value : void 0;
|
|
35
|
+
};
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
exports.AttributeToken = void 0;
|
|
38
|
+
const lint_1 = require("../util/lint");
|
|
39
|
+
const string_1 = require("../util/string");
|
|
40
|
+
const constants_1 = require("../util/constants");
|
|
41
|
+
const sharable_1 = require("../util/sharable");
|
|
42
|
+
const rect_1 = require("../lib/rect");
|
|
43
|
+
const index_1 = require("../index");
|
|
44
|
+
const index_2 = require("./index");
|
|
45
|
+
const atom_1 = require("./atom");
|
|
46
|
+
/* NOT FOR BROWSER */
|
|
47
|
+
const debug_1 = require("../util/debug");
|
|
48
|
+
const fixed_1 = require("../mixin/fixed");
|
|
49
|
+
const stages = { 'ext-attr': 0, 'html-attr': 2, 'table-attr': 3 };
|
|
50
|
+
const insecureStyle = /expression|(?:accelerator|-o-link(?:-source)?|-o-replace)\s*:|(?:url|image(?:-set)?)\s*\(|attr\s*\([^)]+[\s,]url/u;
|
|
51
|
+
/**
|
|
52
|
+
* 扩展和HTML标签属性
|
|
53
|
+
* @classdesc `{childNodes: [AtomToken, Token|AtomToken]}`
|
|
54
|
+
*/
|
|
55
|
+
let AttributeToken = (() => {
|
|
56
|
+
let _classDecorators = [fixed_1.fixedToken];
|
|
57
|
+
let _classDescriptor;
|
|
58
|
+
let _classExtraInitializers = [];
|
|
59
|
+
let _classThis;
|
|
60
|
+
let _classSuper = index_2.Token;
|
|
61
|
+
var AttributeToken = class extends _classSuper {
|
|
62
|
+
static { _classThis = this; }
|
|
63
|
+
static {
|
|
64
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
65
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
66
|
+
AttributeToken = _classThis = _classDescriptor.value;
|
|
67
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
68
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
69
|
+
}
|
|
70
|
+
#type;
|
|
71
|
+
#tag;
|
|
72
|
+
#equal;
|
|
73
|
+
#quotes;
|
|
74
|
+
/* NOT FOR BROWSER END */
|
|
75
|
+
get type() {
|
|
76
|
+
return this.#type;
|
|
77
|
+
}
|
|
78
|
+
/** 标签名 */
|
|
79
|
+
get tag() {
|
|
80
|
+
return this.#tag;
|
|
81
|
+
}
|
|
82
|
+
/** 引号是否匹配 */
|
|
83
|
+
get balanced() {
|
|
84
|
+
return !this.#equal || this.#quotes[0] === this.#quotes[1];
|
|
85
|
+
}
|
|
86
|
+
/* NOT FOR BROWSER */
|
|
87
|
+
set balanced(value) {
|
|
88
|
+
if (value) {
|
|
89
|
+
this.close();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** getValue()的getter */
|
|
93
|
+
get value() {
|
|
94
|
+
return this.getValue();
|
|
95
|
+
}
|
|
96
|
+
set value(value) {
|
|
97
|
+
this.setValue(value);
|
|
98
|
+
}
|
|
99
|
+
/* NOT FOR BROWSER END */
|
|
100
|
+
/**
|
|
101
|
+
* @param type 标签类型
|
|
102
|
+
* @param tag 标签名
|
|
103
|
+
* @param key 属性名
|
|
104
|
+
* @param equal 等号
|
|
105
|
+
* @param value 属性值
|
|
106
|
+
* @param quotes 引号
|
|
107
|
+
*/
|
|
108
|
+
constructor(type, tag, key, equal = '', value, quotes = [], config = index_1.default.getConfig(), accum = []) {
|
|
109
|
+
const keyToken = new atom_1.AtomToken(key, 'attr-key', config, accum, type === 'ext-attr' ? { AstText: ':' } : { 'Stage-2': ':', '!ExtToken': '', '!HeadingToken': '' });
|
|
110
|
+
let valueToken;
|
|
111
|
+
if (key === 'title' || tag === 'img' && key === 'alt') {
|
|
112
|
+
valueToken = new index_2.Token(value, config, accum, {
|
|
113
|
+
[`Stage-${stages[type]}`]: ':', ConverterToken: ':',
|
|
114
|
+
});
|
|
115
|
+
valueToken.type = 'attr-value';
|
|
116
|
+
valueToken.setAttribute('stage', constants_1.MAX_STAGE - 1);
|
|
117
|
+
}
|
|
118
|
+
else if (tag === 'gallery' && key === 'caption'
|
|
119
|
+
|| tag === 'choose' && (key === 'before' || key === 'after')) {
|
|
120
|
+
const newConfig = {
|
|
121
|
+
...config,
|
|
122
|
+
excludes: [...config.excludes, 'heading', 'html', 'table', 'hr', 'list'],
|
|
123
|
+
};
|
|
124
|
+
valueToken = new index_2.Token(value, newConfig, accum, {
|
|
125
|
+
AstText: ':',
|
|
126
|
+
ArgToken: ':',
|
|
127
|
+
TranscludeToken: ':',
|
|
128
|
+
LinkToken: ':',
|
|
129
|
+
FileToken: ':',
|
|
130
|
+
CategoryToken: ':',
|
|
131
|
+
QuoteToken: ':',
|
|
132
|
+
ExtLinkToken: ':',
|
|
133
|
+
MagicLinkToken: ':',
|
|
134
|
+
ConverterToken: ':',
|
|
135
|
+
});
|
|
136
|
+
valueToken.type = 'attr-value';
|
|
137
|
+
valueToken.setAttribute('stage', 1);
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
valueToken = new atom_1.AtomToken(value, 'attr-value', config, accum, {
|
|
141
|
+
[`Stage-${stages[type]}`]: ':',
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
super(undefined, config, accum);
|
|
145
|
+
this.#type = type;
|
|
146
|
+
this.append(keyToken, valueToken);
|
|
147
|
+
this.#equal = equal;
|
|
148
|
+
this.#quotes = [...quotes];
|
|
149
|
+
this.#tag = tag;
|
|
150
|
+
this.setAttribute('name', (0, string_1.removeComment)(key).trim().toLowerCase());
|
|
151
|
+
}
|
|
152
|
+
/** @private */
|
|
153
|
+
afterBuild() {
|
|
154
|
+
if (this.#equal.includes('\0')) {
|
|
155
|
+
this.#equal = this.buildFromStr(this.#equal, constants_1.BuildMethod.String);
|
|
156
|
+
}
|
|
157
|
+
if (this.parentNode) {
|
|
158
|
+
this.#tag = this.parentNode.name;
|
|
159
|
+
}
|
|
160
|
+
this.setAttribute('name', this.firstChild.toString(true).trim().toLowerCase());
|
|
161
|
+
super.afterBuild();
|
|
162
|
+
}
|
|
163
|
+
/** @private */
|
|
164
|
+
toString(skip) {
|
|
165
|
+
const [quoteStart = '', quoteEnd = ''] = this.#quotes;
|
|
166
|
+
return this.#equal ? super.toString(skip, this.#equal + quoteStart) + quoteEnd : this.firstChild.toString(skip);
|
|
167
|
+
}
|
|
168
|
+
/** @private */
|
|
169
|
+
text() {
|
|
170
|
+
return this.#equal ? `${super.text(`${this.#equal.trim()}"`)}"` : this.firstChild.text();
|
|
171
|
+
}
|
|
172
|
+
/** @private */
|
|
173
|
+
getGaps() {
|
|
174
|
+
return this.#equal ? this.#equal.length + (this.#quotes[0]?.length ?? 0) : 0;
|
|
175
|
+
}
|
|
176
|
+
/** @private */
|
|
177
|
+
lint(start = this.getAbsoluteIndex(), re) {
|
|
178
|
+
const errors = super.lint(start, re), { balanced, firstChild, lastChild, type, name, tag } = this, value = this.getValue(), rect = new rect_1.BoundingRect(this, start);
|
|
179
|
+
if (!balanced) {
|
|
180
|
+
const e = (0, lint_1.generateForChild)(lastChild, rect, 'unclosed-quote', index_1.default.msg('unclosed $1', 'quotes'), 'warning');
|
|
181
|
+
e.startIndex--;
|
|
182
|
+
e.startCol--;
|
|
183
|
+
const fix = { range: [e.endIndex, e.endIndex], text: this.#quotes[0] };
|
|
184
|
+
if (lastChild.childNodes.some(({ type: t, data }) => t === 'text' && /\s/u.test(data))) {
|
|
185
|
+
e.suggestions = [
|
|
186
|
+
{
|
|
187
|
+
desc: 'close',
|
|
188
|
+
...fix,
|
|
189
|
+
},
|
|
190
|
+
];
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
e.fix = fix;
|
|
194
|
+
}
|
|
195
|
+
errors.push(e);
|
|
196
|
+
}
|
|
197
|
+
const attrs = sharable_1.extAttrs[tag], attrs2 = sharable_1.htmlAttrs[tag];
|
|
198
|
+
if (!attrs?.has(name)
|
|
199
|
+
&& !attrs2?.has(name)
|
|
200
|
+
// 不是未定义的扩展标签或包含嵌入的HTML标签
|
|
201
|
+
&& (type === 'ext-attr' ? attrs || attrs2 : !/\{\{[^{]+\}\}/u.test(name))
|
|
202
|
+
&& (type === 'ext-attr' && !attrs2
|
|
203
|
+
|| !/^(?:xmlns:[\w:.-]+|data-(?!ooui|mw|parsoid)[^:]*)$/u.test(name)
|
|
204
|
+
&& (tag === 'meta' || tag === 'link' || !sharable_1.commonHtmlAttrs.has(name)))) {
|
|
205
|
+
errors.push((0, lint_1.generateForChild)(firstChild, rect, 'illegal-attr', 'illegal attribute name'));
|
|
206
|
+
}
|
|
207
|
+
else if (sharable_1.obsoleteAttrs[tag]?.has(name)) {
|
|
208
|
+
errors.push((0, lint_1.generateForChild)(firstChild, rect, 'obsolete-attr', 'obsolete attribute', 'warning'));
|
|
209
|
+
}
|
|
210
|
+
else if (name === 'style' && typeof value === 'string' && insecureStyle.test(value)) {
|
|
211
|
+
errors.push((0, lint_1.generateForChild)(lastChild, rect, 'insecure-style', 'insecure style'));
|
|
212
|
+
}
|
|
213
|
+
else if (name === 'tabindex' && typeof value === 'string' && value !== '0') {
|
|
214
|
+
const e = (0, lint_1.generateForChild)(lastChild, rect, 'illegal-attr', 'nonzero tabindex');
|
|
215
|
+
e.suggestions = [
|
|
216
|
+
{
|
|
217
|
+
desc: 'remove',
|
|
218
|
+
range: [start, e.endIndex],
|
|
219
|
+
text: '',
|
|
220
|
+
},
|
|
221
|
+
{
|
|
222
|
+
desc: '0 tabindex',
|
|
223
|
+
range: [e.startIndex, e.endIndex],
|
|
224
|
+
text: '0',
|
|
225
|
+
},
|
|
226
|
+
];
|
|
227
|
+
errors.push(e);
|
|
228
|
+
}
|
|
229
|
+
return errors;
|
|
230
|
+
}
|
|
231
|
+
/** 获取属性值 */
|
|
232
|
+
getValue() {
|
|
233
|
+
return this.#equal ? this.lastChild.text().trim() : this.type === 'ext-attr' || '';
|
|
234
|
+
}
|
|
235
|
+
/** @private */
|
|
236
|
+
print() {
|
|
237
|
+
const [quoteStart = '', quoteEnd = ''] = this.#quotes;
|
|
238
|
+
return this.#equal ? super.print({ sep: (0, string_1.escape)(this.#equal) + quoteStart, post: quoteEnd }) : super.print();
|
|
239
|
+
}
|
|
240
|
+
/* NOT FOR BROWSER */
|
|
241
|
+
cloneNode() {
|
|
242
|
+
const [key, value] = this.cloneChildNodes(), k = key.toString(), config = this.getAttribute('config');
|
|
243
|
+
return debug_1.Shadow.run(() => {
|
|
244
|
+
// @ts-expect-error abstract class
|
|
245
|
+
const token = new AttributeToken(this.type, this.tag, k, this.#equal, '', this.#quotes, config);
|
|
246
|
+
token.firstChild.safeReplaceWith(key);
|
|
247
|
+
token.lastChild.safeReplaceWith(value);
|
|
248
|
+
return token;
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
/** 转义等号 */
|
|
252
|
+
escape() {
|
|
253
|
+
this.#equal = '{{=}}';
|
|
254
|
+
}
|
|
255
|
+
/** 闭合引号 */
|
|
256
|
+
close() {
|
|
257
|
+
const [opening] = this.#quotes;
|
|
258
|
+
if (opening) {
|
|
259
|
+
this.#quotes[1] = opening;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* 设置属性值
|
|
264
|
+
* @param value 参数值
|
|
265
|
+
* @throws `RangeError` 扩展标签属性不能包含 ">"
|
|
266
|
+
* @throws `RangeError` 同时包含单引号和双引号
|
|
267
|
+
*/
|
|
268
|
+
setValue(value) {
|
|
269
|
+
if (value === false) {
|
|
270
|
+
this.remove();
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
else if (value === true) {
|
|
274
|
+
this.#equal = '';
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
else if (this.type === 'ext-attr' && value.includes('>')) {
|
|
278
|
+
throw new RangeError('Attributes of an extension tag cannot contain ">"!');
|
|
279
|
+
}
|
|
280
|
+
else if (value.includes('"') && value.includes(`'`)) {
|
|
281
|
+
throw new RangeError('Attribute values cannot contain single and double quotes simultaneously!');
|
|
282
|
+
}
|
|
283
|
+
const config = this.getAttribute('config'), { childNodes } = index_1.default.parse(value, this.getAttribute('include'), stages[this.type] + 1, config);
|
|
284
|
+
this.lastChild.replaceChildren(...childNodes);
|
|
285
|
+
if (value.includes('"')) {
|
|
286
|
+
this.#quotes = [`'`, `'`];
|
|
287
|
+
}
|
|
288
|
+
else if (value.includes(`'`) || !this.#quotes[0]) {
|
|
289
|
+
this.#quotes = ['"', '"'];
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
this.close();
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* 修改属性名
|
|
297
|
+
* @param key 新属性名
|
|
298
|
+
* @throws `Error` title和alt属性不能更名
|
|
299
|
+
*/
|
|
300
|
+
rename(key) {
|
|
301
|
+
if (this.name === 'title' || this.name === 'alt' && this.tag === 'img') {
|
|
302
|
+
throw new Error(`${this.name} attribute cannot be renamed!`);
|
|
303
|
+
}
|
|
304
|
+
const config = this.getAttribute('config'), { childNodes } = index_1.default.parse(key, this.getAttribute('include'), stages[this.type] + 1, config);
|
|
305
|
+
this.firstChild.replaceChildren(...childNodes);
|
|
306
|
+
}
|
|
307
|
+
/** @private */
|
|
308
|
+
toHtmlInternal() {
|
|
309
|
+
const { type, name, tag, lastChild } = this;
|
|
310
|
+
if (type === 'ext-attr' && !(tag in sharable_1.htmlAttrs)
|
|
311
|
+
|| !sharable_1.htmlAttrs[tag]?.has(name)
|
|
312
|
+
&& !/^(?:xmlns:[\w:.-]+|data-(?!ooui|mw|parsoid)[^:]*)$/u.test(name)
|
|
313
|
+
&& (tag === 'meta' || tag === 'link' || !sharable_1.commonHtmlAttrs.has(name))) {
|
|
314
|
+
return '';
|
|
315
|
+
}
|
|
316
|
+
const value = lastChild.toHtmlInternal().trim();
|
|
317
|
+
if (name === 'style' && insecureStyle.test(value) || name === 'tabindex' && value !== '0') {
|
|
318
|
+
return '';
|
|
319
|
+
}
|
|
320
|
+
return `${name}="${(0, string_1.sanitizeAttr)(value.replace(/\s+| /gu, name === 'id' ? '_' : ' '))}"`;
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
return AttributeToken = _classThis;
|
|
324
|
+
})();
|
|
325
|
+
exports.AttributeToken = AttributeToken;
|
|
326
|
+
constants_1.classes['AttributeToken'] = __filename;
|