wikiparser-node 0.6.1 → 0.6.5-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 (82) hide show
  1. package/bundle/bundle.min.js +40 -0
  2. package/package.json +9 -11
  3. package/README.md +0 -39
  4. package/config/default.json +0 -832
  5. package/config/llwiki.json +0 -630
  6. package/config/moegirl.json +0 -727
  7. package/config/zhwiki.json +0 -1269
  8. package/index.js +0 -277
  9. package/lib/element.js +0 -612
  10. package/lib/node.js +0 -771
  11. package/lib/ranges.js +0 -130
  12. package/lib/text.js +0 -175
  13. package/lib/title.js +0 -70
  14. package/mixin/attributeParent.js +0 -113
  15. package/mixin/fixedToken.js +0 -40
  16. package/mixin/hidden.js +0 -21
  17. package/mixin/sol.js +0 -68
  18. package/parser/brackets.js +0 -118
  19. package/parser/commentAndExt.js +0 -63
  20. package/parser/converter.js +0 -45
  21. package/parser/externalLinks.js +0 -31
  22. package/parser/hrAndDoubleUnderscore.js +0 -36
  23. package/parser/html.js +0 -42
  24. package/parser/links.js +0 -97
  25. package/parser/list.js +0 -59
  26. package/parser/magicLinks.js +0 -41
  27. package/parser/quotes.js +0 -64
  28. package/parser/selector.js +0 -175
  29. package/parser/table.js +0 -114
  30. package/src/arg.js +0 -198
  31. package/src/atom/hidden.js +0 -13
  32. package/src/atom/index.js +0 -43
  33. package/src/attribute.js +0 -460
  34. package/src/charinsert.js +0 -91
  35. package/src/converter.js +0 -176
  36. package/src/converterFlags.js +0 -279
  37. package/src/converterRule.js +0 -259
  38. package/src/extLink.js +0 -175
  39. package/src/gallery.js +0 -146
  40. package/src/hasNowiki/index.js +0 -42
  41. package/src/hasNowiki/pre.js +0 -40
  42. package/src/heading.js +0 -123
  43. package/src/html.js +0 -230
  44. package/src/imageParameter.js +0 -276
  45. package/src/imagemap.js +0 -205
  46. package/src/imagemapLink.js +0 -43
  47. package/src/index.js +0 -842
  48. package/src/link/category.js +0 -49
  49. package/src/link/file.js +0 -321
  50. package/src/link/galleryImage.js +0 -125
  51. package/src/link/index.js +0 -347
  52. package/src/magicLink.js +0 -135
  53. package/src/nested/choose.js +0 -24
  54. package/src/nested/combobox.js +0 -23
  55. package/src/nested/index.js +0 -88
  56. package/src/nested/references.js +0 -23
  57. package/src/nowiki/comment.js +0 -71
  58. package/src/nowiki/dd.js +0 -59
  59. package/src/nowiki/doubleUnderscore.js +0 -56
  60. package/src/nowiki/hr.js +0 -41
  61. package/src/nowiki/index.js +0 -56
  62. package/src/nowiki/list.js +0 -16
  63. package/src/nowiki/noinclude.js +0 -28
  64. package/src/nowiki/quote.js +0 -63
  65. package/src/onlyinclude.js +0 -61
  66. package/src/paramTag/index.js +0 -83
  67. package/src/paramTag/inputbox.js +0 -42
  68. package/src/parameter.js +0 -214
  69. package/src/syntax.js +0 -91
  70. package/src/table/index.js +0 -981
  71. package/src/table/td.js +0 -308
  72. package/src/table/tr.js +0 -299
  73. package/src/tagPair/ext.js +0 -122
  74. package/src/tagPair/include.js +0 -60
  75. package/src/tagPair/index.js +0 -126
  76. package/src/transclude.js +0 -751
  77. package/tool/index.js +0 -1199
  78. package/util/base.js +0 -17
  79. package/util/debug.js +0 -73
  80. package/util/diff.js +0 -76
  81. package/util/lint.js +0 -40
  82. package/util/string.js +0 -105
package/lib/node.js DELETED
@@ -1,771 +0,0 @@
1
- 'use strict';
2
-
3
- const {typeError} = require('../util/debug'),
4
- {text} = require('../util/string'),
5
- assert = require('assert/strict'),
6
- EventEmitter = require('events'),
7
- Parser = require('..');
8
-
9
- /** 类似Node */
10
- class AstNode {
11
- /** @type {string} */ type;
12
- /** @type {this[]} */ childNodes = [];
13
- /** @type {this} */ #parentNode;
14
- /** @type {string[]} */ #optional = [];
15
- #events = new EventEmitter();
16
-
17
- /**
18
- * 检查在某个位置增删子节点是否合法
19
- * @param {number} i 增删位置
20
- * @param {number} addition 将会插入的子节点个数
21
- * @throws `RangeError` 指定位置不存在子节点
22
- */
23
- #verifyChild = (i, addition = 0) => {
24
- if (!Number.isInteger(i)) {
25
- this.typeError('verifyChild', 'Number');
26
- }
27
- const {childNodes: {length}} = this;
28
- if (i < -length || i >= length + addition) {
29
- throw new RangeError(`不存在第 ${i} 个子节点!`);
30
- }
31
- };
32
-
33
- /** 首位子节点 */
34
- get firstChild() {
35
- return this.childNodes[0];
36
- }
37
-
38
- /** 末位子节点 */
39
- get lastChild() {
40
- return this.childNodes.at(-1);
41
- }
42
-
43
- /** 父节点 */
44
- get parentNode() {
45
- return this.#parentNode;
46
- }
47
-
48
- /**
49
- * 后一个兄弟节点
50
- * @complexity `n`
51
- */
52
- get nextSibling() {
53
- const childNodes = this.#parentNode?.childNodes;
54
- return childNodes && childNodes[childNodes.indexOf(this) + 1];
55
- }
56
-
57
- /**
58
- * 前一个兄弟节点
59
- * @complexity `n`
60
- */
61
- get previousSibling() {
62
- const childNodes = this.#parentNode?.childNodes;
63
- return childNodes && childNodes[childNodes.indexOf(this) - 1];
64
- }
65
-
66
- /**
67
- * 后一个非文本兄弟节点
68
- * @complexity `n`
69
- * @returns {this}
70
- */
71
- get nextElementSibling() {
72
- const childNodes = this.#parentNode?.childNodes,
73
- i = childNodes?.indexOf(this);
74
- return childNodes?.slice(i + 1)?.find(({type}) => type !== 'text');
75
- }
76
-
77
- /**
78
- * 前一个非文本兄弟节点
79
- * @complexity `n`
80
- * @returns {this}
81
- */
82
- get previousElementSibling() {
83
- const childNodes = this.#parentNode?.childNodes,
84
- i = childNodes?.indexOf(this);
85
- return childNodes?.slice(0, i)?.findLast(({type}) => type !== 'text');
86
- }
87
-
88
- /** 是否具有根节点 */
89
- get isConnected() {
90
- return this.getRootNode().type === 'root';
91
- }
92
-
93
- /** 不是自身的根节点 */
94
- get ownerDocument() {
95
- const root = this.getRootNode();
96
- return root.type === 'root' && root !== this ? root : undefined;
97
- }
98
-
99
- /**
100
- * 后方是否还有其他节点(不含后代)
101
- * @returns {boolean}
102
- * @complexity `n`
103
- */
104
- get eof() {
105
- const {type, parentNode} = this;
106
- if (type === 'root') {
107
- return true;
108
- }
109
- let {nextSibling} = this;
110
- while (nextSibling?.type === 'text' && String(nextSibling).trim() === '') {
111
- ({nextSibling} = nextSibling);
112
- }
113
- return nextSibling === undefined && parentNode?.eof;
114
- }
115
-
116
- constructor() {
117
- Object.defineProperty(this, 'childNodes', {writable: false});
118
- Object.freeze(this.childNodes);
119
- }
120
-
121
- /**
122
- * 标记仅用于代码调试的方法
123
- * @param {string} method
124
- * @throws `Error`
125
- */
126
- debugOnly(method = 'debugOnly') {
127
- throw new Error(`${this.constructor.name}.${method} 方法仅用于代码调试!`);
128
- }
129
-
130
- /**
131
- * 抛出`TypeError`
132
- * @param {string} method
133
- * @param {...string} types 可接受的参数类型
134
- */
135
- typeError(method, ...types) {
136
- return typeError(this.constructor, method, ...types);
137
- }
138
-
139
- /**
140
- * 冻结部分属性
141
- * @param {string|string[]} keys 属性键
142
- * @param {boolean} permanent 是否永久
143
- */
144
- seal(keys, permanent) {
145
- if (!Parser.running && !Parser.debugging) {
146
- this.debugOnly('seal');
147
- }
148
- keys = Array.isArray(keys) ? keys : [keys];
149
- if (!permanent) {
150
- this.#optional.push(...keys);
151
- }
152
- for (const key of keys) {
153
- Object.defineProperty(this, key, {
154
- writable: false, enumerable: !permanent && Boolean(this[key]), configurable: !permanent,
155
- });
156
- }
157
- return this;
158
- }
159
-
160
- /**
161
- * 是否是全同节点
162
- * @param {this} node 待比较的节点
163
- * @throws `assert.AssertionError`
164
- */
165
- isEqualNode(node) {
166
- try {
167
- assert.deepStrictEqual(this, node);
168
- } catch (e) {
169
- if (e instanceof assert.AssertionError) {
170
- return false;
171
- }
172
- throw e;
173
- }
174
- return true;
175
- }
176
-
177
- /** 获取所有属性键 */
178
- getAttributeNames() {
179
- const names = Object.getOwnPropertyNames(this);
180
- return names.filter(name => typeof this[name] !== 'function');
181
- }
182
-
183
- /** 是否具有任意属性 */
184
- hasAttributes() {
185
- return this.getAttributeNames().length > 0;
186
- }
187
-
188
- /**
189
- * 移除某属性
190
- * @param {PropertyKey} key 属性键
191
- * @throws `RangeError` 不可删除的属性
192
- */
193
- removeAttribute(key) {
194
- if (this.hasAttribute(key)) {
195
- const descriptor = Object.getOwnPropertyDescriptor(this, key);
196
- if (!descriptor || !descriptor.writable) {
197
- throw new RangeError(`属性 ${key} 不可删除!`);
198
- }
199
- delete this[key];
200
- }
201
- }
202
-
203
- /**
204
- * 开关某属性
205
- * @param {PropertyKey} key 属性键
206
- * @param {boolean|undefined} force 强制开启或关闭
207
- * @throws `RangeError` 不为Boolean类型的属性值
208
- */
209
- toggleAttribute(key, force) {
210
- if (force !== undefined && typeof force !== 'boolean') {
211
- this.typeError('toggleAttribute', 'Boolean');
212
- } else if (this.hasAttribute(key) && typeof this[key] !== 'boolean') {
213
- throw new RangeError(`${key} 属性的值不为 Boolean!`);
214
- }
215
- this.setAttribute(key, force === true || force === undefined && !this[key]);
216
- }
217
-
218
- /**
219
- * 是否具有某属性
220
- * @param {PropertyKey} key 属性键
221
- */
222
- hasAttribute(key) {
223
- const type = typeof key;
224
- return type === 'string' || type === 'number' || type === 'symbol'
225
- ? key in this
226
- : this.typeError('hasAttribute', 'String', 'Number', 'Symbol');
227
- }
228
-
229
- /**
230
- * 获取属性值。除非用于私有属性,否则总是返回字符串。
231
- * @template {string} T
232
- * @param {T} key 属性键
233
- * @returns {TokenAttribute<T>}
234
- */
235
- getAttribute(key) {
236
- if (key === 'optional') {
237
- return [...this.#optional];
238
- } else if (key === 'verifyChild') {
239
- return this.#verifyChild;
240
- }
241
- return this.hasAttribute(key) ? String(this[key]) : undefined;
242
- }
243
-
244
- /**
245
- * 设置属性
246
- * @template {string} T
247
- * @param {T} key 属性键
248
- * @param {TokenAttribute<T>} value 属性值
249
- */
250
- setAttribute(key, value) {
251
- if (key === 'parentNode') {
252
- this.#parentNode = value;
253
- } else if (Object.hasOwn(this, key)) {
254
- const descriptor = Object.getOwnPropertyDescriptor(this, key);
255
- if (this.#optional.includes(key)) {
256
- descriptor.enumerable = Boolean(value);
257
- }
258
- const oldValue = this[key],
259
- frozen = oldValue !== null && typeof oldValue === 'object' && Object.isFrozen(oldValue);
260
- Object.defineProperty(this, key, {...descriptor, value});
261
- if (frozen && value !== null && typeof value === 'object') {
262
- Object.freeze(value);
263
- }
264
- } else {
265
- this[key] = value;
266
- }
267
- return this;
268
- }
269
-
270
- /**
271
- * 移除子节点
272
- * @param {number} i 移除位置
273
- */
274
- removeAt(i) {
275
- this.getAttribute('verifyChild')(i);
276
- const childNodes = [...this.childNodes],
277
- [node] = childNodes.splice(i, 1),
278
- e = new Event('remove', {bubbles: true});
279
- node.setAttribute('parentNode');
280
- this.setAttribute('childNodes', childNodes);
281
- this.dispatchEvent(e, {position: i, removed: node});
282
- return node;
283
- }
284
-
285
- /**
286
- * 插入子节点
287
- * @template {this} T
288
- * @param {T} node 待插入的子节点
289
- * @param {number} i 插入位置
290
- * @complexity `n`
291
- * @throws `RangeError` 不能插入祖先节点
292
- */
293
- insertAt(node, i = this.childNodes.length) {
294
- if (!(node instanceof AstNode)) {
295
- this.typeError('insertAt', 'String', 'AstNode');
296
- } else if (node.contains(this)) {
297
- Parser.error('不能插入祖先节点!', node);
298
- throw new RangeError('不能插入祖先节点!');
299
- }
300
- this.getAttribute('verifyChild')(i, 1);
301
- const childNodes = [...this.childNodes],
302
- e = new Event('insert', {bubbles: true}),
303
- j = Parser.running ? -1 : childNodes.indexOf(node);
304
- if (j === -1) {
305
- node.parentNode?.removeChild(node);
306
- node.setAttribute('parentNode', this);
307
- } else {
308
- childNodes.splice(j, 1);
309
- }
310
- childNodes.splice(i, 0, node);
311
- this.setAttribute('childNodes', childNodes);
312
- this.dispatchEvent(e, {position: i < 0 ? i + this.childNodes.length - 1 : i, inserted: node});
313
- return node;
314
- }
315
-
316
- /**
317
- * 获取子节点的位置
318
- * @param {this} node 子节点
319
- * @complexity `n`
320
- * @throws `RangeError` 找不到子节点
321
- */
322
- #getChildIndex(node) {
323
- const {childNodes} = this,
324
- i = childNodes.indexOf(node);
325
- if (i === -1) {
326
- Parser.error('找不到子节点!', node);
327
- throw new RangeError('找不到子节点!');
328
- }
329
- return i;
330
- }
331
-
332
- /**
333
- * 移除子节点
334
- * @template {this} T
335
- * @param {T} node 子节点
336
- * @complexity `n`
337
- */
338
- removeChild(node) {
339
- this.removeAt(this.#getChildIndex(node));
340
- return node;
341
- }
342
-
343
- /**
344
- * 在末尾插入子节点
345
- * @template {this} T
346
- * @param {T} node 插入节点
347
- * @complexity `n`
348
- */
349
- appendChild(node) {
350
- return this.insertAt(node);
351
- }
352
-
353
- /**
354
- * 在指定位置前插入子节点
355
- * @template {this} T
356
- * @param {T} child 插入节点
357
- * @param {this} reference 指定位置处的子节点
358
- * @complexity `n`
359
- */
360
- insertBefore(child, reference) {
361
- return reference === undefined ? this.insertAt(child) : this.insertAt(child, this.#getChildIndex(reference));
362
- }
363
-
364
- /**
365
- * 替换子节点
366
- * @template {this} T
367
- * @param {this} newChild 新子节点
368
- * @param {T} oldChild 原子节点
369
- * @complexity `n`
370
- */
371
- replaceChild(newChild, oldChild) {
372
- const i = this.#getChildIndex(oldChild);
373
- this.removeAt(i);
374
- this.insertAt(newChild, i);
375
- return oldChild;
376
- }
377
-
378
- /**
379
- * 在当前节点前后插入兄弟节点
380
- * @param {this[]} nodes 插入节点
381
- * @param {number} offset 插入的相对位置
382
- * @complexity `n`
383
- * @throws `Error` 不存在父节点
384
- */
385
- #insertAdjacent(nodes, offset) {
386
- const {parentNode} = this;
387
- if (!parentNode) {
388
- throw new Error('不存在父节点!');
389
- }
390
- const i = parentNode.childNodes.indexOf(this) + offset;
391
- for (let j = 0; j < nodes.length; j++) {
392
- parentNode.insertAt(nodes[j], i + j);
393
- }
394
- }
395
-
396
- /**
397
- * 在后方批量插入兄弟节点
398
- * @param {...this} nodes 插入节点
399
- * @complexity `n`
400
- */
401
- after(...nodes) {
402
- this.#insertAdjacent(nodes, 1);
403
- }
404
-
405
- /**
406
- * 在前方批量插入兄弟节点
407
- * @param {...this} nodes 插入节点
408
- * @complexity `n`
409
- */
410
- before(...nodes) {
411
- this.#insertAdjacent(nodes, 0);
412
- }
413
-
414
- /**
415
- * 移除当前节点
416
- * @complexity `n`
417
- * @throws `Error` 不存在父节点
418
- */
419
- remove() {
420
- const {parentNode} = this;
421
- if (!parentNode) {
422
- throw new Error('不存在父节点!');
423
- }
424
- parentNode.removeChild(this);
425
- }
426
-
427
- /**
428
- * 将当前节点批量替换为新的节点
429
- * @param {...this} nodes 插入节点
430
- * @complexity `n`
431
- */
432
- replaceWith(...nodes) {
433
- this.after(...nodes);
434
- this.remove();
435
- }
436
-
437
- /**
438
- * 可见部分
439
- * @param {string} separator 子节点间的连接符
440
- * @returns {string}
441
- * @complexity `n`
442
- */
443
- text(separator = '') {
444
- return text(this.childNodes, separator);
445
- }
446
-
447
- /** 是否具有子节点 */
448
- hasChildNodes() {
449
- return this.childNodes.length > 0;
450
- }
451
-
452
- /**
453
- * 是自身或后代节点
454
- * @param {this} node 待检测节点
455
- * @returns {boolean}
456
- * @complexity `n`
457
- */
458
- contains(node) {
459
- return node instanceof AstNode
460
- ? node === this || this.childNodes.some(child => child.contains(node))
461
- : this.typeError('contains', 'AstNode');
462
- }
463
-
464
- /**
465
- * 添加事件监听
466
- * @param {string|string[]} types 事件类型
467
- * @param {AstListener} listener 监听函数
468
- * @param {{once: boolean}} options 选项
469
- */
470
- addEventListener(types, listener, options) {
471
- if (Array.isArray(types)) {
472
- for (const type of types) {
473
- this.addEventListener(type, listener, options);
474
- }
475
- } else if (typeof types === 'string' && typeof listener === 'function') {
476
- this.#events[options?.once ? 'once' : 'on'](types, listener);
477
- } else {
478
- this.typeError('addEventListener', 'String', 'Function');
479
- }
480
- }
481
-
482
- /**
483
- * 移除事件监听
484
- * @param {string|string[]} types 事件类型
485
- * @param {AstListener} listener 监听函数
486
- */
487
- removeEventListener(types, listener) {
488
- if (Array.isArray(types)) {
489
- for (const type of types) {
490
- this.removeEventListener(type, listener);
491
- }
492
- } else if (typeof types === 'string' && typeof listener === 'function') {
493
- this.#events.off(types, listener);
494
- } else {
495
- this.typeError('removeEventListener', 'String', 'Function');
496
- }
497
- }
498
-
499
- /**
500
- * 移除事件的所有监听
501
- * @param {string|string[]} types 事件类型
502
- */
503
- removeAllEventListeners(types) {
504
- if (Array.isArray(types)) {
505
- for (const type of types) {
506
- this.removeAllEventListeners(type);
507
- }
508
- } else if (types === undefined || typeof types === 'string') {
509
- this.#events.removeAllListeners(types);
510
- } else {
511
- this.typeError('removeAllEventListeners', 'String');
512
- }
513
- }
514
-
515
- /**
516
- * 列举事件监听
517
- * @param {string} type 事件类型
518
- * @returns {AstListener[]}
519
- */
520
- listEventListeners(type) {
521
- return typeof type === 'string' ? this.#events.listeners(type) : this.typeError('listEventListeners', 'String');
522
- }
523
-
524
- /**
525
- * 触发事件
526
- * @param {AstEvent} e 事件对象
527
- * @param {*} data 事件数据
528
- */
529
- dispatchEvent(e, data) {
530
- if (!(e instanceof Event)) {
531
- this.typeError('dispatchEvent', 'Event');
532
- } else if (!e.target) { // 初始化
533
- Object.defineProperty(e, 'target', {value: this, enumerable: true});
534
-
535
- /** 终止冒泡 */
536
- e.stopPropagation = function() {
537
- Object.defineProperty(this, 'bubbles', {value: false});
538
- };
539
- }
540
- Object.defineProperties(e, { // 每次bubble更新
541
- prevTarget: {value: e.currentTarget, enumerable: true, configurable: true},
542
- currentTarget: {value: this, enumerable: true, configurable: true},
543
- });
544
- this.#events.emit(e.type, e, data);
545
- if (e.bubbles && this.parentNode) {
546
- this.parentNode.dispatchEvent(e, data);
547
- }
548
- }
549
-
550
- /** 获取所有祖先节点 */
551
- getAncestors() {
552
- const /** @type {this[]} */ ancestors = [];
553
- let {parentNode} = this;
554
- while (parentNode) {
555
- ancestors.push(parentNode);
556
- ({parentNode} = parentNode);
557
- }
558
- return ancestors;
559
- }
560
-
561
- /**
562
- * 比较和另一个节点的相对位置
563
- * @param {this} other 待比较的节点
564
- * @complexity `n`
565
- * @throws `Error` 不在同一个语法树
566
- */
567
- compareDocumentPosition(other) {
568
- if (!(other instanceof AstNode)) {
569
- this.typeError('compareDocumentPosition', 'AstNode');
570
- } else if (this === other) {
571
- return 0;
572
- } else if (this.contains(other)) {
573
- return -1;
574
- } else if (other.contains(this)) {
575
- return 1;
576
- } else if (this.getRootNode() !== other.getRootNode()) {
577
- throw new Error('不在同一个语法树!');
578
- }
579
- const aAncestors = [...this.getAncestors().reverse(), this],
580
- bAncestors = [...other.getAncestors().reverse(), other],
581
- depth = aAncestors.findIndex((ancestor, i) => bAncestors[i] !== ancestor),
582
- commonAncestor = aAncestors[depth - 1],
583
- {childNodes} = commonAncestor;
584
- return childNodes.indexOf(aAncestors[depth]) - childNodes.indexOf(bAncestors[depth]);
585
- }
586
-
587
- /**
588
- * 合并相邻的文本子节点
589
- * @complexity `n`
590
- */
591
- normalize() {
592
- const AstText = require('./text');
593
- const /** @type {AstText[]} */ childNodes = [...this.childNodes];
594
- for (let i = childNodes.length - 1; i >= 0; i--) {
595
- const {type, data} = childNodes[i];
596
- if (this.getGaps(i - 1)) {
597
- //
598
- } else if (data === '') {
599
- childNodes.splice(i, 1);
600
- } else if (type === 'text' && childNodes[i - 1]?.type === 'text') {
601
- childNodes[i - 1].setAttribute('data', childNodes[i - 1].data + data);
602
- childNodes.splice(i, 1);
603
- }
604
- }
605
- this.setAttribute('childNodes', childNodes);
606
- }
607
-
608
- /** 获取根节点 */
609
- getRootNode() {
610
- let {parentNode} = this;
611
- while (parentNode?.parentNode) {
612
- ({parentNode} = parentNode);
613
- }
614
- return parentNode ?? this;
615
- }
616
-
617
- /**
618
- * 将字符位置转换为行列号
619
- * @param {number} index 字符位置
620
- * @complexity `n`
621
- */
622
- posFromIndex(index) {
623
- if (!Number.isInteger(index)) {
624
- this.typeError('posFromIndex', 'Number');
625
- }
626
- const str = String(this);
627
- if (index >= -str.length && index <= str.length) {
628
- const lines = str.slice(0, index).split('\n');
629
- return {top: lines.length - 1, left: lines.at(-1).length};
630
- }
631
- return undefined;
632
- }
633
-
634
- /**
635
- * 将行列号转换为字符位置
636
- * @param {number} top 行号
637
- * @param {number} left 列号
638
- * @complexity `n`
639
- */
640
- indexFromPos(top, left) {
641
- if (!Number.isInteger(top) || !Number.isInteger(left)) {
642
- this.typeError('indexFromPos', 'Number');
643
- }
644
- const lines = String(this).split('\n');
645
- return top >= 0 && left >= 0 && lines.length >= top + 1 && lines[top].length >= left
646
- ? lines.slice(0, top).reduce((acc, curLine) => acc + curLine.length + 1, 0) + left
647
- : undefined;
648
- }
649
-
650
- /**
651
- * 获取行数和最后一行的列数
652
- * @complexity `n`
653
- */
654
- #getDimension() {
655
- const lines = String(this).split('\n');
656
- return {height: lines.length, width: lines.at(-1).length};
657
- }
658
-
659
- /** 第一个子节点前的间距 */
660
- getPadding() {
661
- return 0;
662
- }
663
-
664
- /** 子节点间距 */
665
- getGaps() {
666
- return 0;
667
- }
668
-
669
- /**
670
- * 获取当前节点的相对字符位置,或其第`j`个子节点的相对字符位置
671
- * @param {number|undefined} j 子节点序号
672
- * @complexity `n`
673
- */
674
- getRelativeIndex(j) {
675
- let /** @type {this[]} */ childNodes;
676
-
677
- /**
678
- * 获取子节点相对于父节点的字符位置,使用前需要先给`childNodes`赋值
679
- * @param {number} end 子节点序号
680
- * @param {this} parent 父节点
681
- * @returns {number}
682
- */
683
- const getIndex = (end, parent) => childNodes.slice(0, end).reduce(
684
- (acc, cur, i) => acc + String(cur).length + parent.getGaps(i),
685
- 0,
686
- ) + parent.getPadding();
687
- if (j === undefined) {
688
- const {parentNode} = this;
689
- if (parentNode) {
690
- ({childNodes} = parentNode);
691
- return getIndex(childNodes.indexOf(this), parentNode);
692
- }
693
- return 0;
694
- }
695
- this.getAttribute('verifyChild')(j, 1);
696
- ({childNodes} = this);
697
- return getIndex(j, this);
698
- }
699
-
700
- /**
701
- * 获取当前节点的绝对位置
702
- * @returns {number}
703
- * @complexity `n`
704
- */
705
- getAbsoluteIndex() {
706
- const {parentNode} = this;
707
- return parentNode ? parentNode.getAbsoluteIndex() + this.getRelativeIndex() : 0;
708
- }
709
-
710
- /**
711
- * 获取当前节点的相对位置,或其第`j`个子节点的相对位置
712
- * @param {number|undefined} j 子节点序号
713
- * @complexity `n`
714
- */
715
- #getPosition(j) {
716
- return j === undefined
717
- ? this.parentNode?.posFromIndex(this.getRelativeIndex()) ?? {top: 0, left: 0}
718
- : this.posFromIndex(this.getRelativeIndex(j));
719
- }
720
-
721
- /**
722
- * 获取当前节点的行列位置和大小
723
- * @complexity `n`
724
- */
725
- getBoundingClientRect() {
726
- return {...this.#getDimension(), ...this.getRootNode().posFromIndex(this.getAbsoluteIndex())};
727
- }
728
-
729
- /**
730
- * 行数
731
- * @complexity `n`
732
- */
733
- get offsetHeight() {
734
- return this.#getDimension().height;
735
- }
736
-
737
- /**
738
- * 最后一行的列数
739
- * @complexity `n`
740
- */
741
- get offsetWidth() {
742
- return this.#getDimension().width;
743
- }
744
-
745
- /**
746
- * 位置、大小和padding
747
- * @complexity `n`
748
- */
749
- get style() {
750
- return {...this.#getPosition(), ...this.#getDimension(), padding: this.getPadding()};
751
- }
752
-
753
- /**
754
- * 相对于父容器的行号
755
- * @complexity `n`
756
- */
757
- get offsetTop() {
758
- return this.#getPosition().top;
759
- }
760
-
761
- /**
762
- * 相对于父容器的列号
763
- * @complexity `n`
764
- */
765
- get offsetLeft() {
766
- return this.#getPosition().left;
767
- }
768
- }
769
-
770
- Parser.classes.AstNode = __filename;
771
- module.exports = AstNode;