wikiparser-node 0.11.0-m → 0.11.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 +7 -0
- package/config/default.json +1 -0
- package/config/llwiki.json +35 -0
- package/config/moegirl.json +44 -0
- package/config/zhwiki.json +466 -0
- package/dist/index.d.ts +4 -0
- package/dist/lib/element.d.ts +116 -2
- package/dist/lib/node.d.ts +193 -10
- package/dist/lib/ranges.d.ts +37 -0
- package/dist/lib/text.d.ts +35 -1
- package/dist/lib/title.d.ts +6 -0
- package/dist/mixin/attributeParent.d.ts +9 -0
- package/dist/mixin/fixedToken.d.ts +8 -0
- package/dist/mixin/singleLine.d.ts +8 -0
- package/dist/mixin/sol.d.ts +8 -0
- package/dist/parser/selector.d.ts +12 -0
- package/dist/src/arg.d.ts +30 -1
- package/dist/src/atom/index.d.ts +2 -1
- package/dist/src/attribute.d.ts +24 -1
- package/dist/src/attributes.d.ts +79 -0
- package/dist/src/charinsert.d.ts +32 -0
- package/dist/src/converter.d.ts +75 -1
- package/dist/src/converterFlags.d.ts +57 -1
- package/dist/src/converterRule.d.ts +47 -1
- package/dist/src/extLink.d.ts +41 -1
- package/dist/src/gallery.d.ts +15 -1
- package/dist/src/heading.d.ts +22 -2
- package/dist/src/html.d.ts +25 -2
- package/dist/src/imageParameter.d.ts +43 -1
- package/dist/src/imagemap.d.ts +12 -1
- package/dist/src/imagemapLink.d.ts +5 -0
- package/dist/src/index.d.ts +136 -3
- package/dist/src/link/category.d.ts +8 -0
- package/dist/src/link/file.d.ts +58 -0
- package/dist/src/link/index.d.ts +61 -2
- package/dist/src/magicLink.d.ts +22 -0
- package/dist/src/nested/index.d.ts +3 -5
- package/dist/src/nowiki/comment.d.ts +13 -1
- package/dist/src/nowiki/dd.d.ts +9 -0
- package/dist/src/nowiki/doubleUnderscore.d.ts +11 -1
- package/dist/src/nowiki/index.d.ts +11 -2
- package/dist/src/nowiki/quote.d.ts +0 -7
- package/dist/src/onlyinclude.d.ts +8 -1
- package/dist/src/paramTag/index.d.ts +8 -3
- package/dist/src/parameter.d.ts +48 -1
- package/dist/src/syntax.d.ts +6 -1
- package/dist/src/table/index.d.ts +257 -0
- package/dist/src/table/td.d.ts +67 -6
- package/dist/src/table/tr.d.ts +74 -0
- package/dist/src/tagPair/ext.d.ts +2 -1
- package/dist/src/tagPair/include.d.ts +9 -0
- package/dist/src/tagPair/index.d.ts +14 -1
- package/dist/src/transclude.d.ts +125 -1
- package/dist/tool/index.d.ts +420 -0
- package/dist/util/base.d.ts +10 -0
- package/dist/util/debug.d.ts +20 -0
- package/dist/util/diff.d.ts +8 -0
- package/dist/util/string.d.ts +24 -0
- package/index.js +256 -4
- package/lib/element.js +488 -6
- package/lib/node.js +495 -6
- package/lib/ranges.js +130 -0
- package/lib/text.js +96 -1
- package/lib/title.js +28 -1
- package/mixin/attributeParent.js +117 -0
- package/mixin/fixedToken.js +40 -0
- package/mixin/hidden.js +3 -0
- package/mixin/singleLine.js +31 -0
- package/mixin/sol.js +54 -0
- package/package.json +5 -2
- package/parser/brackets.js +1 -0
- package/parser/commentAndExt.js +1 -0
- package/parser/converter.js +1 -0
- package/parser/externalLinks.js +1 -0
- package/parser/hrAndDoubleUnderscore.js +1 -0
- package/parser/html.js +1 -0
- package/parser/links.js +5 -4
- package/parser/list.js +1 -0
- package/parser/magicLinks.js +1 -0
- package/parser/quotes.js +1 -0
- package/parser/selector.js +180 -0
- package/parser/table.js +1 -0
- package/src/arg.js +116 -2
- package/src/atom/hidden.js +2 -0
- package/src/atom/index.js +17 -0
- package/src/attribute.js +189 -3
- package/src/attributes.js +307 -4
- package/src/charinsert.js +97 -0
- package/src/converter.js +108 -2
- package/src/converterFlags.js +187 -0
- package/src/converterRule.js +183 -1
- package/src/extLink.js +121 -1
- package/src/gallery.js +54 -0
- package/src/hasNowiki/index.js +12 -0
- package/src/hasNowiki/pre.js +12 -0
- package/src/heading.js +54 -3
- package/src/html.js +118 -3
- package/src/imageParameter.js +164 -2
- package/src/imagemap.js +61 -2
- package/src/imagemapLink.js +13 -1
- package/src/index.js +530 -3
- package/src/link/category.js +32 -1
- package/src/link/file.js +157 -2
- package/src/link/galleryImage.js +60 -2
- package/src/link/index.js +270 -1
- package/src/magicLink.js +83 -0
- package/src/nested/choose.js +1 -0
- package/src/nested/combobox.js +1 -0
- package/src/nested/index.js +27 -0
- package/src/nested/references.js +1 -0
- package/src/nowiki/comment.js +25 -1
- package/src/nowiki/dd.js +47 -1
- package/src/nowiki/doubleUnderscore.js +31 -1
- package/src/nowiki/hr.js +20 -1
- package/src/nowiki/index.js +23 -1
- package/src/nowiki/list.js +5 -2
- package/src/nowiki/noinclude.js +14 -0
- package/src/nowiki/quote.js +14 -0
- package/src/onlyinclude.js +26 -1
- package/src/paramTag/index.js +24 -1
- package/src/paramTag/inputbox.js +4 -1
- package/src/parameter.js +147 -5
- package/src/syntax.js +68 -0
- package/src/table/index.js +941 -2
- package/src/table/td.js +230 -5
- package/src/table/tr.js +247 -2
- package/src/tagPair/ext.js +21 -1
- package/src/tagPair/include.js +24 -0
- package/src/tagPair/index.js +56 -2
- package/src/transclude.js +512 -5
- package/tool/index.js +1209 -0
- package/typings/array.d.ts +29 -0
- package/typings/event.d.ts +22 -0
- package/typings/index.d.ts +67 -0
- package/typings/node.d.ts +19 -0
- package/typings/parser.d.ts +7 -0
- package/typings/table.d.ts +10 -0
- package/typings/token.d.ts +3 -0
- package/typings/tool.d.ts +6 -0
- package/util/debug.js +73 -0
- package/util/string.js +51 -0
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/** @typedef {import('../typings/parser').SelectorArray} SelectorArray */
|
|
4
|
+
/** @typedef {import('../typings/parser').pseudo} pseudo */
|
|
5
|
+
|
|
6
|
+
const Parser = require('..');
|
|
7
|
+
|
|
8
|
+
const /** @type {Set<pseudo>} */ simplePseudos = new Set([
|
|
9
|
+
'root',
|
|
10
|
+
'first-child',
|
|
11
|
+
'first-of-type',
|
|
12
|
+
'last-child',
|
|
13
|
+
'last-of-type',
|
|
14
|
+
'only-child',
|
|
15
|
+
'only-of-type',
|
|
16
|
+
'empty',
|
|
17
|
+
'parent',
|
|
18
|
+
'header',
|
|
19
|
+
'hidden',
|
|
20
|
+
'visible',
|
|
21
|
+
'only-whitespace',
|
|
22
|
+
'local-link',
|
|
23
|
+
'read-only',
|
|
24
|
+
'read-write',
|
|
25
|
+
'invalid',
|
|
26
|
+
'required',
|
|
27
|
+
'optional',
|
|
28
|
+
]),
|
|
29
|
+
/** @type {pseudo[]} */ complexPseudos = [
|
|
30
|
+
'is',
|
|
31
|
+
'not',
|
|
32
|
+
'nth-child',
|
|
33
|
+
'nth-of-type',
|
|
34
|
+
'nth-last-child',
|
|
35
|
+
'nth-last-of-type',
|
|
36
|
+
'contains',
|
|
37
|
+
'has',
|
|
38
|
+
'lang',
|
|
39
|
+
],
|
|
40
|
+
specialChars = [
|
|
41
|
+
['[', '['],
|
|
42
|
+
[']', ']'],
|
|
43
|
+
['(', '('],
|
|
44
|
+
[')', ')'],
|
|
45
|
+
['"', '"'],
|
|
46
|
+
[`'`, '''],
|
|
47
|
+
[':', ':'],
|
|
48
|
+
['\\', '\'],
|
|
49
|
+
['&', '&'],
|
|
50
|
+
],
|
|
51
|
+
pseudoRegex = new RegExp(`:(${complexPseudos.join('|')})$`, 'u'),
|
|
52
|
+
regularRegex = /[[(,>+~]|\s+/u,
|
|
53
|
+
attributeRegex = /^\s*(\w+)\s*(?:([~|^$*!]?=)\s*("[^"]*"|'[^']*'|[^\s[\]]+)(?:\s+(i))?\s*)?\]/u,
|
|
54
|
+
functionRegex = /^(\s*"[^"]*"\s*|\s*'[^']*'\s*|[^()]*)\)/u,
|
|
55
|
+
grouping = new Set([',', '>', '+', '~']),
|
|
56
|
+
combinator = new Set(['>', '+', '~', '']);
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 清理转义符号
|
|
60
|
+
* @param {string} selector
|
|
61
|
+
*/
|
|
62
|
+
const sanitize = selector => {
|
|
63
|
+
for (const [c, escaped] of specialChars) {
|
|
64
|
+
selector = selector.replaceAll(`\\${c}`, escaped);
|
|
65
|
+
}
|
|
66
|
+
return selector;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 还原转义符号
|
|
71
|
+
* @param {string|undefined} selector
|
|
72
|
+
*/
|
|
73
|
+
const desanitize = selector => {
|
|
74
|
+
if (selector === undefined) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
for (const [c, escaped] of specialChars) {
|
|
78
|
+
selector = selector.replaceAll(escaped, c);
|
|
79
|
+
}
|
|
80
|
+
return selector.trim();
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 去除首尾的引号
|
|
85
|
+
* @param {string|undefined} val 属性值或伪选择器函数的参数
|
|
86
|
+
*/
|
|
87
|
+
const deQuote = val => {
|
|
88
|
+
if (val === undefined) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
const quotes = /^(["']).*\1$/u.exec(val)?.[1];
|
|
92
|
+
return quotes ? val.slice(1, -1) : val;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 解析简单伪选择器
|
|
97
|
+
* @param {SelectorArray} step 当前顶部
|
|
98
|
+
* @param {string} str 不含属性和复杂伪选择器的语句
|
|
99
|
+
* @throws `SyntaxError` 非法的选择器
|
|
100
|
+
*/
|
|
101
|
+
const pushSimple = (step, str) => {
|
|
102
|
+
const pieces = str.trim().split(':'),
|
|
103
|
+
// eslint-disable-next-line unicorn/explicit-length-check
|
|
104
|
+
i = pieces.slice(1).findIndex(pseudo => simplePseudos.has(pseudo)) + 1 || pieces.length;
|
|
105
|
+
if (pieces.slice(i).some(pseudo => !simplePseudos.has(pseudo))) {
|
|
106
|
+
throw new SyntaxError(`非法的选择器!\n${str}\n可能需要将':'转义为'\\:'。`);
|
|
107
|
+
}
|
|
108
|
+
step.push(desanitize(pieces.slice(0, i).join(':')), ...pieces.slice(i).map(piece => `:${piece}`));
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 解析选择器
|
|
113
|
+
* @param {string} selector
|
|
114
|
+
* @throws `SyntaxError` 非法的选择器
|
|
115
|
+
*/
|
|
116
|
+
const parseSelector = selector => {
|
|
117
|
+
selector = selector.trim();
|
|
118
|
+
const /** @type {SelectorArray[][]} */ stack = [[[]]];
|
|
119
|
+
let sanitized = sanitize(selector),
|
|
120
|
+
regex = regularRegex,
|
|
121
|
+
mt = regex.exec(sanitized),
|
|
122
|
+
[condition] = stack,
|
|
123
|
+
[step] = condition;
|
|
124
|
+
while (mt) {
|
|
125
|
+
let {0: syntax, index} = mt;
|
|
126
|
+
if (syntax.trim() === '') {
|
|
127
|
+
index += syntax.length;
|
|
128
|
+
const char = sanitized[index];
|
|
129
|
+
syntax = grouping.has(char) ? char : '';
|
|
130
|
+
}
|
|
131
|
+
if (syntax === ',') { // 情形1:并列
|
|
132
|
+
pushSimple(step, sanitized.slice(0, index));
|
|
133
|
+
condition = [[]];
|
|
134
|
+
[step] = condition;
|
|
135
|
+
stack.push(condition);
|
|
136
|
+
} else if (combinator.has(syntax)) { // 情形2:关系
|
|
137
|
+
pushSimple(step, sanitized.slice(0, index));
|
|
138
|
+
if (!step.some(Boolean)) {
|
|
139
|
+
throw new SyntaxError(`非法的选择器!\n${selector}\n可能需要通用选择器'*'。`);
|
|
140
|
+
}
|
|
141
|
+
step.relation = syntax;
|
|
142
|
+
step = [];
|
|
143
|
+
condition.push(step);
|
|
144
|
+
} else if (syntax === '[') { // 情形3:属性开启
|
|
145
|
+
pushSimple(step, sanitized.slice(0, index));
|
|
146
|
+
regex = attributeRegex;
|
|
147
|
+
} else if (syntax.endsWith(']')) { // 情形4:属性闭合
|
|
148
|
+
mt[3] = desanitize(deQuote(mt[3]));
|
|
149
|
+
step.push(mt.slice(1));
|
|
150
|
+
regex = regularRegex;
|
|
151
|
+
} else if (syntax === '(') { // 情形5:伪选择器开启
|
|
152
|
+
const pseudoExec = pseudoRegex.exec(sanitized.slice(0, index));
|
|
153
|
+
if (!pseudoExec) {
|
|
154
|
+
throw new SyntaxError(`非法的选择器!\n${desanitize(sanitized)}\n请检查伪选择器是否存在。`);
|
|
155
|
+
}
|
|
156
|
+
pushSimple(step, sanitized.slice(0, pseudoExec.index));
|
|
157
|
+
step.push(pseudoExec[1]); // 临时存放复杂伪选择器
|
|
158
|
+
regex = functionRegex;
|
|
159
|
+
} else { // 情形6:伪选择器闭合
|
|
160
|
+
const /** @type {pseudo} */ pseudo = step.pop();
|
|
161
|
+
mt.push(pseudo);
|
|
162
|
+
mt[1] = deQuote(mt[1]);
|
|
163
|
+
step.push(mt.slice(1));
|
|
164
|
+
regex = regularRegex;
|
|
165
|
+
}
|
|
166
|
+
sanitized = sanitized.slice(index + syntax.length);
|
|
167
|
+
if (grouping.has(syntax)) {
|
|
168
|
+
sanitized = sanitized.trim();
|
|
169
|
+
}
|
|
170
|
+
mt = regex.exec(sanitized);
|
|
171
|
+
}
|
|
172
|
+
if (regex === regularRegex) {
|
|
173
|
+
pushSimple(step, sanitized);
|
|
174
|
+
return stack;
|
|
175
|
+
}
|
|
176
|
+
throw new SyntaxError(`非法的选择器!\n${selector}\n检测到未闭合的'${regex === attributeRegex ? '[' : '('}'`);
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
Parser.parsers.parseSelector = __filename;
|
|
180
|
+
module.exports = parseSelector;
|
package/parser/table.js
CHANGED
package/src/arg.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {text} = require('../util/string'),
|
|
3
|
+
const {text, noWrap} = require('../util/string'),
|
|
4
4
|
{generateForSelf, generateForChild} = require('../util/lint'),
|
|
5
5
|
Parser = require('..'),
|
|
6
6
|
Token = require('.');
|
|
@@ -24,11 +24,13 @@ class ArgToken extends Token {
|
|
|
24
24
|
*/
|
|
25
25
|
constructor(parts, config = Parser.getConfig(), accum = []) {
|
|
26
26
|
super(undefined, config, true, accum, {
|
|
27
|
+
AtomToken: 0, Token: 1, HiddenToken: '2:',
|
|
27
28
|
});
|
|
28
29
|
for (let i = 0; i < parts.length; i++) {
|
|
29
30
|
if (i === 0 || i > 1) {
|
|
30
31
|
const AtomToken = i === 0 ? require('./atom') : require('./atom/hidden');
|
|
31
32
|
const token = new AtomToken(parts[i], i === 0 ? 'arg-name' : undefined, config, accum, {
|
|
33
|
+
'Stage-2': ':', '!HeadingToken': '',
|
|
32
34
|
});
|
|
33
35
|
super.insertAt(token);
|
|
34
36
|
} else {
|
|
@@ -37,13 +39,15 @@ class ArgToken extends Token {
|
|
|
37
39
|
super.insertAt(token.setAttribute('stage', 2));
|
|
38
40
|
}
|
|
39
41
|
}
|
|
42
|
+
this.getAttribute('protectChildren')(0);
|
|
40
43
|
}
|
|
41
44
|
|
|
42
45
|
/**
|
|
43
46
|
* @override
|
|
47
|
+
* @param {string} selector
|
|
44
48
|
*/
|
|
45
49
|
toString(selector) {
|
|
46
|
-
return `{{{${super.toString(selector, '|')}}}}`;
|
|
50
|
+
return selector && this.matches(selector) ? '' : `{{{${super.toString(selector, '|')}}}}`;
|
|
47
51
|
}
|
|
48
52
|
|
|
49
53
|
/**
|
|
@@ -64,6 +68,11 @@ class ArgToken extends Token {
|
|
|
64
68
|
return 1;
|
|
65
69
|
}
|
|
66
70
|
|
|
71
|
+
/** @override */
|
|
72
|
+
print() {
|
|
73
|
+
return super.print({pre: '{{{', post: '}}}', sep: '|'});
|
|
74
|
+
}
|
|
75
|
+
|
|
67
76
|
/**
|
|
68
77
|
* @override
|
|
69
78
|
* @param {number} start 起始位置
|
|
@@ -88,6 +97,111 @@ class ArgToken extends Token {
|
|
|
88
97
|
}
|
|
89
98
|
return errors;
|
|
90
99
|
}
|
|
100
|
+
|
|
101
|
+
/** @override */
|
|
102
|
+
cloneNode() {
|
|
103
|
+
const [name, ...cloned] = this.cloneChildNodes();
|
|
104
|
+
return Parser.run(() => {
|
|
105
|
+
const token = new ArgToken([''], this.getAttribute('config'));
|
|
106
|
+
token.firstChild.safeReplaceWith(name);
|
|
107
|
+
token.append(...cloned);
|
|
108
|
+
token.afterBuild();
|
|
109
|
+
return token;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** @override */
|
|
114
|
+
afterBuild() {
|
|
115
|
+
this.setAttribute('name', this.firstChild.text().trim());
|
|
116
|
+
const /** @type {import('../typings/event').AstListener} */ argListener = ({prevTarget}) => {
|
|
117
|
+
if (prevTarget === this.firstChild) {
|
|
118
|
+
this.setAttribute('name', prevTarget.text().trim());
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
this.addEventListener(['remove', 'insert', 'replace', 'text'], argListener);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* 移除无效部分
|
|
126
|
+
* @complexity `n`
|
|
127
|
+
*/
|
|
128
|
+
removeRedundant() {
|
|
129
|
+
Parser.run(() => {
|
|
130
|
+
for (let i = this.length - 1; i > 1; i--) {
|
|
131
|
+
super.removeAt(i);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* 移除子节点,且在移除`arg-default`子节点时自动移除全部多余子节点
|
|
138
|
+
* @param {number} i 移除位置
|
|
139
|
+
* @returns {Token}
|
|
140
|
+
*/
|
|
141
|
+
removeAt(i) {
|
|
142
|
+
if (i === 1) {
|
|
143
|
+
this.removeRedundant();
|
|
144
|
+
}
|
|
145
|
+
return super.removeAt(i);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* @override
|
|
150
|
+
* @param {Token} token 待插入的子节点
|
|
151
|
+
* @param {number} i 插入位置
|
|
152
|
+
* @throws `RangeError` 不可插入多余子节点
|
|
153
|
+
*/
|
|
154
|
+
insertAt(token, i = this.length) {
|
|
155
|
+
const j = i < 0 ? i + this.length : i;
|
|
156
|
+
if (j > 1) {
|
|
157
|
+
throw new RangeError(`${this.constructor.name} 不可插入多余的子节点!`);
|
|
158
|
+
}
|
|
159
|
+
super.insertAt(token, i);
|
|
160
|
+
if (j === 1) {
|
|
161
|
+
token.type = 'arg-default';
|
|
162
|
+
}
|
|
163
|
+
return token;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* 设置参数名
|
|
168
|
+
* @param {string} name 新参数名
|
|
169
|
+
* @throws `SyntaxError` 非法的参数名
|
|
170
|
+
*/
|
|
171
|
+
setName(name) {
|
|
172
|
+
name = String(name);
|
|
173
|
+
const root = Parser.parse(`{{{${name}}}}`, this.getAttribute('include'), 2, this.getAttribute('config')),
|
|
174
|
+
{length, firstChild: arg} = root;
|
|
175
|
+
if (length !== 1 || arg.type !== 'arg' || arg.length !== 1) {
|
|
176
|
+
throw new SyntaxError(`非法的参数名称:${noWrap(name)}`);
|
|
177
|
+
}
|
|
178
|
+
const {firstChild} = arg;
|
|
179
|
+
arg.destroy();
|
|
180
|
+
this.firstChild.safeReplaceWith(firstChild);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* 设置预设值
|
|
185
|
+
* @param {string} value 预设值
|
|
186
|
+
* @throws `SyntaxError` 非法的参数预设值
|
|
187
|
+
*/
|
|
188
|
+
setDefault(value) {
|
|
189
|
+
value = String(value);
|
|
190
|
+
const root = Parser.parse(`{{{|${value}}}}`, this.getAttribute('include'), 2, this.getAttribute('config')),
|
|
191
|
+
{length, firstChild: arg} = root;
|
|
192
|
+
if (length !== 1 || arg.type !== 'arg' || arg.length !== 2) {
|
|
193
|
+
throw new SyntaxError(`非法的参数预设值:${noWrap(value)}`);
|
|
194
|
+
}
|
|
195
|
+
const {childNodes: [, oldDefault]} = this,
|
|
196
|
+
{lastChild} = arg;
|
|
197
|
+
arg.destroy();
|
|
198
|
+
if (oldDefault) {
|
|
199
|
+
oldDefault.safeReplaceWith(lastChild);
|
|
200
|
+
} else {
|
|
201
|
+
this.insertAt(lastChild);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
91
204
|
}
|
|
92
205
|
|
|
206
|
+
Parser.classes.ArgToken = __filename;
|
|
93
207
|
module.exports = ArgToken;
|
package/src/atom/hidden.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const hidden = require('../../mixin/hidden'),
|
|
4
|
+
Parser = require('../..'),
|
|
4
5
|
AtomToken = require('.');
|
|
5
6
|
|
|
6
7
|
/** 不可见的节点 */
|
|
@@ -8,4 +9,5 @@ class HiddenToken extends hidden(AtomToken) {
|
|
|
8
9
|
type = 'hidden';
|
|
9
10
|
}
|
|
10
11
|
|
|
12
|
+
Parser.classes.HiddenToken = __filename;
|
|
11
13
|
module.exports = HiddenToken;
|
package/src/atom/index.js
CHANGED
|
@@ -14,6 +14,7 @@ class AtomToken extends Token {
|
|
|
14
14
|
* @param {string} wikitext wikitext
|
|
15
15
|
* @param {string|undefined} type Token.type
|
|
16
16
|
* @param {import('../../typings/token').accum} accum
|
|
17
|
+
* @param {import('../../typings/token').acceptable} acceptable 可接受的子节点设置
|
|
17
18
|
*/
|
|
18
19
|
constructor(wikitext, type, config = Parser.getConfig(), accum = [], acceptable = undefined) {
|
|
19
20
|
super(wikitext, config, true, accum, acceptable);
|
|
@@ -21,6 +22,22 @@ class AtomToken extends Token {
|
|
|
21
22
|
this.type = type;
|
|
22
23
|
}
|
|
23
24
|
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @override
|
|
28
|
+
* @this {AtomToken & {constructor: typeof AtomToken}}
|
|
29
|
+
*/
|
|
30
|
+
cloneNode() {
|
|
31
|
+
const cloned = this.cloneChildNodes(),
|
|
32
|
+
config = this.getAttribute('config'),
|
|
33
|
+
acceptable = this.getAttribute('acceptable');
|
|
34
|
+
return Parser.run(() => {
|
|
35
|
+
const token = new this.constructor(undefined, this.type, config, [], acceptable);
|
|
36
|
+
token.append(...cloned);
|
|
37
|
+
return token;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
24
40
|
}
|
|
25
41
|
|
|
42
|
+
Parser.classes.AtomToken = __filename;
|
|
26
43
|
module.exports = AtomToken;
|
package/src/attribute.js
CHANGED
|
@@ -3,12 +3,16 @@
|
|
|
3
3
|
/** @typedef {import('../typings/token').ParserConfig} ParserConfig */
|
|
4
4
|
|
|
5
5
|
const {generateForChild} = require('../util/lint'),
|
|
6
|
-
{removeComment} = require('../util/string'),
|
|
6
|
+
{noWrap, removeComment} = require('../util/string'),
|
|
7
|
+
fixedToken = require('../mixin/fixedToken'),
|
|
7
8
|
Parser = require('..'),
|
|
8
9
|
Token = require('.'),
|
|
9
10
|
AtomToken = require('./atom');
|
|
10
11
|
|
|
11
|
-
const
|
|
12
|
+
const stages = {'ext-attr': 0, 'html-attr': 2, 'table-attr': 3},
|
|
13
|
+
pre = {'ext-attr': '<pre ', 'html-attr': '<p ', 'table-attr': '{|'},
|
|
14
|
+
post = {'ext-attr': '/>', 'html-attr': '>', 'table-attr': ''},
|
|
15
|
+
commonHtmlAttrs = new Set([
|
|
12
16
|
'id',
|
|
13
17
|
'class',
|
|
14
18
|
'style',
|
|
@@ -107,6 +111,37 @@ const commonHtmlAttrs = new Set([
|
|
|
107
111
|
poll: new Set(['id', 'show-results-before-voting']),
|
|
108
112
|
sm2: typeAttrs,
|
|
109
113
|
flashmp3: typeAttrs,
|
|
114
|
+
score: new Set([
|
|
115
|
+
'line_width_inches',
|
|
116
|
+
'lang',
|
|
117
|
+
'override_midi',
|
|
118
|
+
'raw',
|
|
119
|
+
'note-language',
|
|
120
|
+
'override_audio',
|
|
121
|
+
'override_ogg',
|
|
122
|
+
'sound',
|
|
123
|
+
'vorbis',
|
|
124
|
+
]),
|
|
125
|
+
seo: new Set([
|
|
126
|
+
'title',
|
|
127
|
+
'title_mode',
|
|
128
|
+
'title_separator',
|
|
129
|
+
'keywords',
|
|
130
|
+
'description',
|
|
131
|
+
'robots',
|
|
132
|
+
'google_bot',
|
|
133
|
+
'image',
|
|
134
|
+
'image_width',
|
|
135
|
+
'image_height',
|
|
136
|
+
'image_alt',
|
|
137
|
+
'type',
|
|
138
|
+
'site_name',
|
|
139
|
+
'locale',
|
|
140
|
+
'section',
|
|
141
|
+
'author',
|
|
142
|
+
'published_time',
|
|
143
|
+
'twitter_site',
|
|
144
|
+
]),
|
|
110
145
|
tab: new Set([
|
|
111
146
|
'nested',
|
|
112
147
|
'name',
|
|
@@ -144,7 +179,7 @@ const commonHtmlAttrs = new Set([
|
|
|
144
179
|
* 扩展和HTML标签属性
|
|
145
180
|
* @classdesc `{childNodes: [AtomToken, Token|AtomToken]}`
|
|
146
181
|
*/
|
|
147
|
-
class AttributeToken extends Token {
|
|
182
|
+
class AttributeToken extends fixedToken(Token) {
|
|
148
183
|
#equal;
|
|
149
184
|
#quotes;
|
|
150
185
|
#tag;
|
|
@@ -159,6 +194,10 @@ class AttributeToken extends Token {
|
|
|
159
194
|
return this.getValue();
|
|
160
195
|
}
|
|
161
196
|
|
|
197
|
+
set value(value) {
|
|
198
|
+
this.setValue(value);
|
|
199
|
+
}
|
|
200
|
+
|
|
162
201
|
/** 标签名 */
|
|
163
202
|
get tag() {
|
|
164
203
|
return this.#tag;
|
|
@@ -175,23 +214,36 @@ class AttributeToken extends Token {
|
|
|
175
214
|
*/
|
|
176
215
|
constructor(type, tag, key, equal = '', value = '', quotes = [], config = Parser.getConfig(), accum = []) {
|
|
177
216
|
const keyToken = new AtomToken(key, 'attr-key', config, accum, {
|
|
217
|
+
[type === 'ext-attr' ? 'AstText' : 'Stage-1']: ':', ArgToken: ':', TranscludeToken: ':',
|
|
178
218
|
});
|
|
179
219
|
let valueToken;
|
|
180
220
|
if (key === 'title') {
|
|
181
221
|
valueToken = new Token(value, config, true, accum, {
|
|
222
|
+
[`Stage-${stages[type]}`]: ':', ConverterToken: ':',
|
|
182
223
|
}).setAttribute('type', 'attr-value').setAttribute('stage', Parser.MAX_STAGE - 1);
|
|
183
224
|
} else if (tag === 'gallery' && key === 'caption') {
|
|
184
225
|
/** @type {ParserConfig} */
|
|
185
226
|
const newConfig = {...config, excludes: [...config.excludes, 'quote', 'extLink', 'magicLink', 'list']};
|
|
186
227
|
valueToken = new Token(value, newConfig, true, accum, {
|
|
228
|
+
AstText: ':', LinkToken: ':', FileToken: ':', CategoryToken: ':', ConverterToken: ':',
|
|
187
229
|
}).setAttribute('type', 'attr-value').setAttribute('stage', 5);
|
|
188
230
|
} else if (tag === 'choose' && (key === 'before' || key === 'after')) {
|
|
189
231
|
/** @type {ParserConfig} */
|
|
190
232
|
const newConfig = {...config, excludes: [...config.excludes, 'heading', 'html', 'table', 'hr', 'list']};
|
|
191
233
|
valueToken = new Token(value, newConfig, true, accum, {
|
|
234
|
+
ArgToken: ':',
|
|
235
|
+
TranscludeToken: ':',
|
|
236
|
+
LinkToken: ':',
|
|
237
|
+
FileToken: ':',
|
|
238
|
+
CategoryToken: ':',
|
|
239
|
+
QuoteToken: ':',
|
|
240
|
+
ExtLinkToken: ':',
|
|
241
|
+
MagicLinkToken: ':',
|
|
242
|
+
ConverterToken: ':',
|
|
192
243
|
}).setAttribute('type', 'attr-value').setAttribute('stage', 1);
|
|
193
244
|
} else {
|
|
194
245
|
valueToken = new AtomToken(value, 'attr-value', config, accum, {
|
|
246
|
+
[`Stage-${stages[type]}`]: ':',
|
|
195
247
|
});
|
|
196
248
|
}
|
|
197
249
|
super(undefined, config, true, accum);
|
|
@@ -216,9 +268,13 @@ class AttributeToken extends Token {
|
|
|
216
268
|
|
|
217
269
|
/**
|
|
218
270
|
* @override
|
|
271
|
+
* @param {string} selector
|
|
219
272
|
* @returns {string}
|
|
220
273
|
*/
|
|
221
274
|
toString(selector) {
|
|
275
|
+
if (selector && this.matches(selector)) {
|
|
276
|
+
return '';
|
|
277
|
+
}
|
|
222
278
|
const [quoteStart = '', quoteEnd = ''] = this.#quotes;
|
|
223
279
|
return this.#equal
|
|
224
280
|
? `${super.toString(selector, `${this.#equal}${quoteStart}`)}${quoteEnd}`
|
|
@@ -238,6 +294,12 @@ class AttributeToken extends Token {
|
|
|
238
294
|
return this.#equal ? this.#equal.length + (this.#quotes[0]?.length ?? 0) : 0;
|
|
239
295
|
}
|
|
240
296
|
|
|
297
|
+
/** @override */
|
|
298
|
+
print() {
|
|
299
|
+
const [quoteStart = '', quoteEnd = ''] = this.#quotes;
|
|
300
|
+
return this.#equal ? super.print({sep: `${this.#equal}${quoteStart}`, post: quoteEnd}) : super.print();
|
|
301
|
+
}
|
|
302
|
+
|
|
241
303
|
/**
|
|
242
304
|
* @override
|
|
243
305
|
* @param {number} start 起始位置
|
|
@@ -281,6 +343,130 @@ class AttributeToken extends Token {
|
|
|
281
343
|
}
|
|
282
344
|
return true;
|
|
283
345
|
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* @override
|
|
349
|
+
* @template {string} T
|
|
350
|
+
* @param {T} key 属性键
|
|
351
|
+
* @returns {import('../typings/node').TokenAttribute<T>}
|
|
352
|
+
*/
|
|
353
|
+
getAttribute(key) {
|
|
354
|
+
if (key === 'equal') {
|
|
355
|
+
return this.#equal;
|
|
356
|
+
}
|
|
357
|
+
return key === 'quotes' ? this.#quotes : super.getAttribute(key);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* @override
|
|
362
|
+
* @param {PropertyKey} key 属性键
|
|
363
|
+
*/
|
|
364
|
+
hasAttribute(key) {
|
|
365
|
+
return key === 'equal' || key === 'quotes' || super.hasAttribute(key);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** @override */
|
|
369
|
+
cloneNode() {
|
|
370
|
+
const [key, value] = this.cloneChildNodes(),
|
|
371
|
+
config = this.getAttribute('config');
|
|
372
|
+
return Parser.run(() => {
|
|
373
|
+
const token = new AttributeToken(this.type, this.#tag, '', this.#equal, '', this.#quotes, config);
|
|
374
|
+
token.firstChild.safeReplaceWith(key);
|
|
375
|
+
token.lastChild.safeReplaceWith(value);
|
|
376
|
+
token.afterBuild();
|
|
377
|
+
return token;
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** 转义等号 */
|
|
382
|
+
escape() {
|
|
383
|
+
this.#equal = '{{=}}';
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/** 闭合引号 */
|
|
387
|
+
close() {
|
|
388
|
+
[this.#quotes[1]] = this.#quotes;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* 设置属性值
|
|
393
|
+
* @param {string|boolean} value 参数值
|
|
394
|
+
* @throws `SyntaxError` 非法的标签属性
|
|
395
|
+
*/
|
|
396
|
+
setValue(value) {
|
|
397
|
+
if (value === false) {
|
|
398
|
+
this.remove();
|
|
399
|
+
return;
|
|
400
|
+
} else if (value === true) {
|
|
401
|
+
this.#equal = '';
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
value = String(value);
|
|
405
|
+
const {type} = this,
|
|
406
|
+
key = this.name === 'title' ? 'title' : 'data',
|
|
407
|
+
wikitext = `${pre[type]}${key}="${value}"${post[type]}`,
|
|
408
|
+
root = Parser.parse(wikitext, this.getAttribute('include'), stages[type] + 1, this.getAttribute('config')),
|
|
409
|
+
{length, firstChild: tag} = root;
|
|
410
|
+
let attrs;
|
|
411
|
+
if (length !== 1 || tag.type !== type.slice(0, -5)) {
|
|
412
|
+
throw new SyntaxError(`非法的标签属性:${noWrap(value)}`);
|
|
413
|
+
} else if (type === 'table-attr') {
|
|
414
|
+
if (tag.length !== 2) {
|
|
415
|
+
throw new SyntaxError(`非法的标签属性:${noWrap(value)}`);
|
|
416
|
+
}
|
|
417
|
+
attrs = tag.lastChild;
|
|
418
|
+
} else {
|
|
419
|
+
attrs = tag.firstChild;
|
|
420
|
+
}
|
|
421
|
+
const {length: attrsLength, firstChild} = attrs;
|
|
422
|
+
if (attrsLength !== 1 || firstChild.type !== this.type || firstChild.name !== key) {
|
|
423
|
+
throw new SyntaxError(`非法的标签属性:${noWrap(value)}`);
|
|
424
|
+
}
|
|
425
|
+
const {lastChild} = firstChild;
|
|
426
|
+
firstChild.destroy();
|
|
427
|
+
this.lastChild.safeReplaceWith(lastChild);
|
|
428
|
+
if (this.#quotes[0]) {
|
|
429
|
+
this.close();
|
|
430
|
+
} else {
|
|
431
|
+
this.#quotes = ['"', '"'];
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* 修改属性名
|
|
437
|
+
* @param {string} key 新属性名
|
|
438
|
+
* @throws `Error` title属性不能更名
|
|
439
|
+
* @throws `SyntaxError` 非法的模板参数名
|
|
440
|
+
*/
|
|
441
|
+
rename(key) {
|
|
442
|
+
if (this.name === 'title') {
|
|
443
|
+
throw new Error('title 属性不能更名!');
|
|
444
|
+
}
|
|
445
|
+
key = String(key);
|
|
446
|
+
const {type} = this,
|
|
447
|
+
wikitext = `${pre[type]}${key}${post[type]}`,
|
|
448
|
+
root = Parser.parse(wikitext, this.getAttribute('include'), stages[type] + 1, this.getAttribute('config')),
|
|
449
|
+
{length, firstChild: tag} = root;
|
|
450
|
+
let attrs;
|
|
451
|
+
if (length !== 1 || tag.type !== type.slice(0, -5)) {
|
|
452
|
+
throw new SyntaxError(`非法的标签属性名:${noWrap(key)}`);
|
|
453
|
+
} else if (type === 'table-attr') {
|
|
454
|
+
if (tag.length !== 2) {
|
|
455
|
+
throw new SyntaxError(`非法的标签属性名:${noWrap(key)}`);
|
|
456
|
+
}
|
|
457
|
+
attrs = tag.lastChild;
|
|
458
|
+
} else {
|
|
459
|
+
attrs = tag.firstChild;
|
|
460
|
+
}
|
|
461
|
+
const {length: attrsLength, firstChild: attr} = attrs;
|
|
462
|
+
if (attrsLength !== 1 || attr.type !== this.type || attr.value !== true) {
|
|
463
|
+
throw new SyntaxError(`非法的标签属性名:${noWrap(key)}`);
|
|
464
|
+
}
|
|
465
|
+
const {firstChild} = attr;
|
|
466
|
+
attr.destroy();
|
|
467
|
+
this.firstChild.safeReplaceWith(firstChild);
|
|
468
|
+
}
|
|
284
469
|
}
|
|
285
470
|
|
|
471
|
+
Parser.classes.AttributeToken = __filename;
|
|
286
472
|
module.exports = AttributeToken;
|