wikiparser-node 1.8.0 → 1.9.0-b

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