wikiparser-node 1.0.0-beta.0 → 1.0.0-beta.2

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 (133) hide show
  1. package/config/.schema.json +26 -0
  2. package/dist/index.d.ts +11 -9
  3. package/dist/index.js +22 -24
  4. package/dist/internal.d.ts +44 -0
  5. package/dist/lib/element.d.ts +42 -32
  6. package/dist/lib/element.js +59 -56
  7. package/dist/lib/node.d.ts +29 -36
  8. package/dist/lib/node.js +33 -48
  9. package/dist/lib/range.d.ts +132 -0
  10. package/dist/lib/range.js +387 -0
  11. package/dist/lib/ranges.d.ts +2 -12
  12. package/dist/lib/ranges.js +6 -11
  13. package/dist/lib/text.d.ts +11 -5
  14. package/dist/lib/text.js +25 -13
  15. package/dist/lib/title.d.ts +4 -5
  16. package/dist/lib/title.js +17 -7
  17. package/dist/mixin/attributesParent.js +6 -3
  18. package/dist/mixin/fixed.js +8 -5
  19. package/dist/mixin/hidden.js +7 -3
  20. package/dist/mixin/singleLine.js +6 -3
  21. package/dist/mixin/sol.js +6 -3
  22. package/dist/parser/{brackets.js → braces.js} +34 -26
  23. package/dist/parser/commentAndExt.js +25 -15
  24. package/dist/parser/converter.js +16 -9
  25. package/dist/parser/externalLinks.js +15 -9
  26. package/dist/parser/hrAndDoubleUnderscore.js +17 -10
  27. package/dist/parser/html.js +18 -9
  28. package/dist/parser/links.js +22 -14
  29. package/dist/parser/list.js +20 -10
  30. package/dist/parser/magicLinks.js +14 -7
  31. package/dist/parser/quotes.js +21 -11
  32. package/dist/parser/selector.js +19 -12
  33. package/dist/parser/table.js +25 -16
  34. package/dist/src/arg.d.ts +7 -8
  35. package/dist/src/arg.js +27 -24
  36. package/dist/src/atom.d.ts +4 -5
  37. package/dist/src/atom.js +9 -7
  38. package/dist/src/attribute.d.ts +13 -13
  39. package/dist/src/attribute.js +28 -25
  40. package/dist/src/attributes.d.ts +11 -10
  41. package/dist/src/attributes.js +40 -45
  42. package/dist/src/converter.d.ts +5 -7
  43. package/dist/src/converter.js +22 -16
  44. package/dist/src/converterFlags.d.ts +11 -12
  45. package/dist/src/converterFlags.js +17 -14
  46. package/dist/src/converterRule.d.ts +9 -10
  47. package/dist/src/converterRule.js +27 -27
  48. package/dist/src/extLink.d.ts +4 -6
  49. package/dist/src/extLink.js +25 -22
  50. package/dist/src/gallery.d.ts +9 -13
  51. package/dist/src/gallery.js +28 -23
  52. package/dist/src/heading.d.ts +11 -11
  53. package/dist/src/heading.js +19 -19
  54. package/dist/src/hidden.d.ts +4 -5
  55. package/dist/src/hidden.js +10 -8
  56. package/dist/src/html.d.ts +15 -12
  57. package/dist/src/html.js +18 -15
  58. package/dist/src/imageParameter.d.ts +11 -13
  59. package/dist/src/imageParameter.js +21 -16
  60. package/dist/src/imagemap.d.ts +11 -15
  61. package/dist/src/imagemap.js +26 -21
  62. package/dist/src/imagemapLink.d.ts +12 -17
  63. package/dist/src/imagemapLink.js +19 -14
  64. package/dist/src/index.d.ts +24 -20
  65. package/dist/src/index.js +65 -75
  66. package/dist/src/link/base.d.ts +8 -10
  67. package/dist/src/link/base.js +28 -22
  68. package/dist/src/link/category.d.ts +2 -3
  69. package/dist/src/link/category.js +7 -5
  70. package/dist/src/link/file.d.ts +9 -11
  71. package/dist/src/link/file.js +53 -26
  72. package/dist/src/link/galleryImage.d.ts +8 -8
  73. package/dist/src/link/galleryImage.js +18 -16
  74. package/dist/src/link/index.d.ts +6 -6
  75. package/dist/src/link/index.js +14 -16
  76. package/dist/src/magicLink.d.ts +9 -6
  77. package/dist/src/magicLink.js +23 -18
  78. package/dist/src/nested.d.ts +10 -10
  79. package/dist/src/nested.js +25 -17
  80. package/dist/src/nowiki/base.d.ts +7 -8
  81. package/dist/src/nowiki/base.js +10 -8
  82. package/dist/src/nowiki/comment.d.ts +7 -7
  83. package/dist/src/nowiki/comment.js +12 -9
  84. package/dist/src/nowiki/dd.d.ts +2 -3
  85. package/dist/src/nowiki/dd.js +7 -11
  86. package/dist/src/nowiki/doubleUnderscore.d.ts +5 -7
  87. package/dist/src/nowiki/doubleUnderscore.js +11 -8
  88. package/dist/src/nowiki/hr.d.ts +6 -7
  89. package/dist/src/nowiki/hr.js +11 -8
  90. package/dist/src/nowiki/index.d.ts +5 -7
  91. package/dist/src/nowiki/index.js +8 -6
  92. package/dist/src/nowiki/list.d.ts +4 -4
  93. package/dist/src/nowiki/list.js +8 -6
  94. package/dist/src/nowiki/noinclude.d.ts +3 -3
  95. package/dist/src/nowiki/noinclude.js +8 -6
  96. package/dist/src/nowiki/quote.d.ts +6 -7
  97. package/dist/src/nowiki/quote.js +11 -8
  98. package/dist/src/onlyinclude.d.ts +3 -5
  99. package/dist/src/onlyinclude.js +9 -7
  100. package/dist/src/paramTag/index.d.ts +8 -10
  101. package/dist/src/paramTag/index.js +15 -13
  102. package/dist/src/paramTag/inputbox.d.ts +4 -6
  103. package/dist/src/paramTag/inputbox.js +10 -8
  104. package/dist/src/parameter.d.ts +11 -12
  105. package/dist/src/parameter.js +25 -22
  106. package/dist/src/pre.d.ts +5 -10
  107. package/dist/src/pre.js +16 -11
  108. package/dist/src/syntax.d.ts +6 -7
  109. package/dist/src/syntax.js +12 -10
  110. package/dist/src/table/base.d.ts +14 -9
  111. package/dist/src/table/base.js +18 -15
  112. package/dist/src/table/index.d.ts +24 -17
  113. package/dist/src/table/index.js +39 -67
  114. package/dist/src/table/td.d.ts +17 -21
  115. package/dist/src/table/td.js +20 -28
  116. package/dist/src/table/tr.d.ts +6 -11
  117. package/dist/src/table/tr.js +8 -6
  118. package/dist/src/table/trBase.d.ts +12 -22
  119. package/dist/src/table/trBase.js +23 -19
  120. package/dist/src/tagPair/ext.d.ts +10 -10
  121. package/dist/src/tagPair/ext.js +47 -24
  122. package/dist/src/tagPair/include.d.ts +6 -7
  123. package/dist/src/tagPair/include.js +11 -8
  124. package/dist/src/tagPair/index.d.ts +12 -11
  125. package/dist/src/tagPair/index.js +13 -11
  126. package/dist/src/transclude.d.ts +8 -10
  127. package/dist/src/transclude.js +70 -77
  128. package/dist/util/debug.js +2 -12
  129. package/dist/util/diff.js +4 -2
  130. package/dist/util/lint.js +5 -5
  131. package/dist/util/string.js +2 -31
  132. package/package.json +13 -13
  133. package/dist/util/base.js +0 -26
@@ -1,10 +1,12 @@
1
1
  "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AttributeToken = void 0;
2
4
  const lint_1 = require("../util/lint");
3
5
  const string_1 = require("../util/string");
4
- const fixed = require("../mixin/fixed");
5
- const Parser = require("../index");
6
- const Token = require(".");
7
- const AtomToken = require("./atom");
6
+ const fixed_1 = require("../mixin/fixed");
7
+ const index_1 = require("../index");
8
+ const _1 = require(".");
9
+ const atom_1 = require("./atom");
8
10
  const stages = { 'ext-attr': 0, 'html-attr': 2, 'table-attr': 3 }, pre = { 'ext-attr': '<pre ', 'html-attr': '<p ', 'table-attr': '{|' }, post = { 'ext-attr': '/>', 'html-attr': '>', 'table-attr': '' }, commonHtmlAttrs = new Set([
9
11
  'id',
10
12
  'class',
@@ -146,7 +148,7 @@ const stages = { 'ext-attr': 0, 'html-attr': 2, 'table-attr': 3 }, pre = { 'ext-
146
148
  * 扩展和HTML标签属性
147
149
  * @classdesc `{childNodes: [AtomToken, Token|AtomToken]}`
148
150
  */
149
- class AttributeToken extends fixed(Token) {
151
+ class AttributeToken extends (0, fixed_1.fixed)(_1.Token) {
150
152
  /** @browser */
151
153
  #equal;
152
154
  /** @browser */
@@ -186,21 +188,21 @@ class AttributeToken extends fixed(Token) {
186
188
  * @param value 属性值
187
189
  * @param quotes 引号
188
190
  */
189
- constructor(type, tag, key, equal = '', value = '', quotes = [], config = Parser.getConfig(), accum = []) {
190
- const keyToken = new AtomToken(key, 'attr-key', config, accum, {
191
+ constructor(type, tag, key, equal = '', value = '', quotes = [], config = index_1.default.getConfig(), accum = []) {
192
+ const keyToken = new atom_1.AtomToken(key, 'attr-key', config, accum, {
191
193
  [type === 'ext-attr' ? 'AstText' : 'Stage-1']: ':', ArgToken: ':', TranscludeToken: ':',
192
194
  });
193
195
  let valueToken;
194
196
  if (key === 'title') {
195
- valueToken = new Token(value, config, true, accum, {
197
+ valueToken = new _1.Token(value, config, true, accum, {
196
198
  [`Stage-${stages[type]}`]: ':', ConverterToken: ':',
197
199
  });
198
200
  valueToken.type = 'attr-value';
199
- valueToken.setAttribute('stage', Parser.MAX_STAGE - 1);
201
+ valueToken.setAttribute('stage', index_1.default.MAX_STAGE - 1);
200
202
  }
201
203
  else if (tag === 'gallery' && key === 'caption') {
202
204
  const newConfig = { ...config, excludes: [...config.excludes, 'quote', 'extLink', 'magicLink', 'list'] };
203
- valueToken = new Token(value, newConfig, true, accum, {
205
+ valueToken = new _1.Token(value, newConfig, true, accum, {
204
206
  AstText: ':', LinkToken: ':', FileToken: ':', CategoryToken: ':', ConverterToken: ':',
205
207
  });
206
208
  valueToken.type = 'attr-value';
@@ -208,7 +210,7 @@ class AttributeToken extends fixed(Token) {
208
210
  }
209
211
  else if (tag === 'choose' && (key === 'before' || key === 'after')) {
210
212
  const newConfig = { ...config, excludes: [...config.excludes, 'heading', 'html', 'table', 'hr', 'list'] };
211
- valueToken = new Token(value, newConfig, true, accum, {
213
+ valueToken = new _1.Token(value, newConfig, true, accum, {
212
214
  ArgToken: ':',
213
215
  TranscludeToken: ':',
214
216
  LinkToken: ':',
@@ -223,7 +225,7 @@ class AttributeToken extends fixed(Token) {
223
225
  valueToken.setAttribute('stage', 1);
224
226
  }
225
227
  else {
226
- valueToken = new AtomToken(value, 'attr-value', config, accum, {
228
+ valueToken = new atom_1.AtomToken(value, 'attr-value', config, accum, {
227
229
  [`Stage-${stages[type]}`]: ':',
228
230
  });
229
231
  }
@@ -266,8 +268,8 @@ class AttributeToken extends fixed(Token) {
266
268
  return this.#equal ? `${super.text(`${this.#equal.trim()}"`)}"` : this.firstChild.text();
267
269
  }
268
270
  /** @private */
269
- getGaps() {
270
- return this.#equal ? this.#equal.length + (this.#quotes[0]?.length ?? 0) : 0;
271
+ getGaps(i) {
272
+ return this.#equal && i === 0 ? this.#equal.length + (this.#quotes[0]?.length ?? 0) : 0;
271
273
  }
272
274
  /**
273
275
  * @override
@@ -335,7 +337,8 @@ class AttributeToken extends fixed(Token) {
335
337
  /** @override */
336
338
  cloneNode() {
337
339
  const [key, value] = this.cloneChildNodes(), config = this.getAttribute('config');
338
- return Parser.run(() => {
340
+ return index_1.default.run(() => {
341
+ // @ts-expect-error abstract class
339
342
  const token = new AttributeToken(this.type, this.#tag, '', this.#equal, '', this.#quotes, config);
340
343
  token.firstChild.safeReplaceWith(key);
341
344
  token.lastChild.safeReplaceWith(value);
@@ -368,7 +371,7 @@ class AttributeToken extends fixed(Token) {
368
371
  this.#equal = '';
369
372
  return;
370
373
  }
371
- const { type } = this, key = this.name === 'title' ? 'title' : 'data', wikitext = `${pre[type]}${key}="${value}"${post[type]}`, root = Parser.parse(wikitext, this.getAttribute('include'), stages[type] + 1, this.getAttribute('config')), { length, firstChild: tag } = root;
374
+ const { type } = this, key = this.name === 'title' ? 'title' : 'data', wikitext = `${pre[type]}${key}="${value}"${post[type]}`, root = index_1.default.parse(wikitext, this.getAttribute('include'), stages[type] + 1, this.getAttribute('config')), { length, firstChild: tag } = root;
372
375
  let attrs;
373
376
  if (length !== 1 || tag.type !== type.slice(0, -5)) {
374
377
  throw new SyntaxError(`非法的标签属性:${(0, string_1.noWrap)(value)}`);
@@ -400,13 +403,13 @@ class AttributeToken extends fixed(Token) {
400
403
  * 修改属性名
401
404
  * @param key 新属性名
402
405
  * @throws `Error` title属性不能更名
403
- * @throws `SyntaxError` 非法的模板参数名
406
+ * @throws `SyntaxError` 非法的标签属性名
404
407
  */
405
408
  rename(key) {
406
409
  if (this.name === 'title') {
407
410
  throw new Error('title 属性不能更名!');
408
411
  }
409
- const { type } = this, wikitext = `${pre[type]}${key}${post[type]}`, root = Parser.parse(wikitext, this.getAttribute('include'), stages[type] + 1, this.getAttribute('config')), { length, firstChild: tag } = root;
412
+ const { type } = this, wikitext = `${pre[type]}${key}${post[type]}`, root = index_1.default.parse(wikitext, this.getAttribute('include'), stages[type] + 1, this.getAttribute('config')), { length, firstChild: tag } = root;
410
413
  let attrs;
411
414
  if (length !== 1 || tag.type !== type.slice(0, -5)) {
412
415
  throw new SyntaxError(`非法的标签属性名:${(0, string_1.noWrap)(key)}`);
@@ -421,13 +424,13 @@ class AttributeToken extends fixed(Token) {
421
424
  attrs = tag.firstChild;
422
425
  }
423
426
  const { firstChild: attr } = attrs;
424
- if (attrs.length !== 1 || attr.type !== this.type || attr.value !== true) {
425
- throw new SyntaxError(`非法的标签属性名:${(0, string_1.noWrap)(key)}`);
427
+ if (attrs.length === 1 && attr.type === this.type && attr.value === true) {
428
+ const { firstChild } = attr;
429
+ attr.destroy();
430
+ this.firstChild.safeReplaceWith(firstChild);
426
431
  }
427
- const { firstChild } = attr;
428
- attr.destroy();
429
- this.firstChild.safeReplaceWith(firstChild);
432
+ throw new SyntaxError(`非法的标签属性名:${(0, string_1.noWrap)(key)}`);
430
433
  }
431
434
  }
432
- Parser.classes['AttributeToken'] = __filename;
433
- module.exports = AttributeToken;
435
+ exports.AttributeToken = AttributeToken;
436
+ index_1.default.classes['AttributeToken'] = __filename;
@@ -1,13 +1,14 @@
1
- import * as Parser from '../index';
2
- import Token = require('.');
3
- import AtomToken = require('./atom');
4
- import AttributeToken = require('./attribute');
1
+ import { Token } from '.';
2
+ import { AtomToken } from './atom';
3
+ import { AttributeToken } from './attribute';
4
+ import type { LintError } from '../index';
5
+ import type { ExtToken, HtmlToken, TdToken, TrToken, TableToken } from '../internal';
5
6
  declare type AttributesTypes = 'ext-attrs' | 'html-attrs' | 'table-attrs';
6
7
  /**
7
8
  * 扩展和HTML标签属性
8
9
  * @classdesc `{childNodes: ...AtomToken|AttributeToken}`
9
10
  */
10
- declare abstract class AttributesToken extends Token {
11
+ export declare abstract class AttributesToken extends Token {
11
12
  #private;
12
13
  type: AttributesTypes;
13
14
  name: string;
@@ -17,8 +18,8 @@ declare abstract class AttributesToken extends Token {
17
18
  abstract get firstElementChild(): AtomToken | AttributeToken;
18
19
  abstract get lastChild(): AtomToken | AttributeToken;
19
20
  abstract get lastElementChild(): AtomToken | AttributeToken;
20
- abstract get parentNode(): import('./tagPair/ext') | import('./html') | import('./table/base') | undefined;
21
- abstract get parentElement(): import('./tagPair/ext') | import('./html') | import('./table/base') | undefined;
21
+ abstract get parentNode(): ExtToken | HtmlToken | TableToken | TrToken | TdToken | undefined;
22
+ abstract get parentElement(): ExtToken | HtmlToken | TableToken | TrToken | TdToken | undefined;
22
23
  /** getAttrs()方法的getter写法 */
23
24
  get attributes(): Record<string, string | true>;
24
25
  /** 以字符串表示的class属性 */
@@ -37,7 +38,7 @@ declare abstract class AttributesToken extends Token {
37
38
  * @param type 标签类型
38
39
  * @param name 标签名
39
40
  */
40
- constructor(attr: string | undefined, type: AttributesTypes, name: string, config?: Parser.Config, accum?: Token[]);
41
+ constructor(attr: string | undefined, type: AttributesTypes, name: string, config?: import("../index").Config, accum?: Token[]);
41
42
  /**
42
43
  * 所有指定属性名的AttributeToken
43
44
  * @browser
@@ -60,7 +61,7 @@ declare abstract class AttributesToken extends Token {
60
61
  * @override
61
62
  * @browser
62
63
  */
63
- lint(start?: number): Parser.LintError[];
64
+ lint(start?: number): LintError[];
64
65
  /**
65
66
  * @override
66
67
  * @browser
@@ -115,4 +116,4 @@ declare abstract class AttributesToken extends Token {
115
116
  /** @override */
116
117
  text(): string;
117
118
  }
118
- export = AttributesToken;
119
+ export {};
@@ -1,16 +1,18 @@
1
1
  "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AttributesToken = void 0;
2
4
  const lint_1 = require("../util/lint");
3
5
  const string_1 = require("../util/string");
4
- const Parser = require("../index");
5
- const Token = require(".");
6
- const AtomToken = require("./atom");
7
- const AttributeToken = require("./attribute");
6
+ const index_1 = require("../index");
7
+ const _1 = require(".");
8
+ const atom_1 = require("./atom");
9
+ const attribute_1 = require("./attribute");
8
10
  const stages = { 'ext-attrs': 0, 'html-attrs': 2, 'table-attrs': 3 };
9
11
  /**
10
12
  * 扩展和HTML标签属性
11
13
  * @classdesc `{childNodes: ...AtomToken|AttributeToken}`
12
14
  */
13
- class AttributesToken extends Token {
15
+ class AttributesToken extends _1.Token {
14
16
  /** getAttrs()方法的getter写法 */
15
17
  get attributes() {
16
18
  return this.getAttrs();
@@ -45,7 +47,7 @@ class AttributesToken extends Token {
45
47
  * @param type 标签类型
46
48
  * @param name 标签名
47
49
  */
48
- constructor(attr, type, name, config = Parser.getConfig(), accum = []) {
50
+ constructor(attr, type, name, config = index_1.default.getConfig(), accum = []) {
49
51
  super(undefined, config, true, accum, {
50
52
  AtomToken: ':', AttributeToken: ':',
51
53
  });
@@ -62,7 +64,7 @@ class AttributesToken extends Token {
62
64
  let out = '', mt = regex.exec(attr), lastIndex = 0;
63
65
  const insertDirty = /** 插入无效属性 */ () => {
64
66
  if (out) {
65
- super.insertAt(new AtomToken(out, `${type.slice(0, -1)}-dirty`, config, accum, { [`Stage-${stages[type]}`]: ':' }));
67
+ super.insertAt(new atom_1.AtomToken(out, `${type.slice(0, -1)}-dirty`, config, accum, { [`Stage-${stages[type]}`]: ':' }));
66
68
  out = '';
67
69
  }
68
70
  };
@@ -71,7 +73,8 @@ class AttributesToken extends Token {
71
73
  out += attr.slice(lastIndex, index);
72
74
  if (/^(?:[\w:]|\0\d+[t!~{}+-]\x7F)(?:[\w:.-]|\0\d+[t!~{}+-]\x7F)*$/u.test((0, string_1.removeComment)(key).trim())) {
73
75
  const value = quoted ?? unquoted, quotes = [quoteStart, quoteEnd],
74
- token = new AttributeToken(type.slice(0, -1), name, key, equal, value, quotes, config, accum);
76
+ // @ts-expect-error abstract class
77
+ token = new attribute_1.AttributeToken(type.slice(0, -1), name, key, equal, value, quotes, config, accum);
75
78
  insertDirty();
76
79
  super.insertAt(token);
77
80
  }
@@ -98,9 +101,7 @@ class AttributesToken extends Token {
98
101
  * @param key 属性名
99
102
  */
100
103
  getAttrTokens(key) {
101
- return typeof key === 'string'
102
- ? this.childNodes.filter(child => child instanceof AttributeToken && child.name === key.toLowerCase().trim())
103
- : this.typeError('getAttrTokens', 'String');
104
+ return this.childNodes.filter(child => child instanceof attribute_1.AttributeToken && child.name === key.toLowerCase().trim());
104
105
  }
105
106
  /**
106
107
  * 指定属性名的最后一个AttributeToken
@@ -124,7 +125,7 @@ class AttributesToken extends Token {
124
125
  * @browser
125
126
  */
126
127
  lint(start = this.getAbsoluteIndex()) {
127
- const errors = super.lint(start), { parentNode, length, childNodes } = this, attrs = {}, duplicated = new Set();
128
+ const errors = super.lint(start), { parentNode, length, childNodes } = this, attrs = new Map(), duplicated = new Set();
128
129
  let rect;
129
130
  if (parentNode?.type === 'html' && parentNode.closing && this.text().trim()) {
130
131
  rect = { start, ...this.getRootNode().posFromIndex(start) };
@@ -132,28 +133,28 @@ class AttributesToken extends Token {
132
133
  }
133
134
  for (let i = 0; i < length; i++) {
134
135
  const attr = childNodes[i];
135
- if (attr instanceof AtomToken && attr.text().trim()) {
136
+ if (attr instanceof atom_1.AtomToken && attr.text().trim()) {
136
137
  rect ??= { start, ...this.getRootNode().posFromIndex(start) };
137
138
  errors.push({
138
139
  ...(0, lint_1.generateForChild)(attr, rect, 'containing invalid attribute'),
139
140
  excerpt: childNodes.slice(i).map(String).join('').slice(0, 50),
140
141
  });
141
142
  }
142
- else if (attr instanceof AttributeToken) {
143
+ else if (attr instanceof attribute_1.AttributeToken) {
143
144
  const { name } = attr;
144
- if (Object.hasOwn(attrs, name)) {
145
+ if (attrs.has(name)) {
145
146
  duplicated.add(name);
146
- attrs[name].push(attr);
147
+ attrs.get(name).push(attr);
147
148
  }
148
149
  else if (name !== 'class') {
149
- attrs[name] = [attr];
150
+ attrs.set(name, [attr]);
150
151
  }
151
152
  }
152
153
  }
153
154
  if (duplicated.size > 0) {
154
155
  rect ??= { start, ...this.getRootNode().posFromIndex(start) };
155
156
  for (const key of duplicated) {
156
- errors.push(...attrs[key].map(attr => (0, lint_1.generateForChild)(attr, rect, Parser.msg('duplicated $1 attribute', key))));
157
+ errors.push(...attrs.get(key).map(attr => (0, lint_1.generateForChild)(attr, rect, index_1.default.msg('duplicated $1 attribute', key))));
157
158
  }
158
159
  }
159
160
  return errors;
@@ -164,7 +165,7 @@ class AttributesToken extends Token {
164
165
  */
165
166
  print() {
166
167
  return String(this)
167
- ? `<span class="wpb-${this.type}">${this.childNodes.map(child => child.print(child instanceof AtomToken && child.text().trim() ? { class: 'hidden' } : undefined)).join('')}</span>`
168
+ ? `<span class="wpb-${this.type}">${this.childNodes.map(child => child.print(child instanceof atom_1.AtomToken && child.text().trim() ? { class: 'hidden' } : undefined)).join('')}</span>`
168
169
  : '';
169
170
  }
170
171
  /** 清理标签属性 */
@@ -172,19 +173,20 @@ class AttributesToken extends Token {
172
173
  let dirty = false;
173
174
  for (let i = this.length - 1; i >= 0; i--) {
174
175
  const child = this.childNodes[i];
175
- if (child instanceof AtomToken && child.text().trim()) {
176
+ if (child instanceof atom_1.AtomToken && child.text().trim()) {
176
177
  dirty = true;
177
178
  this.removeAt(i);
178
179
  }
179
180
  }
180
- if (!Parser.running && dirty) {
181
- Parser.warn(`${this.constructor.name}.sanitize 方法将清理无效属性!`);
181
+ if (!index_1.default.running && dirty) {
182
+ index_1.default.warn(`${this.constructor.name}.sanitize 方法将清理无效属性!`);
182
183
  }
183
184
  }
184
185
  /** @override */
185
186
  cloneNode() {
186
187
  const cloned = this.cloneChildNodes();
187
- return Parser.run(() => {
188
+ return index_1.default.run(() => {
189
+ // @ts-expect-error abstract class
188
190
  const token = new AttributesToken(undefined, this.type, this.name, this.getAttribute('config'));
189
191
  token.append(...cloned);
190
192
  return token;
@@ -192,7 +194,7 @@ class AttributesToken extends Token {
192
194
  }
193
195
  /** 所有无效属性 */
194
196
  getDirtyAttrs() {
195
- return this.childNodes.filter(child => child instanceof AtomToken && child.text().trim());
197
+ return this.childNodes.filter(child => child instanceof atom_1.AtomToken && child.text().trim());
196
198
  }
197
199
  /**
198
200
  * @override
@@ -201,7 +203,7 @@ class AttributesToken extends Token {
201
203
  * @throws `RangeError` 不是AttributeToken或标签不匹配
202
204
  */
203
205
  insertAt(token, i = this.length) {
204
- if (!(token instanceof AttributeToken)) {
206
+ if (!(token instanceof attribute_1.AttributeToken)) {
205
207
  throw new RangeError(`${this.constructor.name}只能插入AttributeToken!`);
206
208
  }
207
209
  else if (token.type !== this.type.slice(0, -1) || token.tag !== this.name) {
@@ -209,7 +211,7 @@ class AttributesToken extends Token {
209
211
  }
210
212
  else if (i === this.length) {
211
213
  const { lastChild } = this;
212
- if (lastChild instanceof AttributeToken) {
214
+ if (lastChild instanceof attribute_1.AttributeToken) {
213
215
  lastChild.close();
214
216
  }
215
217
  }
@@ -222,10 +224,10 @@ class AttributesToken extends Token {
222
224
  super.insertAt(token, i);
223
225
  const { previousVisibleSibling, nextVisibleSibling } = token, type = `${this.type.slice(0, -1)}-dirty`, config = this.getAttribute('config'), acceptable = { [`Stage-${stages[this.type]}`]: ':' };
224
226
  if (nextVisibleSibling && !/^\s/u.test(String(nextVisibleSibling))) {
225
- super.insertAt(Parser.run(() => new AtomToken(' ', type, config, [], acceptable)), i + 1);
227
+ super.insertAt(index_1.default.run(() => new atom_1.AtomToken(' ', type, config, [], acceptable)), i + 1);
226
228
  }
227
229
  if (previousVisibleSibling && !/\s$/u.test(String(previousVisibleSibling))) {
228
- super.insertAt(Parser.run(() => new AtomToken(' ', type, config, [], acceptable)), i);
230
+ super.insertAt(index_1.default.run(() => new atom_1.AtomToken(' ', type, config, [], acceptable)), i);
229
231
  }
230
232
  return token;
231
233
  }
@@ -237,10 +239,7 @@ class AttributesToken extends Token {
237
239
  * @throws `RangeError` 无效的属性名
238
240
  */
239
241
  setAttr(key, value) {
240
- if (typeof key !== 'string' || typeof value !== 'string' && typeof value !== 'boolean') {
241
- this.typeError('setAttr', 'String', 'Boolean');
242
- }
243
- else if (this.type === 'ext-attrs' && typeof value === 'string' && value.includes('>')) {
242
+ if (this.type === 'ext-attrs' && typeof value === 'string' && value.includes('>')) {
244
243
  throw new RangeError('扩展标签属性不能包含 ">"!');
245
244
  }
246
245
  const k = key.toLowerCase().trim(), attr = this.getAttrToken(k);
@@ -253,11 +252,12 @@ class AttributesToken extends Token {
253
252
  }
254
253
  const config = this.getAttribute('config'), include = this.getAttribute('include'), parsedKey = this.type === 'ext-attrs'
255
254
  ? k
256
- : Parser.run(() => String(new Token(k, config).parseOnce(0, include).parseOnce()));
255
+ : index_1.default.run(() => String(new _1.Token(k, config).parseOnce(0, include).parseOnce()));
257
256
  if (!/^(?:[\w:]|\0\d+[t!~{}+-]\x7F)(?:[\w:.-]|\0\d+[t!~{}+-]\x7F)*$/u.test(parsedKey)) {
258
257
  throw new RangeError(`无效的属性名:${k}!`);
259
258
  }
260
- const newAttr = Parser.run(() => new AttributeToken(this.type.slice(0, -1), this.name, k, value === true ? '' : '=', value === true ? '' : value, ['"', '"'], config));
259
+ // @ts-expect-error abstract class
260
+ const newAttr = index_1.default.run(() => new attribute_1.AttributeToken(this.type.slice(0, -1), this.name, k, value === true ? '' : '=', value === true ? '' : value, ['"', '"'], config));
261
261
  this.insertAt(newAttr);
262
262
  }
263
263
  /** @private */
@@ -291,13 +291,11 @@ class AttributesToken extends Token {
291
291
  * @param key 属性键
292
292
  */
293
293
  hasAttr(key) {
294
- return typeof key === 'string'
295
- ? this.getAttrTokens(key).length > 0
296
- : this.typeError('hasAttr', 'String');
294
+ return this.getAttrTokens(key).length > 0;
297
295
  }
298
296
  /** 获取全部的标签属性名 */
299
297
  getAttrNames() {
300
- return new Set(this.childNodes.filter(child => child instanceof AttributeToken).map(({ name }) => name));
298
+ return new Set(this.childNodes.filter(child => child instanceof attribute_1.AttributeToken).map(({ name }) => name));
301
299
  }
302
300
  /** 标签是否具有任意属性 */
303
301
  hasAttrs() {
@@ -305,7 +303,7 @@ class AttributesToken extends Token {
305
303
  }
306
304
  /** 获取全部标签属性 */
307
305
  getAttrs() {
308
- const attrs = this.childNodes.filter(child => child instanceof AttributeToken);
306
+ const attrs = this.childNodes.filter(child => child instanceof attribute_1.AttributeToken);
309
307
  return Object.fromEntries(attrs.map(({ name, value }) => [name, value]));
310
308
  }
311
309
  /**
@@ -324,9 +322,6 @@ class AttributesToken extends Token {
324
322
  * @throws `RangeError` 不为Boolean类型的属性值
325
323
  */
326
324
  toggleAttr(key, force) {
327
- if (typeof key !== 'string') {
328
- this.typeError('toggleAttr', 'String');
329
- }
330
325
  const k = key.toLowerCase().trim(), attr = this.getAttrToken(k);
331
326
  if (attr && attr.getValue() !== true) {
332
327
  throw new RangeError(`${k} 属性的值不为 Boolean!`);
@@ -363,9 +358,9 @@ class AttributesToken extends Token {
363
358
  if (this.type === 'table-attrs') {
364
359
  (0, string_1.normalizeSpace)(this);
365
360
  }
366
- const str = (0, string_1.text)(this.childNodes.filter(child => child instanceof AttributeToken), ' ');
361
+ const str = (0, string_1.text)(this.childNodes.filter(child => child instanceof attribute_1.AttributeToken), ' ');
367
362
  return `${this.#leadingSpace(str)}${str}`;
368
363
  }
369
364
  }
370
- Parser.classes['AttributesToken'] = __filename;
371
- module.exports = AttributesToken;
365
+ exports.AttributesToken = AttributesToken;
366
+ index_1.default.classes['AttributesToken'] = __filename;
@@ -1,12 +1,11 @@
1
- import * as Parser from '../index';
2
- import Token = require('.');
3
- import ConverterFlagsToken = require('./converterFlags');
4
- import ConverterRuleToken = require('./converterRule');
1
+ import { Token } from '.';
2
+ import { ConverterFlagsToken } from './converterFlags';
3
+ import { ConverterRuleToken } from './converterRule';
5
4
  /**
6
5
  * 转换
7
6
  * @classdesc `{childNodes: [ConverterFlagsToken, ...ConverterRuleToken]}`
8
7
  */
9
- declare abstract class ConverterToken extends Token {
8
+ export declare abstract class ConverterToken extends Token {
10
9
  /** @browser */
11
10
  readonly type = "converter";
12
11
  childNodes: [ConverterFlagsToken, ...ConverterRuleToken[]];
@@ -24,7 +23,7 @@ declare abstract class ConverterToken extends Token {
24
23
  * @param flags 转换类型标记
25
24
  * @param rules 转换规则
26
25
  */
27
- constructor(flags: string[], rules: [string, ...string[]], config?: Parser.Config, accum?: Token[]);
26
+ constructor(flags: string[], rules: [string, ...string[]], config?: import("../index").Config, accum?: Token[]);
28
27
  /**
29
28
  * @override
30
29
  * @browser
@@ -74,4 +73,3 @@ declare abstract class ConverterToken extends Token {
74
73
  */
75
74
  toggleFlag(flag: string): void;
76
75
  }
77
- export = ConverterToken;
@@ -1,14 +1,16 @@
1
1
  "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConverterToken = void 0;
2
4
  const string_1 = require("../util/string");
3
- const Parser = require("../index");
4
- const Token = require(".");
5
- const ConverterFlagsToken = require("./converterFlags");
6
- const ConverterRuleToken = require("./converterRule");
5
+ const index_1 = require("../index");
6
+ const _1 = require(".");
7
+ const converterFlags_1 = require("./converterFlags");
8
+ const converterRule_1 = require("./converterRule");
7
9
  /**
8
10
  * 转换
9
11
  * @classdesc `{childNodes: [ConverterFlagsToken, ...ConverterRuleToken]}`
10
12
  */
11
- class ConverterToken extends Token {
13
+ class ConverterToken extends _1.Token {
12
14
  /** @browser */
13
15
  type = 'converter';
14
16
  /** 是否不转换 */
@@ -24,17 +26,21 @@ class ConverterToken extends Token {
24
26
  * @param flags 转换类型标记
25
27
  * @param rules 转换规则
26
28
  */
27
- constructor(flags, rules, config = Parser.getConfig(), accum = []) {
29
+ constructor(flags, rules, config = index_1.default.getConfig(), accum = []) {
28
30
  super(undefined, config, true, accum);
29
- this.append(new ConverterFlagsToken(flags, config, accum));
31
+ // @ts-expect-error abstract class
32
+ this.append(new converterFlags_1.ConverterFlagsToken(flags, config, accum));
30
33
  const [firstRule] = rules, hasColon = firstRule.includes(':'),
31
- firstRuleToken = new ConverterRuleToken(firstRule, hasColon, config, accum);
34
+ // @ts-expect-error abstract class
35
+ firstRuleToken = new converterRule_1.ConverterRuleToken(firstRule, hasColon, config, accum);
32
36
  if (hasColon && firstRuleToken.length === 1) {
33
- this.insertAt(new ConverterRuleToken(rules.join(';'), false, config, accum));
37
+ // @ts-expect-error abstract class
38
+ this.insertAt(new converterRule_1.ConverterRuleToken(rules.join(';'), false, config, accum));
34
39
  }
35
40
  else {
36
41
  this.append(firstRuleToken,
37
- ...rules.slice(1).map(rule => new ConverterRuleToken(rule, true, config, accum)));
42
+ // @ts-expect-error abstract class
43
+ ...rules.slice(1).map(rule => new converterRule_1.ConverterRuleToken(rule, true, config, accum)));
38
44
  }
39
45
  this.protectChildren(0);
40
46
  }
@@ -61,9 +67,8 @@ class ConverterToken extends Token {
61
67
  return 2;
62
68
  }
63
69
  /** @private */
64
- getGaps(i = 0) {
65
- const j = i < 0 ? i + this.length : i;
66
- return j || this.firstChild.length > 0 ? 1 : 0;
70
+ getGaps(i) {
71
+ return i || this.firstChild.length > 0 ? 1 : 0;
67
72
  }
68
73
  /**
69
74
  * @override
@@ -76,7 +81,8 @@ class ConverterToken extends Token {
76
81
  /** @override */
77
82
  cloneNode() {
78
83
  const [flags, ...rules] = this.cloneChildNodes();
79
- return Parser.run(() => {
84
+ return index_1.default.run(() => {
85
+ // @ts-expect-error abstract class
80
86
  const token = new ConverterToken([], [], this.getAttribute('config'));
81
87
  token.firstChild.safeReplaceWith(flags);
82
88
  token.append(...rules);
@@ -131,5 +137,5 @@ class ConverterToken extends Token {
131
137
  this.firstChild.toggleFlag(flag);
132
138
  }
133
139
  }
134
- Parser.classes['ConverterToken'] = __filename;
135
- module.exports = ConverterToken;
140
+ exports.ConverterToken = ConverterToken;
141
+ index_1.default.classes['ConverterToken'] = __filename;
@@ -1,12 +1,12 @@
1
- import * as Parser from '../index';
2
- import Token = require('.');
3
- import AtomToken = require('./atom');
4
- import type { TokenAttributeGetter } from '../lib/node';
1
+ import { Token } from '.';
2
+ import { AtomToken } from './atom';
3
+ import type { LintError } from '../index';
4
+ import type { ConverterToken, ConverterRuleToken } from '../internal';
5
5
  /**
6
6
  * 转换flags
7
7
  * @classdesc `{childNodes: ...AtomToken}`
8
8
  */
9
- declare abstract class ConverterFlagsToken extends Token {
9
+ export declare abstract class ConverterFlagsToken extends Token {
10
10
  #private;
11
11
  /** @browser */
12
12
  readonly type = "converter-flags";
@@ -16,17 +16,17 @@ declare abstract class ConverterFlagsToken extends Token {
16
16
  abstract get firstElementChild(): AtomToken | undefined;
17
17
  abstract get lastChild(): AtomToken | undefined;
18
18
  abstract get lastElementChild(): AtomToken | undefined;
19
- abstract get parentNode(): import('./converter') | undefined;
20
- abstract get parentElement(): import('./converter') | undefined;
19
+ abstract get parentNode(): ConverterToken | undefined;
20
+ abstract get parentElement(): ConverterToken | undefined;
21
21
  abstract get previousSibling(): undefined;
22
22
  abstract get previousElementSibling(): undefined;
23
- abstract get nextSibling(): import('./converterRule') | undefined;
24
- abstract get nextElementSibling(): import('./converterRule') | undefined;
23
+ abstract get nextSibling(): ConverterRuleToken | undefined;
24
+ abstract get nextElementSibling(): ConverterRuleToken | undefined;
25
25
  /**
26
26
  * @browser
27
27
  * @param flags 转换类型标记
28
28
  */
29
- constructor(flags: string[], config?: Parser.Config, accum?: Token[]);
29
+ constructor(flags: string[], config?: import("../index").Config, accum?: Token[]);
30
30
  /**
31
31
  * @override
32
32
  * @browser
@@ -56,7 +56,7 @@ declare abstract class ConverterFlagsToken extends Token {
56
56
  * @override
57
57
  * @browser
58
58
  */
59
- lint(start?: number): Parser.LintError[];
59
+ lint(start?: number): LintError[];
60
60
  /** @override */
61
61
  cloneNode(): this;
62
62
  /**
@@ -105,4 +105,3 @@ declare abstract class ConverterFlagsToken extends Token {
105
105
  */
106
106
  toggleFlag(flag: string): void;
107
107
  }
108
- export = ConverterFlagsToken;