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