wikiparser-node 1.18.2 → 1.18.4

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 (76) hide show
  1. package/README.md +6 -1
  2. package/bundle/bundle-es7.min.js +29 -28
  3. package/bundle/bundle-lsp.min.js +31 -30
  4. package/bundle/bundle.min.js +30 -29
  5. package/config/minimum.json +7 -0
  6. package/dist/addon/token.js +37 -28
  7. package/dist/addon/transclude.js +1 -3
  8. package/dist/base.d.mts +4 -0
  9. package/dist/base.d.ts +4 -0
  10. package/dist/base.js +1 -0
  11. package/dist/base.mjs +2 -1
  12. package/dist/bin/config.js +3 -2
  13. package/dist/index.d.ts +14 -1
  14. package/dist/index.js +72 -72
  15. package/dist/lib/element.d.ts +6 -0
  16. package/dist/lib/element.js +543 -466
  17. package/dist/lib/lsp.d.ts +1 -0
  18. package/dist/lib/lsp.js +20 -15
  19. package/dist/lib/redirectMap.d.ts +7 -0
  20. package/dist/lib/redirectMap.js +31 -0
  21. package/dist/lib/text.d.ts +2 -2
  22. package/dist/lib/text.js +385 -325
  23. package/dist/lib/title.d.ts +23 -4
  24. package/dist/lib/title.js +17 -5
  25. package/dist/mixin/noEscape.d.ts +4 -0
  26. package/dist/mixin/noEscape.js +22 -0
  27. package/dist/mixin/readOnly.d.ts +4 -0
  28. package/dist/mixin/readOnly.js +26 -0
  29. package/dist/parser/braces.js +79 -37
  30. package/dist/parser/commentAndExt.js +5 -16
  31. package/dist/parser/links.js +1 -1
  32. package/dist/parser/quotes.js +1 -1
  33. package/dist/parser/redirect.js +1 -3
  34. package/dist/src/arg.js +253 -202
  35. package/dist/src/attribute.d.ts +0 -5
  36. package/dist/src/attribute.js +4 -8
  37. package/dist/src/converter.js +213 -162
  38. package/dist/src/gallery.js +1 -2
  39. package/dist/src/heading.js +5 -6
  40. package/dist/src/imageParameter.js +16 -16
  41. package/dist/src/imagemap.js +3 -2
  42. package/dist/src/index.d.ts +1 -1
  43. package/dist/src/index.js +722 -694
  44. package/dist/src/link/base.js +292 -241
  45. package/dist/src/link/file.js +13 -17
  46. package/dist/src/link/galleryImage.js +1 -1
  47. package/dist/src/link/redirectTarget.js +1 -2
  48. package/dist/src/magicLink.d.ts +0 -6
  49. package/dist/src/magicLink.js +5 -15
  50. package/dist/src/nested.js +7 -7
  51. package/dist/src/nowiki/base.js +2 -1
  52. package/dist/src/nowiki/index.js +4 -3
  53. package/dist/src/onlyinclude.js +95 -44
  54. package/dist/src/parameter.d.ts +0 -6
  55. package/dist/src/parameter.js +1 -13
  56. package/dist/src/redirect.js +6 -6
  57. package/dist/src/syntax.d.ts +4 -1
  58. package/dist/src/syntax.js +4 -1
  59. package/dist/src/table/base.d.ts +0 -5
  60. package/dist/src/table/base.js +2 -9
  61. package/dist/src/table/index.d.ts +1 -0
  62. package/dist/src/table/index.js +7 -4
  63. package/dist/src/table/td.d.ts +1 -0
  64. package/dist/src/table/td.js +2 -3
  65. package/dist/src/table/trBase.js +31 -14
  66. package/dist/src/tagPair/index.js +2 -1
  67. package/dist/src/transclude.js +713 -657
  68. package/dist/util/debug.js +10 -3
  69. package/dist/util/diff.js +1 -1
  70. package/dist/util/string.js +4 -5
  71. package/extensions/dist/base.js +8 -6
  72. package/extensions/dist/lint.js +1 -1
  73. package/extensions/es7/base.js +8 -6
  74. package/extensions/es7/lint.js +1 -1
  75. package/extensions/ui.css +1 -1
  76. package/package.json +3 -3
package/dist/lib/text.js CHANGED
@@ -1,4 +1,42 @@
1
1
  "use strict";
2
+ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
3
+ var useValue = arguments.length > 2;
4
+ for (var i = 0; i < initializers.length; i++) {
5
+ value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
6
+ }
7
+ return useValue ? value : void 0;
8
+ };
9
+ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
10
+ function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
11
+ var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
12
+ var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
13
+ var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
14
+ var _, done = false;
15
+ for (var i = decorators.length - 1; i >= 0; i--) {
16
+ var context = {};
17
+ for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
18
+ for (var p in contextIn.access) context.access[p] = contextIn.access[p];
19
+ context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
20
+ var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
21
+ if (kind === "accessor") {
22
+ if (result === void 0) continue;
23
+ if (result === null || typeof result !== "object") throw new TypeError("Object expected");
24
+ if (_ = accept(result.get)) descriptor.get = _;
25
+ if (_ = accept(result.set)) descriptor.set = _;
26
+ if (_ = accept(result.init)) initializers.unshift(_);
27
+ }
28
+ else if (_ = accept(result)) {
29
+ if (kind === "field") initializers.unshift(_);
30
+ else descriptor[key] = _;
31
+ }
32
+ }
33
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
34
+ done = true;
35
+ };
36
+ var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
37
+ if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
38
+ return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
39
+ };
2
40
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
41
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
42
  };
@@ -11,6 +49,7 @@ const node_1 = require("./node");
11
49
  /* NOT FOR BROWSER */
12
50
  const constants_1 = require("../util/constants");
13
51
  const debug_1 = require("../util/debug");
52
+ const readOnly_1 = require("../mixin/readOnly");
14
53
  /* NOT FOR BROWSER END */
15
54
  const sp = String.raw `[${string_1.zs}\t]*`, source = String.raw `<\s*(?:/\s*)?([a-z]\w*)|\{+|\}+|\[{2,}|\[(?![^[]*?\])|((?:^|\])[^[]*?)\]+|(?:rfc|pmid)(?=[-::]?${sp}\d)|isbn(?=[-::]?${sp}(?:\d(?:${sp}|-)){6})`;
16
55
  // eslint-disable-next-line @typescript-eslint/no-unused-expressions, es-x/no-regexp-unicode-property-escapes
@@ -98,365 +137,386 @@ catch /* istanbul ignore next */ {
98
137
  *
99
138
  * 文本节点
100
139
  */
101
- class AstText extends node_1.AstNode {
102
- data = '';
103
- get type() {
104
- return 'text';
105
- }
106
- /* NOT FOR BROWSER */
107
- /** text length / 文本长度 */
108
- get length() {
109
- return this.data.length;
110
- }
111
- set length(n) {
112
- if (n >= 0 && n < this.length) {
113
- this.replaceData(this.data.slice(0, n));
140
+ let AstText = (() => {
141
+ let _classSuper = node_1.AstNode;
142
+ let _instanceExtraInitializers = [];
143
+ let _private_setData_decorators;
144
+ let _private_setData_descriptor;
145
+ return class AstText extends _classSuper {
146
+ static {
147
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
148
+ _private_setData_decorators = [(0, readOnly_1.readOnly)()];
149
+ __esDecorate(this, _private_setData_descriptor = { value: __setFunctionName(function (text) {
150
+ /* NOT FOR BROWSER */
151
+ const { data } = this, e = new Event('text', { bubbles: true });
152
+ /* NOT FOR BROWSER END */
153
+ this.setAttribute('data', text);
154
+ /* NOT FOR BROWSER */
155
+ if (data !== text) {
156
+ this.dispatchEvent(e, { type: 'text', oldText: data });
157
+ }
158
+ }, "#setData") }, _private_setData_decorators, { kind: "method", name: "#setData", static: false, private: true, access: { has: obj => #setData in obj, get: obj => obj.#setData }, metadata: _metadata }, null, _instanceExtraInitializers);
159
+ if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
114
160
  }
115
- }
116
- /* NOT FOR BROWSER END */
117
- /** @param text 包含文本 */
118
- constructor(text) {
119
- super();
120
- Object.defineProperties(this, {
121
- childNodes: { enumerable: false, configurable: false },
122
- data: {
123
- value: text,
124
- /* NOT FOR BROWSER */
125
- writable: false,
126
- },
127
- });
128
- }
129
- /** @private */
130
- toString(skip) {
131
- return skip && !this.parentNode?.getAttribute('built') ? (0, string_1.removeComment)(this.data) : this.data;
132
- }
133
- /** @private */
134
- text() {
135
- return this.data;
136
- }
137
- /** @private */
138
- lint(start = this.getAbsoluteIndex(), errorRegex) {
139
- if (errorRegex === false) {
140
- return [];
161
+ data = (__runInitializers(this, _instanceExtraInitializers), '');
162
+ get type() {
163
+ return 'text';
141
164
  }
142
- const { data, parentNode, nextSibling, previousSibling } = this;
143
- /* istanbul ignore if */
144
- if (!parentNode) {
145
- throw new Error('An isolated text node cannot be linted!');
165
+ /* NOT FOR BROWSER */
166
+ /** text length / 文本长度 */
167
+ get length() {
168
+ return this.data.length;
146
169
  }
147
- const { type, name, parentNode: grandparent } = parentNode;
148
- let isHtmlAttrVal = false;
149
- if (type === 'attr-value') {
150
- const { type: grandType, name: grandName, tag } = grandparent;
151
- if (grandType !== 'ext-attr') {
152
- isHtmlAttrVal = true;
153
- }
154
- else if (tag === 'choose' && (grandName === 'before' || grandName === 'after')) {
155
- return [];
170
+ set length(n) {
171
+ if (n >= 0 && n < this.length) {
172
+ this.replaceData(this.data.slice(0, n));
156
173
  }
157
174
  }
158
- errorRegex ??= type === 'free-ext-link'
159
- || type === 'ext-link-url'
160
- || type === 'ext-link-text'
161
- || type === 'image-parameter' && name === 'link'
162
- || isHtmlAttrVal
163
- ? errorSyntaxUrl
164
- : errorSyntax;
165
- if (data.search(errorRegex) === -1) {
166
- return [];
175
+ /* NOT FOR BROWSER END */
176
+ /** @param text 包含文本 */
177
+ constructor(text) {
178
+ super();
179
+ Object.defineProperties(this, {
180
+ data: {
181
+ value: text,
182
+ /* NOT FOR BROWSER */
183
+ writable: false,
184
+ },
185
+ /* NOT FOR BROWSER */
186
+ childNodes: { enumerable: false, configurable: false },
187
+ });
188
+ }
189
+ /** @private */
190
+ toString(skip) {
191
+ return skip && !this.parentNode?.getAttribute('built') ? (0, string_1.removeComment)(this.data) : this.data;
192
+ }
193
+ /** @private */
194
+ text() {
195
+ return this.data;
167
196
  }
168
- errorRegex.lastIndex = 0;
169
- const errors = [], nextType = nextSibling?.type, nextName = nextSibling?.name, previousType = previousSibling?.type, root = this.getRootNode(), rootStr = root.toString(), { ext, html } = root.getAttribute('config'), { top, left } = root.posFromIndex(start), tags = new Set([
170
- 'onlyinclude',
171
- 'noinclude',
172
- 'includeonly',
173
- ...ext,
174
- ...html[0],
175
- ...html[1],
176
- ...html[2],
177
- ...disallowedTags,
178
- ]);
179
- for (let mt = errorRegex.exec(data); mt; mt = errorRegex.exec(data)) {
180
- const [, tag, prefix] = mt;
181
- let { index } = mt, error = mt[0].toLowerCase();
182
- if (prefix && prefix !== ']') {
183
- const { length } = prefix;
184
- index += length;
185
- error = error.slice(length);
197
+ /** @private */
198
+ lint(start = this.getAbsoluteIndex(), errorRegex) {
199
+ if (errorRegex === false) {
200
+ return [];
186
201
  }
187
- const { 0: char, length } = error, magicLink = char === 'r' || char === 'p' || char === 'i';
188
- if (char === '<' && !tags.has(tag.toLowerCase())
189
- || char === '[' && type === 'ext-link-text' && (/&(?:rbrack|#93|#x5[Dd];);/u.test(data.slice(index + 1))
190
- || nextSibling?.is('ext') && nextName === 'nowiki'
191
- && nextSibling.innerText?.includes(']'))
192
- || magicLink && (!parentNode.getAttribute('plain') || noLinkTypes.has(type))) {
193
- continue;
202
+ const { data, parentNode, nextSibling, previousSibling } = this;
203
+ /* istanbul ignore if */
204
+ if (!parentNode) {
205
+ throw new Error('An isolated text node cannot be linted!');
206
+ }
207
+ const { type, name, parentNode: grandparent } = parentNode;
208
+ let isHtmlAttrVal = false;
209
+ if (type === 'attr-value') {
210
+ const { type: grandType, name: grandName, tag } = grandparent;
211
+ if (grandType !== 'ext-attr') {
212
+ isHtmlAttrVal = true;
213
+ }
214
+ else if (tag === 'choose' && (grandName === 'before' || grandName === 'after')) {
215
+ return [];
216
+ }
194
217
  }
195
- else if (char === ']' && (index || length > 1)) {
196
- errorRegex.lastIndex--;
218
+ errorRegex ??= type === 'free-ext-link'
219
+ || type === 'ext-link-url'
220
+ || type === 'ext-link-text'
221
+ || type === 'image-parameter' && name === 'link'
222
+ || isHtmlAttrVal
223
+ ? errorSyntaxUrl
224
+ : errorSyntax;
225
+ if (data.search(errorRegex) === -1) {
226
+ return [];
197
227
  }
198
- const startIndex = start + index, endIndex = startIndex + length, nextChar = rootStr[endIndex], previousChar = rootStr[startIndex - 1], severity = length > 1 && !(char === '<' && !/[\s/>]/u.test(nextChar ?? '')
199
- || isHtmlAttrVal && (char === '[' || char === ']')
200
- || magicLink && type === 'parameter-value')
201
- || char === '{' && (nextChar === char || previousChar === '-')
202
- || char === '}' && (previousChar === char || nextChar === '-')
203
- || char === '[' && (nextChar === char
204
- || type === 'ext-link-text'
205
- || nextType === 'free-ext-link' && !data.slice(index + 1).trim())
206
- || char === ']' && (previousChar === char
207
- || previousType === 'free-ext-link' && !data.slice(0, index).includes(']'))
208
- ? 'error'
209
- : 'warning';
210
- const leftBracket = char === '{' || char === '[', rightBracket = char === ']' || char === '}';
211
- if (severity === 'warning' && (leftBracket || rightBracket)) {
212
- const regex = regexes[char], remains = leftBracket ? data.slice(index + 1) : data.slice(0, index);
213
- if (char === '{' && regex.exec(remains)?.[0] === '}'
214
- || char === '}' && regex.exec(remains)?.[0] === '{') {
228
+ errorRegex.lastIndex = 0;
229
+ const errors = [], nextType = nextSibling?.type, nextName = nextSibling?.name, previousType = previousSibling?.type, root = this.getRootNode(), rootStr = root.toString(), { ext, html } = root.getAttribute('config'), { top, left } = root.posFromIndex(start), tags = new Set([
230
+ 'onlyinclude',
231
+ 'noinclude',
232
+ 'includeonly',
233
+ ...ext,
234
+ ...html[0],
235
+ ...html[1],
236
+ ...html[2],
237
+ ...disallowedTags,
238
+ ]);
239
+ for (let mt = errorRegex.exec(data); mt; mt = errorRegex.exec(data)) {
240
+ const [, tag, prefix] = mt;
241
+ let { index, 0: error } = mt;
242
+ if (prefix && prefix !== ']') {
243
+ const { length } = prefix;
244
+ index += length;
245
+ error = error.slice(length);
246
+ }
247
+ error = error.toLowerCase();
248
+ const { 0: char, length } = error, magicLink = char === 'r' || char === 'p' || char === 'i';
249
+ if (char === '<' && !tags.has(tag.toLowerCase())
250
+ || char === '[' && type === 'ext-link-text' && (/&(?:rbrack|#93|#x5[Dd];);/u.test(data.slice(index + 1))
251
+ || nextSibling?.is('ext') && nextName === 'nowiki'
252
+ && nextSibling.innerText?.includes(']'))
253
+ || magicLink && (!parentNode.getAttribute('plain') || noLinkTypes.has(type))) {
215
254
  continue;
216
255
  }
217
- else if (!remains.includes(char)) {
218
- const sibling = leftBracket ? 'nextSibling' : 'previousSibling';
219
- let cur = this[sibling];
220
- while (cur && (cur.type !== 'text' || !regex.test(cur.data))) {
221
- cur = cur[sibling];
222
- }
223
- if (cur && regex.exec(cur.data)[0] !== char) {
256
+ else if (char === ']' && (index || length > 1)) {
257
+ errorRegex.lastIndex--;
258
+ }
259
+ const startIndex = start + index, endIndex = startIndex + length, nextChar = rootStr[endIndex], previousChar = rootStr[startIndex - 1], severity = length > 1 && !(char === '<' && !/[\s/>]/u.test(nextChar ?? '')
260
+ || isHtmlAttrVal && (char === '[' || char === ']')
261
+ || magicLink && type === 'parameter-value')
262
+ || char === '{' && (nextChar === char || previousChar === '-')
263
+ || char === '}' && (previousChar === char || nextChar === '-')
264
+ || char === '[' && (nextChar === char
265
+ || type === 'ext-link-text'
266
+ || nextType === 'free-ext-link' && !data.slice(index + 1).trim())
267
+ || char === ']' && (previousChar === char
268
+ || previousType === 'free-ext-link' && !data.slice(0, index).includes(']'))
269
+ ? 'error'
270
+ : 'warning';
271
+ const leftBracket = char === '{' || char === '[', rightBracket = char === ']' || char === '}';
272
+ if (severity === 'warning' && (leftBracket || rightBracket)) {
273
+ const regex = regexes[char], remains = leftBracket ? data.slice(index + 1) : data.slice(0, index);
274
+ if (char === '{' && regex.exec(remains)?.[0] === '}'
275
+ || char === '}' && regex.exec(remains)?.[0] === '{') {
224
276
  continue;
225
277
  }
278
+ else if (!remains.includes(char)) {
279
+ const sibling = leftBracket ? 'nextSibling' : 'previousSibling';
280
+ let cur = this[sibling];
281
+ while (cur && (cur.type !== 'text' || !regex.test(cur.data))) {
282
+ cur = cur[sibling];
283
+ }
284
+ if (cur && regex.exec(cur.data)[0] !== char) {
285
+ continue;
286
+ }
287
+ }
226
288
  }
289
+ if (magicLink) {
290
+ error = error.toUpperCase();
291
+ }
292
+ const pos = this.posFromIndex(index), { line: startLine, character: startCol } = (0, lint_1.getEndPos)(top, left, pos.top + 1, pos.left), e = {
293
+ rule: ruleMap[char],
294
+ message: index_1.default.msg('lonely "$1"', magicLink || char === 'h' ? error : char),
295
+ severity,
296
+ startIndex,
297
+ endIndex,
298
+ startLine,
299
+ endLine: startLine,
300
+ startCol,
301
+ endCol: startCol + length,
302
+ };
303
+ if (char === '<') {
304
+ e.suggestions = [{ desc: 'escape', range: [startIndex, startIndex + 1], text: '&lt;' }];
305
+ }
306
+ else if (char === 'h' && type !== 'link-text' && wordRegex.test(previousChar || '')) {
307
+ e.suggestions = [{ desc: 'whitespace', range: [startIndex, startIndex], text: ' ' }];
308
+ }
309
+ else if (char === '[' && type === 'ext-link-text') {
310
+ const i = parentNode.getAbsoluteIndex() + parentNode.toString().length;
311
+ e.suggestions = [{ desc: 'escape', range: [i, i + 1], text: '&#93;' }];
312
+ }
313
+ else if (char === ']' && previousType === 'free-ext-link' && severity === 'error') {
314
+ const i = start - previousSibling.toString().length;
315
+ e.fix = { range: [i, i], text: '[', desc: 'left bracket' };
316
+ }
317
+ else if (magicLink) {
318
+ e.suggestions = [
319
+ ...mt[0] === error
320
+ ? []
321
+ : [{ desc: 'uppercase', range: [startIndex, endIndex], text: error }],
322
+ ...nextChar === ':' || nextChar === ':'
323
+ ? [{ desc: 'whitespace', range: [endIndex, endIndex + 1], text: ' ' }]
324
+ : [],
325
+ ];
326
+ }
327
+ errors.push(e);
227
328
  }
228
- if (magicLink) {
229
- error = error.toUpperCase();
230
- }
231
- const pos = this.posFromIndex(index), { line: startLine, character: startCol } = (0, lint_1.getEndPos)(top, left, pos.top + 1, pos.left), e = {
232
- rule: ruleMap[char],
233
- message: index_1.default.msg('lonely "$1"', magicLink || char === 'h' ? error : char),
234
- severity,
235
- startIndex,
236
- endIndex,
237
- startLine,
238
- endLine: startLine,
239
- startCol,
240
- endCol: startCol + length,
241
- };
242
- if (char === '<') {
243
- e.suggestions = [{ desc: 'escape', range: [startIndex, startIndex + 1], text: '&lt;' }];
244
- }
245
- else if (char === 'h' && type !== 'link-text' && wordRegex.test(previousChar || '')) {
246
- e.suggestions = [{ desc: 'whitespace', range: [startIndex, startIndex], text: ' ' }];
247
- }
248
- else if (char === '[' && type === 'ext-link-text') {
249
- const i = parentNode.getAbsoluteIndex() + parentNode.toString().length;
250
- e.suggestions = [{ desc: 'escape', range: [i, i + 1], text: '&#93;' }];
251
- }
252
- else if (char === ']' && previousType === 'free-ext-link' && severity === 'error') {
253
- const i = start - previousSibling.toString().length;
254
- e.fix = { range: [i, i], text: '[', desc: 'left bracket' };
255
- }
256
- else if (magicLink) {
257
- e.suggestions = [
258
- ...mt[0] === error
259
- ? []
260
- : [{ desc: 'uppercase', range: [startIndex, endIndex], text: error }],
261
- ...nextChar === ':' || nextChar === ':'
262
- ? [{ desc: 'whitespace', range: [endIndex, endIndex + 1], text: ' ' }]
263
- : [],
264
- ];
265
- }
266
- errors.push(e);
329
+ return errors;
330
+ }
331
+ /**
332
+ * 修改内容
333
+ * @param text 新内容
334
+ */
335
+ get #setData() { return _private_setData_descriptor.value; }
336
+ /**
337
+ * Replace the text
338
+ *
339
+ * 替换字符串
340
+ * @param text new text / 替换的字符串
341
+ */
342
+ replaceData(text) {
343
+ this.#setData(text);
344
+ }
345
+ /** @private */
346
+ print() {
347
+ return (0, string_1.escape)(this.data);
267
348
  }
268
- return errors;
269
- }
270
- /**
271
- * 修改内容
272
- * @param text 新内容
273
- */
274
- #setData(text) {
275
- /* NOT FOR BROWSER */
276
- const { data } = this, e = new Event('text', { bubbles: true });
277
- /* NOT FOR BROWSER END */
278
- this.setAttribute('data', text);
279
349
  /* NOT FOR BROWSER */
280
- if (data !== text) {
281
- this.dispatchEvent(e, { type: 'text', oldText: data });
350
+ /**
351
+ * Clone the node
352
+ *
353
+ * 复制
354
+ */
355
+ cloneNode() {
356
+ return new AstText(this.data);
282
357
  }
283
- }
284
- /**
285
- * Replace the text
286
- *
287
- * 替换字符串
288
- * @param text new text / 替换的字符串
289
- */
290
- replaceData(text) {
291
- this.#setData(text);
292
- }
293
- /** @private */
294
- print() {
295
- return (0, string_1.escape)(this.data);
296
- }
297
- /* NOT FOR BROWSER */
298
- /**
299
- * Clone the node
300
- *
301
- * 复制
302
- */
303
- cloneNode() {
304
- return new AstText(this.data);
305
- }
306
- /**
307
- * Insert text at the end
308
- *
309
- * 在后方添加字符串
310
- * @param text text to be inserted / 添加的字符串
311
- */
312
- appendData(text) {
313
- this.#setData(this.data + text);
314
- }
315
- /**
316
- * Delete text
317
- *
318
- * 删减字符串
319
- * @param offset start position / 起始位置
320
- * @param count number of characters to be deleted / 删减字符数
321
- */
322
- deleteData(offset, count = Infinity) {
323
- this.#setData(this.data.slice(0, offset)
324
- + (offset < 0 && offset + count >= 0 ? '' : this.data.slice(offset + count)));
325
- }
326
- /**
327
- * Insert text
328
- *
329
- * 插入字符串
330
- * @param offset position to be inserted at / 插入位置
331
- * @param text text to be inserted / 待插入的字符串
332
- */
333
- insertData(offset, text) {
334
- this.#setData(this.data.slice(0, offset) + text + this.data.slice(offset));
335
- }
336
- /**
337
- * Get the substring
338
- *
339
- * 提取子串
340
- * @param offset start position / 起始位置
341
- * @param count number of characters / 字符数
342
- */
343
- substringData(offset, count) {
344
- return this.data.substr(offset, count);
345
- }
346
- /**
347
- * Split the text node into two parts
348
- *
349
- * 将文本子节点分裂为两部分
350
- * @param offset position to be splitted at / 分裂位置
351
- * @throws `RangeError` 错误的断开位置
352
- * @throws `Error` 没有父节点
353
- */
354
- splitText(offset) {
355
- /* istanbul ignore if */
356
- if (offset > this.length || offset < -this.length) {
357
- throw new RangeError(`Wrong offset to split: ${offset}`);
358
+ /**
359
+ * Insert text at the end
360
+ *
361
+ * 在后方添加字符串
362
+ * @param text text to be inserted / 添加的字符串
363
+ */
364
+ appendData(text) {
365
+ this.#setData(this.data + text);
358
366
  }
359
- const { parentNode, data } = this;
360
- /* istanbul ignore if */
361
- if (!parentNode) {
362
- throw new Error('The text node to be split has no parent node!');
367
+ /**
368
+ * Delete text
369
+ *
370
+ * 删减字符串
371
+ * @param offset start position / 起始位置
372
+ * @param count number of characters to be deleted / 删减字符数
373
+ */
374
+ deleteData(offset, count = Infinity) {
375
+ this.#setData(this.data.slice(0, offset)
376
+ + (offset < 0 && offset + count >= 0 ? '' : this.data.slice(offset + count)));
363
377
  }
364
- const newText = new AstText(data.slice(offset));
365
- (0, debug_1.setChildNodes)(parentNode, parentNode.childNodes.indexOf(this) + 1, 0, [newText]);
366
- this.setAttribute('data', data.slice(0, offset));
367
- return newText;
368
- }
369
- /** @private */
370
- getRelativeIndex(j) {
371
- /* istanbul ignore else */
372
- if (j === undefined) {
373
- return super.getRelativeIndex();
378
+ /**
379
+ * Insert text
380
+ *
381
+ * 插入字符串
382
+ * @param offset position to be inserted at / 插入位置
383
+ * @param text text to be inserted / 待插入的字符串
384
+ */
385
+ insertData(offset, text) {
386
+ this.#setData(this.data.slice(0, offset) + text + this.data.slice(offset));
374
387
  }
375
- else if (j < 0 || j > this.length) {
376
- throw new RangeError('Exceeding the text length range!');
388
+ /**
389
+ * Get the substring
390
+ *
391
+ * 提取子串
392
+ * @param offset start position / 起始位置
393
+ * @param count number of characters / 字符数
394
+ */
395
+ substringData(offset, count) {
396
+ return this.data.substr(offset, count);
377
397
  }
378
- return j;
379
- }
380
- /**
381
- * Escape `=`
382
- *
383
- * 转义 `=`
384
- */
385
- escape() {
386
- const { TranscludeToken } = require('../src/transclude');
387
- let i = this.data.lastIndexOf('=');
388
- for (; i >= 0; i = this.data.lastIndexOf('=', i - 1)) {
389
- if (i < this.length - 1) {
390
- this.splitText(i + 1);
398
+ /**
399
+ * Split the text node into two parts
400
+ *
401
+ * 将文本子节点分裂为两部分
402
+ * @param offset position to be splitted at / 分裂位置
403
+ * @throws `RangeError` 错误的断开位置
404
+ * @throws `Error` 没有父节点
405
+ */
406
+ splitText(offset) {
407
+ /* istanbul ignore if */
408
+ if (offset > this.length || offset < -this.length) {
409
+ throw new RangeError(`Wrong offset to split: ${offset}`);
410
+ }
411
+ const { parentNode, data } = this;
412
+ /* istanbul ignore if */
413
+ if (!parentNode) {
414
+ throw new Error('The text node to be split has no parent node!');
415
+ }
416
+ const newText = new AstText(data.slice(offset));
417
+ (0, debug_1.setChildNodes)(parentNode, parentNode.childNodes.indexOf(this) + 1, 0, [newText]);
418
+ this.setAttribute('data', data.slice(0, offset));
419
+ return newText;
420
+ }
421
+ /** @private */
422
+ getRelativeIndex(j) {
423
+ /* istanbul ignore else */
424
+ if (j === undefined) {
425
+ return super.getRelativeIndex();
426
+ }
427
+ else if (j < 0 || j > this.length) {
428
+ throw new RangeError('Exceeding the text length range!');
391
429
  }
392
- this.after(debug_1.Shadow.run(
430
+ return j;
431
+ }
432
+ /**
433
+ * Escape `=` and `|`
434
+ *
435
+ * 转义 `=` 和 `|`
436
+ */
437
+ escape() {
438
+ const { TranscludeToken } = require('../src/transclude');
439
+ const config = this.parentNode.getAttribute('config');
440
+ /**
441
+ * Get the last index of `=` or `|`
442
+ * @param j start position from the end
443
+ */
444
+ const lastIndexOf = (j) => Math.max(this.data.lastIndexOf('=', j), this.data.lastIndexOf('|', j));
445
+ let i = lastIndexOf();
446
+ const callback = /** @ignore */ () =>
393
447
  // @ts-expect-error abstract class
394
- () => new TranscludeToken('=', [], this.parentNode.getAttribute('config'))));
395
- this.#setData(this.data.slice(0, i));
448
+ new TranscludeToken(this.data[i] === '=' ? '=' : '!', [], config);
449
+ for (; i >= 0; i = lastIndexOf(i - 1)) {
450
+ if (i < this.length - 1) {
451
+ this.splitText(i + 1);
452
+ }
453
+ this.after(debug_1.Shadow.run(callback));
454
+ this.#setData(this.data.slice(0, i));
455
+ }
396
456
  }
397
- }
398
- /**
399
- * Generate HTML
400
- *
401
- * 生成HTML
402
- * @param nowrap whether to disable line-wrapping / 是否不换行
403
- */
404
- toHtml(nowrap) {
405
- const { data } = this;
406
- return (0, string_1.sanitize)(nowrap ? data.replaceAll('\n', ' ') : data);
407
- }
408
- /** @private */
409
- removeBlankLines() {
410
- if (/\s$/u.test(this.data)) {
411
- const spaces = [], mt = /\n[^\S\n]*$/u.exec(this.data);
412
- let { nextSibling } = this, mt2 = null;
413
- while (nextSibling
414
- && (nextSibling.type === 'comment' || nextSibling.type === 'category' || nextSibling.type === 'text')) {
415
- if (nextSibling.type === 'text') {
416
- mt2 = mt && /^[^\S\n]*(?=\n)/u.exec(nextSibling.data);
417
- if (mt2 || nextSibling.data.trim()) {
418
- break;
457
+ /**
458
+ * Generate HTML
459
+ *
460
+ * 生成HTML
461
+ * @param nowrap whether to disable line-wrapping / 是否不换行
462
+ */
463
+ toHtml(nowrap) {
464
+ const { data } = this;
465
+ return (0, string_1.sanitize)(nowrap ? data.replaceAll('\n', ' ') : data);
466
+ }
467
+ /** @private */
468
+ removeBlankLines() {
469
+ if (/\s$/u.test(this.data)) {
470
+ const spaces = [], mt = /\n[^\S\n]*$/u.exec(this.data);
471
+ let { nextSibling } = this, mt2 = null;
472
+ while (nextSibling
473
+ && (nextSibling.type === 'comment' || nextSibling.type === 'category' || nextSibling.type === 'text')) {
474
+ if (nextSibling.type === 'text') {
475
+ mt2 = mt && /^[^\S\n]*(?=\n)/u.exec(nextSibling.data);
476
+ if (mt2 || nextSibling.data.trim()) {
477
+ break;
478
+ }
479
+ else {
480
+ spaces.push(nextSibling);
481
+ }
419
482
  }
420
- else {
421
- spaces.push(nextSibling);
483
+ else if (mt && nextSibling.type === 'category') {
484
+ // eslint-disable-next-line es-x/no-string-prototype-trimstart-trimend
485
+ const trimmed = this.data.trimEnd();
486
+ if (this.data !== trimmed) {
487
+ const { length } = trimmed;
488
+ this.deleteData(length + this.data.slice(length).indexOf('\n'));
489
+ }
490
+ for (const space of spaces) {
491
+ space.#setData('');
492
+ }
493
+ spaces.length = 0;
422
494
  }
495
+ ({ nextSibling } = nextSibling);
423
496
  }
424
- else if (mt && nextSibling.type === 'category') {
425
- // eslint-disable-next-line es-x/no-string-prototype-trimstart-trimend
426
- const trimmed = this.data.trimEnd();
427
- if (this.data !== trimmed) {
428
- const { length } = trimmed;
429
- this.deleteData(length + this.data.slice(length).indexOf('\n'));
497
+ if (mt2 || nextSibling?.type === 'table') {
498
+ if (mt) {
499
+ this.deleteData(mt.index + (mt2 ? 0 : 1));
500
+ if (mt2) {
501
+ nextSibling.deleteData(0, mt2[0].length);
502
+ }
503
+ }
504
+ else {
505
+ // eslint-disable-next-line es-x/no-string-prototype-trimstart-trimend
506
+ this.#setData(this.data.trimEnd());
430
507
  }
431
508
  for (const space of spaces) {
432
509
  space.#setData('');
433
510
  }
434
- spaces.length = 0;
435
- }
436
- ({ nextSibling } = nextSibling);
437
- }
438
- if (mt2 || nextSibling?.type === 'table') {
439
- if (mt) {
440
- this.deleteData(mt.index + (mt2 ? 0 : 1));
441
- if (mt2) {
442
- nextSibling.deleteData(0, mt2[0].length);
443
- }
444
- }
445
- else {
446
- // eslint-disable-next-line es-x/no-string-prototype-trimstart-trimend
447
- this.#setData(this.data.trimEnd());
448
- }
449
- for (const space of spaces) {
450
- space.#setData('');
451
511
  }
452
512
  }
453
513
  }
454
- }
455
- /** @private */
456
- toHtmlInternal(opt) {
457
- this.removeBlankLines();
458
- return this.toHtml(opt?.nowrap);
459
- }
460
- }
514
+ /** @private */
515
+ toHtmlInternal(opt) {
516
+ this.removeBlankLines();
517
+ return this.toHtml(opt?.nowrap);
518
+ }
519
+ };
520
+ })();
461
521
  exports.AstText = AstText;
462
522
  constants_1.classes['AstText'] = __filename;