wikiparser-node 1.7.0-beta.4 → 1.7.1
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 +181 -0
- package/config/enwiki.json +814 -1
- package/config/llwiki.json +35 -1
- package/config/moegirl.json +44 -1
- package/config/zhwiki.json +466 -1
- package/dist/addon/table.d.ts +1 -0
- package/dist/addon/table.js +485 -0
- package/dist/addon/token.d.ts +1 -0
- package/dist/addon/token.js +220 -0
- package/dist/addon/transclude.d.ts +1 -0
- package/dist/addon/transclude.js +183 -0
- package/dist/base.d.ts +71 -0
- package/dist/base.js +38 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +211 -0
- package/dist/internal.d.ts +46 -0
- package/dist/lib/element.d.ts +151 -0
- package/dist/lib/element.js +628 -0
- package/dist/lib/node.d.ts +145 -0
- package/dist/lib/node.js +420 -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/text.d.ts +63 -0
- package/dist/lib/text.js +355 -0
- package/dist/lib/title.d.ts +40 -0
- package/dist/lib/title.js +197 -0
- package/dist/mixin/attributesParent.d.ts +49 -0
- package/dist/mixin/attributesParent.js +81 -0
- package/dist/mixin/fixed.d.ts +5 -0
- package/dist/mixin/fixed.js +30 -0
- package/dist/mixin/flagsParent.d.ts +41 -0
- package/dist/mixin/flagsParent.js +59 -0
- package/dist/mixin/hidden.d.ts +4 -0
- package/dist/mixin/hidden.js +24 -0
- package/dist/mixin/magicLinkParent.d.ts +19 -0
- package/dist/mixin/magicLinkParent.js +42 -0
- package/dist/mixin/singleLine.d.ts +5 -0
- package/dist/mixin/singleLine.js +26 -0
- package/dist/mixin/sol.d.ts +5 -0
- package/dist/mixin/sol.js +43 -0
- package/dist/mixin/syntax.d.ts +8 -0
- package/dist/mixin/syntax.js +50 -0
- package/dist/parser/braces.js +130 -0
- package/dist/parser/commentAndExt.js +89 -0
- package/dist/parser/converter.js +40 -0
- package/dist/parser/externalLinks.js +44 -0
- package/dist/parser/hrAndDoubleUnderscore.js +40 -0
- package/dist/parser/html.js +38 -0
- package/dist/parser/links.js +99 -0
- package/dist/parser/list.js +97 -0
- package/dist/parser/magicLinks.js +42 -0
- package/dist/parser/quotes.js +67 -0
- package/dist/parser/redirect.js +26 -0
- package/dist/parser/selector.js +190 -0
- package/dist/parser/table.js +123 -0
- package/dist/src/arg.d.ts +58 -0
- package/dist/src/arg.js +214 -0
- package/dist/src/atom.d.ts +12 -0
- package/dist/src/atom.js +27 -0
- package/dist/src/attribute.d.ts +71 -0
- package/dist/src/attribute.js +502 -0
- package/dist/src/attributes.d.ts +109 -0
- package/dist/src/attributes.js +356 -0
- package/dist/src/converter.d.ts +33 -0
- package/dist/src/converter.js +134 -0
- package/dist/src/converterFlags.d.ts +87 -0
- package/dist/src/converterFlags.js +242 -0
- package/dist/src/converterRule.d.ts +77 -0
- package/dist/src/converterRule.js +209 -0
- package/dist/src/extLink.d.ts +43 -0
- package/dist/src/extLink.js +191 -0
- package/dist/src/gallery.d.ts +54 -0
- package/dist/src/gallery.js +139 -0
- package/dist/src/heading.d.ts +47 -0
- package/dist/src/heading.js +190 -0
- package/dist/src/hidden.d.ts +7 -0
- package/dist/src/hidden.js +23 -0
- package/dist/src/html.d.ts +65 -0
- package/dist/src/html.js +358 -0
- package/dist/src/imageParameter.d.ts +65 -0
- package/dist/src/imageParameter.js +257 -0
- package/dist/src/imagemap.d.ts +54 -0
- package/dist/src/imagemap.js +144 -0
- package/dist/src/imagemapLink.d.ts +31 -0
- package/dist/src/imagemapLink.js +93 -0
- package/dist/src/index.d.ts +142 -0
- package/dist/src/index.js +701 -0
- package/dist/src/link/base.d.ts +52 -0
- package/dist/src/link/base.js +228 -0
- package/dist/src/link/category.d.ts +22 -0
- package/dist/src/link/category.js +30 -0
- package/dist/src/link/file.d.ts +98 -0
- package/dist/src/link/file.js +272 -0
- package/dist/src/link/galleryImage.d.ts +32 -0
- package/dist/src/link/galleryImage.js +158 -0
- package/dist/src/link/index.d.ts +56 -0
- package/dist/src/link/index.js +133 -0
- package/dist/src/link/redirectTarget.d.ts +51 -0
- package/dist/src/link/redirectTarget.js +101 -0
- package/dist/src/magicLink.d.ts +59 -0
- package/dist/src/magicLink.js +220 -0
- package/dist/src/nested.d.ts +43 -0
- package/dist/src/nested.js +105 -0
- package/dist/src/nowiki/base.d.ts +31 -0
- package/dist/src/nowiki/base.js +93 -0
- package/dist/src/nowiki/comment.d.ts +25 -0
- package/dist/src/nowiki/comment.js +76 -0
- package/dist/src/nowiki/dd.d.ts +8 -0
- package/dist/src/nowiki/dd.js +24 -0
- package/dist/src/nowiki/doubleUnderscore.d.ts +18 -0
- package/dist/src/nowiki/doubleUnderscore.js +101 -0
- package/dist/src/nowiki/hr.d.ts +5 -0
- package/dist/src/nowiki/hr.js +63 -0
- package/dist/src/nowiki/index.d.ts +16 -0
- package/dist/src/nowiki/index.js +27 -0
- package/dist/src/nowiki/list.d.ts +16 -0
- package/dist/src/nowiki/list.js +109 -0
- package/dist/src/nowiki/listBase.d.ts +5 -0
- package/dist/src/nowiki/listBase.js +61 -0
- package/dist/src/nowiki/noinclude.d.ts +10 -0
- package/dist/src/nowiki/noinclude.js +23 -0
- package/dist/src/nowiki/quote.d.ts +14 -0
- package/dist/src/nowiki/quote.js +131 -0
- package/dist/src/onlyinclude.d.ts +16 -0
- package/dist/src/onlyinclude.js +57 -0
- package/dist/src/paramTag/index.d.ts +37 -0
- package/dist/src/paramTag/index.js +77 -0
- package/dist/src/paramTag/inputbox.d.ts +8 -0
- package/dist/src/paramTag/inputbox.js +22 -0
- package/dist/src/parameter.d.ts +70 -0
- package/dist/src/parameter.js +264 -0
- package/dist/src/pre.d.ts +31 -0
- package/dist/src/pre.js +59 -0
- package/dist/src/redirect.d.ts +34 -0
- package/dist/src/redirect.js +119 -0
- package/dist/src/syntax.d.ts +19 -0
- package/dist/src/syntax.js +86 -0
- package/dist/src/table/base.d.ts +27 -0
- package/dist/src/table/base.js +81 -0
- package/dist/src/table/index.d.ts +242 -0
- package/dist/src/table/index.js +498 -0
- package/dist/src/table/td.d.ts +86 -0
- package/dist/src/table/td.js +355 -0
- package/dist/src/table/tr.d.ts +32 -0
- package/dist/src/table/tr.js +58 -0
- package/dist/src/table/trBase.d.ts +53 -0
- package/dist/src/table/trBase.js +156 -0
- package/dist/src/tagPair/ext.d.ts +32 -0
- package/dist/src/tagPair/ext.js +203 -0
- package/dist/src/tagPair/include.d.ts +35 -0
- package/dist/src/tagPair/include.js +74 -0
- package/dist/src/tagPair/index.d.ts +27 -0
- package/dist/src/tagPair/index.js +128 -0
- package/dist/src/transclude.d.ts +166 -0
- package/dist/src/transclude.js +584 -0
- package/dist/util/constants.js +27 -0
- package/dist/util/debug.js +81 -0
- package/dist/util/diff.js +83 -0
- package/dist/util/lint.js +31 -0
- package/dist/util/string.js +60 -0
- package/errors/README +1 -0
- package/package.json +17 -29
- package/printed/README +1 -0
- package/bundle/bundle.min.js +0 -38
- package/extensions/dist/base.js +0 -64
- package/extensions/dist/editor.js +0 -159
- package/extensions/dist/highlight.js +0 -58
- package/extensions/dist/lint.js +0 -72
- package/extensions/editor.css +0 -64
- package/extensions/ui.css +0 -144
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AstRange = void 0;
|
|
4
|
+
const constants_1 = require("../util/constants");
|
|
5
|
+
/**
|
|
6
|
+
* 计算绝对位置
|
|
7
|
+
* @param referenceNode 容器
|
|
8
|
+
* @param offset 相对位置
|
|
9
|
+
*/
|
|
10
|
+
const getIndex = (referenceNode, offset) => referenceNode.getAbsoluteIndex() + referenceNode.getRelativeIndex(offset);
|
|
11
|
+
/**
|
|
12
|
+
* 获取父节点或抛出错误
|
|
13
|
+
* @param node 参考节点
|
|
14
|
+
* @throws `RangeError` 参考节点没有父节点
|
|
15
|
+
*/
|
|
16
|
+
const getParent = (node) => {
|
|
17
|
+
const { parentNode } = node;
|
|
18
|
+
if (parentNode) {
|
|
19
|
+
return parentNode;
|
|
20
|
+
}
|
|
21
|
+
throw new RangeError('The reference node has no parent node!');
|
|
22
|
+
};
|
|
23
|
+
/** 模拟Range对象 */
|
|
24
|
+
class AstRange {
|
|
25
|
+
#startContainer;
|
|
26
|
+
#startOffset;
|
|
27
|
+
#endContainer;
|
|
28
|
+
#endOffset;
|
|
29
|
+
/** 起点容器 */
|
|
30
|
+
get startContainer() {
|
|
31
|
+
return this.#startContainer ?? this.#notInit(true);
|
|
32
|
+
}
|
|
33
|
+
/** 起点位置 */
|
|
34
|
+
get startOffset() {
|
|
35
|
+
return this.#startOffset ?? this.#notInit(true);
|
|
36
|
+
}
|
|
37
|
+
/** 起点绝对位置 */
|
|
38
|
+
get startIndex() {
|
|
39
|
+
return getIndex(this.startContainer, this.startOffset);
|
|
40
|
+
}
|
|
41
|
+
/** 起点行列位置 */
|
|
42
|
+
get startPos() {
|
|
43
|
+
return this.startContainer.getRootNode().posFromIndex(this.startIndex);
|
|
44
|
+
}
|
|
45
|
+
/** 终点容器 */
|
|
46
|
+
get endContainer() {
|
|
47
|
+
return this.#endContainer ?? this.#notInit(false);
|
|
48
|
+
}
|
|
49
|
+
/** 终点位置 */
|
|
50
|
+
get endOffset() {
|
|
51
|
+
return this.#endOffset ?? this.#notInit(false);
|
|
52
|
+
}
|
|
53
|
+
/** 终点绝对位置 */
|
|
54
|
+
get endIndex() {
|
|
55
|
+
return getIndex(this.endContainer, this.endOffset);
|
|
56
|
+
}
|
|
57
|
+
/** 终点行列位置 */
|
|
58
|
+
get endPos() {
|
|
59
|
+
return this.endContainer.getRootNode().posFromIndex(this.endIndex);
|
|
60
|
+
}
|
|
61
|
+
/** 起始和终止位置是否重合 */
|
|
62
|
+
get collapsed() {
|
|
63
|
+
return this.startContainer === this.endContainer && this.startOffset === this.endOffset;
|
|
64
|
+
}
|
|
65
|
+
/** 最近的公共祖先 */
|
|
66
|
+
get commonAncestorContainer() {
|
|
67
|
+
const { startContainer, endContainer } = this;
|
|
68
|
+
return startContainer.contains(endContainer) ? startContainer : startContainer.parentNode;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* 未初始化时抛出错误
|
|
72
|
+
* @param start 是否未初始化起点
|
|
73
|
+
* @throws `Error` 未初始化
|
|
74
|
+
*/
|
|
75
|
+
#notInit(start) {
|
|
76
|
+
throw new Error(`Please set the ${start ? 'start' : 'end'} position first!`);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 检查起点和终点的设置是否有效
|
|
80
|
+
* @throws `RangeError` 起点和终点不是兄弟节点
|
|
81
|
+
* @throws `RangeError` 起点位于终点之后
|
|
82
|
+
*/
|
|
83
|
+
#check() {
|
|
84
|
+
const { startContainer, startOffset, endContainer, endOffset } = this, msg1 = 'The start and end positions are not siblings!', msg2 = 'The start position cannot be after the end position!';
|
|
85
|
+
if (startContainer === endContainer) {
|
|
86
|
+
if (startOffset > endOffset) {
|
|
87
|
+
throw new RangeError(msg2);
|
|
88
|
+
}
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const { type: startType, parentNode: startParent } = startContainer, { type: endType, parentNode: endParent } = endContainer;
|
|
92
|
+
if (startType !== 'text') {
|
|
93
|
+
if (endType !== 'text' || startContainer !== endParent) {
|
|
94
|
+
throw new RangeError(msg1);
|
|
95
|
+
}
|
|
96
|
+
else if (startOffset > endParent.childNodes.indexOf(endContainer)) {
|
|
97
|
+
throw new RangeError(msg2);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
else if (endType === 'text') {
|
|
101
|
+
if (!startParent || startParent !== endParent) {
|
|
102
|
+
throw new RangeError(msg1);
|
|
103
|
+
}
|
|
104
|
+
const { childNodes } = startParent;
|
|
105
|
+
if (childNodes.indexOf(startContainer) > childNodes.indexOf(endContainer)) {
|
|
106
|
+
throw new RangeError(msg2);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
else if (startParent !== endContainer) {
|
|
110
|
+
throw new RangeError(msg1);
|
|
111
|
+
}
|
|
112
|
+
else if (endOffset <= startParent.childNodes.indexOf(startContainer)) {
|
|
113
|
+
throw new RangeError(msg2);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* 设置起点
|
|
118
|
+
* @param startNode 起点容器
|
|
119
|
+
* @param offset 起点位置
|
|
120
|
+
* @throws `RangeError` offset取值超出范围
|
|
121
|
+
*/
|
|
122
|
+
setStart(startNode, offset) {
|
|
123
|
+
const { length } = startNode;
|
|
124
|
+
if (offset < 0 || offset > length) {
|
|
125
|
+
throw new RangeError(`The range of startOffset should be 0 ~ ${length}`);
|
|
126
|
+
}
|
|
127
|
+
const startContainer = this.#startContainer, startOffset = this.#startOffset;
|
|
128
|
+
this.#startContainer = startNode;
|
|
129
|
+
this.#startOffset = offset;
|
|
130
|
+
if (this.#endContainer) {
|
|
131
|
+
try {
|
|
132
|
+
this.#check();
|
|
133
|
+
}
|
|
134
|
+
catch (e) {
|
|
135
|
+
this.#startContainer = startContainer;
|
|
136
|
+
this.#startOffset = startOffset;
|
|
137
|
+
throw e;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* 设置终点
|
|
143
|
+
* @param endNode 终点容器
|
|
144
|
+
* @param offset 终点位置
|
|
145
|
+
* @throws `RangeError` offset取值超出范围
|
|
146
|
+
*/
|
|
147
|
+
setEnd(endNode, offset) {
|
|
148
|
+
const { length } = endNode;
|
|
149
|
+
if (offset < 0 || offset > length) {
|
|
150
|
+
throw new RangeError(`The range of endOffset should be 0 ~ ${length}`);
|
|
151
|
+
}
|
|
152
|
+
const endContainer = this.#endContainer, endOffset = this.#endOffset;
|
|
153
|
+
this.#endContainer = endNode;
|
|
154
|
+
this.#endOffset = offset;
|
|
155
|
+
if (this.#startContainer) {
|
|
156
|
+
try {
|
|
157
|
+
this.#check();
|
|
158
|
+
}
|
|
159
|
+
catch (e) {
|
|
160
|
+
this.#endContainer = endContainer;
|
|
161
|
+
this.#endOffset = endOffset;
|
|
162
|
+
throw e;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* 在节点后设置
|
|
168
|
+
* @param method 方法名
|
|
169
|
+
* @param referenceNode 节点
|
|
170
|
+
*/
|
|
171
|
+
#setAfter(method, referenceNode) {
|
|
172
|
+
const parentNode = getParent(referenceNode);
|
|
173
|
+
this[method](parentNode, parentNode.childNodes.indexOf(referenceNode) + 1);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* 在节点后设置起点
|
|
177
|
+
* @param referenceNode 节点
|
|
178
|
+
*/
|
|
179
|
+
setStartAfter(referenceNode) {
|
|
180
|
+
this.#setAfter('setStart', referenceNode);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* 在节点后设置终点
|
|
184
|
+
* @param referenceNode 节点
|
|
185
|
+
*/
|
|
186
|
+
setEndAfter(referenceNode) {
|
|
187
|
+
this.#setAfter('setEnd', referenceNode);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* 在节点前设置
|
|
191
|
+
* @param method 方法名
|
|
192
|
+
* @param referenceNode 节点
|
|
193
|
+
*/
|
|
194
|
+
#setBefore(method, referenceNode) {
|
|
195
|
+
const parentNode = getParent(referenceNode);
|
|
196
|
+
this[method](parentNode, parentNode.childNodes.indexOf(referenceNode));
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* 在节点前设置起点
|
|
200
|
+
* @param referenceNode 节点
|
|
201
|
+
*/
|
|
202
|
+
setStartBefore(referenceNode) {
|
|
203
|
+
this.#setBefore('setStart', referenceNode);
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* 在节点前设置终点
|
|
207
|
+
* @param referenceNode 节点
|
|
208
|
+
*/
|
|
209
|
+
setEndBefore(referenceNode) {
|
|
210
|
+
this.#setBefore('setEnd', referenceNode);
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* 设置Range包含整个节点的内容
|
|
214
|
+
* @param referenceNode 节点
|
|
215
|
+
*/
|
|
216
|
+
selectNodeContents(referenceNode) {
|
|
217
|
+
this.#startContainer = referenceNode;
|
|
218
|
+
this.#startOffset = 0;
|
|
219
|
+
this.#endContainer = referenceNode;
|
|
220
|
+
this.#endOffset = referenceNode.length;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* 设置Range包含整个节点
|
|
224
|
+
* @param referenceNode 节点
|
|
225
|
+
*/
|
|
226
|
+
selectNode(referenceNode) {
|
|
227
|
+
const parentNode = getParent(referenceNode), i = parentNode.childNodes.indexOf(referenceNode);
|
|
228
|
+
this.#startContainer = parentNode;
|
|
229
|
+
this.#startOffset = i;
|
|
230
|
+
this.#endContainer = parentNode;
|
|
231
|
+
this.#endOffset = i + 1;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* 使起始和终止位置重合
|
|
235
|
+
* @param toStart 重合至起始位置
|
|
236
|
+
*/
|
|
237
|
+
collapse(toStart) {
|
|
238
|
+
if (toStart) {
|
|
239
|
+
this.#endContainer = this.startContainer;
|
|
240
|
+
this.#endOffset = this.startOffset;
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
this.#startContainer = this.endContainer;
|
|
244
|
+
this.#startOffset = this.endOffset;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* 比较端点和Range的位置
|
|
249
|
+
* @param referenceNode 端点容器
|
|
250
|
+
* @param offset 端点位置
|
|
251
|
+
* @throws `RangeError` 不在同一个文档
|
|
252
|
+
*/
|
|
253
|
+
comparePoint(referenceNode, offset) {
|
|
254
|
+
const { startContainer, startIndex, endContainer, endIndex } = this;
|
|
255
|
+
if (startContainer.getRootNode() !== referenceNode.getRootNode()) {
|
|
256
|
+
throw new RangeError('The point to be compared is not in the same document!');
|
|
257
|
+
}
|
|
258
|
+
const index = getIndex(referenceNode, offset);
|
|
259
|
+
if (index < startIndex || index === startIndex && !startContainer.contains(referenceNode)) {
|
|
260
|
+
return -1;
|
|
261
|
+
}
|
|
262
|
+
return index < endIndex || index === endIndex && endContainer.contains(referenceNode) ? 0 : 1;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* 端点是否在Range中
|
|
266
|
+
* @param referenceNode 端点容器
|
|
267
|
+
* @param offset 端点位置
|
|
268
|
+
*/
|
|
269
|
+
isPointInRange(referenceNode, offset) {
|
|
270
|
+
return this.comparePoint(referenceNode, offset) === 0;
|
|
271
|
+
}
|
|
272
|
+
/** 复制AstRange对象 */
|
|
273
|
+
cloneRange() {
|
|
274
|
+
const range = new AstRange();
|
|
275
|
+
range.setStart(this.startContainer, this.startOffset);
|
|
276
|
+
range.setEnd(this.endContainer, this.endOffset);
|
|
277
|
+
return range;
|
|
278
|
+
}
|
|
279
|
+
/** 清空Range */
|
|
280
|
+
detach() {
|
|
281
|
+
this.#startContainer = undefined;
|
|
282
|
+
this.#startOffset = undefined;
|
|
283
|
+
this.#endContainer = undefined;
|
|
284
|
+
this.#endOffset = undefined;
|
|
285
|
+
}
|
|
286
|
+
/** 删除Range中的内容 */
|
|
287
|
+
deleteContents() {
|
|
288
|
+
const { startContainer, endContainer, commonAncestorContainer } = this, { childNodes } = commonAncestorContainer;
|
|
289
|
+
let { startOffset, endOffset } = this;
|
|
290
|
+
if (commonAncestorContainer.type === 'text') {
|
|
291
|
+
commonAncestorContainer.deleteData(startOffset, endOffset);
|
|
292
|
+
this.#endOffset = this.#startOffset;
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
else if (startContainer.type === 'text') {
|
|
296
|
+
startContainer.deleteData(startOffset, Infinity);
|
|
297
|
+
startOffset = childNodes.indexOf(startContainer) + 1;
|
|
298
|
+
}
|
|
299
|
+
if (endContainer.type === 'text') {
|
|
300
|
+
endContainer.deleteData(0, endOffset);
|
|
301
|
+
endOffset = childNodes.indexOf(endContainer);
|
|
302
|
+
}
|
|
303
|
+
for (let i = endOffset - 1; i >= startOffset; i--) {
|
|
304
|
+
commonAncestorContainer.removeAt(i);
|
|
305
|
+
}
|
|
306
|
+
this.#startContainer = commonAncestorContainer;
|
|
307
|
+
this.#startOffset = startOffset;
|
|
308
|
+
this.#endContainer = commonAncestorContainer;
|
|
309
|
+
this.#endOffset = startOffset;
|
|
310
|
+
}
|
|
311
|
+
/** 获取行列位置和大小 */
|
|
312
|
+
getBoundingClientRect() {
|
|
313
|
+
const { startPos: { top, left }, endPos: { top: bottom, left: right } } = this;
|
|
314
|
+
return { top, left, height: bottom - top + 1, width: bottom === top ? right - left : right };
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* 在起始位置插入节点
|
|
318
|
+
* @param newNode 插入的节点
|
|
319
|
+
*/
|
|
320
|
+
insertNode(newNode) {
|
|
321
|
+
const { startContainer, startOffset } = this, endContainer = this.#endContainer;
|
|
322
|
+
if (startContainer.type === 'text') {
|
|
323
|
+
if (startOffset) {
|
|
324
|
+
startContainer.splitText(startOffset);
|
|
325
|
+
this.#startContainer = startContainer.nextSibling;
|
|
326
|
+
this.#startOffset = 0;
|
|
327
|
+
startContainer.after(newNode);
|
|
328
|
+
if (endContainer === startContainer) {
|
|
329
|
+
this.#endContainer = this.#startContainer;
|
|
330
|
+
this.#endOffset -= startOffset;
|
|
331
|
+
}
|
|
332
|
+
else if (endContainer) {
|
|
333
|
+
this.#endOffset += 2;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
startContainer.before(newNode);
|
|
338
|
+
if (endContainer && endContainer !== startContainer) {
|
|
339
|
+
this.#endOffset++;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
else {
|
|
344
|
+
startContainer.insertAt(newNode, startOffset);
|
|
345
|
+
this.#startOffset++;
|
|
346
|
+
if (endContainer === startContainer) {
|
|
347
|
+
this.#endOffset++;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
/** @private */
|
|
352
|
+
toString() {
|
|
353
|
+
const { startContainer, startIndex, endIndex } = this;
|
|
354
|
+
return String(startContainer.getRootNode()).slice(startIndex, endIndex);
|
|
355
|
+
}
|
|
356
|
+
/** 获取范围内的全部节点 */
|
|
357
|
+
extractContents() {
|
|
358
|
+
const { startContainer, startOffset, endContainer, endOffset, commonAncestorContainer } = this, { childNodes } = commonAncestorContainer;
|
|
359
|
+
let from, to;
|
|
360
|
+
if (commonAncestorContainer.type === 'text') {
|
|
361
|
+
if (startOffset === endOffset) {
|
|
362
|
+
return [];
|
|
363
|
+
}
|
|
364
|
+
else if (endOffset < commonAncestorContainer.length) {
|
|
365
|
+
commonAncestorContainer.splitText(endOffset);
|
|
366
|
+
}
|
|
367
|
+
if (startOffset) {
|
|
368
|
+
const nextSibling = commonAncestorContainer.splitText(startOffset);
|
|
369
|
+
this.#startContainer = nextSibling;
|
|
370
|
+
this.#startOffset = 0;
|
|
371
|
+
this.#endContainer = nextSibling;
|
|
372
|
+
this.#endOffset -= startOffset;
|
|
373
|
+
return [nextSibling];
|
|
374
|
+
}
|
|
375
|
+
return [commonAncestorContainer];
|
|
376
|
+
}
|
|
377
|
+
else if (endContainer.type === 'text') {
|
|
378
|
+
if (endOffset && endOffset < endContainer.length) {
|
|
379
|
+
endContainer.splitText(endOffset);
|
|
380
|
+
}
|
|
381
|
+
to = childNodes.indexOf(endContainer) + (endOffset && 1);
|
|
382
|
+
}
|
|
383
|
+
else {
|
|
384
|
+
to = endOffset;
|
|
385
|
+
}
|
|
386
|
+
if (startContainer.type === 'text') {
|
|
387
|
+
if (startOffset && startOffset < startContainer.length) {
|
|
388
|
+
this.#startContainer = startContainer.splitText(startOffset);
|
|
389
|
+
this.#startOffset = 0;
|
|
390
|
+
this.#endOffset++;
|
|
391
|
+
to++;
|
|
392
|
+
}
|
|
393
|
+
from = childNodes.indexOf(startContainer) + (startOffset && 1);
|
|
394
|
+
}
|
|
395
|
+
else {
|
|
396
|
+
from = startOffset;
|
|
397
|
+
}
|
|
398
|
+
return commonAncestorContainer.childNodes.slice(from, to);
|
|
399
|
+
}
|
|
400
|
+
/** 拷贝范围内的全部节点 */
|
|
401
|
+
cloneContents() {
|
|
402
|
+
return this.extractContents().map(node => node.cloneNode());
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
exports.AstRange = AstRange;
|
|
406
|
+
constants_1.classes['AstRange'] = __filename;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** 模拟Python的Range对象。除`step`至少为`1`外,允许负数、小数或`end < start`的情形。 */
|
|
2
|
+
export declare class Range {
|
|
3
|
+
readonly start: number;
|
|
4
|
+
readonly end: number;
|
|
5
|
+
readonly step: number;
|
|
6
|
+
/**
|
|
7
|
+
* @param s 表达式
|
|
8
|
+
* @throws `RangeError` 起点、终点和步长均应为整数
|
|
9
|
+
* @throws `RangeError` n的系数不能为0
|
|
10
|
+
* @throws `RangeError` 应使用CSS选择器或Python切片的格式
|
|
11
|
+
*/
|
|
12
|
+
constructor(str: string);
|
|
13
|
+
/**
|
|
14
|
+
* 将Range转换为针对特定数组的下标集
|
|
15
|
+
* @param arr 参考数组`[0, 1, 2, ...]`
|
|
16
|
+
*/
|
|
17
|
+
applyTo(arr: readonly number[]): number[];
|
|
18
|
+
}
|
|
19
|
+
/** @extends {Array<number|Range>} */
|
|
20
|
+
export declare class Ranges extends Array<number | Range> {
|
|
21
|
+
/** @param a 表达式数组 */
|
|
22
|
+
constructor(a?: number | string | Range | readonly (number | string | Range)[]);
|
|
23
|
+
/**
|
|
24
|
+
* 将Ranges转换为针对特定Array的下标集
|
|
25
|
+
* @param arr 参考数组
|
|
26
|
+
*/
|
|
27
|
+
applyTo(arr: number | readonly unknown[]): number[];
|
|
28
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Ranges = exports.Range = void 0;
|
|
4
|
+
const constants_1 = require("../util/constants");
|
|
5
|
+
const debug_1 = require("../util/debug");
|
|
6
|
+
const diff_1 = require("../util/diff");
|
|
7
|
+
/** 模拟Python的Range对象。除`step`至少为`1`外,允许负数、小数或`end < start`的情形。 */
|
|
8
|
+
class Range {
|
|
9
|
+
start;
|
|
10
|
+
end;
|
|
11
|
+
step;
|
|
12
|
+
/**
|
|
13
|
+
* @param s 表达式
|
|
14
|
+
* @throws `RangeError` 起点、终点和步长均应为整数
|
|
15
|
+
* @throws `RangeError` n的系数不能为0
|
|
16
|
+
* @throws `RangeError` 应使用CSS选择器或Python切片的格式
|
|
17
|
+
*/
|
|
18
|
+
constructor(str) {
|
|
19
|
+
str = str.trim();
|
|
20
|
+
if (str === 'odd') {
|
|
21
|
+
Object.assign(this, { start: 1, end: Infinity, step: 2 });
|
|
22
|
+
}
|
|
23
|
+
else if (str === 'even') {
|
|
24
|
+
Object.assign(this, { start: 0, end: Infinity, step: 2 });
|
|
25
|
+
}
|
|
26
|
+
else if (str.includes(':')) {
|
|
27
|
+
const [start, end, step = '1'] = str.split(':', 3);
|
|
28
|
+
this.start = Number(start);
|
|
29
|
+
this.end = Number(end?.trim() || Infinity);
|
|
30
|
+
this.step = Math.max(Number(step), 1);
|
|
31
|
+
if (!Number.isInteger(this.start)) {
|
|
32
|
+
throw new RangeError(`The start of a range, \`${start}\`, should be an integer!`);
|
|
33
|
+
}
|
|
34
|
+
else if (this.end !== Infinity && !Number.isInteger(this.end)) {
|
|
35
|
+
throw new RangeError(`The end of a range, \`${end}\`, should be an integer!`);
|
|
36
|
+
}
|
|
37
|
+
else if (!Number.isInteger(this.step)) {
|
|
38
|
+
throw new RangeError(`The step of a range, \`${step}\`, should be an integer!`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
const mt = /^([+-])?(\d+)?n(?:\s*([+-])\s*(\d+))?$/u
|
|
43
|
+
.exec(str);
|
|
44
|
+
if (mt) {
|
|
45
|
+
const [, sgnA = '+', a = 1, sgnB = '+'] = mt, b = Number(mt[4] ?? 0);
|
|
46
|
+
this.step = Number(a);
|
|
47
|
+
if (this.step === 0) {
|
|
48
|
+
throw new RangeError(`In the argument \`${str}\`, the coefficient of "n" must not be 0!`);
|
|
49
|
+
}
|
|
50
|
+
else if (sgnA === '+') { // `an+b` or `an-b`
|
|
51
|
+
this.start = sgnB === '+' || b === 0 ? b : this.step - 1 - (b - 1) % this.step;
|
|
52
|
+
this.end = Infinity;
|
|
53
|
+
}
|
|
54
|
+
else if (sgnB === '-') { // `-an-b`
|
|
55
|
+
this.start = 0;
|
|
56
|
+
this.end = b > 0 ? 0 : this.step;
|
|
57
|
+
}
|
|
58
|
+
else { // `-an+b`
|
|
59
|
+
this.start = b % this.step;
|
|
60
|
+
this.end = this.step + b;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
throw new RangeError(`The argument \`${str}\` should be either in the form of "an+b" as in CSS selectors or Python slices!`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* 将Range转换为针对特定数组的下标集
|
|
70
|
+
* @param arr 参考数组`[0, 1, 2, ...]`
|
|
71
|
+
*/
|
|
72
|
+
applyTo(arr) {
|
|
73
|
+
return arr.slice(this.start, this.end).filter((_, j) => j % this.step === 0);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.Range = Range;
|
|
77
|
+
/** @extends {Array<number|Range>} */
|
|
78
|
+
class Ranges extends Array {
|
|
79
|
+
/** @param a 表达式数组 */
|
|
80
|
+
constructor(a) {
|
|
81
|
+
super();
|
|
82
|
+
if (a === undefined) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
for (const ele of (Array.isArray(a) ? a : [a])) {
|
|
86
|
+
if (ele instanceof Range) {
|
|
87
|
+
this.push(ele);
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
else if (typeof ele === 'string' && !ele.trim()) {
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
const number = Number(ele);
|
|
94
|
+
if (Number.isInteger(number)) {
|
|
95
|
+
this.push(number);
|
|
96
|
+
}
|
|
97
|
+
else if (typeof ele === 'string' && Number.isNaN(number)) {
|
|
98
|
+
try {
|
|
99
|
+
this.push(new Range(ele));
|
|
100
|
+
}
|
|
101
|
+
catch (e) {
|
|
102
|
+
if (e instanceof RangeError) {
|
|
103
|
+
(0, diff_1.error)(e.message);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* 将Ranges转换为针对特定Array的下标集
|
|
111
|
+
* @param arr 参考数组
|
|
112
|
+
*/
|
|
113
|
+
applyTo(arr) {
|
|
114
|
+
const length = typeof arr === 'number' ? arr : arr.length, a = (0, debug_1.emptyArray)(length, i => i);
|
|
115
|
+
return [
|
|
116
|
+
...new Set([...this].flatMap(ele => {
|
|
117
|
+
if (typeof ele === 'number') {
|
|
118
|
+
return ele < 0 ? ele + length : ele;
|
|
119
|
+
}
|
|
120
|
+
return ele.applyTo(a);
|
|
121
|
+
})),
|
|
122
|
+
].filter(i => i >= 0 && i < length).sort();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
exports.Ranges = Ranges;
|
|
126
|
+
constants_1.classes['Ranges'] = __filename;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { AstNode } from './node';
|
|
2
|
+
import type { LintError } from '../base';
|
|
3
|
+
/** 文本节点 */
|
|
4
|
+
export declare class AstText extends AstNode {
|
|
5
|
+
#private;
|
|
6
|
+
readonly type = "text";
|
|
7
|
+
readonly name: undefined;
|
|
8
|
+
readonly data: string;
|
|
9
|
+
/** 文本长度 */
|
|
10
|
+
get length(): number;
|
|
11
|
+
/** @param text 包含文本 */
|
|
12
|
+
constructor(text: string);
|
|
13
|
+
/** 可见部分 */
|
|
14
|
+
text(): string;
|
|
15
|
+
/**
|
|
16
|
+
* @override
|
|
17
|
+
* @param start
|
|
18
|
+
* @param errorRegex 正则表达式
|
|
19
|
+
* @throws `Error` 孤立文本节点
|
|
20
|
+
*/
|
|
21
|
+
lint(start?: number, errorRegex?: RegExp): LintError[];
|
|
22
|
+
/**
|
|
23
|
+
* 替换字符串
|
|
24
|
+
* @param text 替换的字符串
|
|
25
|
+
*/
|
|
26
|
+
replaceData(text: string): void;
|
|
27
|
+
/** @override */
|
|
28
|
+
print(): string;
|
|
29
|
+
/** 复制 */
|
|
30
|
+
cloneNode(): AstText;
|
|
31
|
+
/**
|
|
32
|
+
* 在后方添加字符串
|
|
33
|
+
* @param text 添加的字符串
|
|
34
|
+
*/
|
|
35
|
+
appendData(text: string): void;
|
|
36
|
+
/**
|
|
37
|
+
* 删减字符串
|
|
38
|
+
* @param offset 起始位置
|
|
39
|
+
* @param count 删减字符数
|
|
40
|
+
*/
|
|
41
|
+
deleteData(offset: number, count: number): void;
|
|
42
|
+
/**
|
|
43
|
+
* 插入字符串
|
|
44
|
+
* @param offset 插入位置
|
|
45
|
+
* @param text 待插入的字符串
|
|
46
|
+
*/
|
|
47
|
+
insertData(offset: number, text: string): void;
|
|
48
|
+
/**
|
|
49
|
+
* 提取子串
|
|
50
|
+
* @param offset 起始位置
|
|
51
|
+
* @param count 字符数
|
|
52
|
+
*/
|
|
53
|
+
substringData(offset: number, count: number): string;
|
|
54
|
+
/**
|
|
55
|
+
* 将文本子节点分裂为两部分
|
|
56
|
+
* @param offset 分裂位置
|
|
57
|
+
* @throws `RangeError` 错误的断开位置
|
|
58
|
+
* @throws `Error` 没有父节点
|
|
59
|
+
*/
|
|
60
|
+
splitText(offset: number): AstText;
|
|
61
|
+
/** 转义 `=` */
|
|
62
|
+
escape(): void;
|
|
63
|
+
}
|