wikiparser-node 1.7.0-beta.4 → 1.7.0

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