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