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