wikiparser-node 1.0.0-beta.3 → 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.
Files changed (146) hide show
  1. package/README.md +10 -9
  2. package/bundle/bundle.min.js +37 -0
  3. package/config/llwiki.json +1 -35
  4. package/config/minimum.json +136 -0
  5. package/config/moegirl.json +1 -44
  6. package/config/zhwiki.json +1 -466
  7. package/extensions/dist/base.js +105 -0
  8. package/extensions/dist/editor.js +172 -0
  9. package/extensions/dist/highlight.js +46 -0
  10. package/extensions/dist/lint.js +43 -0
  11. package/extensions/editor.css +63 -0
  12. package/extensions/ui.css +119 -0
  13. package/package.json +35 -20
  14. package/config/.schema.json +0 -160
  15. package/dist/index.d.ts +0 -62
  16. package/dist/index.js +0 -305
  17. package/dist/internal.d.ts +0 -44
  18. package/dist/lib/element.d.ts +0 -186
  19. package/dist/lib/element.js +0 -659
  20. package/dist/lib/node.d.ts +0 -178
  21. package/dist/lib/node.js +0 -458
  22. package/dist/lib/range.d.ts +0 -132
  23. package/dist/lib/range.js +0 -387
  24. package/dist/lib/ranges.d.ts +0 -28
  25. package/dist/lib/ranges.js +0 -125
  26. package/dist/lib/text.d.ts +0 -78
  27. package/dist/lib/text.js +0 -227
  28. package/dist/lib/title.d.ts +0 -33
  29. package/dist/lib/title.js +0 -110
  30. package/dist/mixin/attributesParent.js +0 -93
  31. package/dist/mixin/fixed.js +0 -32
  32. package/dist/mixin/hidden.js +0 -23
  33. package/dist/mixin/singleLine.js +0 -26
  34. package/dist/mixin/sol.js +0 -44
  35. package/dist/parser/braces.js +0 -125
  36. package/dist/parser/commentAndExt.js +0 -58
  37. package/dist/parser/converter.js +0 -38
  38. package/dist/parser/externalLinks.js +0 -28
  39. package/dist/parser/hrAndDoubleUnderscore.js +0 -40
  40. package/dist/parser/html.js +0 -38
  41. package/dist/parser/links.js +0 -94
  42. package/dist/parser/list.js +0 -61
  43. package/dist/parser/magicLinks.js +0 -39
  44. package/dist/parser/quotes.js +0 -67
  45. package/dist/parser/selector.js +0 -165
  46. package/dist/parser/table.js +0 -117
  47. package/dist/src/arg.d.ts +0 -77
  48. package/dist/src/arg.js +0 -199
  49. package/dist/src/atom.d.ts +0 -11
  50. package/dist/src/atom.js +0 -24
  51. package/dist/src/attribute.d.ts +0 -106
  52. package/dist/src/attribute.js +0 -436
  53. package/dist/src/attributes.d.ts +0 -119
  54. package/dist/src/attributes.js +0 -366
  55. package/dist/src/converter.d.ts +0 -75
  56. package/dist/src/converter.js +0 -141
  57. package/dist/src/converterFlags.d.ts +0 -107
  58. package/dist/src/converterFlags.js +0 -238
  59. package/dist/src/converterRule.d.ts +0 -96
  60. package/dist/src/converterRule.js +0 -255
  61. package/dist/src/extLink.d.ts +0 -63
  62. package/dist/src/extLink.js +0 -157
  63. package/dist/src/gallery.d.ts +0 -70
  64. package/dist/src/gallery.js +0 -137
  65. package/dist/src/heading.d.ts +0 -82
  66. package/dist/src/heading.js +0 -135
  67. package/dist/src/hidden.d.ts +0 -19
  68. package/dist/src/hidden.js +0 -26
  69. package/dist/src/html.d.ts +0 -113
  70. package/dist/src/html.js +0 -245
  71. package/dist/src/imageParameter.d.ts +0 -82
  72. package/dist/src/imageParameter.js +0 -256
  73. package/dist/src/imagemap.d.ts +0 -77
  74. package/dist/src/imagemap.js +0 -169
  75. package/dist/src/imagemapLink.d.ts +0 -48
  76. package/dist/src/imagemapLink.js +0 -43
  77. package/dist/src/index.d.ts +0 -156
  78. package/dist/src/index.js +0 -819
  79. package/dist/src/link/base.d.ts +0 -69
  80. package/dist/src/link/base.js +0 -231
  81. package/dist/src/link/category.d.ts +0 -14
  82. package/dist/src/link/category.js +0 -28
  83. package/dist/src/link/file.d.ts +0 -105
  84. package/dist/src/link/file.js +0 -269
  85. package/dist/src/link/galleryImage.d.ts +0 -37
  86. package/dist/src/link/galleryImage.js +0 -100
  87. package/dist/src/link/index.d.ts +0 -50
  88. package/dist/src/link/index.js +0 -134
  89. package/dist/src/magicLink.d.ts +0 -46
  90. package/dist/src/magicLink.js +0 -131
  91. package/dist/src/nested.d.ts +0 -47
  92. package/dist/src/nested.js +0 -92
  93. package/dist/src/nowiki/base.d.ts +0 -38
  94. package/dist/src/nowiki/base.js +0 -31
  95. package/dist/src/nowiki/comment.d.ts +0 -42
  96. package/dist/src/nowiki/comment.js +0 -64
  97. package/dist/src/nowiki/dd.d.ts +0 -22
  98. package/dist/src/nowiki/dd.js +0 -46
  99. package/dist/src/nowiki/doubleUnderscore.d.ts +0 -35
  100. package/dist/src/nowiki/doubleUnderscore.js +0 -48
  101. package/dist/src/nowiki/hr.d.ts +0 -30
  102. package/dist/src/nowiki/hr.js +0 -36
  103. package/dist/src/nowiki/index.d.ts +0 -19
  104. package/dist/src/nowiki/index.js +0 -23
  105. package/dist/src/nowiki/list.d.ts +0 -16
  106. package/dist/src/nowiki/list.js +0 -13
  107. package/dist/src/nowiki/noinclude.d.ts +0 -21
  108. package/dist/src/nowiki/noinclude.js +0 -24
  109. package/dist/src/nowiki/quote.d.ts +0 -27
  110. package/dist/src/nowiki/quote.js +0 -58
  111. package/dist/src/onlyinclude.d.ts +0 -38
  112. package/dist/src/onlyinclude.js +0 -66
  113. package/dist/src/paramTag/index.d.ts +0 -50
  114. package/dist/src/paramTag/index.js +0 -78
  115. package/dist/src/paramTag/inputbox.d.ts +0 -7
  116. package/dist/src/paramTag/inputbox.js +0 -21
  117. package/dist/src/parameter.d.ts +0 -95
  118. package/dist/src/parameter.js +0 -204
  119. package/dist/src/pre.d.ts +0 -27
  120. package/dist/src/pre.js +0 -44
  121. package/dist/src/syntax.d.ts +0 -22
  122. package/dist/src/syntax.js +0 -67
  123. package/dist/src/table/base.d.ts +0 -60
  124. package/dist/src/table/base.js +0 -80
  125. package/dist/src/table/index.d.ts +0 -247
  126. package/dist/src/table/index.js +0 -783
  127. package/dist/src/table/td.d.ts +0 -119
  128. package/dist/src/table/td.js +0 -268
  129. package/dist/src/table/tr.d.ts +0 -31
  130. package/dist/src/table/tr.js +0 -50
  131. package/dist/src/table/trBase.d.ts +0 -62
  132. package/dist/src/table/trBase.js +0 -157
  133. package/dist/src/tagPair/ext.d.ts +0 -54
  134. package/dist/src/tagPair/ext.js +0 -150
  135. package/dist/src/tagPair/include.d.ts +0 -43
  136. package/dist/src/tagPair/include.js +0 -43
  137. package/dist/src/tagPair/index.d.ts +0 -57
  138. package/dist/src/tagPair/index.js +0 -113
  139. package/dist/src/transclude.d.ts +0 -200
  140. package/dist/src/transclude.js +0 -732
  141. package/dist/util/debug.js +0 -42
  142. package/dist/util/diff.js +0 -71
  143. package/dist/util/lint.js +0 -38
  144. package/dist/util/string.js +0 -74
  145. package/errors/README +0 -1
  146. package/printed/README +0 -1
@@ -1,132 +0,0 @@
1
- import type { AstNodes, Token } 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 endContainer(): AstNodes;
14
- /** 终点位置 */
15
- get endOffset(): number;
16
- /** 终点绝对位置 */
17
- get endIndex(): number;
18
- /** 起始和终止位置是否重合 */
19
- get collapsed(): boolean;
20
- /** 最近的公共祖先 */
21
- get commonAncestorContainer(): AstNodes;
22
- /**
23
- * 设置起点
24
- * @param startNode 起点容器
25
- * @param startOffset 起点位置
26
- * @throws `RangeError` 不在同一个文档
27
- * @throws `RangeError` offset取值超出范围
28
- */
29
- setStart(startNode: AstNodes, startOffset: number): void;
30
- /**
31
- * 设置终点
32
- * @param endNode 终点容器
33
- * @param endOffset 终点位置
34
- * @throws `RangeError` 不在同一个文档
35
- * @throws `RangeError` offset取值超出范围
36
- */
37
- setEnd(endNode: AstNodes, endOffset: number): void;
38
- /**
39
- * 在节点后设置起点
40
- * @param referenceNode 节点
41
- */
42
- setStartAfter(referenceNode: AstNodes): void;
43
- /**
44
- * 在节点后设置终点
45
- * @param referenceNode 节点
46
- */
47
- setEndAfter(referenceNode: AstNodes): void;
48
- /**
49
- * 在节点前设置起点
50
- * @param referenceNode 节点
51
- */
52
- setStartBefore(referenceNode: AstNodes): void;
53
- /**
54
- * 在节点前设置终点
55
- * @param referenceNode 节点
56
- */
57
- setEndBefore(referenceNode: AstNodes): void;
58
- /**
59
- * 根据字符位置设置起点
60
- * @param root 根节点
61
- * @param index 字符位置
62
- */
63
- setStartIndex(root: Token, index: number): void;
64
- /**
65
- * 根据字符位置设置终点
66
- * @param root 根节点
67
- * @param index 字符位置
68
- */
69
- setEndIndex(root: Token, index: number): void;
70
- /**
71
- * 根据行列号设置起点
72
- * @param root 根节点
73
- * @param x 列
74
- * @param y 行
75
- */
76
- setStartPoint(root: Token, x: number, y: number): void;
77
- /**
78
- * 根据行列号设置终点
79
- * @param root 根节点
80
- * @param x 列
81
- * @param y 行
82
- */
83
- setEndPoint(root: Token, x: number, y: number): void;
84
- /**
85
- * 设置Range包含整个节点的内容
86
- * @param referenceNode 节点
87
- */
88
- selectNodeContents(referenceNode: AstNodes): void;
89
- /**
90
- * 设置Range包含整个节点
91
- * @param referenceNode 节点
92
- */
93
- selectNode(referenceNode: AstNodes): void;
94
- /**
95
- * 使起始和终止位置重合
96
- * @param toStart 重合至起始位置
97
- */
98
- collapse(toStart?: boolean): void;
99
- /**
100
- * 比较端点和Range的位置
101
- * @param referenceNode 端点容器
102
- * @param offset 端点位置
103
- * @throws `RangeError` 不在同一个文档
104
- */
105
- comparePoint(referenceNode: AstNodes, offset: number): -1 | 0 | 1;
106
- /**
107
- * 端点是否在Range中
108
- * @param referenceNode 端点容器
109
- * @param offset 端点位置
110
- * @throws `RangeError` 不在同一个文档
111
- */
112
- isPointInRange(referenceNode: AstNodes, offset: number): boolean;
113
- /**
114
- * 是否与节点相交
115
- * @param referenceNode 节点
116
- * @throws `RangeError` 不在同一个文档
117
- */
118
- intersectsNode(referenceNode: AstNodes): boolean;
119
- /** 复制 */
120
- cloneRange(): AstRange;
121
- /** 删除 */
122
- deleteContents(): void;
123
- /** 获取行列位置和大小 */
124
- getBoundingClientRect(): Dimension & Position;
125
- /**
126
- * 在起始位置插入节点
127
- * @param newNode 插入的节点
128
- */
129
- insertNode(newNode: AstNodes): void;
130
- /** 范围内的全部文本 */
131
- toString(): string;
132
- }
package/dist/lib/range.js DELETED
@@ -1,387 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AstRange = void 0;
4
- const index_1 = require("../index");
5
- /**
6
- * 计算绝对位置
7
- * @param referenceNode 容器
8
- * @param offset 相对位置
9
- */
10
- const getIndex = (referenceNode, offset) => referenceNode.getAbsoluteIndex() + referenceNode.getRelativeIndex(offset);
11
- /** 模拟Range对象 */
12
- class AstRange {
13
- #startContainer;
14
- #startOffset;
15
- #endContainer;
16
- #endOffset;
17
- /**
18
- * 未初始化时抛出错误
19
- * @param start 是否未初始化起点
20
- * @throws `Error` 未初始化
21
- */
22
- // eslint-disable-next-line class-methods-use-this
23
- #notInit(start) {
24
- throw new Error(`请先指定${start ? '起始' : '终止'}位置!`);
25
- }
26
- /** 起点容器 */
27
- get startContainer() {
28
- return this.#startContainer ?? this.#notInit(true);
29
- }
30
- /** 起点位置 */
31
- get startOffset() {
32
- return this.#startOffset ?? this.#notInit(true);
33
- }
34
- /** 起点绝对位置 */
35
- get startIndex() {
36
- const { startContainer, startOffset } = this;
37
- return getIndex(startContainer, startOffset);
38
- }
39
- /** 终点容器 */
40
- get endContainer() {
41
- return this.#endContainer ?? this.#notInit(false);
42
- }
43
- /** 终点位置 */
44
- get endOffset() {
45
- return this.#endOffset ?? this.#notInit(false);
46
- }
47
- /** 终点绝对位置 */
48
- get endIndex() {
49
- const { endContainer, endOffset } = this;
50
- return getIndex(endContainer, endOffset);
51
- }
52
- /** 起始和终止位置是否重合 */
53
- get collapsed() {
54
- return this.startIndex === this.endIndex;
55
- }
56
- /** 最近的公共祖先 */
57
- get commonAncestorContainer() {
58
- const { startContainer, endContainer } = this;
59
- let parentNode = startContainer;
60
- while (!parentNode.contains(endContainer)) {
61
- ({ parentNode } = parentNode);
62
- }
63
- return parentNode;
64
- }
65
- /**
66
- * 设置起点
67
- * @param startNode 起点容器
68
- * @param startOffset 起点位置
69
- * @throws `RangeError` 不在同一个文档
70
- * @throws `RangeError` offset取值超出范围
71
- */
72
- setStart(startNode, startOffset) {
73
- const root = this.#endContainer?.getRootNode(), { length } = startNode;
74
- if (root && root !== startNode.getRootNode()) {
75
- throw new RangeError('起点不在同一个文档中!');
76
- }
77
- else if (startOffset < 0 || startOffset > length) {
78
- throw new RangeError(`offset取值范围应为 0 ~ ${length}`);
79
- }
80
- else if (root && getIndex(startNode, startOffset) > this.endIndex) {
81
- throw new RangeError('起点不能位于终点之后!');
82
- }
83
- this.#startContainer = startNode;
84
- this.#startOffset = startOffset;
85
- }
86
- /**
87
- * 设置终点
88
- * @param endNode 终点容器
89
- * @param endOffset 终点位置
90
- * @throws `RangeError` 不在同一个文档
91
- * @throws `RangeError` offset取值超出范围
92
- */
93
- setEnd(endNode, endOffset) {
94
- const root = this.#startContainer?.getRootNode(), { length } = endNode;
95
- if (root && root !== endNode.getRootNode()) {
96
- throw new RangeError('终点不在同一个文档中!');
97
- }
98
- else if (endOffset < 0 || endOffset > length) {
99
- throw new RangeError(`offset取值范围应为 0 ~ ${length}`);
100
- }
101
- else if (root && getIndex(endNode, endOffset) < this.startIndex) {
102
- throw new RangeError('终点不能位于起点之前!');
103
- }
104
- this.#endContainer = endNode;
105
- this.#endOffset = endOffset;
106
- }
107
- /**
108
- * 在节点后设置
109
- * @param method 方法名
110
- * @param referenceNode 节点
111
- */
112
- #setAfter(method, referenceNode) {
113
- if (referenceNode.type === 'root') {
114
- this[method](referenceNode, referenceNode.length);
115
- }
116
- else {
117
- const { parentNode } = referenceNode;
118
- this[method](parentNode, parentNode.childNodes.indexOf(referenceNode) + 1);
119
- }
120
- }
121
- /**
122
- * 在节点后设置起点
123
- * @param referenceNode 节点
124
- */
125
- setStartAfter(referenceNode) {
126
- this.#setAfter('setStart', referenceNode);
127
- }
128
- /**
129
- * 在节点后设置终点
130
- * @param referenceNode 节点
131
- */
132
- setEndAfter(referenceNode) {
133
- this.#setAfter('setEnd', referenceNode);
134
- }
135
- /**
136
- * 在节点前设置
137
- * @param method 方法名
138
- * @param referenceNode 节点
139
- */
140
- #setBefore(method, referenceNode) {
141
- if (referenceNode.type === 'root') {
142
- this[method](referenceNode, 0);
143
- }
144
- else {
145
- const { parentNode } = referenceNode;
146
- this[method](parentNode, parentNode.childNodes.indexOf(referenceNode));
147
- }
148
- }
149
- /**
150
- * 在节点前设置起点
151
- * @param referenceNode 节点
152
- */
153
- setStartBefore(referenceNode) {
154
- this.#setBefore('setStart', referenceNode);
155
- }
156
- /**
157
- * 在节点前设置终点
158
- * @param referenceNode 节点
159
- */
160
- setEndBefore(referenceNode) {
161
- this.#setBefore('setEnd', referenceNode);
162
- }
163
- /**
164
- * 根据字符位置设置
165
- * @param method 方法名
166
- * @param root 根节点
167
- * @param index 字符位置
168
- * @throws `RangeError` 超出有效范围
169
- */
170
- #setIndex(method, root, index) {
171
- const caretPosition = root.caretPositionFromIndex(index);
172
- if (caretPosition === undefined) {
173
- throw new RangeError('字符坐标超出有效的范围!');
174
- }
175
- const { offsetNode, offset } = caretPosition;
176
- if (offsetNode.type === 'text') {
177
- this[method](offsetNode, offset);
178
- }
179
- else {
180
- const i = offsetNode.childNodes.findIndex(child => child.getRelativeIndex() >= offset);
181
- this[method](offsetNode, i === -1 ? offsetNode.length : i);
182
- }
183
- }
184
- /**
185
- * 根据字符位置设置起点
186
- * @param root 根节点
187
- * @param index 字符位置
188
- */
189
- setStartIndex(root, index) {
190
- this.#setIndex('setStart', root, index);
191
- }
192
- /**
193
- * 根据字符位置设置终点
194
- * @param root 根节点
195
- * @param index 字符位置
196
- */
197
- setEndIndex(root, index) {
198
- this.#setIndex('setEnd', root, index);
199
- }
200
- /**
201
- * 根据行列号设置
202
- * @param method 方法名
203
- * @param root 根节点
204
- * @param x 列
205
- * @param y 行
206
- */
207
- #setPoint(method, root, x, y) {
208
- this.#setIndex(method, root, root.indexFromPos(y, x));
209
- }
210
- /**
211
- * 根据行列号设置起点
212
- * @param root 根节点
213
- * @param x 列
214
- * @param y 行
215
- */
216
- setStartPoint(root, x, y) {
217
- this.#setPoint('setStart', root, x, y);
218
- }
219
- /**
220
- * 根据行列号设置终点
221
- * @param root 根节点
222
- * @param x 列
223
- * @param y 行
224
- */
225
- setEndPoint(root, x, y) {
226
- this.#setPoint('setEnd', root, x, y);
227
- }
228
- /**
229
- * 设置Range包含整个节点的内容
230
- * @param referenceNode 节点
231
- */
232
- selectNodeContents(referenceNode) {
233
- this.#startContainer = referenceNode;
234
- this.#startOffset = 0;
235
- this.#endContainer = referenceNode;
236
- this.#endOffset = referenceNode.length;
237
- }
238
- /**
239
- * 设置Range包含整个节点
240
- * @param referenceNode 节点
241
- */
242
- selectNode(referenceNode) {
243
- if (referenceNode.type === 'root') {
244
- this.selectNodeContents(referenceNode);
245
- }
246
- else {
247
- const { parentNode } = referenceNode, i = parentNode.childNodes.indexOf(referenceNode);
248
- this.#startContainer = parentNode;
249
- this.#startOffset = i;
250
- this.#endContainer = parentNode;
251
- this.#endOffset = i + 1;
252
- }
253
- }
254
- /**
255
- * 使起始和终止位置重合
256
- * @param toStart 重合至起始位置
257
- */
258
- collapse(toStart = false) {
259
- if (toStart) {
260
- this.#endContainer = this.startContainer;
261
- this.#endOffset = this.startOffset;
262
- }
263
- else {
264
- this.#startContainer = this.endContainer;
265
- this.#startOffset = this.endOffset;
266
- }
267
- }
268
- /**
269
- * 比较端点和Range的位置
270
- * @param referenceNode 端点容器
271
- * @param offset 端点位置
272
- * @throws `RangeError` 不在同一个文档
273
- */
274
- comparePoint(referenceNode, offset) {
275
- const { startContainer, startIndex, endIndex } = this;
276
- if (startContainer.getRootNode() !== referenceNode.getRootNode()) {
277
- throw new RangeError('待比较的端点不在同一个文档中!');
278
- }
279
- const index = getIndex(referenceNode, offset);
280
- if (index < startIndex) {
281
- return -1;
282
- }
283
- return index > endIndex ? 1 : 0;
284
- }
285
- /**
286
- * 端点是否在Range中
287
- * @param referenceNode 端点容器
288
- * @param offset 端点位置
289
- * @throws `RangeError` 不在同一个文档
290
- */
291
- isPointInRange(referenceNode, offset) {
292
- return this.comparePoint(referenceNode, offset) === 0;
293
- }
294
- /**
295
- * 是否与节点相交
296
- * @param referenceNode 节点
297
- * @throws `RangeError` 不在同一个文档
298
- */
299
- intersectsNode(referenceNode) {
300
- const { startContainer, startIndex, endIndex } = this;
301
- if (startContainer.getRootNode() !== referenceNode.getRootNode()) {
302
- throw new RangeError('待比较的端点不在同一个文档中!');
303
- }
304
- const index = referenceNode.getAbsoluteIndex();
305
- return index < endIndex && index + String(referenceNode).length > startIndex;
306
- }
307
- /** 复制 */
308
- cloneRange() {
309
- const range = new AstRange();
310
- range.setStart(this.startContainer, this.startOffset);
311
- range.setEnd(this.endContainer, this.endOffset);
312
- return range;
313
- }
314
- /** 删除 */
315
- deleteContents() {
316
- const { startContainer, startOffset } = this;
317
- let { endContainer, endOffset } = this, parentNode;
318
- while (!endContainer.contains(startContainer)) {
319
- ({ parentNode } = endContainer);
320
- if (endContainer.type === 'text') {
321
- endContainer.deleteData(0, endOffset);
322
- }
323
- else {
324
- for (let i = endOffset - 1; i >= 0; i--) {
325
- endContainer.removeAt(i);
326
- }
327
- }
328
- endOffset = parentNode.childNodes.indexOf(endContainer);
329
- endContainer = parentNode;
330
- }
331
- while (endContainer !== startContainer) {
332
- const { childNodes } = endContainer, j = childNodes.findIndex(child => child.contains(startContainer));
333
- for (let i = endOffset - 1; i > j; i--) {
334
- endContainer.removeAt(i);
335
- }
336
- endContainer = childNodes[j];
337
- endOffset = endContainer.length;
338
- }
339
- if (endContainer.type === 'text') {
340
- endContainer.deleteData(startOffset, endOffset);
341
- }
342
- else {
343
- for (let i = endOffset - 1; i >= startOffset; i--) {
344
- endContainer.removeAt(i);
345
- }
346
- }
347
- this.#endContainer = endContainer;
348
- this.#endOffset = startOffset;
349
- }
350
- /** 获取行列位置和大小 */
351
- getBoundingClientRect() {
352
- let { startContainer, startOffset, endContainer, endOffset } = this, parentNode;
353
- while (!startContainer.contains(endContainer)) {
354
- ({ parentNode } = startContainer);
355
- startOffset = parentNode.childNodes.indexOf(startContainer);
356
- startContainer = parentNode;
357
- }
358
- while (endContainer !== startContainer) {
359
- ({ parentNode } = endContainer);
360
- endOffset = parentNode.childNodes.indexOf(endContainer) + 1;
361
- endContainer = parentNode;
362
- }
363
- const { top, left } = startContainer.posFromIndex(startContainer.getRelativeIndex(startOffset)), { top: bottom, left: right } = startContainer.posFromIndex(startContainer.getRelativeIndex(endOffset));
364
- return { top, left, height: bottom - top + 1, width: bottom === top ? right - left : right };
365
- }
366
- /**
367
- * 在起始位置插入节点
368
- * @param newNode 插入的节点
369
- */
370
- insertNode(newNode) {
371
- const { startContainer, startOffset } = this;
372
- if (startContainer.type === 'text') {
373
- startContainer.splitText(startOffset);
374
- startContainer.after(newNode);
375
- }
376
- else {
377
- startContainer.insertAt(newNode, startOffset);
378
- }
379
- }
380
- /** 范围内的全部文本 */
381
- toString() {
382
- const { startContainer, startIndex, endIndex } = this;
383
- return String(startContainer.getRootNode()).slice(startIndex, endIndex);
384
- }
385
- }
386
- exports.AstRange = AstRange;
387
- index_1.default.classes['AstRange'] = __filename;
@@ -1,28 +0,0 @@
1
- /** 模拟Python的Range对象。除`step`至少为`1`外,允许负数、小数或`end < start`的情形。 */
2
- export declare class Range {
3
- start: number;
4
- end: number;
5
- step: number;
6
- /**
7
- * @param s 表达式
8
- * @throws `RangeError` 起点、终点和步长均应为整数
9
- * @throws `RangeError` n的系数不能为0
10
- * @throws `RangeError` 应使用CSS选择器或Python切片的格式
11
- */
12
- constructor(s: string | Range);
13
- /**
14
- * 将Range转换为针对特定数组的下标集
15
- * @param arr 参考数组
16
- */
17
- applyTo(arr: number | unknown[]): 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 | (number | string | Range)[]);
23
- /**
24
- * 将Ranges转换为针对特定Array的下标集
25
- * @param arr 参考数组
26
- */
27
- applyTo(arr: number | unknown[]): number[];
28
- }
@@ -1,125 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Ranges = exports.Range = void 0;
4
- const index_1 = require("../index");
5
- /** 模拟Python的Range对象。除`step`至少为`1`外,允许负数、小数或`end < start`的情形。 */
6
- class Range {
7
- start;
8
- end;
9
- step;
10
- /**
11
- * @param s 表达式
12
- * @throws `RangeError` 起点、终点和步长均应为整数
13
- * @throws `RangeError` n的系数不能为0
14
- * @throws `RangeError` 应使用CSS选择器或Python切片的格式
15
- */
16
- constructor(s) {
17
- if (s instanceof Range) {
18
- Object.assign(this, s);
19
- return;
20
- }
21
- const str = s.trim();
22
- if (str === 'odd') {
23
- Object.assign(this, { start: 1, end: Infinity, step: 2 });
24
- }
25
- else if (str === 'even') {
26
- Object.assign(this, { start: 0, end: Infinity, step: 2 });
27
- }
28
- else if (str.includes(':')) {
29
- const [start, end, step = '1'] = str.split(':', 3);
30
- this.start = Number(start);
31
- this.end = Number(end || Infinity);
32
- this.step = Math.max(Number(step), 1);
33
- if (!Number.isInteger(this.start)) {
34
- throw new RangeError(`起点 ${this.start} 应为整数!`);
35
- }
36
- else if (this.end !== Infinity && !Number.isInteger(this.end)) {
37
- throw new RangeError(`终点 ${this.end} 应为整数!`);
38
- }
39
- else if (!Number.isInteger(this.step)) {
40
- throw new RangeError(`步长 ${this.step} 应为整数!`);
41
- }
42
- }
43
- else {
44
- const mt = /^([+-])?(\d+)?n(?:([+-])(\d+))?$/u
45
- .exec(str);
46
- if (mt) {
47
- const [, sgnA = '+', a = 1, sgnB = '+'] = mt, b = Number(mt[4] ?? 0);
48
- this.step = Number(a);
49
- if (this.step === 0) {
50
- throw new RangeError(`参数 ${str} 中 "n" 的系数不允许为 0!`);
51
- }
52
- else if (sgnA === '+') {
53
- this.start = sgnB === '+' || b === 0 ? b : this.step - 1 - (b - 1) % this.step;
54
- this.end = Infinity;
55
- }
56
- else if (sgnB === '-') {
57
- this.start = 0;
58
- this.end = b > 0 ? 0 : this.step;
59
- }
60
- else {
61
- this.start = b % this.step;
62
- this.end = this.step + b;
63
- }
64
- }
65
- else {
66
- throw new RangeError(`参数 ${str} 应写作CSS选择器的 "an+b" 形式或Python切片!`);
67
- }
68
- }
69
- }
70
- /**
71
- * 将Range转换为针对特定数组的下标集
72
- * @param arr 参考数组
73
- */
74
- applyTo(arr) {
75
- return new Array(typeof arr === 'number' ? arr : arr.length).fill(undefined).map((_, i) => i)
76
- .slice(this.start, this.end)
77
- .filter((_, j) => j % this.step === 0);
78
- }
79
- }
80
- exports.Range = Range;
81
- /** @extends {Array<number|Range>} */
82
- class Ranges extends Array {
83
- /** @param a 表达式数组 */
84
- constructor(a) {
85
- super();
86
- if (a === undefined) {
87
- return;
88
- }
89
- const arr = Array.isArray(a) ? a : [a];
90
- for (const ele of arr) {
91
- if (ele instanceof Range) {
92
- this.push(new Range(ele));
93
- continue;
94
- }
95
- const number = Number(ele);
96
- if (Number.isInteger(number)) {
97
- this.push(number);
98
- }
99
- else if (typeof ele === 'string' && Number.isNaN(number)) {
100
- try {
101
- const range = new Range(ele);
102
- this.push(range);
103
- }
104
- catch { }
105
- }
106
- }
107
- }
108
- /**
109
- * 将Ranges转换为针对特定Array的下标集
110
- * @param arr 参考数组
111
- */
112
- applyTo(arr) {
113
- const length = typeof arr === 'number' ? arr : arr.length;
114
- return [
115
- ...new Set([...this].flatMap(ele => {
116
- if (typeof ele === 'number') {
117
- return ele < 0 ? ele + length : ele;
118
- }
119
- return ele.applyTo(length);
120
- })),
121
- ].filter(i => i >= 0 && i < length).sort();
122
- }
123
- }
124
- exports.Ranges = Ranges;
125
- index_1.default.classes['Ranges'] = __filename;