wikiparser-node 0.9.0 → 0.9.2-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.
- package/bundle/bundle.min.js +38 -0
- package/extensions/editor.css +62 -0
- package/extensions/editor.js +328 -0
- package/extensions/ui.css +119 -0
- package/package.json +13 -12
- package/README.md +0 -39
- package/config/default.json +0 -832
- package/config/llwiki.json +0 -630
- package/config/moegirl.json +0 -729
- package/config/zhwiki.json +0 -1269
- package/index.js +0 -333
- package/lib/element.js +0 -611
- package/lib/node.js +0 -770
- package/lib/ranges.js +0 -130
- package/lib/text.js +0 -245
- package/lib/title.js +0 -83
- package/mixin/attributeParent.js +0 -117
- package/mixin/fixedToken.js +0 -40
- package/mixin/hidden.js +0 -21
- package/mixin/singleLine.js +0 -31
- package/mixin/sol.js +0 -54
- package/parser/brackets.js +0 -126
- package/parser/commentAndExt.js +0 -59
- package/parser/converter.js +0 -46
- package/parser/externalLinks.js +0 -33
- package/parser/hrAndDoubleUnderscore.js +0 -38
- package/parser/html.js +0 -42
- package/parser/links.js +0 -94
- package/parser/list.js +0 -59
- package/parser/magicLinks.js +0 -41
- package/parser/quotes.js +0 -64
- package/parser/selector.js +0 -177
- package/parser/table.js +0 -114
- package/src/arg.js +0 -207
- package/src/atom/hidden.js +0 -13
- package/src/atom/index.js +0 -43
- package/src/attribute.js +0 -470
- package/src/attributes.js +0 -453
- package/src/charinsert.js +0 -97
- package/src/converter.js +0 -176
- package/src/converterFlags.js +0 -284
- package/src/converterRule.js +0 -256
- package/src/extLink.js +0 -180
- package/src/gallery.js +0 -149
- package/src/hasNowiki/index.js +0 -44
- package/src/hasNowiki/pre.js +0 -40
- package/src/heading.js +0 -134
- package/src/html.js +0 -254
- package/src/imageParameter.js +0 -301
- package/src/imagemap.js +0 -199
- package/src/imagemapLink.js +0 -41
- package/src/index.js +0 -936
- package/src/link/category.js +0 -44
- package/src/link/file.js +0 -287
- package/src/link/galleryImage.js +0 -120
- package/src/link/index.js +0 -384
- package/src/magicLink.js +0 -149
- package/src/nested/choose.js +0 -24
- package/src/nested/combobox.js +0 -23
- package/src/nested/index.js +0 -93
- package/src/nested/references.js +0 -23
- package/src/nowiki/comment.js +0 -71
- package/src/nowiki/dd.js +0 -59
- package/src/nowiki/doubleUnderscore.js +0 -56
- package/src/nowiki/hr.js +0 -41
- package/src/nowiki/index.js +0 -56
- package/src/nowiki/list.js +0 -16
- package/src/nowiki/noinclude.js +0 -28
- package/src/nowiki/quote.js +0 -69
- package/src/onlyinclude.js +0 -64
- package/src/paramTag/index.js +0 -89
- package/src/paramTag/inputbox.js +0 -35
- package/src/parameter.js +0 -239
- package/src/syntax.js +0 -91
- package/src/table/index.js +0 -983
- package/src/table/td.js +0 -338
- package/src/table/tr.js +0 -319
- package/src/tagPair/ext.js +0 -148
- package/src/tagPair/include.js +0 -50
- package/src/tagPair/index.js +0 -126
- package/src/transclude.js +0 -843
- package/tool/index.js +0 -1202
- package/util/base.js +0 -17
- package/util/debug.js +0 -73
- package/util/diff.js +0 -76
- package/util/lint.js +0 -55
- package/util/string.js +0 -126
package/src/attributes.js
DELETED
|
@@ -1,453 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const {generateForSelf, generateForChild} = require('../util/lint'),
|
|
4
|
-
{toCase, normalizeSpace, text, removeComment} = require('../util/string'),
|
|
5
|
-
Parser = require('..'),
|
|
6
|
-
Token = require('.'),
|
|
7
|
-
AtomToken = require('./atom'),
|
|
8
|
-
AttributeToken = require('./attribute');
|
|
9
|
-
|
|
10
|
-
const stages = {'ext-attrs': 0, 'html-attrs': 2, 'table-attrs': 3};
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 扩展和HTML标签属性
|
|
14
|
-
* @classdesc `{childNodes: ...AtomToken|AttributeToken}`
|
|
15
|
-
*/
|
|
16
|
-
class AttributesToken extends Token {
|
|
17
|
-
/**
|
|
18
|
-
* @override
|
|
19
|
-
* @param {string} key 属性键
|
|
20
|
-
* @param {string|undefined} equal 属性规则运算符,`equal`存在时`val`和`i`也一定存在
|
|
21
|
-
* @param {string|undefined} val 属性值
|
|
22
|
-
* @param {string|undefined} i 是否对大小写不敏感
|
|
23
|
-
*/
|
|
24
|
-
#matchesAttr = (key, equal, val, i) => {
|
|
25
|
-
if (!equal) {
|
|
26
|
-
return this.hasAttr(key);
|
|
27
|
-
} else if (!this.hasAttr(key)) {
|
|
28
|
-
return equal === '!=';
|
|
29
|
-
}
|
|
30
|
-
val = toCase(val, i);
|
|
31
|
-
const attr = this.getAttr(key),
|
|
32
|
-
thisVal = toCase(attr === true ? '' : attr, i);
|
|
33
|
-
switch (equal) {
|
|
34
|
-
case '~=':
|
|
35
|
-
return attr !== true && thisVal.split(/\s/u).includes(val);
|
|
36
|
-
case '|=': // 允许`val === ''`
|
|
37
|
-
return thisVal === val || thisVal.startsWith(`${val}-`);
|
|
38
|
-
case '^=':
|
|
39
|
-
return attr !== true && thisVal.startsWith(val);
|
|
40
|
-
case '$=':
|
|
41
|
-
return attr !== true && thisVal.endsWith(val);
|
|
42
|
-
case '*=':
|
|
43
|
-
return attr !== true && thisVal.includes(val);
|
|
44
|
-
case '!=':
|
|
45
|
-
return thisVal !== val;
|
|
46
|
-
default: // `=`
|
|
47
|
-
return thisVal === val;
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
/** getAttrs()方法的getter写法 */
|
|
52
|
-
get attributes() {
|
|
53
|
-
return this.getAttrs();
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/** 以字符串表示的class属性 */
|
|
57
|
-
get className() {
|
|
58
|
-
const attr = this.getAttr('class');
|
|
59
|
-
return typeof attr === 'string' ? attr : '';
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
set className(className) {
|
|
63
|
-
this.setAttr('class', className);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** 以Set表示的class属性 */
|
|
67
|
-
get classList() {
|
|
68
|
-
return new Set(this.className.split(/\s/u));
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/** id属性 */
|
|
72
|
-
get id() {
|
|
73
|
-
const attr = this.getAttr('id');
|
|
74
|
-
return typeof attr === 'string' ? attr : '';
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
set id(id) {
|
|
78
|
-
this.setAttr('id', id);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** 是否含有无效属性 */
|
|
82
|
-
get sanitized() {
|
|
83
|
-
return this.getDirtyAttrs().length === 0;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* @param {string} attr 标签属性
|
|
88
|
-
* @param {'ext-attrs'|'html-attrs'|'table-attrs'} type 标签类型
|
|
89
|
-
* @param {string} name 标签名
|
|
90
|
-
* @param {accum} accum
|
|
91
|
-
*/
|
|
92
|
-
constructor(attr, type, name, config = Parser.getConfig(), accum = []) {
|
|
93
|
-
super(undefined, config, true, accum, {
|
|
94
|
-
AtomToken: ':', AttributeToken: ':',
|
|
95
|
-
});
|
|
96
|
-
this.type = type;
|
|
97
|
-
this.setAttribute('name', name);
|
|
98
|
-
if (attr) {
|
|
99
|
-
const regex = new RegExp(
|
|
100
|
-
`([^\\s/](?:(?!\0\\d+~\x7F)[^\\s/=])*)` // 属性名
|
|
101
|
-
+ '(?:'
|
|
102
|
-
+ '((?:\\s|\0\\d+c\x7F)*' // `=`前的空白字符
|
|
103
|
-
+ '(?:=|\0\\d+~\x7F)' // `=`
|
|
104
|
-
+ '(?:\\s|\0\\d+c\x7F)*)' // `=`后的空白字符
|
|
105
|
-
+ `(?:(["'])(.*?)(\\3|$)|(\\S*))` // 属性值
|
|
106
|
-
+ ')?',
|
|
107
|
-
'gsu',
|
|
108
|
-
);
|
|
109
|
-
let out = '',
|
|
110
|
-
mt = regex.exec(attr),
|
|
111
|
-
lastIndex = 0;
|
|
112
|
-
const insertDirty = /** 插入无效属性 */ () => {
|
|
113
|
-
if (out) {
|
|
114
|
-
super.insertAt(new AtomToken(out, `${type.slice(0, -1)}-dirty`, config, accum, {
|
|
115
|
-
[`Stage-${stages[type]}`]: ':',
|
|
116
|
-
}));
|
|
117
|
-
out = '';
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
while (mt) {
|
|
121
|
-
const {index, 0: full, 1: key, 2: equal, 3: quoteStart, 4: quoted, 5: quoteEnd, 6: unquoted} = mt;
|
|
122
|
-
out += attr.slice(lastIndex, index);
|
|
123
|
-
if (/^(?:[\w:]|\0\d+[t!~{}+-]\x7F)(?:[\w:.-]|\0\d+[t!~{}+-]\x7F)*$/u.test(removeComment(key).trim())) {
|
|
124
|
-
const value = quoted ?? unquoted,
|
|
125
|
-
quotes = [quoteStart, quoteEnd],
|
|
126
|
-
token = new AttributeToken(type.slice(0, -1), name, key, equal, value, quotes, config, accum);
|
|
127
|
-
insertDirty();
|
|
128
|
-
super.insertAt(token);
|
|
129
|
-
} else {
|
|
130
|
-
out += full;
|
|
131
|
-
}
|
|
132
|
-
({lastIndex} = regex);
|
|
133
|
-
mt = regex.exec(attr);
|
|
134
|
-
}
|
|
135
|
-
out += attr.slice(lastIndex);
|
|
136
|
-
insertDirty();
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* @override
|
|
142
|
-
* @this {AttributesToken & {parentNode: TdToken}}
|
|
143
|
-
*/
|
|
144
|
-
afterBuild() {
|
|
145
|
-
const TdToken = require('./table/td');
|
|
146
|
-
if (this.type === 'table-attrs') {
|
|
147
|
-
this.setAttribute('name', this.parentNode?.subtype === 'caption' ? 'caption' : this.parentNode?.type);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* 所有指定属性名的AttributeToken
|
|
153
|
-
* @param {string} key 属性名
|
|
154
|
-
* @returns {AttributeToken[]}
|
|
155
|
-
*/
|
|
156
|
-
getAttrTokens(key) {
|
|
157
|
-
return typeof key === 'string'
|
|
158
|
-
? this.childNodes.filter(
|
|
159
|
-
child => child instanceof AttributeToken && child.name === key.toLowerCase().trim(),
|
|
160
|
-
)
|
|
161
|
-
: this.typeError('getAttrTokens', 'String');
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* 制定属性名的最后一个AttributeToken
|
|
166
|
-
* @param {string} key 属性名
|
|
167
|
-
*/
|
|
168
|
-
getAttrToken(key) {
|
|
169
|
-
const tokens = this.getAttrTokens(key);
|
|
170
|
-
return tokens.at(-1);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* 获取标签属性
|
|
175
|
-
* @param {string} key 属性键
|
|
176
|
-
*/
|
|
177
|
-
getAttr(key) {
|
|
178
|
-
return this.getAttrToken(key)?.getValue();
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* @override
|
|
183
|
-
* @this {AttributesToken & {parentNode: HtmlToken}}
|
|
184
|
-
* @param {number} start 起始位置
|
|
185
|
-
*/
|
|
186
|
-
lint(start = this.getAbsoluteIndex()) {
|
|
187
|
-
const HtmlToken = require('./html');
|
|
188
|
-
const errors = super.lint(start),
|
|
189
|
-
{parentNode: {closing}, length, childNodes} = this,
|
|
190
|
-
/** @type {Record<string, AttributeToken[]>} */ attrs = {},
|
|
191
|
-
/** @type {Set<string>} */ duplicated = new Set();
|
|
192
|
-
let rect;
|
|
193
|
-
if (closing && this.text().trim()) {
|
|
194
|
-
rect = {start, ...this.getRootNode().posFromIndex(start)};
|
|
195
|
-
errors.push(generateForSelf(this, rect, 'attributes of a closing tag'));
|
|
196
|
-
}
|
|
197
|
-
for (let i = 0; i < length; i++) {
|
|
198
|
-
const /** @type {AtomToken|AttributeToken} */ attr = childNodes[i];
|
|
199
|
-
if (attr instanceof AtomToken && attr.text().trim()) {
|
|
200
|
-
rect ||= {start, ...this.getRootNode().posFromIndex(start)};
|
|
201
|
-
errors.push({
|
|
202
|
-
...generateForChild(attr, rect, 'containing invalid attribute'),
|
|
203
|
-
excerpt: childNodes.slice(i).map(String).join('').slice(0, 50),
|
|
204
|
-
});
|
|
205
|
-
} else if (attr instanceof AttributeToken) {
|
|
206
|
-
const {name} = attr;
|
|
207
|
-
if (name in attrs) {
|
|
208
|
-
duplicated.add(name);
|
|
209
|
-
attrs[name].push(attr);
|
|
210
|
-
} else if (name !== 'class') {
|
|
211
|
-
attrs[name] = [attr];
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
if (duplicated.size > 0) {
|
|
216
|
-
rect ||= {start, ...this.getRootNode().posFromIndex(start)};
|
|
217
|
-
for (const key of duplicated) {
|
|
218
|
-
errors.push(...attrs[key].map(attr => generateForChild(attr, rect, Parser.msg('duplicated $1 attribute', key))));
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
return errors;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* @override
|
|
226
|
-
* @this {AttributesToken & {parentNode: HtmlToken}}
|
|
227
|
-
*/
|
|
228
|
-
print() {
|
|
229
|
-
const HtmlToken = require('./html');
|
|
230
|
-
return String(this)
|
|
231
|
-
? `<span class="wpb-${this.type}">${this.childNodes.map(child => child.print({
|
|
232
|
-
class: child instanceof AtomToken && child.text().trim() && 'hidden',
|
|
233
|
-
})).join('')}</span>`
|
|
234
|
-
: '';
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
/** 清理标签属性 */
|
|
238
|
-
sanitize() {
|
|
239
|
-
let dirty = false;
|
|
240
|
-
for (let i = this.length - 1; i >= 0; i--) {
|
|
241
|
-
const child = this.childNodes[i];
|
|
242
|
-
if (child instanceof AtomToken && child.text().trim()) {
|
|
243
|
-
dirty = true;
|
|
244
|
-
this.removeAt(i);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
if (!Parser.running && dirty) {
|
|
248
|
-
Parser.warn(`${this.constructor.name}.sanitize 方法将清理无效属性!`);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/** @override */
|
|
253
|
-
cloneNode() {
|
|
254
|
-
const cloned = this.cloneChildNodes();
|
|
255
|
-
return Parser.run(() => {
|
|
256
|
-
const token = new AttributesToken(undefined, this.type, this.name, this.getAttribute('config'));
|
|
257
|
-
token.append(...cloned);
|
|
258
|
-
return token;
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* 所有无效属性
|
|
264
|
-
* @returns {AtomToken[]}
|
|
265
|
-
*/
|
|
266
|
-
getDirtyAttrs() {
|
|
267
|
-
return this.childNodes.filter(child => child instanceof AtomToken && child.text().trim());
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* @override
|
|
272
|
-
* @param {AttributeToken} token 待插入的子节点
|
|
273
|
-
* @param {number} i 插入位置
|
|
274
|
-
* @throws `RangeError` 不是AttributeToken或标签不匹配
|
|
275
|
-
*/
|
|
276
|
-
insertAt(token, i = this.length) {
|
|
277
|
-
if (!(token instanceof AttributeToken)) {
|
|
278
|
-
throw new RangeError(`${this.constructor.name}只能插入AttributeToken!`);
|
|
279
|
-
} else if (token.type !== this.type.slice(0, -1) || token.tag !== this.name) {
|
|
280
|
-
throw new RangeError(`待插入的AttributeToken只可用于${token.tag}标签!`);
|
|
281
|
-
} else if (i === this.length) {
|
|
282
|
-
const {lastChild} = this;
|
|
283
|
-
if (lastChild instanceof AttributeToken) {
|
|
284
|
-
lastChild.close();
|
|
285
|
-
}
|
|
286
|
-
} else {
|
|
287
|
-
token.close();
|
|
288
|
-
}
|
|
289
|
-
if (this.closest('parameter')) {
|
|
290
|
-
token.escape();
|
|
291
|
-
}
|
|
292
|
-
super.insertAt(token, i);
|
|
293
|
-
const {previousVisibleSibling, nextVisibleSibling} = token,
|
|
294
|
-
type = `${this.type.slice(0, -1)}-dirty`,
|
|
295
|
-
config = this.getAttribute('config'),
|
|
296
|
-
acceptable = {[`Stage-${stages[this.type]}`]: ':'};
|
|
297
|
-
if (nextVisibleSibling && !/^\s/u.test(String(nextVisibleSibling))) {
|
|
298
|
-
super.insertAt(Parser.run(() => new AtomToken(' ', type, config, [], acceptable)), i + 1);
|
|
299
|
-
}
|
|
300
|
-
if (previousVisibleSibling && !/\s$/u.test(String(previousVisibleSibling))) {
|
|
301
|
-
super.insertAt(Parser.run(() => new AtomToken(' ', type, config, [], acceptable)), i);
|
|
302
|
-
}
|
|
303
|
-
return token;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
/**
|
|
307
|
-
* 设置标签属性
|
|
308
|
-
* @param {string} key 属性键
|
|
309
|
-
* @param {string|boolean} value 属性值
|
|
310
|
-
* @throws `RangeError` 扩展标签属性不能包含">"
|
|
311
|
-
* @throws `RangeError` 无效的属性名
|
|
312
|
-
*/
|
|
313
|
-
setAttr(key, value) {
|
|
314
|
-
if (typeof key !== 'string' || typeof value !== 'string' && typeof value !== 'boolean') {
|
|
315
|
-
this.typeError('setAttr', 'String', 'Boolean');
|
|
316
|
-
} else if (this.type === 'ext-attrs' && typeof value === 'string' && value.includes('>')) {
|
|
317
|
-
throw new RangeError('扩展标签属性不能包含 ">"!');
|
|
318
|
-
}
|
|
319
|
-
key = key.toLowerCase().trim();
|
|
320
|
-
const attr = this.getAttrToken(key);
|
|
321
|
-
if (attr) {
|
|
322
|
-
attr.setValue(value);
|
|
323
|
-
return;
|
|
324
|
-
} else if (value === false) {
|
|
325
|
-
return;
|
|
326
|
-
}
|
|
327
|
-
const config = this.getAttribute('config'),
|
|
328
|
-
include = this.getAttribute('include'),
|
|
329
|
-
parsedKey = this.type === 'ext-attrs'
|
|
330
|
-
? key
|
|
331
|
-
: Parser.run(() => {
|
|
332
|
-
const token = new Token(key, config),
|
|
333
|
-
parseOnce = token.getAttribute('parseOnce');
|
|
334
|
-
parseOnce(0, include);
|
|
335
|
-
return String(parseOnce());
|
|
336
|
-
});
|
|
337
|
-
if (!/^(?:[\w:]|\0\d+[t!~{}+-]\x7F)(?:[\w:.-]|\0\d+[t!~{}+-]\x7F)*$/u.test(parsedKey)) {
|
|
338
|
-
throw new RangeError(`无效的属性名:${key}!`);
|
|
339
|
-
}
|
|
340
|
-
const newAttr = Parser.run(() => new AttributeToken(
|
|
341
|
-
this.type.slice(0, -1), this.name, key, value === true ? '' : '=', value, ['"', '"'], config,
|
|
342
|
-
));
|
|
343
|
-
this.insertAt(newAttr);
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
/**
|
|
347
|
-
* @override
|
|
348
|
-
* @template {string} T
|
|
349
|
-
* @param {T} key 属性键
|
|
350
|
-
* @returns {TokenAttribute<T>}
|
|
351
|
-
*/
|
|
352
|
-
getAttribute(key) {
|
|
353
|
-
return key === 'matchesAttr' ? this.#matchesAttr : super.getAttribute(key);
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
/**
|
|
357
|
-
* 标签是否具有某属性
|
|
358
|
-
* @param {string} key 属性键
|
|
359
|
-
*/
|
|
360
|
-
hasAttr(key) {
|
|
361
|
-
return typeof key === 'string'
|
|
362
|
-
? this.getAttrTokens(key).length > 0
|
|
363
|
-
: this.typeError('hasAttr', 'String');
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
/** 获取全部的标签属性名 */
|
|
367
|
-
getAttrNames() {
|
|
368
|
-
return new Set(this.childNodes.filter(child => child instanceof AttributeToken).map(({name}) => name));
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
/** 标签是否具有任意属性 */
|
|
372
|
-
hasAttrs() {
|
|
373
|
-
return this.getAttrNames().size > 0;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
/** 获取全部标签属性 */
|
|
377
|
-
getAttrs() {
|
|
378
|
-
const /** @type {AttributeToken[]} */ attrs = this.childNodes.filter(child => child instanceof AttributeToken);
|
|
379
|
-
return Object.fromEntries(attrs.map(({name, value}) => [name, value]));
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
/**
|
|
383
|
-
* 移除标签属性
|
|
384
|
-
* @param {string} key 属性键
|
|
385
|
-
*/
|
|
386
|
-
removeAttr(key) {
|
|
387
|
-
for (const attr of this.getAttrTokens(key)) {
|
|
388
|
-
attr.remove();
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
/**
|
|
393
|
-
* 开关标签属性
|
|
394
|
-
* @param {string} key 属性键
|
|
395
|
-
* @param {boolean|undefined} force 强制开启或关闭
|
|
396
|
-
* @throws `RangeError` 不为Boolean类型的属性值
|
|
397
|
-
*/
|
|
398
|
-
toggleAttr(key, force) {
|
|
399
|
-
if (typeof key !== 'string') {
|
|
400
|
-
this.typeError('toggleAttr', 'String');
|
|
401
|
-
} else if (force !== undefined) {
|
|
402
|
-
force = Boolean(force);
|
|
403
|
-
}
|
|
404
|
-
key = key.toLowerCase().trim();
|
|
405
|
-
const attr = this.getAttrToken(key);
|
|
406
|
-
if (attr && attr.getValue() !== true) {
|
|
407
|
-
throw new RangeError(`${key} 属性的值不为 Boolean!`);
|
|
408
|
-
} else if (attr) {
|
|
409
|
-
attr.setValue(force === true);
|
|
410
|
-
} else if (force !== false) {
|
|
411
|
-
this.setAttr(key, true);
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
/**
|
|
416
|
-
* 生成引导空格
|
|
417
|
-
* @param {string} str 属性字符串
|
|
418
|
-
*/
|
|
419
|
-
#leadingSpace(str = super.toString()) {
|
|
420
|
-
const {type} = this,
|
|
421
|
-
leadingRegex = {'ext-attrs': /^\s/u, 'html-attrs': /^[/\s]/u};
|
|
422
|
-
return str && type !== 'table-attrs' && !leadingRegex[type].test(str) ? ' ' : '';
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
/**
|
|
426
|
-
* @override
|
|
427
|
-
* @param {string} selector
|
|
428
|
-
*/
|
|
429
|
-
toString(selector) {
|
|
430
|
-
if (this.type === 'table-attrs') {
|
|
431
|
-
normalizeSpace(this);
|
|
432
|
-
}
|
|
433
|
-
const str = super.toString(selector);
|
|
434
|
-
return `${this.#leadingSpace(str)}${str}`;
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
/** @override */
|
|
438
|
-
getPadding() {
|
|
439
|
-
return this.#leadingSpace().length;
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
/** @override */
|
|
443
|
-
text() {
|
|
444
|
-
if (this.type === 'table-attrs') {
|
|
445
|
-
normalizeSpace(this);
|
|
446
|
-
}
|
|
447
|
-
const str = text(this.childNodes.filter(child => child instanceof AttributeToken), ' ');
|
|
448
|
-
return `${this.#leadingSpace(str)}${str}`;
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
Parser.classes.AttributesToken = __filename;
|
|
453
|
-
module.exports = AttributesToken;
|
package/src/charinsert.js
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const Parser = require('..'),
|
|
4
|
-
singleLine = require('../mixin/singleLine'),
|
|
5
|
-
Token = require('.'),
|
|
6
|
-
HasNowikiToken = require('./hasNowiki');
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* `<charinsert>`
|
|
10
|
-
* @classdesc `{childNodes: [...HasNowikiToken]}`
|
|
11
|
-
*/
|
|
12
|
-
class CharinsertToken extends Token {
|
|
13
|
-
type = 'ext-inner';
|
|
14
|
-
name = 'charinsert';
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @param {string} wikitext wikitext
|
|
18
|
-
* @param {accum} accum
|
|
19
|
-
*/
|
|
20
|
-
constructor(wikitext, config = Parser.getConfig(), accum = []) {
|
|
21
|
-
super(undefined, config, true, accum, {
|
|
22
|
-
SingleLineHasNowikiToken: ':',
|
|
23
|
-
});
|
|
24
|
-
const SingleLineHasNowikiToken = singleLine(HasNowikiToken);
|
|
25
|
-
this.append(
|
|
26
|
-
...wikitext.split('\n').map(str => new SingleLineHasNowikiToken(str, 'charinsert-line', config, accum)),
|
|
27
|
-
);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* @override
|
|
32
|
-
* @param {string} selector
|
|
33
|
-
*/
|
|
34
|
-
toString(selector) {
|
|
35
|
-
return super.toString(selector, '\n');
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** @override */
|
|
39
|
-
getGaps() {
|
|
40
|
-
return 1;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** @override */
|
|
44
|
-
print() {
|
|
45
|
-
return super.print({sep: '\n'});
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** @override */
|
|
49
|
-
cloneNode() {
|
|
50
|
-
const cloned = this.cloneChildNodes();
|
|
51
|
-
return Parser.run(() => {
|
|
52
|
-
const token = new CharinsertToken(undefined, this.getAttribute('config'));
|
|
53
|
-
token.append(...cloned);
|
|
54
|
-
return token;
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* 获取某一行的插入选项
|
|
60
|
-
* @param {number|HasNowikiToken} child 行号或子节点
|
|
61
|
-
*/
|
|
62
|
-
getLineItems(child) {
|
|
63
|
-
if (Number.isInteger(child)) {
|
|
64
|
-
child = this.childNodes.at(child);
|
|
65
|
-
}
|
|
66
|
-
if (!(child instanceof HasNowikiToken)) {
|
|
67
|
-
this.typeError('getLineItems', 'Number', 'HasNowikiToken');
|
|
68
|
-
}
|
|
69
|
-
const entities = {'\t': '	', '\r': '', ' ': ' '};
|
|
70
|
-
return String(child).replace(
|
|
71
|
-
/<nowiki>(.+?)<\/nowiki>/giu,
|
|
72
|
-
/** @type {function(...string): string} */ (_, inner) => inner.replace(/[\t\r ]/gu, s => entities[s]),
|
|
73
|
-
).split(/\s/u).filter(Boolean)
|
|
74
|
-
.map(item => {
|
|
75
|
-
const parts = item.split('+');
|
|
76
|
-
if (parts.length === 1) {
|
|
77
|
-
return parts[0];
|
|
78
|
-
}
|
|
79
|
-
return parts[0] ? parts.slice(0, 2) : '+';
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** 获取所有插入选项 */
|
|
84
|
-
getAllItems() {
|
|
85
|
-
return this.childNodes.flatMap(child => this.getLineItems(child));
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/** @override */
|
|
89
|
-
text() {
|
|
90
|
-
return this.childNodes.map(
|
|
91
|
-
child => this.getLineItems(child).map(str => Array.isArray(str) ? str.join('+') : str).join(' '),
|
|
92
|
-
).join('\n');
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
Parser.classes.CharinsertToken = __filename;
|
|
97
|
-
module.exports = CharinsertToken;
|