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