wikiparser-node 1.4.2 → 1.4.3

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 (103) hide show
  1. package/dist/addon/table.js +5 -1
  2. package/dist/base.d.ts +6 -1
  3. package/dist/index.d.ts +0 -2
  4. package/dist/index.js +3 -3
  5. package/dist/lib/element.d.ts +3 -2
  6. package/dist/lib/element.js +20 -13
  7. package/dist/lib/node.js +13 -10
  8. package/dist/lib/text.js +3 -3
  9. package/dist/lib/title.js +4 -12
  10. package/dist/mixin/attributesParent.d.ts +32 -2
  11. package/dist/mixin/attributesParent.js +14 -25
  12. package/dist/mixin/fixed.d.ts +2 -1
  13. package/dist/mixin/fixed.js +3 -1
  14. package/dist/mixin/flagsParent.d.ts +28 -17
  15. package/dist/mixin/flagsParent.js +12 -25
  16. package/dist/mixin/magicLinkParent.d.ts +14 -8
  17. package/dist/mixin/magicLinkParent.js +7 -8
  18. package/dist/mixin/singleLine.d.ts +2 -1
  19. package/dist/mixin/singleLine.js +3 -2
  20. package/dist/mixin/sol.d.ts +2 -4
  21. package/dist/mixin/sol.js +8 -6
  22. package/dist/mixin/syntax.d.ts +5 -2
  23. package/dist/mixin/syntax.js +14 -18
  24. package/dist/parser/links.js +1 -3
  25. package/dist/src/arg.d.ts +4 -4
  26. package/dist/src/arg.js +4 -4
  27. package/dist/src/attribute.d.ts +9 -7
  28. package/dist/src/attribute.js +271 -218
  29. package/dist/src/attributes.d.ts +3 -3
  30. package/dist/src/attributes.js +0 -1
  31. package/dist/src/converter.d.ts +7 -2
  32. package/dist/src/converter.js +113 -64
  33. package/dist/src/converterFlags.d.ts +5 -5
  34. package/dist/src/converterFlags.js +0 -1
  35. package/dist/src/converterRule.d.ts +5 -5
  36. package/dist/src/converterRule.js +0 -1
  37. package/dist/src/extLink.d.ts +7 -2
  38. package/dist/src/extLink.js +159 -110
  39. package/dist/src/gallery.d.ts +5 -5
  40. package/dist/src/gallery.js +0 -1
  41. package/dist/src/heading.d.ts +8 -8
  42. package/dist/src/heading.js +172 -125
  43. package/dist/src/html.d.ts +8 -5
  44. package/dist/src/html.js +263 -214
  45. package/dist/src/imageParameter.d.ts +2 -2
  46. package/dist/src/imageParameter.js +1 -0
  47. package/dist/src/imagemap.d.ts +5 -5
  48. package/dist/src/imagemap.js +4 -10
  49. package/dist/src/imagemapLink.d.ts +7 -4
  50. package/dist/src/imagemapLink.js +75 -26
  51. package/dist/src/index.js +3 -4
  52. package/dist/src/link/base.d.ts +2 -2
  53. package/dist/src/link/base.js +1 -4
  54. package/dist/src/link/file.d.ts +1 -1
  55. package/dist/src/link/file.js +0 -1
  56. package/dist/src/link/galleryImage.d.ts +2 -1
  57. package/dist/src/link/galleryImage.js +136 -88
  58. package/dist/src/link/index.js +0 -1
  59. package/dist/src/magicLink.d.ts +11 -3
  60. package/dist/src/magicLink.js +170 -120
  61. package/dist/src/nested.d.ts +5 -5
  62. package/dist/src/nested.js +1 -0
  63. package/dist/src/nowiki/base.d.ts +6 -2
  64. package/dist/src/nowiki/base.js +79 -28
  65. package/dist/src/nowiki/comment.d.ts +0 -2
  66. package/dist/src/nowiki/comment.js +0 -9
  67. package/dist/src/nowiki/doubleUnderscore.js +88 -38
  68. package/dist/src/nowiki/hr.d.ts +1 -4
  69. package/dist/src/nowiki/hr.js +53 -4
  70. package/dist/src/nowiki/index.d.ts +2 -2
  71. package/dist/src/nowiki/index.js +1 -0
  72. package/dist/src/nowiki/list.d.ts +1 -4
  73. package/dist/src/nowiki/list.js +88 -39
  74. package/dist/src/nowiki/listBase.d.ts +1 -1
  75. package/dist/src/nowiki/listBase.js +52 -2
  76. package/dist/src/nowiki/quote.d.ts +3 -3
  77. package/dist/src/nowiki/quote.js +102 -54
  78. package/dist/src/paramTag/index.d.ts +5 -5
  79. package/dist/src/paramTag/index.js +2 -1
  80. package/dist/src/parameter.d.ts +11 -8
  81. package/dist/src/parameter.js +225 -176
  82. package/dist/src/pre.d.ts +5 -5
  83. package/dist/src/pre.js +1 -0
  84. package/dist/src/syntax.d.ts +6 -0
  85. package/dist/src/syntax.js +74 -23
  86. package/dist/src/table/base.d.ts +6 -3
  87. package/dist/src/table/base.js +100 -49
  88. package/dist/src/table/index.d.ts +6 -3
  89. package/dist/src/table/index.js +5 -9
  90. package/dist/src/table/td.d.ts +9 -5
  91. package/dist/src/table/td.js +270 -217
  92. package/dist/src/table/tr.d.ts +4 -4
  93. package/dist/src/table/tr.js +1 -0
  94. package/dist/src/tagPair/ext.d.ts +6 -3
  95. package/dist/src/tagPair/ext.js +168 -118
  96. package/dist/src/tagPair/include.d.ts +2 -4
  97. package/dist/src/tagPair/include.js +0 -8
  98. package/dist/src/tagPair/index.d.ts +1 -1
  99. package/dist/src/tagPair/index.js +115 -66
  100. package/dist/src/transclude.d.ts +3 -3
  101. package/dist/src/transclude.js +4 -9
  102. package/dist/util/debug.js +0 -2
  103. package/package.json +3 -3
package/dist/src/html.js CHANGED
@@ -1,4 +1,38 @@
1
1
  "use strict";
2
+ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
3
+ function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
4
+ var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
5
+ var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
6
+ var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
7
+ var _, done = false;
8
+ for (var i = decorators.length - 1; i >= 0; i--) {
9
+ var context = {};
10
+ for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
11
+ for (var p in contextIn.access) context.access[p] = contextIn.access[p];
12
+ context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
13
+ var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
14
+ if (kind === "accessor") {
15
+ if (result === void 0) continue;
16
+ if (result === null || typeof result !== "object") throw new TypeError("Object expected");
17
+ if (_ = accept(result.get)) descriptor.get = _;
18
+ if (_ = accept(result.set)) descriptor.set = _;
19
+ if (_ = accept(result.init)) initializers.unshift(_);
20
+ }
21
+ else if (_ = accept(result)) {
22
+ if (kind === "field") initializers.unshift(_);
23
+ else descriptor[key] = _;
24
+ }
25
+ }
26
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
27
+ done = true;
28
+ };
29
+ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
30
+ var useValue = arguments.length > 2;
31
+ for (var i = 0; i < initializers.length; i++) {
32
+ value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
33
+ }
34
+ return useValue ? value : void 0;
35
+ };
2
36
  Object.defineProperty(exports, "__esModule", { value: true });
3
37
  exports.HtmlToken = void 0;
4
38
  const lint_1 = require("../util/lint");
@@ -41,246 +75,261 @@ const magicWords = new Set(['if', 'ifeq', 'ifexpr', 'ifexist', 'iferror', 'switc
41
75
  * HTML标签
42
76
  * @classdesc `{childNodes: [AttributesToken]}`
43
77
  */
44
- class HtmlToken extends (0, attributesParent_1.attributesParent)((0, fixed_1.fixedToken)(index_2.Token)) {
45
- type = 'html';
46
- #closing;
47
- #selfClosing;
48
- #tag;
49
- /** 是否是闭合标签 */
50
- get closing() {
51
- return this.#closing;
52
- }
53
- /* NOT FOR BROWSER */
54
- /** @throws `Error` 自封闭标签或空标签 */
55
- set closing(value) {
56
- if (!value) {
57
- this.#closing = false;
58
- return;
78
+ let HtmlToken = (() => {
79
+ let _classDecorators = [fixed_1.fixedToken, (0, attributesParent_1.attributesParent)()];
80
+ let _classDescriptor;
81
+ let _classExtraInitializers = [];
82
+ let _classThis;
83
+ let _classSuper = index_2.Token;
84
+ var HtmlToken = class extends _classSuper {
85
+ static { _classThis = this; }
86
+ static {
87
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
88
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
89
+ HtmlToken = _classThis = _classDescriptor.value;
90
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
91
+ __runInitializers(_classThis, _classExtraInitializers);
59
92
  }
60
- else if (this.selfClosing) {
61
- throw new Error('这是一个自封闭标签!');
62
- }
63
- const { html: [, , tags] } = this.getAttribute('config');
64
- if (tags.includes(this.name)) {
65
- throw new Error('这是一个空标签!');
66
- }
67
- this.#closing = true;
68
- }
69
- /** 是否自封闭 */
70
- get selfClosing() {
71
- return this.#selfClosing;
72
- }
73
- /** @throws `Error` 闭合标签或无效自封闭标签 */
74
- set selfClosing(value) {
75
- if (!value) {
76
- this.#selfClosing = false;
77
- return;
93
+ type = 'html';
94
+ #closing;
95
+ #selfClosing;
96
+ #tag;
97
+ /* NOT FOR BROWSER END */
98
+ /** 是否是闭合标签 */
99
+ get closing() {
100
+ return this.#closing;
78
101
  }
79
- else if (this.closing) {
80
- throw new Error('这是一个闭合标签!');
102
+ /* NOT FOR BROWSER */
103
+ /** @throws `Error` 自封闭标签或空标签 */
104
+ set closing(value) {
105
+ if (!value) {
106
+ this.#closing = false;
107
+ return;
108
+ }
109
+ else if (this.selfClosing) {
110
+ throw new Error('这是一个自封闭标签!');
111
+ }
112
+ const { html: [, , tags] } = this.getAttribute('config');
113
+ if (tags.includes(this.name)) {
114
+ throw new Error('这是一个空标签!');
115
+ }
116
+ this.#closing = true;
81
117
  }
82
- const { html: [tags] } = this.getAttribute('config');
83
- if (tags.includes(this.name)) {
84
- throw new Error(`<${this.name}>标签自封闭无效!`);
118
+ /** 是否自封闭 */
119
+ get selfClosing() {
120
+ return this.#selfClosing;
85
121
  }
86
- this.#selfClosing = true;
87
- }
88
- /* NOT FOR BROWSER END */
89
- /**
90
- * @param name 标签名
91
- * @param attr 标签属性
92
- * @param closing 是否闭合
93
- * @param selfClosing 是否自封闭
94
- */
95
- constructor(name, attr, closing, selfClosing, config = index_1.default.getConfig(), accum = []) {
96
- super(undefined, config, accum);
97
- this.insertAt(attr);
98
- this.setAttribute('name', name.toLowerCase());
99
- this.#closing = closing;
100
- this.#selfClosing = selfClosing;
101
- this.#tag = name;
102
- }
103
- /** @private */
104
- toString() {
105
- return `<${this.closing ? '/' : ''}${this.#tag}${super.toString()}${this.#selfClosing ? '/' : ''}>`;
106
- }
107
- /** @override */
108
- text() {
109
- const { closing, name } = this, tag = `${this.#tag}${closing ? '' : super.text()}`, { html } = this.getAttribute('config');
110
- if (html[2].includes(name)) {
111
- return closing && name !== 'br' ? '' : `<${tag}>`;
112
- }
113
- return `<${closing ? '/' : ''}${tag}${this.#selfClosing && html[1].includes(name) ? '/' : ''}>`;
114
- }
115
- /** @private */
116
- getAttribute(key) {
117
- /* NOT FOR BROWSER */
118
- if (key === 'tag') {
119
- return this.#tag;
122
+ /** @throws `Error` 闭合标签或无效自封闭标签 */
123
+ set selfClosing(value) {
124
+ if (!value) {
125
+ this.#selfClosing = false;
126
+ return;
127
+ }
128
+ else if (this.closing) {
129
+ throw new Error('这是一个闭合标签!');
130
+ }
131
+ const { html: [tags] } = this.getAttribute('config');
132
+ if (tags.includes(this.name)) {
133
+ throw new Error(`<${this.name}>标签自封闭无效!`);
134
+ }
135
+ this.#selfClosing = true;
120
136
  }
121
137
  /* NOT FOR BROWSER END */
122
- return key === 'padding'
123
- ? this.#tag.length + (this.closing ? 2 : 1)
124
- : super.getAttribute(key);
125
- }
126
- /** @override */
127
- lint(start = this.getAbsoluteIndex()) {
128
- const errors = super.lint(start);
129
- let refError;
130
- if (this.name === 'h1' && !this.closing) {
131
- refError = (0, lint_1.generateForSelf)(this, { start }, '<h1>');
132
- errors.push(refError);
138
+ /**
139
+ * @param name 标签名
140
+ * @param attr 标签属性
141
+ * @param closing 是否闭合
142
+ * @param selfClosing 是否自封闭
143
+ */
144
+ constructor(name, attr, closing, selfClosing, config = index_1.default.getConfig(), accum = []) {
145
+ super(undefined, config, accum);
146
+ this.insertAt(attr);
147
+ this.setAttribute('name', name.toLowerCase());
148
+ this.#closing = closing;
149
+ this.#selfClosing = selfClosing;
150
+ this.#tag = name;
133
151
  }
134
- if (this.closest('table-attrs')) {
135
- refError ??= (0, lint_1.generateForSelf)(this, { start }, '');
136
- errors.push({
137
- ...refError,
138
- message: index_1.default.msg('HTML tag in table attributes'),
139
- });
152
+ /** @private */
153
+ toString() {
154
+ return `<${this.closing ? '/' : ''}${this.#tag}${super.toString()}${this.#selfClosing ? '/' : ''}>`;
140
155
  }
141
- try {
142
- this.findMatchingTag();
156
+ /** @override */
157
+ text() {
158
+ const { closing, name } = this, tag = `${this.#tag}${closing ? '' : super.text()}`, { html } = this.getAttribute('config');
159
+ if (html[2].includes(name)) {
160
+ return closing && name !== 'br' ? '' : `<${tag}>`;
161
+ }
162
+ return `<${closing ? '/' : ''}${tag}${this.#selfClosing && html[1].includes(name) ? '/' : ''}>`;
143
163
  }
144
- catch (e) {
145
- if (e instanceof SyntaxError) {
146
- const { message } = e;
164
+ /** @private */
165
+ getAttribute(key) {
166
+ /* NOT FOR BROWSER */
167
+ if (key === 'tag') {
168
+ return this.#tag;
169
+ }
170
+ /* NOT FOR BROWSER END */
171
+ return key === 'padding'
172
+ ? this.#tag.length + (this.closing ? 2 : 1)
173
+ : super.getAttribute(key);
174
+ }
175
+ /** @override */
176
+ lint(start = this.getAbsoluteIndex()) {
177
+ const errors = super.lint(start);
178
+ let refError;
179
+ if (this.name === 'h1' && !this.closing) {
180
+ refError = (0, lint_1.generateForSelf)(this, { start }, '<h1>');
181
+ errors.push(refError);
182
+ }
183
+ if (this.closest('table-attrs')) {
147
184
  refError ??= (0, lint_1.generateForSelf)(this, { start }, '');
148
- const [msg] = message.split(':'), error = { ...refError, message: index_1.default.msg(msg) };
149
- if (msg === 'unclosed tag' && !this.closest('heading-title')) {
150
- if (formattingTags.has(this.name)) {
151
- const childNodes = this.parentNode?.childNodes, i = childNodes?.indexOf(this);
152
- if (!childNodes?.slice(0, i).some(({ type, name }) => type === 'html' && name === this.name)) {
185
+ errors.push({
186
+ ...refError,
187
+ message: index_1.default.msg('HTML tag in table attributes'),
188
+ });
189
+ }
190
+ try {
191
+ this.findMatchingTag();
192
+ }
193
+ catch (e) {
194
+ if (e instanceof SyntaxError) {
195
+ const { message } = e;
196
+ refError ??= (0, lint_1.generateForSelf)(this, { start }, '');
197
+ const [msg] = message.split(':'), error = { ...refError, message: index_1.default.msg(msg) };
198
+ if (msg === 'unclosed tag' && !this.closest('heading-title')) {
199
+ if (formattingTags.has(this.name)) {
200
+ const childNodes = this.parentNode?.childNodes, i = childNodes?.indexOf(this);
201
+ if (!childNodes?.slice(0, i).some(({ type, name }) => type === 'html' && name === this.name)) {
202
+ error.severity = 'warning';
203
+ }
204
+ }
205
+ else {
153
206
  error.severity = 'warning';
154
207
  }
155
208
  }
156
- else {
157
- error.severity = 'warning';
158
- }
159
- }
160
- else if (msg === 'unmatched closing tag') {
161
- const ancestor = this.closest('magic-word');
162
- if (ancestor && magicWords.has(ancestor.name)) {
163
- error.severity = 'warning';
209
+ else if (msg === 'unmatched closing tag') {
210
+ const ancestor = this.closest('magic-word');
211
+ if (ancestor && magicWords.has(ancestor.name)) {
212
+ error.severity = 'warning';
213
+ }
164
214
  }
215
+ errors.push(error);
165
216
  }
166
- errors.push(error);
167
217
  }
218
+ if (obsoleteTags.has(this.name)) {
219
+ refError ??= (0, lint_1.generateForSelf)(this, { start }, '');
220
+ errors.push({
221
+ ...refError,
222
+ message: index_1.default.msg('obsolete HTML tag'),
223
+ severity: 'warning',
224
+ });
225
+ }
226
+ if ((this.name === 'b' || this.name === 'strong') && this.closest('heading-title')) {
227
+ refError ??= (0, lint_1.generateForSelf)(this, { start }, '');
228
+ errors.push({
229
+ ...refError,
230
+ message: index_1.default.msg('bold in section header'),
231
+ severity: 'warning',
232
+ });
233
+ }
234
+ return errors;
168
235
  }
169
- if (obsoleteTags.has(this.name)) {
170
- refError ??= (0, lint_1.generateForSelf)(this, { start }, '');
171
- errors.push({
172
- ...refError,
173
- message: index_1.default.msg('obsolete HTML tag'),
174
- severity: 'warning',
175
- });
236
+ /**
237
+ * 搜索匹配的标签
238
+ * @throws `SyntaxError` 同时闭合和自封闭的标签
239
+ * @throws `SyntaxError` 无效自封闭标签
240
+ * @throws `SyntaxError` 未匹配的标签
241
+ */
242
+ findMatchingTag() {
243
+ const { html } = this.getAttribute('config'), { name: tagName, parentNode, closing } = this, string = (0, string_1.noWrap)(String(this));
244
+ if (closing && (this.#selfClosing || html[2].includes(tagName))) {
245
+ throw new SyntaxError(`tag that is both closing and self-closing: ${string}`);
246
+ }
247
+ else if (html[2].includes(tagName) || this.#selfClosing && html[1].includes(tagName)) { // 自封闭标签
248
+ return this;
249
+ }
250
+ else if (this.#selfClosing && html[0].includes(tagName)) {
251
+ throw new SyntaxError(`invalid self-closing tag: ${string}`);
252
+ }
253
+ else if (!parentNode) {
254
+ return undefined;
255
+ }
256
+ const { childNodes } = parentNode, i = childNodes.indexOf(this), siblings = (closing ? childNodes.slice(0, i).reverse() : childNodes.slice(i + 1))
257
+ .filter((child) => child.type === 'html' && child.name === tagName);
258
+ let imbalance = closing ? -1 : 1;
259
+ for (const token of siblings) {
260
+ if (token.#closing) {
261
+ imbalance--;
262
+ }
263
+ else {
264
+ imbalance++;
265
+ }
266
+ if (imbalance === 0) {
267
+ return token;
268
+ }
269
+ }
270
+ throw new SyntaxError(`${closing ? 'unmatched closing' : 'unclosed'} tag: ${string}`);
176
271
  }
177
- if ((this.name === 'b' || this.name === 'strong') && this.closest('heading-title')) {
178
- refError ??= (0, lint_1.generateForSelf)(this, { start }, '');
179
- errors.push({
180
- ...refError,
181
- message: index_1.default.msg('bold in section header'),
182
- severity: 'warning',
272
+ /** @override */
273
+ print() {
274
+ const { closing, name } = this, { html } = this.getAttribute('config');
275
+ return super.print({
276
+ pre: `&lt;${this.closing ? '/' : ''}${this.#tag}`,
277
+ post: `${this.#selfClosing ? '/' : ''}&gt;`,
278
+ class: closing && html[2].includes(name) && name !== 'br' ? 'html-invalid' : 'html',
183
279
  });
184
280
  }
185
- return errors;
186
- }
187
- /**
188
- * 搜索匹配的标签
189
- * @throws `SyntaxError` 同时闭合和自封闭的标签
190
- * @throws `SyntaxError` 无效自封闭标签
191
- * @throws `SyntaxError` 未匹配的标签
192
- */
193
- findMatchingTag() {
194
- const { html } = this.getAttribute('config'), { name: tagName, parentNode, closing } = this, string = (0, string_1.noWrap)(String(this));
195
- if (closing && (this.#selfClosing || html[2].includes(tagName))) {
196
- throw new SyntaxError(`tag that is both closing and self-closing: ${string}`);
197
- }
198
- else if (html[2].includes(tagName) || this.#selfClosing && html[1].includes(tagName)) { // 自封闭标签
199
- return this;
281
+ /** @override */
282
+ json() {
283
+ const json = super.json();
284
+ Object.assign(json, { closing: this.closing, selfClosing: this.#selfClosing });
285
+ return json;
200
286
  }
201
- else if (this.#selfClosing && html[0].includes(tagName)) {
202
- throw new SyntaxError(`invalid self-closing tag: ${string}`);
287
+ /* NOT FOR BROWSER */
288
+ /** @override */
289
+ cloneNode() {
290
+ const [attr] = this.cloneChildNodes(), config = this.getAttribute('config');
291
+ // @ts-expect-error abstract class
292
+ return debug_1.Shadow.run(() => new HtmlToken(this.#tag, attr, this.closing, this.selfClosing, config));
203
293
  }
204
- else if (!parentNode) {
205
- return undefined;
294
+ /**
295
+ * 更换标签名
296
+ * @param tag 标签名
297
+ * @throws `RangeError` 非法的HTML标签
298
+ */
299
+ replaceTag(tag) {
300
+ const name = tag.toLowerCase();
301
+ if (!this.getAttribute('config').html.flat().includes(name)) {
302
+ throw new RangeError(`非法的HTML标签:${tag}`);
303
+ }
304
+ this.setAttribute('name', name);
305
+ this.#tag = tag;
206
306
  }
207
- const { childNodes } = parentNode, i = childNodes.indexOf(this), siblings = (closing ? childNodes.slice(0, i).reverse() : childNodes.slice(i + 1))
208
- .filter((child) => child.type === 'html' && child.name === tagName);
209
- let imbalance = closing ? -1 : 1;
210
- for (const token of siblings) {
211
- if (token.#closing) {
212
- imbalance--;
307
+ /**
308
+ * 修复无效自封闭标签
309
+ * @throws `Error` 无法修复无效自封闭标签
310
+ */
311
+ fix() {
312
+ const config = this.getAttribute('config'), { parentNode, name: tagName, firstChild, selfClosing } = this;
313
+ if (!parentNode || !selfClosing || !config.html[0].includes(tagName)) {
314
+ return;
213
315
  }
214
- else {
215
- imbalance++;
316
+ else if (firstChild.text().trim()) {
317
+ this.#selfClosing = false;
318
+ this.after(index_1.default.parse(`</${this.name}>`, false, 3, this.getAttribute('config')).firstChild);
319
+ return;
216
320
  }
217
- if (imbalance === 0) {
218
- return token;
321
+ const { childNodes } = parentNode, i = childNodes.indexOf(this), prevSiblings = childNodes.slice(0, i)
322
+ .filter((child) => child.type === 'html' && child.name === tagName), imbalance = prevSiblings.reduce((acc, { closing }) => acc + (closing ? 1 : -1), 0);
323
+ if (imbalance < 0) {
324
+ this.#selfClosing = false;
325
+ this.#closing = true;
326
+ }
327
+ else {
328
+ throw new Error(`无法修复无效自封闭标签:前文共有 ${imbalance} 个未匹配的闭合标签`);
219
329
  }
220
330
  }
221
- throw new SyntaxError(`${closing ? 'unmatched closing' : 'unclosed'} tag: ${string}`);
222
- }
223
- /** @override */
224
- print() {
225
- const { closing, name } = this, { html } = this.getAttribute('config');
226
- return super.print({
227
- pre: `&lt;${this.closing ? '/' : ''}${this.#tag}`,
228
- post: `${this.#selfClosing ? '/' : ''}&gt;`,
229
- class: closing && html[2].includes(name) && name !== 'br' ? 'html-invalid' : 'html',
230
- });
231
- }
232
- /** @override */
233
- json() {
234
- return {
235
- ...super.json(),
236
- closing: this.closing,
237
- selfClosing: this.#selfClosing,
238
- };
239
- }
240
- /* NOT FOR BROWSER */
241
- /** @override */
242
- cloneNode() {
243
- const [attr] = this.cloneChildNodes(), config = this.getAttribute('config');
244
- // @ts-expect-error abstract class
245
- return debug_1.Shadow.run(() => new HtmlToken(this.#tag, attr, this.closing, this.selfClosing, config));
246
- }
247
- /**
248
- * 更换标签名
249
- * @param tag 标签名
250
- * @throws `RangeError` 非法的HTML标签
251
- */
252
- replaceTag(tag) {
253
- const name = tag.toLowerCase();
254
- if (!this.getAttribute('config').html.flat().includes(name)) {
255
- throw new RangeError(`非法的HTML标签:${tag}`);
256
- }
257
- this.setAttribute('name', name);
258
- this.#tag = tag;
259
- }
260
- /**
261
- * 修复无效自封闭标签
262
- * @throws `Error` 无法修复无效自封闭标签
263
- */
264
- fix() {
265
- const config = this.getAttribute('config'), { parentNode, name: tagName, firstChild, selfClosing } = this;
266
- if (!parentNode || !selfClosing || !config.html[0].includes(tagName)) {
267
- return;
268
- }
269
- else if (firstChild.text().trim()) {
270
- this.#selfClosing = false;
271
- this.after(index_1.default.parse(`</${this.name}>`, false, 3, this.getAttribute('config')).firstChild);
272
- return;
273
- }
274
- const { childNodes } = parentNode, i = childNodes.indexOf(this), prevSiblings = childNodes.slice(0, i)
275
- .filter((child) => child.type === 'html' && child.name === tagName), imbalance = prevSiblings.reduce((acc, { closing }) => acc + (closing ? 1 : -1), 0);
276
- if (imbalance < 0) {
277
- this.#selfClosing = false;
278
- this.#closing = true;
279
- }
280
- else {
281
- throw new Error(`无法修复无效自封闭标签:前文共有 ${imbalance} 个未匹配的闭合标签`);
282
- }
283
- }
284
- }
331
+ };
332
+ return HtmlToken = _classThis;
333
+ })();
285
334
  exports.HtmlToken = HtmlToken;
286
335
  constants_1.classes['HtmlToken'] = __filename;
@@ -10,10 +10,10 @@ export declare abstract class ImageParameterToken extends Token {
10
10
  readonly type = "image-parameter";
11
11
  readonly name: string;
12
12
  abstract get parentNode(): FileToken | undefined;
13
- abstract get parentElement(): FileToken | undefined;
14
13
  abstract get nextSibling(): this | undefined;
15
- abstract get nextElementSibling(): this | undefined;
16
14
  abstract get previousSibling(): AtomToken | this;
15
+ abstract get parentElement(): FileToken | undefined;
16
+ abstract get nextElementSibling(): this | undefined;
17
17
  abstract get previousElementSibling(): AtomToken | this;
18
18
  /** 图片链接 */
19
19
  get link(): string | Title | undefined;
@@ -44,6 +44,7 @@ function validate(key, val, config = index_1.default.getConfig(), halfParsed = f
44
44
  class ImageParameterToken extends index_2.Token {
45
45
  type = 'image-parameter';
46
46
  #syntax = '';
47
+ /* NOT FOR BROWSER END */
47
48
  /** 图片链接 */
48
49
  get link() {
49
50
  return this.name === 'link' ? validate('link', super.text(), this.getAttribute('config')) : undefined;
@@ -13,16 +13,16 @@ export declare abstract class ImagemapToken extends Token {
13
13
  readonly type = "ext-inner";
14
14
  readonly name: 'imagemap';
15
15
  readonly childNodes: readonly (GalleryImageToken | NoincludeToken | ImagemapLinkToken | AstText)[];
16
- abstract get children(): (GalleryImageToken | NoincludeToken | ImagemapLinkToken)[];
17
16
  abstract get firstChild(): NoincludeToken | GalleryImageToken | undefined;
18
- abstract get firstElementChild(): NoincludeToken | GalleryImageToken | undefined;
19
17
  abstract get lastChild(): GalleryImageToken | NoincludeToken | ImagemapLinkToken | AstText | undefined;
20
- abstract get lastElementChild(): GalleryImageToken | NoincludeToken | ImagemapLinkToken | undefined;
21
18
  abstract get nextSibling(): undefined;
22
- abstract get nextElementSibling(): undefined;
23
19
  abstract get previousSibling(): AttributesToken;
24
- abstract get previousElementSibling(): AttributesToken;
25
20
  abstract get parentNode(): ExtToken | undefined;
21
+ abstract get children(): (GalleryImageToken | NoincludeToken | ImagemapLinkToken)[];
22
+ abstract get firstElementChild(): NoincludeToken | GalleryImageToken | undefined;
23
+ abstract get lastElementChild(): GalleryImageToken | NoincludeToken | ImagemapLinkToken | undefined;
24
+ abstract get nextElementSibling(): undefined;
25
+ abstract get previousElementSibling(): AttributesToken;
26
26
  abstract get parentElement(): ExtToken | undefined;
27
27
  /** 图片 */
28
28
  get image(): GalleryImageToken | undefined;
@@ -16,6 +16,7 @@ const imagemapLink_1 = require("./imagemapLink");
16
16
  */
17
17
  class ImagemapToken extends index_2.Token {
18
18
  type = 'ext-inner';
19
+ /* NOT FOR BROWSER END */
19
20
  /** 图片 */
20
21
  get image() {
21
22
  return this.childNodes.find((0, debug_1.isToken)('imagemap-image'));
@@ -29,7 +30,7 @@ class ImagemapToken extends index_2.Token {
29
30
  /** @param inner 标签内部wikitext */
30
31
  constructor(inner, config = index_1.default.getConfig(), accum = []) {
31
32
  super(undefined, config, accum, {
32
- GalleryImageToken: ':', ImagemapLinkToken: ':', SingleLineNoincludeToken: ':', AstText: ':',
33
+ GalleryImageToken: ':', ImagemapLinkToken: ':', NoincludeToken: ':', AstText: ':',
33
34
  });
34
35
  if (!inner) {
35
36
  return;
@@ -43,11 +44,7 @@ class ImagemapToken extends index_2.Token {
43
44
  }
44
45
  else if (first) {
45
46
  const [file, ...options] = line.split('|'), title = this.normalizeTitle(file, 0, true);
46
- if (title.valid
47
- /* NOT FOR BROWSER */
48
- && !title.interwiki
49
- /* NOT FOR BROWSER END */
50
- && title.ns === 6) {
47
+ if (title.valid && !title.interwiki && title.ns === 6) {
51
48
  // @ts-expect-error abstract class
52
49
  const token = new galleryImage_1.GalleryImageToken('imagemap', file, options.length > 0 ? options.join('|') : undefined, config, accum);
53
50
  super.insertAt(token);
@@ -55,9 +52,6 @@ class ImagemapToken extends index_2.Token {
55
52
  continue;
56
53
  }
57
54
  else {
58
- /* NOT FOR BROWSER */
59
- index_1.default.error('<imagemap>标签内必须先包含一张合法图片!', line);
60
- /* NOT FOR BROWSER END */
61
55
  error = true;
62
56
  }
63
57
  }
@@ -136,7 +130,7 @@ class ImagemapToken extends index_2.Token {
136
130
  * @throws `Error` 禁止移除图片
137
131
  */
138
132
  removeAt(i) {
139
- if (this.childNodes[i].type === 'imagemap-image') {
133
+ if (this.childNodes[i]?.type === 'imagemap-image') {
140
134
  throw new Error('禁止移除<imagemap>内的图片!');
141
135
  }
142
136
  return super.removeAt(i);
@@ -5,15 +5,19 @@ import { LinkToken } from './link/index';
5
5
  import { ExtLinkToken } from './extLink';
6
6
  import type { Title } from '../lib/title';
7
7
  import type { AstText, ImagemapToken } from '../internal';
8
+ /**
9
+ * `<imagemap>`内的链接
10
+ * @classdesc `{childNodes: [AstText, LinkToken|ExtLinkToken, NoincludeToken]}`
11
+ */
8
12
  export declare abstract class ImagemapLinkToken extends Token {
9
13
  readonly type = "imagemap-link";
10
14
  readonly childNodes: readonly [AstText, LinkToken | ExtLinkToken, NoincludeToken];
11
- abstract get children(): [LinkToken | ExtLinkToken, NoincludeToken];
12
15
  abstract get firstChild(): AstText;
13
- abstract get firstElementChild(): LinkToken | ExtLinkToken;
14
16
  abstract get lastChild(): NoincludeToken;
15
- abstract get lastElementChild(): NoincludeToken;
16
17
  abstract get parentNode(): ImagemapToken | undefined;
18
+ abstract get children(): [LinkToken | ExtLinkToken, NoincludeToken];
19
+ abstract get firstElementChild(): LinkToken | ExtLinkToken;
20
+ abstract get lastElementChild(): NoincludeToken;
17
21
  abstract get parentElement(): ImagemapToken | undefined;
18
22
  /** 内外链接 */
19
23
  get link(): string | Title;
@@ -25,4 +29,3 @@ export declare abstract class ImagemapLinkToken extends Token {
25
29
  */
26
30
  constructor(pre: string, linkStuff: readonly [string, string | undefined, string | undefined] | readonly [string, string | undefined], post: string, config?: Parser.Config, accum?: Token[]);
27
31
  }
28
- export {};