wikiparser-node 1.0.0-beta.4 → 1.0.1-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/README.md +10 -9
- package/bundle/bundle.min.js +37 -0
- package/config/llwiki.json +1 -35
- package/config/moegirl.json +1 -44
- package/config/zhwiki.json +1 -466
- package/extensions/dist/base.js +105 -0
- package/extensions/dist/editor.js +172 -0
- package/extensions/dist/highlight.js +46 -0
- package/extensions/dist/lint.js +43 -0
- package/extensions/editor.css +63 -0
- package/extensions/ui.css +119 -0
- package/package.json +29 -16
- package/config/.schema.json +0 -160
- package/dist/index.d.ts +0 -62
- package/dist/index.js +0 -305
- package/dist/internal.d.ts +0 -44
- package/dist/lib/element.d.ts +0 -190
- package/dist/lib/element.js +0 -674
- package/dist/lib/node.d.ts +0 -178
- package/dist/lib/node.js +0 -457
- package/dist/lib/range.d.ts +0 -132
- package/dist/lib/range.js +0 -384
- package/dist/lib/ranges.d.ts +0 -28
- package/dist/lib/ranges.js +0 -125
- package/dist/lib/text.d.ts +0 -78
- package/dist/lib/text.js +0 -227
- package/dist/lib/title.d.ts +0 -34
- package/dist/lib/title.js +0 -111
- package/dist/mixin/attributesParent.js +0 -93
- package/dist/mixin/fixed.js +0 -32
- package/dist/mixin/hidden.js +0 -23
- package/dist/mixin/singleLine.js +0 -26
- package/dist/mixin/sol.js +0 -44
- package/dist/parser/braces.js +0 -120
- package/dist/parser/commentAndExt.js +0 -64
- package/dist/parser/converter.js +0 -39
- package/dist/parser/externalLinks.js +0 -27
- package/dist/parser/hrAndDoubleUnderscore.js +0 -37
- package/dist/parser/html.js +0 -35
- package/dist/parser/links.js +0 -93
- package/dist/parser/list.js +0 -58
- package/dist/parser/magicLinks.js +0 -39
- package/dist/parser/quotes.js +0 -66
- package/dist/parser/selector.js +0 -162
- package/dist/parser/table.js +0 -108
- package/dist/src/arg.d.ts +0 -78
- package/dist/src/arg.js +0 -203
- package/dist/src/atom.d.ts +0 -12
- package/dist/src/atom.js +0 -24
- package/dist/src/attribute.d.ts +0 -107
- package/dist/src/attribute.js +0 -446
- package/dist/src/attributes.d.ts +0 -120
- package/dist/src/attributes.js +0 -360
- package/dist/src/converter.d.ts +0 -76
- package/dist/src/converter.js +0 -134
- package/dist/src/converterFlags.d.ts +0 -108
- package/dist/src/converterFlags.js +0 -237
- package/dist/src/converterRule.d.ts +0 -97
- package/dist/src/converterRule.js +0 -253
- package/dist/src/extLink.d.ts +0 -64
- package/dist/src/extLink.js +0 -156
- package/dist/src/gallery.d.ts +0 -72
- package/dist/src/gallery.js +0 -133
- package/dist/src/heading.d.ts +0 -84
- package/dist/src/heading.js +0 -134
- package/dist/src/hidden.d.ts +0 -20
- package/dist/src/hidden.js +0 -26
- package/dist/src/html.d.ts +0 -115
- package/dist/src/html.js +0 -247
- package/dist/src/imageParameter.d.ts +0 -83
- package/dist/src/imageParameter.js +0 -255
- package/dist/src/imagemap.d.ts +0 -79
- package/dist/src/imagemap.js +0 -164
- package/dist/src/imagemapLink.d.ts +0 -49
- package/dist/src/imagemapLink.js +0 -39
- package/dist/src/index.d.ts +0 -157
- package/dist/src/index.js +0 -818
- package/dist/src/link/base.d.ts +0 -70
- package/dist/src/link/base.js +0 -229
- package/dist/src/link/category.d.ts +0 -14
- package/dist/src/link/category.js +0 -29
- package/dist/src/link/file.d.ts +0 -106
- package/dist/src/link/file.js +0 -271
- package/dist/src/link/galleryImage.d.ts +0 -37
- package/dist/src/link/galleryImage.js +0 -101
- package/dist/src/link/index.d.ts +0 -50
- package/dist/src/link/index.js +0 -136
- package/dist/src/magicLink.d.ts +0 -47
- package/dist/src/magicLink.js +0 -130
- package/dist/src/nested.d.ts +0 -49
- package/dist/src/nested.js +0 -88
- package/dist/src/nowiki/base.d.ts +0 -39
- package/dist/src/nowiki/base.js +0 -31
- package/dist/src/nowiki/comment.d.ts +0 -43
- package/dist/src/nowiki/comment.js +0 -62
- package/dist/src/nowiki/dd.d.ts +0 -22
- package/dist/src/nowiki/dd.js +0 -47
- package/dist/src/nowiki/doubleUnderscore.d.ts +0 -37
- package/dist/src/nowiki/doubleUnderscore.js +0 -48
- package/dist/src/nowiki/hr.d.ts +0 -24
- package/dist/src/nowiki/hr.js +0 -29
- package/dist/src/nowiki/index.d.ts +0 -20
- package/dist/src/nowiki/index.js +0 -24
- package/dist/src/nowiki/list.d.ts +0 -16
- package/dist/src/nowiki/list.js +0 -14
- package/dist/src/nowiki/noinclude.d.ts +0 -21
- package/dist/src/nowiki/noinclude.js +0 -25
- package/dist/src/nowiki/quote.d.ts +0 -20
- package/dist/src/nowiki/quote.js +0 -66
- package/dist/src/onlyinclude.d.ts +0 -39
- package/dist/src/onlyinclude.js +0 -66
- package/dist/src/paramTag/index.d.ts +0 -52
- package/dist/src/paramTag/index.js +0 -78
- package/dist/src/paramTag/inputbox.d.ts +0 -8
- package/dist/src/paramTag/inputbox.js +0 -22
- package/dist/src/parameter.d.ts +0 -96
- package/dist/src/parameter.js +0 -203
- package/dist/src/pre.d.ts +0 -29
- package/dist/src/pre.js +0 -49
- package/dist/src/syntax.d.ts +0 -23
- package/dist/src/syntax.js +0 -67
- package/dist/src/table/base.d.ts +0 -61
- package/dist/src/table/base.js +0 -78
- package/dist/src/table/index.d.ts +0 -248
- package/dist/src/table/index.js +0 -782
- package/dist/src/table/td.d.ts +0 -121
- package/dist/src/table/td.js +0 -267
- package/dist/src/table/tr.d.ts +0 -32
- package/dist/src/table/tr.js +0 -51
- package/dist/src/table/trBase.d.ts +0 -62
- package/dist/src/table/trBase.js +0 -157
- package/dist/src/tagPair/ext.d.ts +0 -55
- package/dist/src/tagPair/ext.js +0 -138
- package/dist/src/tagPair/include.d.ts +0 -43
- package/dist/src/tagPair/include.js +0 -42
- package/dist/src/tagPair/index.d.ts +0 -57
- package/dist/src/tagPair/index.js +0 -107
- package/dist/src/transclude.d.ts +0 -201
- package/dist/src/transclude.js +0 -727
- package/dist/util/debug.js +0 -42
- package/dist/util/diff.js +0 -71
- package/dist/util/lint.js +0 -48
- package/dist/util/string.js +0 -75
- package/errors/README +0 -1
- package/printed/README +0 -1
package/dist/lib/element.js
DELETED
|
@@ -1,674 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AstElement = void 0;
|
|
4
|
-
const fs = require("fs");
|
|
5
|
-
const path = require("path");
|
|
6
|
-
const string_1 = require("../util/string");
|
|
7
|
-
const selector_1 = require("../parser/selector");
|
|
8
|
-
const ranges_1 = require("./ranges");
|
|
9
|
-
const title_1 = require("./title");
|
|
10
|
-
const Parser = require("../index");
|
|
11
|
-
const node_1 = require("./node");
|
|
12
|
-
const lintIgnoredExt = new Set([
|
|
13
|
-
'nowiki',
|
|
14
|
-
'pre',
|
|
15
|
-
'charinsert',
|
|
16
|
-
'score',
|
|
17
|
-
'syntaxhighlight',
|
|
18
|
-
'source',
|
|
19
|
-
'math',
|
|
20
|
-
'chem',
|
|
21
|
-
'ce',
|
|
22
|
-
'graph',
|
|
23
|
-
'mapframe',
|
|
24
|
-
'maplink',
|
|
25
|
-
'quiz',
|
|
26
|
-
'templatedata',
|
|
27
|
-
'timeline',
|
|
28
|
-
]);
|
|
29
|
-
/**
|
|
30
|
-
* 检查某个下标是否符合表达式
|
|
31
|
-
* @param str 表达式
|
|
32
|
-
* @param i 待检查的下标
|
|
33
|
-
*/
|
|
34
|
-
const nth = (str, i) => new ranges_1.Ranges(str.split(',')).applyTo(i + 1).includes(i);
|
|
35
|
-
/**
|
|
36
|
-
* 检测:lang()伪选择器
|
|
37
|
-
* @param node 节点
|
|
38
|
-
* @param node.attributes 节点属性
|
|
39
|
-
* @param regex 语言正则
|
|
40
|
-
*/
|
|
41
|
-
const matchesLang = ({ attributes }, regex) => {
|
|
42
|
-
const lang = attributes?.['lang'];
|
|
43
|
-
return typeof lang === 'string' && regex.test(lang);
|
|
44
|
-
};
|
|
45
|
-
/** 类似HTMLElement */
|
|
46
|
-
class AstElement extends node_1.AstNode {
|
|
47
|
-
/** @browser */
|
|
48
|
-
name;
|
|
49
|
-
/**
|
|
50
|
-
* 子节点总数
|
|
51
|
-
* @browser
|
|
52
|
-
*/
|
|
53
|
-
get length() {
|
|
54
|
-
return this.childNodes.length;
|
|
55
|
-
}
|
|
56
|
-
/** 全部非文本子节点 */
|
|
57
|
-
get children() {
|
|
58
|
-
return this.childNodes.filter(({ type }) => type !== 'text');
|
|
59
|
-
}
|
|
60
|
-
/** 首位非文本子节点 */
|
|
61
|
-
get firstElementChild() {
|
|
62
|
-
return this.childNodes.find(({ type }) => type !== 'text');
|
|
63
|
-
}
|
|
64
|
-
/** 末位非文本子节点 */
|
|
65
|
-
get lastElementChild() {
|
|
66
|
-
return this.children.at(-1);
|
|
67
|
-
}
|
|
68
|
-
/** 非文本子节点总数 */
|
|
69
|
-
get childElementCount() {
|
|
70
|
-
return this.children.length;
|
|
71
|
-
}
|
|
72
|
-
/** 父节点 */
|
|
73
|
-
get parentElement() {
|
|
74
|
-
return this.parentNode;
|
|
75
|
-
}
|
|
76
|
-
/** AstElement.prototype.text()的getter写法 */
|
|
77
|
-
get outerText() {
|
|
78
|
-
return this.text();
|
|
79
|
-
}
|
|
80
|
-
/** 不可见 */
|
|
81
|
-
get hidden() {
|
|
82
|
-
return this.text() === '';
|
|
83
|
-
}
|
|
84
|
-
/** 后一个可见的兄弟节点 */
|
|
85
|
-
get nextVisibleSibling() {
|
|
86
|
-
let { nextSibling } = this;
|
|
87
|
-
while (nextSibling?.text() === '') {
|
|
88
|
-
({ nextSibling } = nextSibling);
|
|
89
|
-
}
|
|
90
|
-
return nextSibling;
|
|
91
|
-
}
|
|
92
|
-
/** 前一个可见的兄弟节点 */
|
|
93
|
-
get previousVisibleSibling() {
|
|
94
|
-
let { previousSibling } = this;
|
|
95
|
-
while (previousSibling?.text() === '') {
|
|
96
|
-
({ previousSibling } = previousSibling);
|
|
97
|
-
}
|
|
98
|
-
return previousSibling;
|
|
99
|
-
}
|
|
100
|
-
/** 内部高度 */
|
|
101
|
-
get clientHeight() {
|
|
102
|
-
const { innerText } = this;
|
|
103
|
-
return typeof innerText === 'string' ? innerText.split('\n').length : undefined;
|
|
104
|
-
}
|
|
105
|
-
/** 内部宽度 */
|
|
106
|
-
get clientWidth() {
|
|
107
|
-
const { innerText } = this;
|
|
108
|
-
return typeof innerText === 'string' ? innerText.split('\n').at(-1).length : undefined;
|
|
109
|
-
}
|
|
110
|
-
constructor() {
|
|
111
|
-
super();
|
|
112
|
-
this.seal('name');
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* 可见部分
|
|
116
|
-
* @browser
|
|
117
|
-
* @param separator 子节点间的连接符
|
|
118
|
-
*/
|
|
119
|
-
text(separator) {
|
|
120
|
-
return (0, string_1.text)(this.childNodes, separator);
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* 合并相邻的文本子节点
|
|
124
|
-
* @browser
|
|
125
|
-
*/
|
|
126
|
-
normalize() {
|
|
127
|
-
const childNodes = [...this.childNodes];
|
|
128
|
-
for (let i = childNodes.length - 1; i >= 0; i--) {
|
|
129
|
-
const cur = childNodes[i], prev = childNodes[i - 1];
|
|
130
|
-
if (cur.type !== 'text' || this.getGaps(i - 1)) {
|
|
131
|
-
//
|
|
132
|
-
}
|
|
133
|
-
else if (cur.data === '') {
|
|
134
|
-
childNodes.splice(i, 1);
|
|
135
|
-
}
|
|
136
|
-
else if (prev?.type === 'text') {
|
|
137
|
-
prev.setAttribute('data', prev.data + cur.data);
|
|
138
|
-
childNodes.splice(i, 1);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
this.setAttribute('childNodes', childNodes);
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* 移除子节点
|
|
145
|
-
* @browser
|
|
146
|
-
* @param i 移除位置
|
|
147
|
-
*/
|
|
148
|
-
removeAt(i) {
|
|
149
|
-
this.verifyChild(i);
|
|
150
|
-
const childNodes = [...this.childNodes], e = new Event('remove', { bubbles: true }), [node] = childNodes.splice(i, 1);
|
|
151
|
-
node.setAttribute('parentNode', undefined);
|
|
152
|
-
this.setAttribute('childNodes', childNodes);
|
|
153
|
-
this.dispatchEvent(e, { position: i, removed: node });
|
|
154
|
-
return node;
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* 插入子节点
|
|
158
|
-
* @browser
|
|
159
|
-
* @param node 待插入的子节点
|
|
160
|
-
* @param i 插入位置
|
|
161
|
-
* @throws `RangeError` 不能插入祖先节点
|
|
162
|
-
*/
|
|
163
|
-
insertAt(node, i = this.length) {
|
|
164
|
-
if (node.contains(this)) {
|
|
165
|
-
Parser.error('不能插入祖先节点!', node);
|
|
166
|
-
throw new RangeError('不能插入祖先节点!');
|
|
167
|
-
}
|
|
168
|
-
this.verifyChild(i, 1);
|
|
169
|
-
const childNodes = [...this.childNodes], e = new Event('insert', { bubbles: true }), j = Parser.running ? -1 : childNodes.indexOf(node);
|
|
170
|
-
if (j === -1) {
|
|
171
|
-
node.parentNode?.removeChild(node);
|
|
172
|
-
node.setAttribute('parentNode', this);
|
|
173
|
-
}
|
|
174
|
-
else {
|
|
175
|
-
childNodes.splice(j, 1);
|
|
176
|
-
}
|
|
177
|
-
childNodes.splice(i, 0, node);
|
|
178
|
-
this.setAttribute('childNodes', childNodes);
|
|
179
|
-
this.dispatchEvent(e, { position: i < 0 ? i + this.length - 1 : i, inserted: node });
|
|
180
|
-
return node;
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* 最近的祖先节点
|
|
184
|
-
* @browser
|
|
185
|
-
* @param selector 选择器
|
|
186
|
-
*/
|
|
187
|
-
closest(selector) {
|
|
188
|
-
let { parentNode } = this, condition;
|
|
189
|
-
if (/[^a-z\-,\s]/u.test(selector)) {
|
|
190
|
-
const stack = (0, selector_1.parseSelector)(selector);
|
|
191
|
-
condition = /** @implements */ (token) => token.#matchesStack(stack);
|
|
192
|
-
}
|
|
193
|
-
else {
|
|
194
|
-
const types = new Set(selector.split(',').map(str => str.trim()));
|
|
195
|
-
condition = /** @implements */ ({ type }) => types.has(type);
|
|
196
|
-
}
|
|
197
|
-
while (parentNode) {
|
|
198
|
-
if (condition(parentNode)) {
|
|
199
|
-
return parentNode;
|
|
200
|
-
}
|
|
201
|
-
({ parentNode } = parentNode);
|
|
202
|
-
}
|
|
203
|
-
return undefined;
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* 在末尾批量插入子节点
|
|
207
|
-
* @browser
|
|
208
|
-
* @param elements 插入节点
|
|
209
|
-
*/
|
|
210
|
-
append(...elements) {
|
|
211
|
-
for (const element of elements) {
|
|
212
|
-
this.insertAt(element);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* 批量替换子节点
|
|
217
|
-
* @browser
|
|
218
|
-
* @param elements 新的子节点
|
|
219
|
-
*/
|
|
220
|
-
replaceChildren(...elements) {
|
|
221
|
-
for (let i = this.length - 1; i >= 0; i--) {
|
|
222
|
-
this.removeAt(i);
|
|
223
|
-
}
|
|
224
|
-
this.append(...elements);
|
|
225
|
-
}
|
|
226
|
-
/**
|
|
227
|
-
* 修改文本子节点
|
|
228
|
-
* @browser
|
|
229
|
-
* @param str 新文本
|
|
230
|
-
* @param i 子节点位置
|
|
231
|
-
* @throws `RangeError` 对应位置的子节点不是文本节点
|
|
232
|
-
*/
|
|
233
|
-
setText(str, i = 0) {
|
|
234
|
-
this.verifyChild(i);
|
|
235
|
-
const oldText = this.childNodes.at(i), { type, constructor: { name } } = oldText;
|
|
236
|
-
if (type === 'text') {
|
|
237
|
-
const { data } = oldText;
|
|
238
|
-
oldText.replaceData(str);
|
|
239
|
-
return data;
|
|
240
|
-
}
|
|
241
|
-
throw new RangeError(`第 ${i} 个子节点是 ${name}!`);
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* 还原为wikitext
|
|
245
|
-
* @browser
|
|
246
|
-
* @param omit 忽略的节点类型
|
|
247
|
-
* @param separator 子节点间的连接符
|
|
248
|
-
*/
|
|
249
|
-
toString(omit, separator = '') {
|
|
250
|
-
return omit && this.matchesTypes(omit)
|
|
251
|
-
? ''
|
|
252
|
-
: this.childNodes.map(child => child.toString(omit)).join(separator);
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Linter
|
|
256
|
-
* @browser
|
|
257
|
-
* @param start
|
|
258
|
-
*/
|
|
259
|
-
lint(start = this.getAbsoluteIndex()) {
|
|
260
|
-
const { SyntaxToken } = require('../src/syntax');
|
|
261
|
-
if (this instanceof SyntaxToken || this.constructor.hidden
|
|
262
|
-
|| this.type === 'ext-inner' && lintIgnoredExt.has(this.name)) {
|
|
263
|
-
return [];
|
|
264
|
-
}
|
|
265
|
-
const errors = [];
|
|
266
|
-
for (let i = 0, cur = start + this.getPadding(); i < this.length; i++) {
|
|
267
|
-
const child = this.childNodes[i];
|
|
268
|
-
errors.push(...child.lint(cur));
|
|
269
|
-
cur += String(child).length + this.getGaps(i);
|
|
270
|
-
}
|
|
271
|
-
return errors;
|
|
272
|
-
}
|
|
273
|
-
/** @private */
|
|
274
|
-
matchesTypes(types) {
|
|
275
|
-
return types.has(this.type);
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* 以HTML格式打印
|
|
279
|
-
* @browser
|
|
280
|
-
* @param opt 选项
|
|
281
|
-
*/
|
|
282
|
-
print(opt = {}) {
|
|
283
|
-
return String(this) ? `<span class="wpb-${opt.class ?? this.type}">${(0, string_1.print)(this.childNodes, opt)}</span>` : '';
|
|
284
|
-
}
|
|
285
|
-
/**
|
|
286
|
-
* 保存为JSON
|
|
287
|
-
* @param file 文件名
|
|
288
|
-
*/
|
|
289
|
-
json(file) {
|
|
290
|
-
const json = {
|
|
291
|
-
...this,
|
|
292
|
-
childNodes: this.childNodes.map(child => child.type === 'text' ? child.data : child.json()),
|
|
293
|
-
};
|
|
294
|
-
if (typeof file === 'string') {
|
|
295
|
-
fs.writeFileSync(path.join(__dirname.slice(0, -4), '..', 'printed', `${file}${file.endsWith('.json') ? '' : '.json'}`), JSON.stringify(json, null, 2));
|
|
296
|
-
}
|
|
297
|
-
return json;
|
|
298
|
-
}
|
|
299
|
-
/** 销毁 */
|
|
300
|
-
destroy() {
|
|
301
|
-
this.parentNode?.destroy();
|
|
302
|
-
for (const child of this.childNodes) {
|
|
303
|
-
child.setAttribute('parentNode', undefined);
|
|
304
|
-
}
|
|
305
|
-
Object.setPrototypeOf(this, null);
|
|
306
|
-
}
|
|
307
|
-
/** 是否受保护。保护条件来自Token,这里仅提前用于:required和:optional伪选择器。 */
|
|
308
|
-
#isProtected() {
|
|
309
|
-
const { parentNode } = this;
|
|
310
|
-
if (!parentNode) {
|
|
311
|
-
return undefined;
|
|
312
|
-
}
|
|
313
|
-
const { childNodes, fixed } = parentNode, protectedIndices = parentNode.getAttribute('protectedChildren').applyTo(childNodes);
|
|
314
|
-
return fixed || protectedIndices.includes(childNodes.indexOf(this));
|
|
315
|
-
}
|
|
316
|
-
/** @private */
|
|
317
|
-
matchesAttr(key, equal, val = '', i) {
|
|
318
|
-
if (!equal) {
|
|
319
|
-
return this.hasAttribute(key);
|
|
320
|
-
}
|
|
321
|
-
else if (!this.hasAttribute(key)) {
|
|
322
|
-
return equal === '!=';
|
|
323
|
-
}
|
|
324
|
-
const v = (0, string_1.toCase)(val, i);
|
|
325
|
-
let thisVal = this.getAttribute(key);
|
|
326
|
-
if (thisVal instanceof RegExp) {
|
|
327
|
-
thisVal = thisVal.source;
|
|
328
|
-
}
|
|
329
|
-
if (equal === '~=') {
|
|
330
|
-
const thisVals = typeof thisVal === 'string' ? thisVal.split(/\s/u) : thisVal;
|
|
331
|
-
// @ts-expect-error noImplicitAny
|
|
332
|
-
return Boolean(thisVals?.[Symbol.iterator])
|
|
333
|
-
// @ts-expect-error spread unknown
|
|
334
|
-
&& [...thisVals].some(w => typeof w === 'string' && (0, string_1.toCase)(w, i) === v);
|
|
335
|
-
}
|
|
336
|
-
else if (typeof thisVal !== 'string') {
|
|
337
|
-
throw new RangeError(`复杂属性 ${key} 不能用于选择器!`);
|
|
338
|
-
}
|
|
339
|
-
const stringVal = (0, string_1.toCase)(thisVal, i);
|
|
340
|
-
switch (equal) {
|
|
341
|
-
case '|=':
|
|
342
|
-
return stringVal === v || stringVal.startsWith(`${v}-`);
|
|
343
|
-
case '^=':
|
|
344
|
-
return stringVal.startsWith(v);
|
|
345
|
-
case '$=':
|
|
346
|
-
return stringVal.endsWith(v);
|
|
347
|
-
case '*=':
|
|
348
|
-
return stringVal.includes(v);
|
|
349
|
-
case '!=':
|
|
350
|
-
return stringVal !== v;
|
|
351
|
-
default: // `=`
|
|
352
|
-
return stringVal === v;
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
/**
|
|
356
|
-
* 检查是否符合解析后的选择器,不含节点关系
|
|
357
|
-
* @param step 解析后的选择器
|
|
358
|
-
* @throws `SyntaxError` 错误的正则伪选择器
|
|
359
|
-
* @throws `SyntaxError` 未定义的伪选择器
|
|
360
|
-
*/
|
|
361
|
-
#matches(step) {
|
|
362
|
-
const { parentNode, type, name, childNodes, link, fixed, constructor: { name: tokenName }, } = this, children = parentNode?.children, childrenOfType = children?.filter(({ type: t }) => t === type), siblingsCount = children?.length ?? 1, siblingsCountOfType = childrenOfType?.length ?? 1, index = (children?.indexOf(this) ?? 0) + 1, indexOfType = (childrenOfType?.indexOf(this) ?? 0) + 1, lastIndex = siblingsCount - index + 1, lastIndexOfType = siblingsCountOfType - indexOfType + 1;
|
|
363
|
-
return step.every(selector => {
|
|
364
|
-
if (typeof selector === 'string') {
|
|
365
|
-
switch (selector) { // 情形1:简单伪选择器、type和name
|
|
366
|
-
case '*':
|
|
367
|
-
return true;
|
|
368
|
-
case ':root':
|
|
369
|
-
return !parentNode;
|
|
370
|
-
case ':first-child':
|
|
371
|
-
return index === 1;
|
|
372
|
-
case ':first-of-type':
|
|
373
|
-
return indexOfType === 1;
|
|
374
|
-
case ':last-child':
|
|
375
|
-
return lastIndex === 1;
|
|
376
|
-
case ':last-of-type':
|
|
377
|
-
return lastIndexOfType === 1;
|
|
378
|
-
case ':only-child':
|
|
379
|
-
return siblingsCount === 1;
|
|
380
|
-
case ':only-of-type':
|
|
381
|
-
return siblingsCountOfType === 1;
|
|
382
|
-
case ':empty':
|
|
383
|
-
return !childNodes.some(child => child.type !== 'text' || child.data);
|
|
384
|
-
case ':parent':
|
|
385
|
-
return childNodes.some(child => child.type !== 'text' || child.data);
|
|
386
|
-
case ':header':
|
|
387
|
-
return type === 'heading';
|
|
388
|
-
case ':hidden':
|
|
389
|
-
return this.text() === '';
|
|
390
|
-
case ':visible':
|
|
391
|
-
return this.text() !== '';
|
|
392
|
-
case ':only-whitespace':
|
|
393
|
-
return this.text().trim() === '';
|
|
394
|
-
case ':any-link':
|
|
395
|
-
return type === 'link' || type === 'free-ext-link' || type === 'ext-link'
|
|
396
|
-
|| (type === 'file' || type === 'gallery-image' && link);
|
|
397
|
-
case ':local-link':
|
|
398
|
-
return (type === 'link' || type === 'file' || type === 'gallery-image')
|
|
399
|
-
&& link instanceof title_1.Title && link.title === '';
|
|
400
|
-
case ':read-only':
|
|
401
|
-
return fixed;
|
|
402
|
-
case ':read-write':
|
|
403
|
-
return !fixed;
|
|
404
|
-
case ':invalid':
|
|
405
|
-
return type === 'table-inter' || tokenName === 'HiddenToken';
|
|
406
|
-
case ':required':
|
|
407
|
-
return this.#isProtected() === true;
|
|
408
|
-
case ':optional':
|
|
409
|
-
return this.#isProtected() === false;
|
|
410
|
-
default: {
|
|
411
|
-
const [t, n] = selector.split('#');
|
|
412
|
-
return (!t || t === type || Boolean(Parser.typeAliases[type]?.includes(t)))
|
|
413
|
-
&& (!n || n === name);
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
else if (selector.length === 4) { // 情形2:属性选择器
|
|
418
|
-
return this.matchesAttr(...selector);
|
|
419
|
-
}
|
|
420
|
-
const [s, pseudo] = selector; // 情形3:复杂伪选择器
|
|
421
|
-
switch (pseudo) {
|
|
422
|
-
case 'is':
|
|
423
|
-
return this.matches(s);
|
|
424
|
-
case 'not':
|
|
425
|
-
return !this.matches(s);
|
|
426
|
-
case 'nth-child':
|
|
427
|
-
return nth(s, index);
|
|
428
|
-
case 'nth-of-type':
|
|
429
|
-
return nth(s, indexOfType);
|
|
430
|
-
case 'nth-last-child':
|
|
431
|
-
return nth(s, lastIndex);
|
|
432
|
-
case 'nth-last-of-type':
|
|
433
|
-
return nth(s, lastIndexOfType);
|
|
434
|
-
case 'contains':
|
|
435
|
-
return this.text().includes(s);
|
|
436
|
-
case 'has':
|
|
437
|
-
return Boolean(this.querySelector(s));
|
|
438
|
-
case 'lang': {
|
|
439
|
-
const regex = new RegExp(`^${s}(?:-|$)`, 'u');
|
|
440
|
-
return matchesLang(this, regex)
|
|
441
|
-
|| this.getAncestors().some(ancestor => matchesLang(ancestor, regex));
|
|
442
|
-
}
|
|
443
|
-
case 'regex': {
|
|
444
|
-
const mt = /^([^,]+),\s*\/(.+)\/([a-z]*)$/u.exec(s);
|
|
445
|
-
if (!mt) {
|
|
446
|
-
throw new SyntaxError('错误的伪选择器用法。请使用形如 ":regex(\'attr, /re/i\')" 的格式。');
|
|
447
|
-
}
|
|
448
|
-
try {
|
|
449
|
-
return new RegExp(mt[2], mt[3]).test(String(this.getAttribute(mt[1].trim())));
|
|
450
|
-
}
|
|
451
|
-
catch {
|
|
452
|
-
throw new SyntaxError(`错误的正则表达式:/${mt[2]}/${mt[3]}`);
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
default:
|
|
456
|
-
throw new SyntaxError(`未定义的伪选择器:${pseudo}`);
|
|
457
|
-
}
|
|
458
|
-
});
|
|
459
|
-
}
|
|
460
|
-
/**
|
|
461
|
-
* 检查是否符合解析后的选择器
|
|
462
|
-
* @param condition 解析后的选择器
|
|
463
|
-
*/
|
|
464
|
-
#matchesArray(condition) {
|
|
465
|
-
const step = condition.pop();
|
|
466
|
-
if (this.#matches(step)) {
|
|
467
|
-
const { parentNode, previousElementSibling } = this;
|
|
468
|
-
switch (condition.at(-1)?.relation) {
|
|
469
|
-
case undefined:
|
|
470
|
-
return true;
|
|
471
|
-
case '>':
|
|
472
|
-
return Boolean(parentNode && parentNode.#matchesArray(condition));
|
|
473
|
-
case '+':
|
|
474
|
-
return Boolean(previousElementSibling && previousElementSibling.#matchesArray(condition));
|
|
475
|
-
case '~': {
|
|
476
|
-
if (!parentNode) {
|
|
477
|
-
return false;
|
|
478
|
-
}
|
|
479
|
-
const { children } = parentNode, i = children.indexOf(this);
|
|
480
|
-
return children.slice(0, i).some(child => child.#matchesArray(condition));
|
|
481
|
-
}
|
|
482
|
-
default: // ' '
|
|
483
|
-
return this.getAncestors().some(ancestor => ancestor.#matchesArray(condition));
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
return false;
|
|
487
|
-
}
|
|
488
|
-
/**
|
|
489
|
-
* 检查是否符合解析后的组合选择器
|
|
490
|
-
* @param stack 解析后的一组选择器
|
|
491
|
-
*/
|
|
492
|
-
#matchesStack(stack) {
|
|
493
|
-
return stack.some(condition => this.#matchesArray([...condition]));
|
|
494
|
-
}
|
|
495
|
-
/**
|
|
496
|
-
* 检查是否符合选择器
|
|
497
|
-
* @param selector 选择器
|
|
498
|
-
*/
|
|
499
|
-
matches(selector) {
|
|
500
|
-
return selector === undefined || this.#matchesStack((0, selector_1.parseSelector)(selector));
|
|
501
|
-
}
|
|
502
|
-
/**
|
|
503
|
-
* 符合条件的第一个后代节点
|
|
504
|
-
* @param condition 条件
|
|
505
|
-
*/
|
|
506
|
-
#getElementBy(condition) {
|
|
507
|
-
for (const child of this.children) {
|
|
508
|
-
if (condition(child)) {
|
|
509
|
-
return child;
|
|
510
|
-
}
|
|
511
|
-
const descendant = child.#getElementBy(condition);
|
|
512
|
-
if (descendant) {
|
|
513
|
-
return descendant;
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
return undefined;
|
|
517
|
-
}
|
|
518
|
-
/**
|
|
519
|
-
* 符合选择器的第一个后代节点
|
|
520
|
-
* @param selector 选择器
|
|
521
|
-
*/
|
|
522
|
-
querySelector(selector) {
|
|
523
|
-
const stack = (0, selector_1.parseSelector)(selector);
|
|
524
|
-
return this.#getElementBy(token => token.#matchesStack(stack));
|
|
525
|
-
}
|
|
526
|
-
/**
|
|
527
|
-
* 类型选择器
|
|
528
|
-
* @param types
|
|
529
|
-
*/
|
|
530
|
-
getElementByTypes(types) {
|
|
531
|
-
const typeSet = new Set(types.split(',').map(str => str.trim()));
|
|
532
|
-
return this.#getElementBy(({ type }) => typeSet.has(type));
|
|
533
|
-
}
|
|
534
|
-
/**
|
|
535
|
-
* id选择器
|
|
536
|
-
* @param id id名
|
|
537
|
-
*/
|
|
538
|
-
getElementById(id) {
|
|
539
|
-
return this.#getElementBy(token => 'id' in token && token.id === id);
|
|
540
|
-
}
|
|
541
|
-
/**
|
|
542
|
-
* 符合条件的所有后代节点
|
|
543
|
-
* @param condition 条件
|
|
544
|
-
*/
|
|
545
|
-
#getElementsBy(condition) {
|
|
546
|
-
const descendants = [];
|
|
547
|
-
for (const child of this.children) {
|
|
548
|
-
if (condition(child)) {
|
|
549
|
-
descendants.push(child);
|
|
550
|
-
}
|
|
551
|
-
descendants.push(...child.#getElementsBy(condition));
|
|
552
|
-
}
|
|
553
|
-
return descendants;
|
|
554
|
-
}
|
|
555
|
-
/**
|
|
556
|
-
* 符合选择器的所有后代节点
|
|
557
|
-
* @param selector 选择器
|
|
558
|
-
*/
|
|
559
|
-
querySelectorAll(selector) {
|
|
560
|
-
const stack = (0, selector_1.parseSelector)(selector);
|
|
561
|
-
return this.#getElementsBy(token => token.#matchesStack(stack));
|
|
562
|
-
}
|
|
563
|
-
/**
|
|
564
|
-
* 类选择器
|
|
565
|
-
* @param className 类名之一
|
|
566
|
-
*/
|
|
567
|
-
getElementsByClassName(className) {
|
|
568
|
-
return this.#getElementsBy(token => 'classList' in token && token.classList.has(className));
|
|
569
|
-
}
|
|
570
|
-
/**
|
|
571
|
-
* 标签名选择器
|
|
572
|
-
* @param tag 标签名
|
|
573
|
-
*/
|
|
574
|
-
getElementsByTagName(tag) {
|
|
575
|
-
return this.#getElementsBy(({ type, name }) => name === tag && (type === 'html' || type === 'ext'));
|
|
576
|
-
}
|
|
577
|
-
/**
|
|
578
|
-
* 获取某一行的wikitext
|
|
579
|
-
* @param n 行号
|
|
580
|
-
*/
|
|
581
|
-
getLine(n) {
|
|
582
|
-
return String(this).split('\n', n + 1)[n];
|
|
583
|
-
}
|
|
584
|
-
/**
|
|
585
|
-
* 在开头批量插入子节点
|
|
586
|
-
* @param elements 插入节点
|
|
587
|
-
*/
|
|
588
|
-
prepend(...elements) {
|
|
589
|
-
for (let i = 0; i < elements.length; i++) {
|
|
590
|
-
this.insertAt(elements[i], i);
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
/**
|
|
594
|
-
* 获取子节点的位置
|
|
595
|
-
* @param node 子节点
|
|
596
|
-
* @throws `RangeError` 找不到子节点
|
|
597
|
-
*/
|
|
598
|
-
#getChildIndex(node) {
|
|
599
|
-
const i = this.childNodes.indexOf(node);
|
|
600
|
-
if (i === -1) {
|
|
601
|
-
Parser.error('找不到子节点!', node);
|
|
602
|
-
throw new RangeError('找不到子节点!');
|
|
603
|
-
}
|
|
604
|
-
return i;
|
|
605
|
-
}
|
|
606
|
-
/**
|
|
607
|
-
* 移除子节点
|
|
608
|
-
* @param node 子节点
|
|
609
|
-
*/
|
|
610
|
-
removeChild(node) {
|
|
611
|
-
this.removeAt(this.#getChildIndex(node));
|
|
612
|
-
return node;
|
|
613
|
-
}
|
|
614
|
-
/** @ignore */
|
|
615
|
-
appendChild(node) {
|
|
616
|
-
return this.insertAt(node);
|
|
617
|
-
}
|
|
618
|
-
/** @ignore */
|
|
619
|
-
insertBefore(child, reference) {
|
|
620
|
-
return reference === undefined
|
|
621
|
-
? this.insertAt(child)
|
|
622
|
-
: this.insertAt(child, this.#getChildIndex(reference));
|
|
623
|
-
}
|
|
624
|
-
/**
|
|
625
|
-
* 替换子节点
|
|
626
|
-
* @param newChild 新子节点
|
|
627
|
-
* @param oldChild 原子节点
|
|
628
|
-
*/
|
|
629
|
-
replaceChild(newChild, oldChild) {
|
|
630
|
-
const i = this.#getChildIndex(oldChild);
|
|
631
|
-
this.removeAt(i);
|
|
632
|
-
this.insertAt(newChild, i);
|
|
633
|
-
return oldChild;
|
|
634
|
-
}
|
|
635
|
-
/**
|
|
636
|
-
* 输出AST
|
|
637
|
-
* @param depth 当前深度
|
|
638
|
-
*/
|
|
639
|
-
echo(depth = 0) {
|
|
640
|
-
const indent = ' '.repeat(depth), str = String(this), { childNodes, type, length } = this;
|
|
641
|
-
if (childNodes.every(child => child.type === 'text' || !String(child))) {
|
|
642
|
-
console.log(`${indent}\x1B[32m<%s>\x1B[0m${(0, string_1.noWrap)(str)}\x1B[32m</%s>\x1B[0m`, type, type);
|
|
643
|
-
return;
|
|
644
|
-
}
|
|
645
|
-
Parser.info(`${indent}<${type}>`);
|
|
646
|
-
let i = this.getPadding();
|
|
647
|
-
if (i) {
|
|
648
|
-
console.log(`${indent} ${(0, string_1.noWrap)(str.slice(0, i))}`);
|
|
649
|
-
}
|
|
650
|
-
for (let j = 0; j < length; j++) {
|
|
651
|
-
const child = childNodes[j], childStr = String(child), gap = j === length - 1 ? 0 : this.getGaps(j);
|
|
652
|
-
if (!childStr) {
|
|
653
|
-
// pass
|
|
654
|
-
}
|
|
655
|
-
else if (child.type === 'text') {
|
|
656
|
-
console.log(`${indent} ${(0, string_1.noWrap)(child.data)}`);
|
|
657
|
-
}
|
|
658
|
-
else {
|
|
659
|
-
child.echo(depth + 1);
|
|
660
|
-
}
|
|
661
|
-
i += childStr.length;
|
|
662
|
-
if (gap) {
|
|
663
|
-
console.log(`${indent} ${(0, string_1.noWrap)(str.slice(i, i + gap))}`);
|
|
664
|
-
i += gap;
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
if (i < str.length) {
|
|
668
|
-
console.log(`${indent} ${(0, string_1.noWrap)(str.slice(i))}`);
|
|
669
|
-
}
|
|
670
|
-
Parser.info(`${indent}</${type}>`);
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
exports.AstElement = AstElement;
|
|
674
|
-
Parser.classes['AstElement'] = __filename;
|