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