wikiparser-node 1.12.5-b → 1.12.6

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 (174) hide show
  1. package/config/.schema.json +203 -0
  2. package/config/default.json +30 -29
  3. package/config/enwiki.json +829 -15
  4. package/config/llwiki.json +56 -21
  5. package/config/moegirl.json +65 -21
  6. package/config/zhwiki.json +494 -28
  7. package/dist/addon/table.js +494 -0
  8. package/dist/addon/token.js +392 -0
  9. package/dist/addon/transclude.js +184 -0
  10. package/dist/base.d.ts +82 -0
  11. package/dist/base.js +40 -0
  12. package/dist/index.d.ts +33 -0
  13. package/dist/index.js +275 -0
  14. package/dist/internal.d.ts +47 -0
  15. package/dist/lib/element.d.ts +125 -0
  16. package/dist/lib/element.js +377 -0
  17. package/dist/lib/node.d.ts +173 -0
  18. package/dist/lib/node.js +478 -0
  19. package/dist/lib/range.d.ts +105 -0
  20. package/dist/lib/range.js +406 -0
  21. package/dist/lib/ranges.d.ts +28 -0
  22. package/dist/lib/ranges.js +126 -0
  23. package/dist/lib/rect.d.ts +18 -0
  24. package/dist/lib/rect.js +36 -0
  25. package/dist/lib/text.d.ts +58 -0
  26. package/dist/lib/text.js +414 -0
  27. package/dist/lib/title.d.ts +49 -0
  28. package/dist/lib/title.js +253 -0
  29. package/dist/mixin/attributesParent.d.ts +49 -0
  30. package/dist/mixin/attributesParent.js +80 -0
  31. package/dist/mixin/fixed.d.ts +5 -0
  32. package/dist/mixin/fixed.js +32 -0
  33. package/dist/mixin/flagsParent.d.ts +43 -0
  34. package/dist/mixin/flagsParent.js +64 -0
  35. package/dist/mixin/hidden.d.ts +7 -0
  36. package/dist/mixin/hidden.js +39 -0
  37. package/dist/mixin/magicLinkParent.d.ts +19 -0
  38. package/dist/mixin/magicLinkParent.js +43 -0
  39. package/dist/mixin/singleLine.d.ts +5 -0
  40. package/dist/mixin/singleLine.js +25 -0
  41. package/dist/mixin/sol.d.ts +6 -0
  42. package/dist/mixin/sol.js +45 -0
  43. package/dist/mixin/syntax.d.ts +8 -0
  44. package/dist/mixin/syntax.js +46 -0
  45. package/dist/parser/braces.js +152 -0
  46. package/dist/parser/commentAndExt.js +84 -0
  47. package/dist/parser/converter.js +41 -0
  48. package/dist/parser/externalLinks.js +39 -0
  49. package/dist/parser/hrAndDoubleUnderscore.js +44 -0
  50. package/dist/parser/html.js +40 -0
  51. package/dist/parser/links.js +103 -0
  52. package/dist/parser/list.js +116 -0
  53. package/dist/parser/magicLinks.js +55 -0
  54. package/dist/parser/quotes.js +69 -0
  55. package/dist/parser/redirect.js +28 -0
  56. package/dist/parser/selector.js +393 -0
  57. package/dist/parser/table.js +125 -0
  58. package/dist/src/arg.d.ts +49 -0
  59. package/dist/src/arg.js +220 -0
  60. package/dist/src/atom.d.ts +14 -0
  61. package/dist/src/atom.js +54 -0
  62. package/dist/src/attribute.d.ts +64 -0
  63. package/dist/src/attribute.js +482 -0
  64. package/dist/src/attributes.d.ts +103 -0
  65. package/dist/src/attributes.js +380 -0
  66. package/dist/src/converter.d.ts +28 -0
  67. package/dist/src/converter.js +153 -0
  68. package/dist/src/converterFlags.d.ts +80 -0
  69. package/dist/src/converterFlags.js +241 -0
  70. package/dist/src/converterRule.d.ts +71 -0
  71. package/dist/src/converterRule.js +218 -0
  72. package/dist/src/extLink.d.ts +36 -0
  73. package/dist/src/extLink.js +223 -0
  74. package/dist/src/gallery.d.ts +51 -0
  75. package/dist/src/gallery.js +166 -0
  76. package/dist/src/heading.d.ts +44 -0
  77. package/dist/src/heading.js +222 -0
  78. package/dist/src/hidden.d.ts +9 -0
  79. package/dist/src/hidden.js +82 -0
  80. package/dist/src/html.d.ts +61 -0
  81. package/dist/src/html.js +344 -0
  82. package/dist/src/imageParameter.d.ts +60 -0
  83. package/dist/src/imageParameter.js +262 -0
  84. package/dist/src/imagemap.d.ts +47 -0
  85. package/dist/src/imagemap.js +148 -0
  86. package/dist/src/imagemapLink.d.ts +35 -0
  87. package/dist/src/imagemapLink.js +99 -0
  88. package/dist/src/index.d.ts +136 -0
  89. package/dist/src/index.js +790 -0
  90. package/dist/src/link/base.d.ts +52 -0
  91. package/dist/src/link/base.js +258 -0
  92. package/dist/src/link/category.d.ts +22 -0
  93. package/dist/src/link/category.js +36 -0
  94. package/dist/src/link/file.d.ts +102 -0
  95. package/dist/src/link/file.js +345 -0
  96. package/dist/src/link/galleryImage.d.ts +29 -0
  97. package/dist/src/link/galleryImage.js +133 -0
  98. package/dist/src/link/index.d.ts +39 -0
  99. package/dist/src/link/index.js +100 -0
  100. package/dist/src/link/redirectTarget.d.ts +27 -0
  101. package/dist/src/link/redirectTarget.js +71 -0
  102. package/dist/src/magicLink.d.ts +57 -0
  103. package/dist/src/magicLink.js +261 -0
  104. package/dist/src/nested.d.ts +40 -0
  105. package/dist/src/nested.js +108 -0
  106. package/dist/src/nowiki/base.d.ts +28 -0
  107. package/dist/src/nowiki/base.js +90 -0
  108. package/dist/src/nowiki/comment.d.ts +14 -0
  109. package/dist/src/nowiki/comment.js +123 -0
  110. package/dist/src/nowiki/dd.d.ts +8 -0
  111. package/dist/src/nowiki/dd.js +74 -0
  112. package/dist/src/nowiki/doubleUnderscore.d.ts +15 -0
  113. package/dist/src/nowiki/doubleUnderscore.js +101 -0
  114. package/dist/src/nowiki/hr.d.ts +5 -0
  115. package/dist/src/nowiki/hr.js +72 -0
  116. package/dist/src/nowiki/index.d.ts +14 -0
  117. package/dist/src/nowiki/index.js +30 -0
  118. package/dist/src/nowiki/list.d.ts +5 -0
  119. package/dist/src/nowiki/list.js +67 -0
  120. package/dist/src/nowiki/listBase.d.ts +23 -0
  121. package/dist/src/nowiki/listBase.js +100 -0
  122. package/dist/src/nowiki/noinclude.d.ts +6 -0
  123. package/dist/src/nowiki/noinclude.js +77 -0
  124. package/dist/src/nowiki/quote.d.ts +14 -0
  125. package/dist/src/nowiki/quote.js +149 -0
  126. package/dist/src/onlyinclude.d.ts +13 -0
  127. package/dist/src/onlyinclude.js +60 -0
  128. package/dist/src/paramTag/index.d.ts +28 -0
  129. package/dist/src/paramTag/index.js +80 -0
  130. package/dist/src/paramTag/inputbox.d.ts +8 -0
  131. package/dist/src/paramTag/inputbox.js +24 -0
  132. package/dist/src/parameter.d.ts +60 -0
  133. package/dist/src/parameter.js +267 -0
  134. package/dist/src/pre.d.ts +28 -0
  135. package/dist/src/pre.js +62 -0
  136. package/dist/src/redirect.d.ts +30 -0
  137. package/dist/src/redirect.js +128 -0
  138. package/dist/src/syntax.d.ts +15 -0
  139. package/dist/src/syntax.js +87 -0
  140. package/dist/src/table/base.d.ts +28 -0
  141. package/dist/src/table/base.js +81 -0
  142. package/dist/src/table/index.d.ts +230 -0
  143. package/dist/src/table/index.js +506 -0
  144. package/dist/src/table/td.d.ts +72 -0
  145. package/dist/src/table/td.js +375 -0
  146. package/dist/src/table/tr.d.ts +30 -0
  147. package/dist/src/table/tr.js +61 -0
  148. package/dist/src/table/trBase.d.ts +49 -0
  149. package/dist/src/table/trBase.js +165 -0
  150. package/dist/src/tagPair/ext.d.ts +29 -0
  151. package/dist/src/tagPair/ext.js +229 -0
  152. package/dist/src/tagPair/include.d.ts +33 -0
  153. package/dist/src/tagPair/include.js +132 -0
  154. package/dist/src/tagPair/index.d.ts +23 -0
  155. package/dist/src/tagPair/index.js +130 -0
  156. package/dist/src/transclude.d.ts +159 -0
  157. package/dist/src/transclude.js +598 -0
  158. package/dist/util/constants.js +26 -0
  159. package/dist/util/debug.js +95 -0
  160. package/dist/util/diff.js +83 -0
  161. package/dist/util/html.js +146 -0
  162. package/dist/util/lint.js +32 -0
  163. package/dist/util/string.js +107 -0
  164. package/errors/README +3 -0
  165. package/package.json +21 -28
  166. package/printed/README +3 -0
  167. package/bundle/bundle.min.js +0 -37
  168. package/extensions/dist/base.js +0 -163
  169. package/extensions/dist/codejar.js +0 -52
  170. package/extensions/dist/editor.js +0 -159
  171. package/extensions/dist/highlight.js +0 -30
  172. package/extensions/dist/lint.js +0 -72
  173. package/extensions/editor.css +0 -59
  174. package/extensions/ui.css +0 -162
@@ -0,0 +1,478 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AstNode = void 0;
4
+ /* NOT FOR BROWSER */
5
+ const assert = require("assert/strict");
6
+ const EventEmitter = require("events");
7
+ const constants_1 = require("../util/constants");
8
+ /**
9
+ * 计算字符串的行列数
10
+ * @param str 字符串
11
+ */
12
+ const getDimension = (str) => {
13
+ const lines = str.split('\n'), height = lines.length;
14
+ return { height, width: lines[height - 1].length };
15
+ };
16
+ /**
17
+ * 获取子节点相对于父节点的字符位置
18
+ * @param j 子节点序号
19
+ * @param parent 父节点
20
+ */
21
+ const getIndex = (j, parent) => parent.childNodes.slice(0, j).reduce((acc, cur, i) => acc + cur.toString().length + parent.getGaps(i), 0)
22
+ + parent.getAttribute('padding');
23
+ /* NOT FOR BROWSER */
24
+ /**
25
+ * 定制TypeError消息
26
+ * @param {Function} Constructor 类
27
+ * @param method
28
+ * @param args 可接受的参数类型
29
+ * @throws `TypeError`
30
+ */
31
+ const typeError = ({ name }, method, ...args) => {
32
+ throw new TypeError(`${name}.${method} method only accepts ${args.join('、')} as input parameters!`);
33
+ };
34
+ /* NOT FOR BROWSER END */
35
+ /** 类似Node */
36
+ class AstNode {
37
+ childNodes = [];
38
+ #parentNode;
39
+ /* NOT FOR BROWSER */
40
+ #optional = new Set();
41
+ #events = new EventEmitter();
42
+ /** 首位子节点 */
43
+ get firstChild() {
44
+ return this.childNodes[0];
45
+ }
46
+ /** 末位子节点 */
47
+ get lastChild() {
48
+ return this.childNodes[this.childNodes.length - 1];
49
+ }
50
+ /** 父节点 */
51
+ get parentNode() {
52
+ return this.#parentNode;
53
+ }
54
+ /** 后一个兄弟节点 */
55
+ get nextSibling() {
56
+ const childNodes = this.parentNode?.childNodes;
57
+ return childNodes && childNodes[childNodes.indexOf(this) + 1];
58
+ }
59
+ /** 前一个兄弟节点 */
60
+ get previousSibling() {
61
+ const childNodes = this.parentNode?.childNodes;
62
+ return childNodes && childNodes[childNodes.indexOf(this) - 1];
63
+ }
64
+ /** 行数 */
65
+ get offsetHeight() {
66
+ return this.#getDimension().height;
67
+ }
68
+ /** 最后一行的列数 */
69
+ get offsetWidth() {
70
+ return this.#getDimension().width;
71
+ }
72
+ /* NOT FOR BROWSER */
73
+ /** 后一个非文本兄弟节点 */
74
+ get nextElementSibling() {
75
+ const childNodes = this.parentNode?.childNodes, i = childNodes?.indexOf(this);
76
+ return childNodes?.slice(i + 1).find((child) => child.type !== 'text');
77
+ }
78
+ /** 前一个非文本兄弟节点 */
79
+ get previousElementSibling() {
80
+ const childNodes = this.parentNode?.childNodes, i = childNodes?.indexOf(this);
81
+ return childNodes?.slice(0, i).reverse().find((child) => child.type !== 'text');
82
+ }
83
+ /** 后一个可见的兄弟节点 */
84
+ get nextVisibleSibling() {
85
+ let { nextSibling } = this;
86
+ while (nextSibling?.text() === '') {
87
+ ({ nextSibling } = nextSibling);
88
+ }
89
+ return nextSibling;
90
+ }
91
+ /** 前一个可见的兄弟节点 */
92
+ get previousVisibleSibling() {
93
+ let { previousSibling } = this;
94
+ while (previousSibling?.text() === '') {
95
+ ({ previousSibling } = previousSibling);
96
+ }
97
+ return previousSibling;
98
+ }
99
+ /** 是否具有根节点 */
100
+ get isConnected() {
101
+ return this.getRootNode().type === 'root';
102
+ }
103
+ /** 后方是否还有其他节点(不含后代) */
104
+ get eof() {
105
+ const { parentNode } = this;
106
+ if (!parentNode) {
107
+ return true;
108
+ }
109
+ let { nextSibling } = this;
110
+ while (nextSibling?.type === 'text' && nextSibling.data.trim() === '') {
111
+ ({ nextSibling } = nextSibling);
112
+ }
113
+ return nextSibling === undefined && parentNode.eof;
114
+ }
115
+ /** 相对于父容器的行号 */
116
+ get offsetTop() {
117
+ return this.#getPosition().top;
118
+ }
119
+ /** 相对于父容器的列号 */
120
+ get offsetLeft() {
121
+ return this.#getPosition().left;
122
+ }
123
+ /** 位置、大小和padding */
124
+ get style() {
125
+ return { ...this.#getPosition(), ...this.#getDimension(), padding: this.getAttribute('padding') };
126
+ }
127
+ /** @private */
128
+ get fixed() {
129
+ return false;
130
+ }
131
+ /** 字体样式 */
132
+ get font() {
133
+ const { parentNode } = this, acceptable = parentNode?.getAcceptable();
134
+ if (!parentNode || acceptable && !('QuoteToken' in acceptable)) {
135
+ return { bold: false, italic: false };
136
+ }
137
+ const { childNodes, type } = parentNode;
138
+ let { bold = false, italic = false } = type === 'ext-link-text' && parentNode.parentNode || {};
139
+ for (let i = childNodes.indexOf(this) - 1; i >= 0; i--) {
140
+ const child = childNodes[i];
141
+ if (child.is('quote')) {
142
+ bold = child.bold !== bold;
143
+ italic = child.italic !== italic;
144
+ }
145
+ else if (child.type === 'text' && child.data.includes('\n')) {
146
+ break;
147
+ }
148
+ }
149
+ return { bold, italic };
150
+ }
151
+ /** 是否粗体 */
152
+ get bold() {
153
+ return this.font.bold;
154
+ }
155
+ /** 是否斜体 */
156
+ get italic() {
157
+ return this.font.italic;
158
+ }
159
+ constructor() {
160
+ Object.defineProperty(this, 'childNodes', { writable: false });
161
+ Object.freeze(this.childNodes);
162
+ }
163
+ /* NOT FOR BROWSER END */
164
+ /** @private */
165
+ getAttribute(key) {
166
+ return (key === 'padding' ? 0 : this[key]);
167
+ }
168
+ /** @private */
169
+ setAttribute(key, value) {
170
+ if (key === 'parentNode') {
171
+ this.#parentNode = value;
172
+ /* NOT FOR BROWSER */
173
+ }
174
+ else if (Object.hasOwn(this, key)) {
175
+ const descriptor = Object.getOwnPropertyDescriptor(this, key);
176
+ if (this.#optional.has(key)) {
177
+ descriptor.enumerable = Boolean(value);
178
+ }
179
+ const oldValue = this[key], frozen = typeof oldValue === 'object' && Object.isFrozen(oldValue);
180
+ Object.defineProperty(this, key, { ...descriptor, value });
181
+ if (frozen && typeof value === 'object') {
182
+ Object.freeze(value);
183
+ }
184
+ /* NOT FOR BROWSER END */
185
+ }
186
+ else {
187
+ this[key] = value; // eslint-disable-line @typescript-eslint/no-explicit-any
188
+ }
189
+ }
190
+ /** 获取根节点 */
191
+ getRootNode() {
192
+ let { parentNode } = this;
193
+ while (parentNode?.parentNode) {
194
+ ({ parentNode } = parentNode);
195
+ }
196
+ return parentNode ?? this;
197
+ }
198
+ /**
199
+ * 将字符位置转换为行列号
200
+ * @param index 字符位置
201
+ */
202
+ posFromIndex(index) {
203
+ const str = String(this);
204
+ if (index >= -str.length && index <= str.length) {
205
+ const { height, width } = getDimension(str.slice(0, index));
206
+ return { top: height - 1, left: width };
207
+ }
208
+ return undefined;
209
+ }
210
+ /** 获取行数和最后一行的列数 */
211
+ #getDimension() {
212
+ return getDimension(String(this));
213
+ }
214
+ /** @private */
215
+ getGaps(_) {
216
+ return 0;
217
+ }
218
+ /**
219
+ * 获取当前节点的相对字符位置,或其第`j`个子节点的相对字符位置
220
+ * @param j 子节点序号
221
+ */
222
+ getRelativeIndex(j) {
223
+ if (j === undefined) {
224
+ const { parentNode } = this;
225
+ return parentNode ? getIndex(parentNode.childNodes.indexOf(this), parentNode) : 0;
226
+ }
227
+ /* NOT FOR BROWSER */
228
+ this.verifyChild(j, 1);
229
+ /* NOT FOR BROWSER END */
230
+ return getIndex(j, this);
231
+ }
232
+ /** 获取当前节点的绝对位置 */
233
+ getAbsoluteIndex() {
234
+ const { parentNode } = this;
235
+ return parentNode ? parentNode.getAbsoluteIndex() + this.getRelativeIndex() : 0;
236
+ }
237
+ /** @private */
238
+ seal(key, permanent) {
239
+ /* NOT FOR BROWSER */
240
+ if (!permanent) {
241
+ this.#optional.add(key);
242
+ }
243
+ /* NOT FOR BROWSER END */
244
+ Object.defineProperty(this, key, {
245
+ enumerable: !permanent && Boolean(this[key]),
246
+ configurable: true,
247
+ /* NOT FOR BROWSER */
248
+ writable: false,
249
+ });
250
+ }
251
+ /**
252
+ * 是否是某种类型的节点
253
+ * @param type 节点类型
254
+ */
255
+ is(type) {
256
+ return this.type === type;
257
+ }
258
+ /* NOT FOR BROWSER */
259
+ /** @private */
260
+ typeError(method, ...types) {
261
+ return typeError(this.constructor, method, ...types);
262
+ }
263
+ /** @private */
264
+ constructorError(msg) {
265
+ throw new Error(`${this.constructor.name} ${msg}!`);
266
+ }
267
+ /**
268
+ * 是否是全同节点
269
+ * @param node 待比较的节点
270
+ * @throws `assert.AssertionError`
271
+ */
272
+ isEqualNode(node) {
273
+ try {
274
+ assert.deepEqual(this, node);
275
+ }
276
+ catch (e) {
277
+ if (e instanceof assert.AssertionError) {
278
+ return false;
279
+ }
280
+ throw e;
281
+ }
282
+ return true;
283
+ }
284
+ /**
285
+ * 在当前节点前后插入兄弟节点
286
+ * @param nodes 插入节点
287
+ * @param offset 插入的相对位置
288
+ * @throws `Error` 不存在父节点
289
+ */
290
+ #insertAdjacent(nodes, offset) {
291
+ const { parentNode } = this;
292
+ if (!parentNode) {
293
+ throw new Error('There is no parent node!');
294
+ }
295
+ const i = parentNode.childNodes.indexOf(this) + offset;
296
+ for (let j = 0; j < nodes.length; j++) {
297
+ parentNode.insertAt(nodes[j], i + j);
298
+ }
299
+ }
300
+ /**
301
+ * 在后方批量插入兄弟节点
302
+ * @param nodes 插入节点
303
+ */
304
+ after(...nodes) {
305
+ this.#insertAdjacent(nodes, 1);
306
+ }
307
+ /**
308
+ * 在前方批量插入兄弟节点
309
+ * @param nodes 插入节点
310
+ */
311
+ before(...nodes) {
312
+ this.#insertAdjacent(nodes, 0);
313
+ }
314
+ /** 移除当前节点 */
315
+ remove() {
316
+ this.parentNode?.removeChild(this);
317
+ }
318
+ /**
319
+ * 将当前节点批量替换为新的节点
320
+ * @param nodes 插入节点
321
+ */
322
+ replaceWith(...nodes) {
323
+ this.after(...nodes);
324
+ this.remove();
325
+ }
326
+ /**
327
+ * 是自身或后代节点
328
+ * @param node 待检测节点
329
+ */
330
+ contains(node) {
331
+ return node === this || this.childNodes.some(child => child.contains(node));
332
+ }
333
+ /** @private */
334
+ verifyChild(i, addition = 0) {
335
+ const { childNodes: { length } } = this;
336
+ if (i < -length || i >= length + addition) {
337
+ throw new RangeError(`The child node at position ${i} does not exist!`);
338
+ }
339
+ }
340
+ /**
341
+ * 添加事件监听
342
+ * @param types 事件类型
343
+ * @param listener 监听函数
344
+ * @param options 选项
345
+ * @param options.once 仅执行一次
346
+ */
347
+ addEventListener(types, listener, options) {
348
+ if (Array.isArray(types)) {
349
+ for (const type of types) {
350
+ this.addEventListener(type, listener, options);
351
+ }
352
+ }
353
+ else {
354
+ this.#events[options?.once ? 'once' : 'on'](types, listener);
355
+ }
356
+ }
357
+ /**
358
+ * 移除事件监听
359
+ * @param types 事件类型
360
+ * @param listener 监听函数
361
+ */
362
+ removeEventListener(types, listener) {
363
+ if (Array.isArray(types)) {
364
+ for (const type of types) {
365
+ this.removeEventListener(type, listener);
366
+ }
367
+ }
368
+ else {
369
+ this.#events.off(types, listener);
370
+ }
371
+ }
372
+ /**
373
+ * 移除事件的所有监听
374
+ * @param types 事件类型
375
+ */
376
+ removeAllEventListeners(types) {
377
+ if (Array.isArray(types)) {
378
+ for (const type of types) {
379
+ this.removeAllEventListeners(type);
380
+ }
381
+ }
382
+ else if (typeof types === 'string') {
383
+ this.#events.removeAllListeners(types);
384
+ }
385
+ }
386
+ /**
387
+ * 列举事件监听
388
+ * @param type 事件类型
389
+ */
390
+ listEventListeners(type) {
391
+ return this.#events.listeners(type);
392
+ }
393
+ /**
394
+ * 触发事件
395
+ * @param e 事件对象
396
+ * @param data 事件数据
397
+ */
398
+ dispatchEvent(e, data) {
399
+ if (!e.target) { // 初始化
400
+ Object.defineProperty(e, 'target', { value: this, enumerable: true });
401
+ }
402
+ Object.defineProperties(e, {
403
+ prevTarget: { value: e.currentTarget, enumerable: true, configurable: true },
404
+ currentTarget: { value: this, enumerable: true, configurable: true },
405
+ });
406
+ this.#events.emit(e.type, e, data);
407
+ if (e.bubbles && !e.cancelBubble && this.parentNode) {
408
+ this.parentNode.dispatchEvent(e, data);
409
+ }
410
+ }
411
+ /** 获取所有祖先节点 */
412
+ getAncestors() {
413
+ const ancestors = [];
414
+ let { parentNode } = this;
415
+ while (parentNode) {
416
+ ancestors.push(parentNode);
417
+ ({ parentNode } = parentNode);
418
+ }
419
+ return ancestors;
420
+ }
421
+ /**
422
+ * 比较和另一个节点的相对位置
423
+ * @param other 待比较的节点
424
+ * @throws `RangeError` 不在同一个语法树
425
+ */
426
+ compareDocumentPosition(other) {
427
+ if (this === other) {
428
+ return 0;
429
+ }
430
+ else if (this.contains(other)) {
431
+ return -1;
432
+ }
433
+ else if (other.contains(this)) {
434
+ return 1;
435
+ }
436
+ else if (this.getRootNode() !== other.getRootNode()) {
437
+ throw new RangeError('Nodes to be compared are not in the same document!');
438
+ }
439
+ const aAncestors = [...this.getAncestors().reverse(), this], bAncestors = [...other.getAncestors().reverse(), other], depth = aAncestors.findIndex((ancestor, i) => bAncestors[i] !== ancestor), { childNodes } = aAncestors[depth - 1];
440
+ return childNodes.indexOf(aAncestors[depth]) - childNodes.indexOf(bAncestors[depth]);
441
+ }
442
+ /**
443
+ * 将行列号转换为字符位置
444
+ * @param top 行号
445
+ * @param left 列号
446
+ */
447
+ indexFromPos(top, left) {
448
+ const lines = String(this).split('\n');
449
+ return top >= 0 && left >= 0 && top <= lines.length - 1 && left <= lines[top].length
450
+ ? lines.slice(0, top).reduce((acc, cur) => acc + cur.length + 1, 0) + left
451
+ : undefined;
452
+ }
453
+ /** 获取当前节点的相对位置 */
454
+ #getPosition() {
455
+ return this.parentNode?.posFromIndex(this.getRelativeIndex()) ?? { top: 0, left: 0 };
456
+ }
457
+ /** 获取当前节点的行列位置和大小 */
458
+ getBoundingClientRect() {
459
+ return { ...this.#getDimension(), ...this.getRootNode().posFromIndex(this.getAbsoluteIndex()) };
460
+ }
461
+ /** 销毁 */
462
+ destroy() {
463
+ this.parentNode?.destroy();
464
+ for (const child of this.childNodes) {
465
+ child.setAttribute('parentNode', undefined);
466
+ }
467
+ Object.setPrototypeOf(this, null);
468
+ }
469
+ /**
470
+ * 获取某一行的wikitext
471
+ * @param n 行号
472
+ */
473
+ getLine(n) {
474
+ return String(this).split('\n', n + 1)[n];
475
+ }
476
+ }
477
+ exports.AstNode = AstNode;
478
+ constants_1.classes['AstNode'] = __filename;
@@ -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
+ }