wikiparser-node 1.13.0-b → 1.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (174) hide show
  1. package/config/.schema.json +203 -0
  2. package/config/default.json +32 -31
  3. package/config/enwiki.json +829 -15
  4. package/config/llwiki.json +56 -21
  5. package/config/moegirl.json +65 -21
  6. package/config/zhwiki.json +498 -32
  7. package/dist/addon/table.js +494 -0
  8. package/dist/addon/token.js +392 -0
  9. package/dist/addon/transclude.js +184 -0
  10. package/dist/base.d.ts +109 -0
  11. package/dist/base.js +67 -0
  12. package/dist/index.d.ts +33 -0
  13. package/dist/index.js +279 -0
  14. package/dist/internal.d.ts +47 -0
  15. package/dist/lib/element.d.ts +125 -0
  16. package/dist/lib/element.js +364 -0
  17. package/dist/lib/node.d.ts +173 -0
  18. package/dist/lib/node.js +478 -0
  19. package/dist/lib/range.d.ts +105 -0
  20. package/dist/lib/range.js +406 -0
  21. package/dist/lib/ranges.d.ts +28 -0
  22. package/dist/lib/ranges.js +126 -0
  23. package/dist/lib/rect.d.ts +18 -0
  24. package/dist/lib/rect.js +36 -0
  25. package/dist/lib/text.d.ts +58 -0
  26. package/dist/lib/text.js +414 -0
  27. package/dist/lib/title.d.ts +49 -0
  28. package/dist/lib/title.js +251 -0
  29. package/dist/mixin/attributesParent.d.ts +49 -0
  30. package/dist/mixin/attributesParent.js +80 -0
  31. package/dist/mixin/fixed.d.ts +5 -0
  32. package/dist/mixin/fixed.js +32 -0
  33. package/dist/mixin/flagsParent.d.ts +43 -0
  34. package/dist/mixin/flagsParent.js +64 -0
  35. package/dist/mixin/hidden.d.ts +7 -0
  36. package/dist/mixin/hidden.js +39 -0
  37. package/dist/mixin/magicLinkParent.d.ts +19 -0
  38. package/dist/mixin/magicLinkParent.js +43 -0
  39. package/dist/mixin/singleLine.d.ts +5 -0
  40. package/dist/mixin/singleLine.js +25 -0
  41. package/dist/mixin/sol.d.ts +6 -0
  42. package/dist/mixin/sol.js +45 -0
  43. package/dist/mixin/syntax.d.ts +8 -0
  44. package/dist/mixin/syntax.js +46 -0
  45. package/dist/parser/braces.js +152 -0
  46. package/dist/parser/commentAndExt.js +84 -0
  47. package/dist/parser/converter.js +41 -0
  48. package/dist/parser/externalLinks.js +39 -0
  49. package/dist/parser/hrAndDoubleUnderscore.js +44 -0
  50. package/dist/parser/html.js +40 -0
  51. package/dist/parser/links.js +103 -0
  52. package/dist/parser/list.js +116 -0
  53. package/dist/parser/magicLinks.js +55 -0
  54. package/dist/parser/quotes.js +69 -0
  55. package/dist/parser/redirect.js +28 -0
  56. package/dist/parser/selector.js +443 -0
  57. package/dist/parser/table.js +125 -0
  58. package/dist/src/arg.d.ts +49 -0
  59. package/dist/src/arg.js +220 -0
  60. package/dist/src/atom.d.ts +14 -0
  61. package/dist/src/atom.js +54 -0
  62. package/dist/src/attribute.d.ts +64 -0
  63. package/dist/src/attribute.js +482 -0
  64. package/dist/src/attributes.d.ts +103 -0
  65. package/dist/src/attributes.js +383 -0
  66. package/dist/src/converter.d.ts +28 -0
  67. package/dist/src/converter.js +153 -0
  68. package/dist/src/converterFlags.d.ts +80 -0
  69. package/dist/src/converterFlags.js +241 -0
  70. package/dist/src/converterRule.d.ts +71 -0
  71. package/dist/src/converterRule.js +218 -0
  72. package/dist/src/extLink.d.ts +36 -0
  73. package/dist/src/extLink.js +223 -0
  74. package/dist/src/gallery.d.ts +51 -0
  75. package/dist/src/gallery.js +166 -0
  76. package/dist/src/heading.d.ts +44 -0
  77. package/dist/src/heading.js +222 -0
  78. package/dist/src/hidden.d.ts +9 -0
  79. package/dist/src/hidden.js +82 -0
  80. package/dist/src/html.d.ts +61 -0
  81. package/dist/src/html.js +344 -0
  82. package/dist/src/imageParameter.d.ts +60 -0
  83. package/dist/src/imageParameter.js +262 -0
  84. package/dist/src/imagemap.d.ts +47 -0
  85. package/dist/src/imagemap.js +148 -0
  86. package/dist/src/imagemapLink.d.ts +35 -0
  87. package/dist/src/imagemapLink.js +99 -0
  88. package/dist/src/index.d.ts +136 -0
  89. package/dist/src/index.js +790 -0
  90. package/dist/src/link/base.d.ts +52 -0
  91. package/dist/src/link/base.js +258 -0
  92. package/dist/src/link/category.d.ts +22 -0
  93. package/dist/src/link/category.js +36 -0
  94. package/dist/src/link/file.d.ts +102 -0
  95. package/dist/src/link/file.js +345 -0
  96. package/dist/src/link/galleryImage.d.ts +29 -0
  97. package/dist/src/link/galleryImage.js +133 -0
  98. package/dist/src/link/index.d.ts +39 -0
  99. package/dist/src/link/index.js +100 -0
  100. package/dist/src/link/redirectTarget.d.ts +27 -0
  101. package/dist/src/link/redirectTarget.js +71 -0
  102. package/dist/src/magicLink.d.ts +57 -0
  103. package/dist/src/magicLink.js +261 -0
  104. package/dist/src/nested.d.ts +40 -0
  105. package/dist/src/nested.js +108 -0
  106. package/dist/src/nowiki/base.d.ts +28 -0
  107. package/dist/src/nowiki/base.js +90 -0
  108. package/dist/src/nowiki/comment.d.ts +14 -0
  109. package/dist/src/nowiki/comment.js +123 -0
  110. package/dist/src/nowiki/dd.d.ts +8 -0
  111. package/dist/src/nowiki/dd.js +74 -0
  112. package/dist/src/nowiki/doubleUnderscore.d.ts +15 -0
  113. package/dist/src/nowiki/doubleUnderscore.js +101 -0
  114. package/dist/src/nowiki/hr.d.ts +5 -0
  115. package/dist/src/nowiki/hr.js +72 -0
  116. package/dist/src/nowiki/index.d.ts +14 -0
  117. package/dist/src/nowiki/index.js +30 -0
  118. package/dist/src/nowiki/list.d.ts +5 -0
  119. package/dist/src/nowiki/list.js +67 -0
  120. package/dist/src/nowiki/listBase.d.ts +23 -0
  121. package/dist/src/nowiki/listBase.js +100 -0
  122. package/dist/src/nowiki/noinclude.d.ts +6 -0
  123. package/dist/src/nowiki/noinclude.js +77 -0
  124. package/dist/src/nowiki/quote.d.ts +14 -0
  125. package/dist/src/nowiki/quote.js +149 -0
  126. package/dist/src/onlyinclude.d.ts +13 -0
  127. package/dist/src/onlyinclude.js +60 -0
  128. package/dist/src/paramTag/index.d.ts +28 -0
  129. package/dist/src/paramTag/index.js +80 -0
  130. package/dist/src/paramTag/inputbox.d.ts +8 -0
  131. package/dist/src/paramTag/inputbox.js +24 -0
  132. package/dist/src/parameter.d.ts +60 -0
  133. package/dist/src/parameter.js +267 -0
  134. package/dist/src/pre.d.ts +28 -0
  135. package/dist/src/pre.js +62 -0
  136. package/dist/src/redirect.d.ts +30 -0
  137. package/dist/src/redirect.js +128 -0
  138. package/dist/src/syntax.d.ts +15 -0
  139. package/dist/src/syntax.js +87 -0
  140. package/dist/src/table/base.d.ts +28 -0
  141. package/dist/src/table/base.js +81 -0
  142. package/dist/src/table/index.d.ts +230 -0
  143. package/dist/src/table/index.js +506 -0
  144. package/dist/src/table/td.d.ts +72 -0
  145. package/dist/src/table/td.js +375 -0
  146. package/dist/src/table/tr.d.ts +30 -0
  147. package/dist/src/table/tr.js +61 -0
  148. package/dist/src/table/trBase.d.ts +49 -0
  149. package/dist/src/table/trBase.js +165 -0
  150. package/dist/src/tagPair/ext.d.ts +29 -0
  151. package/dist/src/tagPair/ext.js +229 -0
  152. package/dist/src/tagPair/include.d.ts +33 -0
  153. package/dist/src/tagPair/include.js +132 -0
  154. package/dist/src/tagPair/index.d.ts +23 -0
  155. package/dist/src/tagPair/index.js +130 -0
  156. package/dist/src/transclude.d.ts +159 -0
  157. package/dist/src/transclude.js +598 -0
  158. package/dist/util/constants.js +26 -0
  159. package/dist/util/debug.js +95 -0
  160. package/dist/util/diff.js +83 -0
  161. package/dist/util/html.js +146 -0
  162. package/dist/util/lint.js +32 -0
  163. package/dist/util/string.js +107 -0
  164. package/errors/README +3 -0
  165. package/package.json +21 -28
  166. package/printed/README +3 -0
  167. package/bundle/bundle.min.js +0 -37
  168. package/extensions/dist/base.js +0 -163
  169. package/extensions/dist/codejar.js +0 -53
  170. package/extensions/dist/editor.js +0 -159
  171. package/extensions/dist/highlight.js +0 -30
  172. package/extensions/dist/lint.js +0 -72
  173. package/extensions/editor.css +0 -59
  174. package/extensions/ui.css +0 -162
@@ -0,0 +1,344 @@
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
+ };
36
+ Object.defineProperty(exports, "__esModule", { value: true });
37
+ exports.HtmlToken = void 0;
38
+ const lint_1 = require("../util/lint");
39
+ const string_1 = require("../util/string");
40
+ const rect_1 = require("../lib/rect");
41
+ const index_1 = require("./index");
42
+ /* NOT FOR BROWSER */
43
+ const debug_1 = require("../util/debug");
44
+ const constants_1 = require("../util/constants");
45
+ const fixed_1 = require("../mixin/fixed");
46
+ const attributesParent_1 = require("../mixin/attributesParent");
47
+ const index_2 = require("../index");
48
+ /* NOT FOR BROWSER END */
49
+ const magicWords = new Set(['if', 'ifeq', 'ifexpr', 'ifexist', 'iferror', 'switch']), formattingTags = new Set([
50
+ 'b',
51
+ 'big',
52
+ 'center',
53
+ 'cite',
54
+ 'code',
55
+ 'del',
56
+ 'dfn',
57
+ 'em',
58
+ 'font',
59
+ 'i',
60
+ 'ins',
61
+ 'kbd',
62
+ 'mark',
63
+ 'pre',
64
+ 'q',
65
+ 's',
66
+ 'samp',
67
+ 'small',
68
+ 'strike',
69
+ 'strong',
70
+ 'sub',
71
+ 'sup',
72
+ 'tt',
73
+ 'u',
74
+ 'var',
75
+ ]), obsoleteTags = new Set(['strike', 'big', 'center', 'font', 'tt']);
76
+ /**
77
+ * HTML标签
78
+ * @classdesc `{childNodes: [AttributesToken]}`
79
+ */
80
+ let HtmlToken = (() => {
81
+ let _classDecorators = [fixed_1.fixedToken, (0, attributesParent_1.attributesParent)()];
82
+ let _classDescriptor;
83
+ let _classExtraInitializers = [];
84
+ let _classThis;
85
+ let _classSuper = index_1.Token;
86
+ var HtmlToken = class extends _classSuper {
87
+ static { _classThis = this; }
88
+ static {
89
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
90
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
91
+ HtmlToken = _classThis = _classDescriptor.value;
92
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
93
+ __runInitializers(_classThis, _classExtraInitializers);
94
+ }
95
+ #closing;
96
+ #selfClosing;
97
+ #tag;
98
+ /* NOT FOR BROWSER END */
99
+ get type() {
100
+ return 'html';
101
+ }
102
+ /** 是否自封闭 */
103
+ get selfClosing() {
104
+ return this.#selfClosing;
105
+ }
106
+ /** 是否是闭合标签 */
107
+ get closing() {
108
+ return this.#closing;
109
+ }
110
+ /* NOT FOR BROWSER */
111
+ /** @throws `Error` 自封闭标签或空标签 */
112
+ set closing(value) {
113
+ if (!value) {
114
+ this.#closing = false;
115
+ return;
116
+ }
117
+ else if (this.selfClosing) {
118
+ throw new Error('This is a self-closing tag!');
119
+ }
120
+ const { html: [, , tags] } = this.getAttribute('config');
121
+ if (tags.includes(this.name)) {
122
+ throw new Error('This is a void tag!');
123
+ }
124
+ this.#closing = true;
125
+ }
126
+ /** @throws `Error` 闭合标签或无效自封闭标签 */
127
+ set selfClosing(value) {
128
+ if (!value) {
129
+ this.#selfClosing = false;
130
+ return;
131
+ }
132
+ else if (this.closing) {
133
+ throw new Error('This is a closing tag!');
134
+ }
135
+ const { html: [tags] } = this.getAttribute('config');
136
+ if (tags.includes(this.name)) {
137
+ throw new Error(`<${this.name}> tag cannot be self-closing!`);
138
+ }
139
+ this.#selfClosing = true;
140
+ }
141
+ get font() {
142
+ return { bold: false, italic: false };
143
+ }
144
+ /* NOT FOR BROWSER END */
145
+ /**
146
+ * @param name 标签名
147
+ * @param attr 标签属性
148
+ * @param closing 是否闭合
149
+ * @param selfClosing 是否自封闭
150
+ */
151
+ constructor(name, attr, closing, selfClosing, config, accum) {
152
+ super(undefined, config, accum);
153
+ this.insertAt(attr);
154
+ this.setAttribute('name', name.toLowerCase());
155
+ this.#closing = closing;
156
+ this.#selfClosing = selfClosing;
157
+ this.#tag = name;
158
+ }
159
+ /** @private */
160
+ toString(skip) {
161
+ return `<${this.closing ? '/' : ''}${this.#tag}${super.toString(skip)}${this.#selfClosing ? '/' : ''}>`;
162
+ }
163
+ /** @private */
164
+ text() {
165
+ const { closing,
166
+ /* NOT FOR BROWSER */
167
+ name, } = this, { html: [, , voidTags] } = this.getAttribute('config'), tag = this.#tag + (closing ? '' : super.text());
168
+ /* NOT FOR BROWSER */
169
+ if (voidTags.includes(name)) {
170
+ return closing && name !== 'br' ? '' : `<${tag}/>`;
171
+ }
172
+ /* NOT FOR BROWSER END */
173
+ return `<${closing ? '/' : ''}${tag}${this.#selfClosing ? '/' : ''}>`;
174
+ }
175
+ /** @private */
176
+ getAttribute(key) {
177
+ return key === 'padding'
178
+ ? this.#tag.length + (this.closing ? 2 : 1)
179
+ : super.getAttribute(key);
180
+ }
181
+ /** @private */
182
+ lint(start = this.getAbsoluteIndex(), re) {
183
+ const errors = super.lint(start, re), rect = new rect_1.BoundingRect(this, start);
184
+ if (this.name === 'h1' && !this.closing) {
185
+ errors.push((0, lint_1.generateForSelf)(this, rect, 'h1', '<h1>'));
186
+ }
187
+ if (this.closest('table-attrs')) {
188
+ errors.push((0, lint_1.generateForSelf)(this, rect, 'parsing-order', 'HTML tag in table attributes'));
189
+ }
190
+ try {
191
+ this.findMatchingTag();
192
+ }
193
+ catch (e) {
194
+ if (e instanceof SyntaxError) {
195
+ const { message } = e;
196
+ const msg = message.split(':')[0].toLowerCase(), error = (0, lint_1.generateForSelf)(this, rect, 'unmatched-tag', msg);
197
+ if (msg === 'unclosed tag' && !this.closest('heading-title')) {
198
+ if (formattingTags.has(this.name)) {
199
+ const childNodes = this.parentNode?.childNodes, i = childNodes?.indexOf(this);
200
+ if (!childNodes?.slice(0, i).some(({ type, name }) => type === 'html' && name === this.name)) {
201
+ error.severity = 'warning';
202
+ }
203
+ }
204
+ else {
205
+ error.severity = 'warning';
206
+ }
207
+ }
208
+ else if (msg === 'unmatched closing tag') {
209
+ const ancestor = this.closest('magic-word');
210
+ if (ancestor && magicWords.has(ancestor.name)) {
211
+ error.severity = 'warning';
212
+ }
213
+ else {
214
+ error.suggestions = [
215
+ {
216
+ desc: 'remove',
217
+ range: [start, error.endIndex],
218
+ text: '',
219
+ },
220
+ ];
221
+ }
222
+ }
223
+ else if (msg === 'tag that is both closing and self-closing') {
224
+ const { html: [, , voidTags] } = this.getAttribute('config');
225
+ if (voidTags.includes(this.name)) {
226
+ error.fix = { range: [start + 1, start + 2], text: '' };
227
+ }
228
+ }
229
+ errors.push(error);
230
+ }
231
+ }
232
+ if (obsoleteTags.has(this.name)) {
233
+ errors.push((0, lint_1.generateForSelf)(this, rect, 'obsolete-tag', 'obsolete HTML tag', 'warning'));
234
+ }
235
+ if ((this.name === 'b' || this.name === 'strong') && this.closest('heading-title')) {
236
+ errors.push((0, lint_1.generateForSelf)(this, rect, 'bold-header', 'bold in section header', 'warning'));
237
+ }
238
+ return errors;
239
+ }
240
+ /**
241
+ * 搜索匹配的标签
242
+ * @throws `SyntaxError` 同时闭合和自封闭的标签
243
+ * @throws `SyntaxError` 无效自封闭标签
244
+ * @throws `SyntaxError` 未匹配的标签
245
+ */
246
+ findMatchingTag() {
247
+ const { html: [normalTags, flexibleTags, voidTags] } = this.getAttribute('config'), { name: tagName, parentNode, closing } = this, string = (0, string_1.noWrap)(this.toString());
248
+ if (closing && (this.#selfClosing || voidTags.includes(tagName))) {
249
+ throw new SyntaxError(`Tag that is both closing and self-closing: ${string}`);
250
+ }
251
+ else if (voidTags.includes(tagName) || this.#selfClosing && flexibleTags.includes(tagName)) { // 自封闭标签
252
+ return this;
253
+ }
254
+ else if (this.#selfClosing && normalTags.includes(tagName)) {
255
+ throw new SyntaxError(`Invalid self-closing tag: ${string}`);
256
+ }
257
+ else if (!parentNode) {
258
+ return undefined;
259
+ }
260
+ const { childNodes } = parentNode, i = childNodes.indexOf(this), siblings = (closing ? childNodes.slice(0, i).reverse() : childNodes.slice(i + 1))
261
+ .filter((child) => child.type === 'html' && child.name === tagName);
262
+ let imbalance = closing ? -1 : 1;
263
+ for (const token of siblings) {
264
+ if (token.#closing) {
265
+ imbalance--;
266
+ }
267
+ else {
268
+ imbalance++;
269
+ }
270
+ if (imbalance === 0) {
271
+ return token;
272
+ }
273
+ }
274
+ throw new SyntaxError(`${closing ? 'Unmatched closing' : 'Unclosed'} tag: ${string}`);
275
+ }
276
+ /** @private */
277
+ print() {
278
+ return super.print({
279
+ pre: `&lt;${this.closing ? '/' : ''}${this.#tag}`,
280
+ post: `${this.#selfClosing ? '/' : ''}&gt;`,
281
+ });
282
+ }
283
+ /** @override */
284
+ json() {
285
+ const json = super.json();
286
+ Object.assign(json, { closing: this.closing, selfClosing: this.#selfClosing });
287
+ return json;
288
+ }
289
+ /* NOT FOR BROWSER */
290
+ cloneNode() {
291
+ const [attr] = this.cloneChildNodes(), config = this.getAttribute('config');
292
+ // @ts-expect-error abstract class
293
+ return debug_1.Shadow.run(() => new HtmlToken(this.#tag, attr, this.closing, this.selfClosing, config));
294
+ }
295
+ /**
296
+ * 更换标签名
297
+ * @param tag 标签名
298
+ * @throws `RangeError` 非法的HTML标签
299
+ */
300
+ replaceTag(tag) {
301
+ const name = tag.toLowerCase();
302
+ if (!this.getAttribute('config').html.some(tags => tags.includes(name))) {
303
+ throw new RangeError(`Invalid HTML tag: ${tag}`);
304
+ }
305
+ this.setAttribute('name', name);
306
+ this.#tag = tag;
307
+ }
308
+ /**
309
+ * 修复无效自封闭标签
310
+ * @throws `Error` 无法修复无效自封闭标签
311
+ */
312
+ fix() {
313
+ const { html: [normalTags] } = this.getAttribute('config'), { parentNode, name: tagName, firstChild, selfClosing } = this;
314
+ if (!parentNode || !selfClosing || !normalTags.includes(tagName)) {
315
+ return;
316
+ }
317
+ else if (firstChild.text().trim()) {
318
+ this.#selfClosing = false;
319
+ this.after(index_2.default.parse(`</${this.name}>`, false, 3, this.getAttribute('config')).firstChild);
320
+ return;
321
+ }
322
+ const { childNodes } = parentNode, i = childNodes.indexOf(this), prevSiblings = childNodes.slice(0, i)
323
+ .filter((child) => child.type === 'html' && child.name === tagName), imbalance = prevSiblings.reduce((acc, { closing }) => acc + (closing ? 1 : -1), 0);
324
+ if (imbalance < 0) {
325
+ this.#selfClosing = false;
326
+ this.#closing = true;
327
+ }
328
+ else {
329
+ throw new Error(`Cannot fix invalid self-closing tag: The previous ${imbalance} closing tag(s) are unmatched`);
330
+ }
331
+ }
332
+ /** @private */
333
+ toHtmlInternal() {
334
+ const { closing, name } = this, { html: [, selfClosingTags, voidTags] } = this.getAttribute('config'), tag = name + (closing ? '' : super.toHtmlInternal());
335
+ if (voidTags.includes(name)) {
336
+ return closing && name !== 'br' ? '' : `<${tag}>`;
337
+ }
338
+ return `<${closing ? '/' : ''}${tag}>${this.#selfClosing && !closing && selfClosingTags.includes(name) ? `</${name}>` : ''}`;
339
+ }
340
+ };
341
+ return HtmlToken = _classThis;
342
+ })();
343
+ exports.HtmlToken = HtmlToken;
344
+ constants_1.classes['HtmlToken'] = __filename;
@@ -0,0 +1,60 @@
1
+ import Parser from '../index';
2
+ import { Token } from './index';
3
+ import type { LintError } from '../base';
4
+ import type { Title } from '../lib/title';
5
+ import type { AtomToken, FileToken, AstNodes, AstText } from '../internal';
6
+ export declare const galleryParams: Set<string>;
7
+ /** 图片参数 */
8
+ export declare abstract class ImageParameterToken extends Token {
9
+ #private;
10
+ readonly name: string;
11
+ abstract get parentNode(): FileToken | undefined;
12
+ abstract get nextSibling(): this | undefined;
13
+ abstract get previousSibling(): AtomToken | this;
14
+ abstract get parentElement(): FileToken | undefined;
15
+ abstract get nextElementSibling(): this | undefined;
16
+ abstract get previousElementSibling(): AtomToken | this;
17
+ get type(): 'image-parameter';
18
+ /** 图片链接 */
19
+ get link(): string | Title | undefined;
20
+ set link(value: string);
21
+ /** getValue()的getter */
22
+ get value(): string | true;
23
+ set value(value: string | true);
24
+ /** 图片大小 */
25
+ get size(): {
26
+ width: string;
27
+ height: string;
28
+ } | undefined;
29
+ set size(size: {
30
+ width: string;
31
+ height: string;
32
+ } | undefined);
33
+ /** 图片宽度 */
34
+ get width(): string | undefined;
35
+ set width(width: string | undefined);
36
+ /** 图片高度 */
37
+ get height(): string | undefined;
38
+ set height(height: string | undefined);
39
+ /** @param str 图片参数 */
40
+ constructor(str: string, extension: string | undefined, config?: Parser.Config, accum?: Token[]);
41
+ /** 获取参数值 */
42
+ getValue(): string | true;
43
+ cloneNode(): this;
44
+ /**
45
+ * @override
46
+ * @param token 待插入的子节点
47
+ * @param i 插入位置
48
+ * @throws `Error` 不接受自定义输入的图片参数
49
+ */
50
+ insertAt(token: string, i?: number): AstText;
51
+ insertAt<T extends AstNodes>(token: T, i?: number): T;
52
+ /**
53
+ * 设置参数值
54
+ * @param value 参数值
55
+ * @throws `Error` 无效参数
56
+ */
57
+ setValue(value?: string | boolean): void;
58
+ /** 获取网址 */
59
+ getUrl(): string | undefined;
60
+ }
@@ -0,0 +1,262 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ImageParameterToken = exports.galleryParams = void 0;
4
+ const string_1 = require("../util/string");
5
+ const lint_1 = require("../util/lint");
6
+ const index_1 = require("../index");
7
+ const index_2 = require("./index");
8
+ /* NOT FOR BROWSER */
9
+ const debug_1 = require("../util/debug");
10
+ const constants_1 = require("../util/constants");
11
+ /* NOT FOR BROWSER END */
12
+ exports.galleryParams = new Set(['alt', 'link', 'lang', 'page', 'caption']);
13
+ function validate(key, val, config, halfParsed, ext) {
14
+ val = val.trim();
15
+ let value = val.replace(key === 'link' ? /\0\d+[tq]\x7F/gu : /\0\d+t\x7F/gu, '').trim();
16
+ switch (key) {
17
+ case 'width':
18
+ return !value && Boolean(val) || /^(?:\d+x?|\d*x\d+)(?:\s*px)?$/u.test(value);
19
+ case 'link': {
20
+ if (!value) {
21
+ return val;
22
+ }
23
+ /* eslint-disable @typescript-eslint/no-unused-expressions */
24
+ /^(?:ftp:\/\/|\/\/|\0\d+m\x7F)/iu;
25
+ /^(?:(?:ftp:\/\/|\/\/)(?:\[[\da-f:.]+\]|[^[\]<>"\t\n\p{Zs}])|\0\d+m\x7F)[^[\]<>"\0\t\n\p{Zs}]*$/iu;
26
+ /* eslint-enable @typescript-eslint/no-unused-expressions */
27
+ const re1 = new RegExp(String.raw `^(?:${config.protocol}|//|\0\d+m\x7F)`, 'iu'), re2 = new RegExp(String.raw `^(?:(?:${config.protocol}|//)${string_1.extUrlCharFirst}|\0\d+m\x7F)${string_1.extUrlChar}$`, 'iu');
28
+ if (re1.test(value)) {
29
+ return re2.test(value) && val;
30
+ }
31
+ else if (value.startsWith('[[') && value.endsWith(']]')) {
32
+ value = value.slice(2, -2);
33
+ }
34
+ const title = index_1.default.normalizeTitle(value, 0, false, config, halfParsed, true, true);
35
+ return title.valid && title;
36
+ }
37
+ case 'lang':
38
+ return (ext === 'svg' || ext === 'svgz') && !/[^a-z\d-]/u.test(value);
39
+ case 'alt':
40
+ case 'class':
41
+ case 'manualthumb':
42
+ return true;
43
+ case 'page':
44
+ return (ext === 'djvu' || ext === 'djv' || ext === 'pdf') && Number(value) > 0;
45
+ default:
46
+ return Boolean(value) && !isNaN(value);
47
+ }
48
+ }
49
+ /** 图片参数 */
50
+ class ImageParameterToken extends index_2.Token {
51
+ #syntax = '';
52
+ #extension;
53
+ /* NOT FOR BROWSER END */
54
+ get type() {
55
+ return 'image-parameter';
56
+ }
57
+ /** 图片链接 */
58
+ get link() {
59
+ return this.name === 'link' ? validate('link', super.text(), this.getAttribute('config')) : undefined;
60
+ }
61
+ /* NOT FOR BROWSER */
62
+ set link(value) {
63
+ if (this.name === 'link') {
64
+ this.setValue(value);
65
+ }
66
+ }
67
+ /** getValue()的getter */
68
+ get value() {
69
+ return this.getValue();
70
+ }
71
+ set value(value) {
72
+ this.setValue(value);
73
+ }
74
+ /** 图片大小 */
75
+ get size() {
76
+ if (this.name === 'width') {
77
+ const size = this.getValue().trim().replace(/px$/u, '').trim();
78
+ if (!size.includes('{{')) {
79
+ const [width, height = ''] = size.split('x');
80
+ return { width, height };
81
+ }
82
+ const token = index_1.default.parse(size, false, 2, this.getAttribute('config')), i = token.childNodes.findIndex(({ type, data }) => type === 'text' && data.includes('x'));
83
+ if (i === -1) {
84
+ return { width: size, height: '' };
85
+ }
86
+ const str = token.childNodes[i];
87
+ str.splitText(str.data.indexOf('x')).splitText(1);
88
+ return { width: (0, string_1.text)(token.childNodes.slice(0, i + 1)), height: (0, string_1.text)(token.childNodes.slice(i + 2)) };
89
+ }
90
+ return undefined;
91
+ }
92
+ set size(size) {
93
+ if (this.name === 'width') {
94
+ this.setValue(size && size.width + (size.height && 'x') + size.height);
95
+ }
96
+ }
97
+ /** 图片宽度 */
98
+ get width() {
99
+ return this.size?.width;
100
+ }
101
+ set width(width) {
102
+ if (this.name === 'width') {
103
+ const { height } = this;
104
+ this.setValue((width || '') + (height && 'x') + height);
105
+ }
106
+ }
107
+ /** 图片高度 */
108
+ get height() {
109
+ return this.size?.height;
110
+ }
111
+ set height(height) {
112
+ if (this.name === 'width') {
113
+ this.setValue(`${this.width}${height ? `x${height}` : ''}`);
114
+ }
115
+ }
116
+ /* NOT FOR BROWSER END */
117
+ /** @param str 图片参数 */
118
+ constructor(str, extension, config = index_1.default.getConfig(), accum) {
119
+ let mt;
120
+ /* eslint-disable @typescript-eslint/no-unused-expressions */
121
+ /^(\s*)link=(.*)(?=$|\n)(\s*)$/u;
122
+ /^(\s*(?!\s))(.*)px(\s*)$/u;
123
+ /* eslint-enable @typescript-eslint/no-unused-expressions */
124
+ const regexes = Object.entries(config.img).map(([syntax, param]) => [
125
+ syntax,
126
+ param,
127
+ new RegExp(String.raw `^(\s*(?!\s))${syntax.replace('$1', '(.*)')}${syntax.endsWith('$1') ? '(?=$|\n)' : ''}(\s*)$`, 'u'),
128
+ ]), param = regexes.find(([, key, regex]) => {
129
+ mt = regex.exec(str);
130
+ return mt
131
+ && (mt.length !== 4
132
+ || validate(key, mt[2], config, true, extension) !== false);
133
+ });
134
+ // @ts-expect-error mt already assigned
135
+ if (param && mt) {
136
+ if (mt.length === 3) {
137
+ super(undefined, config, accum);
138
+ this.#syntax = str;
139
+ }
140
+ else {
141
+ super(mt[2], config, accum, {
142
+ 'Stage-2': ':', '!HeadingToken': ':',
143
+ });
144
+ this.#syntax = mt[1] + param[0] + mt[3];
145
+ }
146
+ this.setAttribute('name', param[1]);
147
+ return;
148
+ }
149
+ super(str, { ...config, excludes: [...config.excludes ?? [], 'list'] }, accum);
150
+ this.setAttribute('name', 'caption');
151
+ this.setAttribute('stage', 7);
152
+ /* NOT FOR BROWSER */
153
+ this.#extension = extension;
154
+ }
155
+ /** @private */
156
+ afterBuild() {
157
+ if (this.parentNode?.type === 'gallery-image' && !exports.galleryParams.has(this.name)) {
158
+ this.setAttribute('name', 'invalid');
159
+ }
160
+ super.afterBuild();
161
+ }
162
+ /** @private */
163
+ toString(skip) {
164
+ return this.#syntax ? this.#syntax.replace('$1', super.toString(skip)) : super.toString(skip);
165
+ }
166
+ /** @private */
167
+ text() {
168
+ return this.#syntax ? this.#syntax.replace('$1', super.text()).trim() : super.text().trim();
169
+ }
170
+ /** @private */
171
+ getAttribute(key) {
172
+ if (key === 'plain') {
173
+ return (this.name === 'caption');
174
+ }
175
+ return key === 'padding'
176
+ ? Math.max(0, this.#syntax.indexOf('$1'))
177
+ : super.getAttribute(key);
178
+ }
179
+ /** @private */
180
+ lint(start = this.getAbsoluteIndex(), re) {
181
+ const errors = super.lint(start, re), { link, name } = this;
182
+ if (name === 'invalid') {
183
+ const e = (0, lint_1.generateForSelf)(this, { start }, 'invalid-gallery', 'invalid image parameter');
184
+ e.fix = { range: [start - 1, e.endIndex], text: '' };
185
+ errors.push(e);
186
+ }
187
+ else if (typeof link === 'object' && link.encoded) {
188
+ errors.push((0, lint_1.generateForSelf)(this, { start }, 'url-encoding', 'unnecessary URL encoding in an internal link'));
189
+ }
190
+ return errors;
191
+ }
192
+ /** 是否是不可变参数 */
193
+ #isVoid() {
194
+ return this.#syntax && !this.#syntax.includes('$1');
195
+ }
196
+ /** 获取参数值 */
197
+ getValue() {
198
+ return this.name === 'invalid' ? this.text() : this.#isVoid() || super.text();
199
+ }
200
+ /** @private */
201
+ print() {
202
+ if (this.#syntax) {
203
+ return `<span class="wpb-image-parameter">${this.#syntax.replace('$1', `<span class="wpb-image-caption">${(0, string_1.print)(this.childNodes)}</span>`)}</span>`;
204
+ }
205
+ return super.print({ class: 'image-caption' });
206
+ }
207
+ /* NOT FOR BROWSER */
208
+ cloneNode() {
209
+ const cloned = this.cloneChildNodes(), config = this.getAttribute('config');
210
+ return debug_1.Shadow.run(() => {
211
+ // @ts-expect-error abstract class
212
+ const token = new ImageParameterToken(this.#syntax.replace('$1', ''), this.#extension, config);
213
+ token.replaceChildren(...cloned);
214
+ return token;
215
+ });
216
+ }
217
+ insertAt(token, i) {
218
+ if (!debug_1.Shadow.running && this.#isVoid()) {
219
+ throw new Error(`Image parameter ${this.name} does not accept custom input!`);
220
+ }
221
+ return super.insertAt(token, i);
222
+ }
223
+ /**
224
+ * 设置参数值
225
+ * @param value 参数值
226
+ * @throws `Error` 无效参数
227
+ */
228
+ setValue(value = false) {
229
+ const { name } = this;
230
+ if (value === false) {
231
+ this.remove();
232
+ return;
233
+ }
234
+ else if (name === 'invalid') {
235
+ throw new Error('Invalid image parameter!');
236
+ }
237
+ const type = this.#isVoid() ? 'Boolean' : 'String';
238
+ if (typeof value !== type.toLowerCase()) { // eslint-disable-line valid-typeof
239
+ this.typeError('setValue', type);
240
+ }
241
+ else if (value !== true) {
242
+ const include = this.getAttribute('include'), config = this.getAttribute('config'), { childNodes } = index_1.default.parse(value, include, name === 'caption' ? undefined : 5, config);
243
+ this.replaceChildren(...childNodes);
244
+ }
245
+ }
246
+ /** 获取网址 */
247
+ getUrl() {
248
+ let { link } = this;
249
+ if (!link) {
250
+ return link;
251
+ }
252
+ else if (typeof link !== 'string') {
253
+ return link.getUrl();
254
+ }
255
+ else if (link.startsWith('//')) {
256
+ link = `https:${link}`;
257
+ }
258
+ return new URL(link).href;
259
+ }
260
+ }
261
+ exports.ImageParameterToken = ImageParameterToken;
262
+ constants_1.classes['ImageParameterToken'] = __filename;