wikiparser-node 1.9.1-b → 1.9.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.
Files changed (171) hide show
  1. package/config/.schema.json +181 -0
  2. package/config/enwiki.json +814 -1
  3. package/config/llwiki.json +35 -1
  4. package/config/moegirl.json +44 -1
  5. package/config/zhwiki.json +466 -1
  6. package/dist/addon/table.js +486 -0
  7. package/dist/addon/token.js +239 -0
  8. package/dist/addon/transclude.js +185 -0
  9. package/dist/base.d.ts +73 -0
  10. package/dist/base.js +39 -0
  11. package/dist/index.d.ts +31 -0
  12. package/dist/index.js +221 -0
  13. package/dist/internal.d.ts +46 -0
  14. package/dist/lib/element.d.ts +124 -0
  15. package/dist/lib/element.js +600 -0
  16. package/dist/lib/node.d.ts +166 -0
  17. package/dist/lib/node.js +480 -0
  18. package/dist/lib/range.d.ts +105 -0
  19. package/dist/lib/range.js +406 -0
  20. package/dist/lib/ranges.d.ts +28 -0
  21. package/dist/lib/ranges.js +126 -0
  22. package/dist/lib/rect.d.ts +18 -0
  23. package/dist/lib/rect.js +34 -0
  24. package/dist/lib/text.d.ts +54 -0
  25. package/dist/lib/text.js +345 -0
  26. package/dist/lib/title.d.ts +40 -0
  27. package/dist/lib/title.js +197 -0
  28. package/dist/mixin/attributesParent.d.ts +49 -0
  29. package/dist/mixin/attributesParent.js +82 -0
  30. package/dist/mixin/fixed.d.ts +5 -0
  31. package/dist/mixin/fixed.js +32 -0
  32. package/dist/mixin/flagsParent.d.ts +41 -0
  33. package/dist/mixin/flagsParent.js +60 -0
  34. package/dist/mixin/hidden.d.ts +6 -0
  35. package/dist/mixin/hidden.js +29 -0
  36. package/dist/mixin/magicLinkParent.d.ts +19 -0
  37. package/dist/mixin/magicLinkParent.js +43 -0
  38. package/dist/mixin/singleLine.d.ts +5 -0
  39. package/dist/mixin/singleLine.js +27 -0
  40. package/dist/mixin/sol.d.ts +5 -0
  41. package/dist/mixin/sol.js +44 -0
  42. package/dist/mixin/syntax.d.ts +8 -0
  43. package/dist/mixin/syntax.js +47 -0
  44. package/dist/parser/braces.js +146 -0
  45. package/dist/parser/commentAndExt.js +79 -0
  46. package/dist/parser/converter.js +39 -0
  47. package/dist/parser/externalLinks.js +37 -0
  48. package/dist/parser/hrAndDoubleUnderscore.js +42 -0
  49. package/dist/parser/html.js +38 -0
  50. package/dist/parser/links.js +101 -0
  51. package/dist/parser/list.js +97 -0
  52. package/dist/parser/magicLinks.js +53 -0
  53. package/dist/parser/quotes.js +67 -0
  54. package/dist/parser/redirect.js +26 -0
  55. package/dist/parser/selector.js +190 -0
  56. package/dist/parser/table.js +123 -0
  57. package/dist/src/arg.d.ts +52 -0
  58. package/dist/src/arg.js +213 -0
  59. package/dist/src/atom.d.ts +12 -0
  60. package/dist/src/atom.js +26 -0
  61. package/dist/src/attribute.d.ts +65 -0
  62. package/dist/src/attribute.js +490 -0
  63. package/dist/src/attributes.d.ts +103 -0
  64. package/dist/src/attributes.js +350 -0
  65. package/dist/src/converter.d.ts +29 -0
  66. package/dist/src/converter.js +134 -0
  67. package/dist/src/converterFlags.d.ts +81 -0
  68. package/dist/src/converterFlags.js +243 -0
  69. package/dist/src/converterRule.d.ts +73 -0
  70. package/dist/src/converterRule.js +210 -0
  71. package/dist/src/extLink.d.ts +37 -0
  72. package/dist/src/extLink.js +191 -0
  73. package/dist/src/gallery.d.ts +48 -0
  74. package/dist/src/gallery.js +139 -0
  75. package/dist/src/heading.d.ts +41 -0
  76. package/dist/src/heading.js +185 -0
  77. package/dist/src/hidden.d.ts +10 -0
  78. package/dist/src/hidden.js +79 -0
  79. package/dist/src/html.d.ts +58 -0
  80. package/dist/src/html.js +339 -0
  81. package/dist/src/imageParameter.d.ts +59 -0
  82. package/dist/src/imageParameter.js +261 -0
  83. package/dist/src/imagemap.d.ts +48 -0
  84. package/dist/src/imagemap.js +145 -0
  85. package/dist/src/imagemapLink.d.ts +31 -0
  86. package/dist/src/imagemapLink.js +93 -0
  87. package/dist/src/index.d.ts +130 -0
  88. package/dist/src/index.js +717 -0
  89. package/dist/src/link/base.d.ts +46 -0
  90. package/dist/src/link/base.js +224 -0
  91. package/dist/src/link/category.d.ts +22 -0
  92. package/dist/src/link/category.js +30 -0
  93. package/dist/src/link/file.d.ts +96 -0
  94. package/dist/src/link/file.js +274 -0
  95. package/dist/src/link/galleryImage.d.ts +30 -0
  96. package/dist/src/link/galleryImage.js +157 -0
  97. package/dist/src/link/index.d.ts +52 -0
  98. package/dist/src/link/index.js +133 -0
  99. package/dist/src/link/redirectTarget.d.ts +37 -0
  100. package/dist/src/link/redirectTarget.js +95 -0
  101. package/dist/src/magicLink.d.ts +60 -0
  102. package/dist/src/magicLink.js +271 -0
  103. package/dist/src/nested.d.ts +41 -0
  104. package/dist/src/nested.js +105 -0
  105. package/dist/src/nowiki/base.d.ts +31 -0
  106. package/dist/src/nowiki/base.js +92 -0
  107. package/dist/src/nowiki/comment.d.ts +15 -0
  108. package/dist/src/nowiki/comment.js +123 -0
  109. package/dist/src/nowiki/dd.d.ts +8 -0
  110. package/dist/src/nowiki/dd.js +24 -0
  111. package/dist/src/nowiki/doubleUnderscore.d.ts +16 -0
  112. package/dist/src/nowiki/doubleUnderscore.js +100 -0
  113. package/dist/src/nowiki/hr.d.ts +5 -0
  114. package/dist/src/nowiki/hr.js +63 -0
  115. package/dist/src/nowiki/index.d.ts +14 -0
  116. package/dist/src/nowiki/index.js +29 -0
  117. package/dist/src/nowiki/list.d.ts +16 -0
  118. package/dist/src/nowiki/list.js +109 -0
  119. package/dist/src/nowiki/listBase.d.ts +5 -0
  120. package/dist/src/nowiki/listBase.js +61 -0
  121. package/dist/src/nowiki/noinclude.d.ts +10 -0
  122. package/dist/src/nowiki/noinclude.js +73 -0
  123. package/dist/src/nowiki/quote.d.ts +17 -0
  124. package/dist/src/nowiki/quote.js +131 -0
  125. package/dist/src/onlyinclude.d.ts +14 -0
  126. package/dist/src/onlyinclude.js +57 -0
  127. package/dist/src/paramTag/index.d.ts +31 -0
  128. package/dist/src/paramTag/index.js +77 -0
  129. package/dist/src/paramTag/inputbox.d.ts +8 -0
  130. package/dist/src/paramTag/inputbox.js +22 -0
  131. package/dist/src/parameter.d.ts +66 -0
  132. package/dist/src/parameter.js +265 -0
  133. package/dist/src/pre.d.ts +29 -0
  134. package/dist/src/pre.js +59 -0
  135. package/dist/src/redirect.d.ts +31 -0
  136. package/dist/src/redirect.js +121 -0
  137. package/dist/src/syntax.d.ts +17 -0
  138. package/dist/src/syntax.js +85 -0
  139. package/dist/src/table/base.d.ts +27 -0
  140. package/dist/src/table/base.js +81 -0
  141. package/dist/src/table/index.d.ts +239 -0
  142. package/dist/src/table/index.js +499 -0
  143. package/dist/src/table/td.d.ts +78 -0
  144. package/dist/src/table/td.js +368 -0
  145. package/dist/src/table/tr.d.ts +30 -0
  146. package/dist/src/table/tr.js +57 -0
  147. package/dist/src/table/trBase.d.ts +49 -0
  148. package/dist/src/table/trBase.js +156 -0
  149. package/dist/src/tagPair/ext.d.ts +30 -0
  150. package/dist/src/tagPair/ext.js +201 -0
  151. package/dist/src/tagPair/include.d.ts +35 -0
  152. package/dist/src/tagPair/include.js +124 -0
  153. package/dist/src/tagPair/index.d.ts +23 -0
  154. package/dist/src/tagPair/index.js +128 -0
  155. package/dist/src/transclude.d.ts +160 -0
  156. package/dist/src/transclude.js +582 -0
  157. package/dist/util/constants.js +27 -0
  158. package/dist/util/debug.js +90 -0
  159. package/dist/util/diff.js +83 -0
  160. package/dist/util/lint.js +32 -0
  161. package/dist/util/string.js +60 -0
  162. package/errors/README +1 -0
  163. package/package.json +20 -30
  164. package/printed/README +1 -0
  165. package/bundle/bundle.min.js +0 -38
  166. package/extensions/dist/base.js +0 -64
  167. package/extensions/dist/editor.js +0 -159
  168. package/extensions/dist/highlight.js +0 -58
  169. package/extensions/dist/lint.js +0 -72
  170. package/extensions/editor.css +0 -64
  171. package/extensions/ui.css +0 -144
@@ -0,0 +1,105 @@
1
+ import type { AstNodes } from '../internal';
2
+ import type { Dimension, Position } from './node';
3
+ /** 模拟Range对象 */
4
+ export declare class AstRange {
5
+ #private;
6
+ /** 起点容器 */
7
+ get startContainer(): AstNodes;
8
+ /** 起点位置 */
9
+ get startOffset(): number;
10
+ /** 起点绝对位置 */
11
+ get startIndex(): number;
12
+ /** 起点行列位置 */
13
+ get startPos(): Position;
14
+ /** 终点容器 */
15
+ get endContainer(): AstNodes;
16
+ /** 终点位置 */
17
+ get endOffset(): number;
18
+ /** 终点绝对位置 */
19
+ get endIndex(): number;
20
+ /** 终点行列位置 */
21
+ get endPos(): Position;
22
+ /** 起始和终止位置是否重合 */
23
+ get collapsed(): boolean;
24
+ /** 最近的公共祖先 */
25
+ get commonAncestorContainer(): AstNodes;
26
+ /**
27
+ * 设置起点
28
+ * @param startNode 起点容器
29
+ * @param offset 起点位置
30
+ * @throws `RangeError` offset取值超出范围
31
+ */
32
+ setStart(startNode: AstNodes, offset: number): void;
33
+ /**
34
+ * 设置终点
35
+ * @param endNode 终点容器
36
+ * @param offset 终点位置
37
+ * @throws `RangeError` offset取值超出范围
38
+ */
39
+ setEnd(endNode: AstNodes, offset: number): void;
40
+ /**
41
+ * 在节点后设置起点
42
+ * @param referenceNode 节点
43
+ */
44
+ setStartAfter(referenceNode: AstNodes): void;
45
+ /**
46
+ * 在节点后设置终点
47
+ * @param referenceNode 节点
48
+ */
49
+ setEndAfter(referenceNode: AstNodes): void;
50
+ /**
51
+ * 在节点前设置起点
52
+ * @param referenceNode 节点
53
+ */
54
+ setStartBefore(referenceNode: AstNodes): void;
55
+ /**
56
+ * 在节点前设置终点
57
+ * @param referenceNode 节点
58
+ */
59
+ setEndBefore(referenceNode: AstNodes): void;
60
+ /**
61
+ * 设置Range包含整个节点的内容
62
+ * @param referenceNode 节点
63
+ */
64
+ selectNodeContents(referenceNode: AstNodes): void;
65
+ /**
66
+ * 设置Range包含整个节点
67
+ * @param referenceNode 节点
68
+ */
69
+ selectNode(referenceNode: AstNodes): void;
70
+ /**
71
+ * 使起始和终止位置重合
72
+ * @param toStart 重合至起始位置
73
+ */
74
+ collapse(toStart?: boolean): void;
75
+ /**
76
+ * 比较端点和Range的位置
77
+ * @param referenceNode 端点容器
78
+ * @param offset 端点位置
79
+ * @throws `RangeError` 不在同一个文档
80
+ */
81
+ comparePoint(referenceNode: AstNodes, offset: number): -1 | 0 | 1;
82
+ /**
83
+ * 端点是否在Range中
84
+ * @param referenceNode 端点容器
85
+ * @param offset 端点位置
86
+ */
87
+ isPointInRange(referenceNode: AstNodes, offset: number): boolean;
88
+ /** 复制AstRange对象 */
89
+ cloneRange(): AstRange;
90
+ /** 清空Range */
91
+ detach(): void;
92
+ /** 删除Range中的内容 */
93
+ deleteContents(): void;
94
+ /** 获取行列位置和大小 */
95
+ getBoundingClientRect(): Dimension & Position;
96
+ /**
97
+ * 在起始位置插入节点
98
+ * @param newNode 插入的节点
99
+ */
100
+ insertNode(newNode: AstNodes | string): void;
101
+ /** 获取范围内的全部节点 */
102
+ extractContents(): AstNodes[];
103
+ /** 拷贝范围内的全部节点 */
104
+ cloneContents(): AstNodes[];
105
+ }
@@ -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 startContainer.getRootNode().toString().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,18 @@
1
+ import type { AstNodes, Position } from './node';
2
+ /** 节点位置 */
3
+ export declare class BoundingRect {
4
+ #private;
5
+ readonly token: AstNodes;
6
+ readonly start: number;
7
+ /** 起点行 */
8
+ get top(): number;
9
+ /** 起点列 */
10
+ get left(): number;
11
+ /**
12
+ * @param token 节点
13
+ * @param start 起点
14
+ */
15
+ constructor(token: AstNodes, start: number);
16
+ /** 计算位置 */
17
+ getPosition(): Position;
18
+ }