wikiparser-node 0.0.0 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- const {typeError} = require('../../util/debug'),
4
- assert = require('assert/strict'),
3
+ const assert = require('assert/strict'),
4
+ {noWrap} = require('../../util/string'),
5
5
  /** @type {Parser} */ Parser = require('../..'),
6
6
  Token = require('..'), // eslint-disable-line no-unused-vars
7
7
  TrToken = require('./tr'),
@@ -50,7 +50,7 @@ class Layout extends Array {
50
50
 
51
51
  /**
52
52
  * 表格
53
- * @classdesc `{childNodes: [SyntaxToken, AttributeToken, ?(string|Token), ...TdToken, ...TrToken, ?SyntaxToken]}`
53
+ * @classdesc `{childNodes: [SyntaxToken, AttributeToken, ?Token, ...TdToken, ...TrToken, ?SyntaxToken]}`
54
54
  */
55
55
  class TableToken extends TrToken {
56
56
  type = 'table';
@@ -65,7 +65,7 @@ class TableToken extends TrToken {
65
65
  constructor(syntax, attr = '', config = Parser.getConfig(), accum = []) {
66
66
  super(syntax, attr, config, accum, TableToken.openingPattern);
67
67
  this.setAttribute('acceptable', {
68
- String: 2, Token: 2, SyntaxToken: [0, -1], AttributeToken: 1, TdToken: '2:', TrToken: '2:',
68
+ Token: 2, SyntaxToken: [0, -1], AttributeToken: 1, TdToken: '2:', TrToken: '2:',
69
69
  });
70
70
  }
71
71
 
@@ -84,26 +84,31 @@ class TableToken extends TrToken {
84
84
  } else if (!Parser.running && i === this.childNodes.length && token instanceof SyntaxToken
85
85
  && (token.getAttribute('pattern') !== closingPattern || !closingPattern.test(token.text()))
86
86
  ) {
87
- throw new SyntaxError(`表格的闭合部分不符合语法!${token.toString().replaceAll('\n', '\\n')}`);
87
+ throw new SyntaxError(`表格的闭合部分不符合语法!${noWrap(token.toString())}`);
88
88
  }
89
89
  return super.insertAt(token, i);
90
90
  }
91
91
 
92
92
  /** @complexity `n` */
93
- close(syntax = '\n|}') {
93
+ close(syntax = '\n|}', halfParsed = false) {
94
+ halfParsed &&= Parser.running;
94
95
  const config = this.getAttribute('config'),
95
- inner = Parser.parse(syntax, this.getAttribute('include'), 2, config),
96
- {lastElementChild} = this;
97
- if (!TableToken.closingPattern.test(inner.text())) {
98
- throw new SyntaxError(`表格的闭合部分不符合语法!${syntax.replaceAll('\n', '\\n')}`);
96
+ accum = this.getAttribute('accum'),
97
+ inner = !halfParsed && Parser.parse(syntax, this.getAttribute('include'), 2, config),
98
+ {lastElementChild} = this,
99
+ {closingPattern} = TableToken;
100
+ if (!halfParsed && !closingPattern.test(inner.text())) {
101
+ throw new SyntaxError(`表格的闭合部分不符合语法!${noWrap(syntax)}`);
99
102
  } else if (lastElementChild instanceof SyntaxToken) {
100
103
  lastElementChild.replaceChildren(...inner.childNodes);
101
104
  } else {
102
105
  this.appendChild(Parser.run(() => {
103
- const token = new SyntaxToken(undefined, TableToken.closingPattern, 'table-syntax', config, [], {
106
+ const token = new SyntaxToken(syntax, closingPattern, 'table-syntax', config, accum, {
104
107
  'Stage-1': ':', '!ExtToken': '', TranscludeToken: ':',
105
108
  });
106
- token.replaceChildren(...inner.childNodes);
109
+ if (inner) {
110
+ token.replaceChildren(...inner.childNodes);
111
+ }
107
112
  return token;
108
113
  }));
109
114
  }
@@ -132,7 +137,7 @@ class TableToken extends TrToken {
132
137
  */
133
138
  getNthRow(n, force = false, insert = false) {
134
139
  if (typeof n !== 'number') {
135
- typeError(this, 'getNthRow', 'Number');
140
+ this.typeError('getNthRow', 'Number');
136
141
  }
137
142
  const nRows = this.getRowCount(),
138
143
  isRow = super.getRowCount();
@@ -240,7 +245,7 @@ class TableToken extends TrToken {
240
245
  */
241
246
  toRenderedCoords({row, column}) {
242
247
  if (typeof row !== 'number' || typeof column !== 'number') {
243
- typeError(this, 'toRenderedCoords', 'Number');
248
+ this.typeError('toRenderedCoords', 'Number');
244
249
  }
245
250
  const rowLayout = this.getLayout({row, column})[row],
246
251
  x = rowLayout?.findIndex(coords => cmpCoords(coords, {row, column}) === 0);
@@ -253,7 +258,7 @@ class TableToken extends TrToken {
253
258
  */
254
259
  toRawCoords({x, y}) {
255
260
  if (typeof x !== 'number' || typeof y !== 'number') {
256
- typeError(this, 'toRawCoords', 'Number');
261
+ this.typeError('toRawCoords', 'Number');
257
262
  }
258
263
  const rowLayout = this.getLayout({x, y})[y],
259
264
  coords = rowLayout?.[x];
@@ -272,7 +277,7 @@ class TableToken extends TrToken {
272
277
  */
273
278
  getFullRow(y) {
274
279
  if (typeof y !== 'number') {
275
- typeError(this, 'getFullRow', 'Number');
280
+ this.typeError('getFullRow', 'Number');
276
281
  }
277
282
  const rows = this.getAllRows();
278
283
  return new Map(
@@ -286,7 +291,7 @@ class TableToken extends TrToken {
286
291
  */
287
292
  getFullCol(x) {
288
293
  if (typeof x !== 'number') {
289
- typeError(this, 'getFullCol', 'Number');
294
+ this.typeError('getFullCol', 'Number');
290
295
  }
291
296
  const layout = this.getLayout(),
292
297
  colLayout = layout.map(row => row[x]).filter(coords => coords),
@@ -438,7 +443,7 @@ class TableToken extends TrToken {
438
443
  */
439
444
  insertTableRow(y, attr = {}, inner = undefined, subtype = 'td', innerAttr = {}) {
440
445
  if (typeof attr !== 'object') {
441
- typeError(this, 'insertTableRow', 'Object');
446
+ this.typeError('insertTableRow', 'Object');
442
447
  }
443
448
  let reference = this.getNthRow(y, false, true);
444
449
  /** @type {TrToken & AttributeToken}} */
@@ -482,7 +487,7 @@ class TableToken extends TrToken {
482
487
  */
483
488
  insertTableCol(x, inner, subtype = 'td', attr = {}) {
484
489
  if (typeof x !== 'number') {
485
- typeError(this, 'insertTableCol', 'Number');
490
+ this.typeError('insertTableCol', 'Number');
486
491
  }
487
492
  const layout = this.getLayout(),
488
493
  rowLength = layout.map(({length}) => length),
@@ -565,7 +570,7 @@ class TableToken extends TrToken {
565
570
  */
566
571
  mergeCells(xlim, ylim) {
567
572
  if ([...xlim, ...ylim].some(arg => typeof arg !== 'number')) {
568
- typeError(this, 'mergeCells', 'Number');
573
+ this.typeError('mergeCells', 'Number');
569
574
  }
570
575
  const layout = this.getLayout(),
571
576
  maxCol = Math.max(...layout.map(({length}) => length));
@@ -711,7 +716,7 @@ class TableToken extends TrToken {
711
716
  */
712
717
  moveTableRowBefore(y, before) {
713
718
  if (typeof y !== 'number' || typeof before !== 'number') {
714
- typeError(this, 'moveTableRowBefore', 'Number');
719
+ this.typeError('moveTableRowBefore', 'Number');
715
720
  }
716
721
  const layout = this.getLayout(),
717
722
  /**
@@ -748,7 +753,7 @@ class TableToken extends TrToken {
748
753
  */
749
754
  moveTableRowAfter(y, after) {
750
755
  if (typeof y !== 'number' || typeof after !== 'number') {
751
- typeError(this, 'moveTableRowAfter', 'Number');
756
+ this.typeError('moveTableRowAfter', 'Number');
752
757
  }
753
758
  const layout = this.getLayout(),
754
759
  afterToken = this.getNthRow(after),
@@ -797,7 +802,7 @@ class TableToken extends TrToken {
797
802
  */
798
803
  #moveCol(x, reference, after = false) {
799
804
  if (typeof x !== 'number' || typeof reference !== 'number') {
800
- typeError(this, `moveTableCol${after ? 'After' : 'Before'}`, 'Number');
805
+ this.typeError(`moveTableCol${after ? 'After' : 'Before'}`, 'Number');
801
806
  }
802
807
  const layout = this.getLayout(),
803
808
  /** @type {(rowLayout: TableCoords[], i: number, oneCol?: boolean) => boolean} */
package/src/table/td.js CHANGED
@@ -110,7 +110,7 @@ class TdToken extends fixedToken(TrToken) {
110
110
  */
111
111
  static create(inner, subtype = 'td', attr = {}, include = false, config = Parser.getConfig()) {
112
112
  if (typeof inner !== 'string' && (!(inner instanceof Token) || !inner.isPlain()) || typeof attr !== 'object') {
113
- throw new TypeError('TdToken.create 方法仅接受 String、Token、Object 作为输入参数!');
113
+ typeError(this, 'create', 'String', 'Token', 'Object');
114
114
  } else if (!['td', 'th', 'caption'].includes(subtype)) {
115
115
  throw new RangeError('单元格的子类型只能为 "td"、"th" 或 "caption"!');
116
116
  } else if (typeof inner === 'string') {
@@ -231,7 +231,7 @@ class TdToken extends fixedToken(TrToken) {
231
231
  */
232
232
  setAttr(key, value) {
233
233
  if (typeof key !== 'string') {
234
- typeError(this, 'setAttr', 'String');
234
+ this.typeError('setAttr', 'String');
235
235
  }
236
236
  key = key.toLowerCase().trim();
237
237
  if (typeof value === 'number' && ['rowspan', 'colspan'].includes(key)) {
package/src/table/tr.js CHANGED
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  const attributeParent = require('../../mixin/attributeParent'),
4
- {typeError} = require('../../util/debug'),
5
4
  /** @type {Parser} */ Parser = require('../..'),
6
5
  Token = require('..'),
7
6
  SyntaxToken = require('../syntax'),
@@ -9,7 +8,7 @@ const attributeParent = require('../../mixin/attributeParent'),
9
8
 
10
9
  /**
11
10
  * 表格行,含开头的换行,不含结尾的换行
12
- * @classdesc `{childNodes: [SyntaxToken, AttributeToken, ?(string|Token), ...TdToken]}`
11
+ * @classdesc `{childNodes: [SyntaxToken, AttributeToken, ?Token, ...TdToken]}`
13
12
  */
14
13
  class TrToken extends attributeParent(Token, 1) {
15
14
  type = 'tr';
@@ -21,7 +20,7 @@ class TrToken extends attributeParent(Token, 1) {
21
20
  * @param {accum} accum
22
21
  */
23
22
  constructor(syntax, attr = '', config = Parser.getConfig(), accum = [], pattern = TrToken.openingPattern) {
24
- super(undefined, config, true, accum, {String: 2, Token: 2, SyntaxToken: 0, AttributeToken: 1, TdToken: '2:'});
23
+ super(undefined, config, true, accum, {Token: 2, SyntaxToken: 0, AttributeToken: 1, TdToken: '2:'});
25
24
  this.append(
26
25
  new SyntaxToken(syntax, pattern, 'table-syntax', config, accum, {
27
26
  'Stage-1': ':', '!ExtToken': '', TranscludeToken: ':',
@@ -75,9 +74,6 @@ class TrToken extends attributeParent(Token, 1) {
75
74
 
76
75
  /** @param {SyntaxToken} syntax */
77
76
  static escape(syntax) {
78
- if (!(syntax instanceof SyntaxToken)) {
79
- typeError('SyntaxToken');
80
- }
81
77
  const wikitext = syntax.childNodes.map(child => typeof child === 'string'
82
78
  ? child.replaceAll('{|', '{{(!}}').replaceAll('|}', '{{!)}}').replaceAll('||', '{{!!}}')
83
79
  .replaceAll('|', '{{!}}')
@@ -131,7 +127,7 @@ class TrToken extends attributeParent(Token, 1) {
131
127
  */
132
128
  insertAt(token, i = this.childNodes.length) {
133
129
  if (!Parser.running && !(token instanceof TrToken)) {
134
- typeError(this, 'insertAt', 'TrToken');
130
+ this.typeError('insertAt', 'TrToken');
135
131
  }
136
132
  const TdToken = require('./td'),
137
133
  child = this.childNodes.at(i);
@@ -201,7 +197,7 @@ class TrToken extends attributeParent(Token, 1) {
201
197
  */
202
198
  getNthCol(n, insert = false) {
203
199
  if (typeof n !== 'number') {
204
- typeError(this, 'getNthCol', 'Number');
200
+ this.typeError('getNthCol', 'Number');
205
201
  }
206
202
  const nCols = this.getColCount();
207
203
  n = n < 0 ? n + nCols : n;
@@ -52,7 +52,7 @@ class TagPairToken extends fixedToken(Token) {
52
52
  const {closed, firstChild, lastChild, nextSibling, name, selfClosing} = this,
53
53
  [opening, closing] = this.#tags;
54
54
  if (!closed && nextSibling) {
55
- Parser.error(`自动闭合 <${name}>`, String(lastChild).replaceAll('\n', '\\n'));
55
+ Parser.error(`自动闭合 <${name}>`, lastChild);
56
56
  this.closed = true;
57
57
  }
58
58
  return selfClosing
package/src/transclude.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- const {removeComment, escapeRegExp, text} = require('../util/string'),
4
- {typeError, externalUse} = require('../util/debug'),
3
+ const {removeComment, escapeRegExp, text, noWrap} = require('../util/string'),
4
+ {externalUse} = require('../util/debug'),
5
5
  /** @type {Parser} */ Parser = require('..'),
6
6
  Token = require('.'),
7
7
  ParameterToken = require('./parameter');
@@ -19,7 +19,7 @@ class TranscludeToken extends Token {
19
19
  /** @complexity `n` */
20
20
  setModifier(modifier = '') {
21
21
  if (typeof modifier !== 'string') {
22
- typeError(this, 'setModifier', 'String');
22
+ this.typeError('setModifier', 'String');
23
23
  }
24
24
  const [,, raw, subst] = this.getAttribute('config').parserFunction,
25
25
  lcModifier = modifier.trim().toLowerCase(),
@@ -291,7 +291,7 @@ class TranscludeToken extends Token {
291
291
  */
292
292
  getArgs(key, exact = false, copy = true) {
293
293
  if (!['string', 'number'].includes(typeof key)) {
294
- typeError(this, 'getArgs', 'String', 'Number');
294
+ this.typeError('getArgs', 'String', 'Number');
295
295
  } else if (!copy && !Parser.debugging && externalUse('getArgs')) {
296
296
  this.debugOnly('getArgs');
297
297
  }
@@ -383,7 +383,7 @@ class TranscludeToken extends Token {
383
383
  if (length !== 1 || !firstElementChild?.matches(templateLike ? 'template#T' : 'magic-word#lc')
384
384
  || firstElementChild.childElementCount !== 2 || !firstElementChild.lastElementChild.anon
385
385
  ) {
386
- throw new SyntaxError(`非法的匿名参数:${val.replaceAll('\n', '\\n')}`);
386
+ throw new SyntaxError(`非法的匿名参数:${noWrap(val)}`);
387
387
  }
388
388
  return this.appendChild(firstElementChild.lastChild);
389
389
  }
@@ -395,7 +395,7 @@ class TranscludeToken extends Token {
395
395
  */
396
396
  setValue(key, value) {
397
397
  if (typeof key !== 'string') {
398
- typeError(this, 'setValue', 'String');
398
+ this.typeError('setValue', 'String');
399
399
  } else if (!this.matches('template, magic-word#invoke')) {
400
400
  throw new Error(`${this.constructor.name}.setValue 方法仅供模板使用!`);
401
401
  }
@@ -411,7 +411,7 @@ class TranscludeToken extends Token {
411
411
  if (length !== 1 || !firstElementChild?.matches('template#T')
412
412
  || firstElementChild.childElementCount !== 2 || firstElementChild.lastElementChild.name !== key
413
413
  ) {
414
- throw new SyntaxError(`非法的命名参数:${key}=${value.replaceAll('\n', '\\n')}`);
414
+ throw new SyntaxError(`非法的命名参数:${key}=${noWrap(value)}`);
415
415
  }
416
416
  this.appendChild(firstElementChild.lastChild);
417
417
  }
@@ -432,7 +432,7 @@ class TranscludeToken extends Token {
432
432
  if (this.type === 'magic-word') {
433
433
  throw new Error(`${this.constructor.name}.replaceTemplate 方法仅用于更换模板!`);
434
434
  } else if (typeof title !== 'string') {
435
- typeError(this, 'replaceTemplate', 'String');
435
+ this.typeError('replaceTemplate', 'String');
436
436
  }
437
437
  const root = Parser.parse(`{{${title}}}`, this.getAttribute('include'), 2, this.getAttribute('config')),
438
438
  {childNodes: {length}, firstElementChild} = root;
@@ -447,7 +447,7 @@ class TranscludeToken extends Token {
447
447
  if (this.type !== 'magic-word' || this.name !== 'invoke') {
448
448
  throw new Error(`${this.constructor.name}.replaceModule 方法仅用于更换模块!`);
449
449
  } else if (typeof title !== 'string') {
450
- typeError(this, 'replaceModule', 'String');
450
+ this.typeError('replaceModule', 'String');
451
451
  }
452
452
  const root = Parser.parse(`{{#invoke:${title}}}`, this.getAttribute('include'), 2, this.getAttribute('config')),
453
453
  {childNodes: {length}, firstElementChild} = root;
@@ -470,7 +470,7 @@ class TranscludeToken extends Token {
470
470
  if (this.type !== 'magic-word' || this.name !== 'invoke') {
471
471
  throw new Error(`${this.constructor.name}.replaceModule 方法仅用于更换模块!`);
472
472
  } else if (typeof func !== 'string') {
473
- typeError(this, 'replaceFunction', 'String');
473
+ this.typeError('replaceFunction', 'String');
474
474
  } else if (this.childElementCount < 2) {
475
475
  throw new Error('尚未指定模块名称!');
476
476
  }
@@ -582,7 +582,10 @@ class TranscludeToken extends Token {
582
582
  Parser.error(`${this.type === 'template'
583
583
  ? this.name
584
584
  : this.normalizeTitle(this.children[1]?.text() ?? '', 828).title
585
- } 还留有 ${remaining} 个重复的 ${key} 参数!`);
585
+ } 还留有 ${remaining} 个重复的 ${key} 参数:${[...this.getArgs(key)].map(arg => {
586
+ const {top, left} = arg.getBoundingClientRect();
587
+ return `第 ${top} 行第 ${left} 列`;
588
+ }).join('、')}`);
586
589
  duplicatedKeys.push(key);
587
590
  continue;
588
591
  }
package/tool/index.js CHANGED
@@ -1,17 +1,18 @@
1
1
  'use strict';
2
2
 
3
3
  const {typeError, externalUse} = require('../util/debug'),
4
- {text} = require('../util/string'),
4
+ {text, noWrap} = require('../util/string'),
5
5
  Token = require('../src'),
6
6
  assert = require('assert/strict');
7
7
 
8
8
  const /** @type {WeakMap<Token, Record<string, any>>} */ dataStore = new WeakMap();
9
9
 
10
10
  /**
11
+ * @param {string} method
11
12
  * @param {string|Token|Token[]} selector
12
13
  * @returns {(token: Token) => boolean}
13
14
  */
14
- const matchesGenerator = selector => {
15
+ const matchesGenerator = (method, selector) => {
15
16
  if (typeof selector === 'string') {
16
17
  return token => token instanceof Token && token.matches(selector);
17
18
  } else if (Array.isArray(selector)) {
@@ -19,7 +20,7 @@ const matchesGenerator = selector => {
19
20
  } else if (selector instanceof Token) {
20
21
  return token => token === selector;
21
22
  }
22
- typeError('String', 'Token', 'Array');
23
+ typeError(TokenCollection, method, 'String', 'Token', 'Array');
23
24
  };
24
25
 
25
26
  /** @extends {Array<Token>} */
@@ -39,7 +40,7 @@ class TokenCollection extends Array {
39
40
  } else if (typeof token === 'string') {
40
41
  this.push(token);
41
42
  } else if (!(token instanceof Token)) {
42
- typeError('String', 'Token');
43
+ this.typeError('constructor', 'String', 'Token');
43
44
  } else if (!this.includes(token)) {
44
45
  this.#roots.add(token.getRootNode());
45
46
  this.push(token);
@@ -49,6 +50,14 @@ class TokenCollection extends Array {
49
50
  this.#sort();
50
51
  }
51
52
 
53
+ /**
54
+ * @param {string} method
55
+ * @param {...string} types
56
+ */
57
+ typeError(method, ...types) {
58
+ return typeError(this.constructor, method, ...types);
59
+ }
60
+
52
61
  #sort() {
53
62
  if (this.some(token => typeof token === 'string')) {
54
63
  return;
@@ -171,7 +180,7 @@ class TokenCollection extends Array {
171
180
  if (typeof selector === 'function') {
172
181
  return this.some((ele, i) => selector.call(ele, i, ele));
173
182
  }
174
- const matches = matchesGenerator(selector);
183
+ const matches = matchesGenerator('is', selector);
175
184
  return this.some(matches);
176
185
  }
177
186
 
@@ -182,7 +191,7 @@ class TokenCollection extends Array {
182
191
  if (typeof selector === 'function') {
183
192
  $arr = $(arr.filter((ele, i) => selector.call(ele, i, ele)));
184
193
  } else {
185
- const matches = matchesGenerator(selector);
194
+ const matches = matchesGenerator('filter', selector);
186
195
  $arr = $(arr.filter(matches));
187
196
  }
188
197
  $arr.prevObject = this;
@@ -198,7 +207,7 @@ class TokenCollection extends Array {
198
207
  } else if (typeof selector === 'string') {
199
208
  $arr = $(arr.filter(ele => ele instanceof Token && !ele.matches(selector)));
200
209
  } else {
201
- const matches = matchesGenerator(selector);
210
+ const matches = matchesGenerator('not', selector);
202
211
  $arr = $(arr.filter(ele => !matches(ele)));
203
212
  }
204
213
  $arr.prevObject = this;
@@ -215,7 +224,7 @@ class TokenCollection extends Array {
215
224
  } else if (selector instanceof Token) {
216
225
  map = arr => arr.some(token => token.contains(selector)) ? [selector] : [];
217
226
  } else {
218
- typeError('String', 'Token', 'Array');
227
+ this.typeError('find', 'String', 'Token', 'Array');
219
228
  }
220
229
  return this._create(map);
221
230
  }
@@ -228,13 +237,13 @@ class TokenCollection extends Array {
228
237
  } else if (selector instanceof Token) {
229
238
  return arr.some(ele => ele.contains(selector));
230
239
  }
231
- typeError('String', 'Token');
240
+ this.typeError('has', 'String', 'Token');
232
241
  }
233
242
 
234
243
  /** @param {string} selector */
235
244
  closest(selector) {
236
245
  if (typeof selector !== 'string') {
237
- typeError('String');
246
+ this.typeError('closest', 'String');
238
247
  }
239
248
  return this._create(arr => arr.map(ele => ele.closest(selector)));
240
249
  }
@@ -314,7 +323,7 @@ class TokenCollection extends Array {
314
323
  * @param {string|Token|Token[]} selector
315
324
  */
316
325
  _until(method, selector, filter = '') {
317
- const matches = matchesGenerator(selector);
326
+ const matches = matchesGenerator(`${method.replace(/All$/, '')}Until`, selector);
318
327
  return this._create(arr => arr.flatMap(ele => {
319
328
  const tokens = $(ele)[method]().toArray(),
320
329
  tokenArray = method === 'nextAll' ? tokens : tokens.reverse(),
@@ -349,7 +358,7 @@ class TokenCollection extends Array {
349
358
  /** @param {[string|Record<string, any>]} key */
350
359
  data(key, value) {
351
360
  if (value !== undefined && typeof key !== 'string') {
352
- typeError('String');
361
+ this.typeError('data', 'String');
353
362
  } else if (value === undefined && typeof key !== 'object') {
354
363
  const data = $.dataStore.get(this._find());
355
364
  return key === undefined ? data : data?.[key];
@@ -371,7 +380,7 @@ class TokenCollection extends Array {
371
380
  /** @param {string|string[]} name */
372
381
  removeData(name) {
373
382
  if (name !== undefined && typeof name !== 'string' && !Array.isArray(name)) {
374
- typeError('String', 'Array');
383
+ this.typeError('removeData', 'String', 'Array');
375
384
  }
376
385
  name = typeof name === 'string' ? name.split(/\s/) : name;
377
386
  for (const token of this._filter()) {
@@ -396,7 +405,7 @@ class TokenCollection extends Array {
396
405
  */
397
406
  _addEventListener(events, selector, handler, once = false) {
398
407
  if (!['string', 'object'].includes(typeof events)) {
399
- typeError('String', 'Object');
408
+ this.typeError(once ? 'once' : 'on', 'String', 'Object');
400
409
  } else if (typeof selector === 'function') {
401
410
  handler = selector;
402
411
  selector = undefined;
@@ -437,7 +446,7 @@ class TokenCollection extends Array {
437
446
  */
438
447
  off(events, selector, handler) {
439
448
  if (!['string', 'object', 'undefined'].includes(typeof events)) {
440
- typeError('String', 'Object');
449
+ this.typeError('off', 'String', 'Object');
441
450
  }
442
451
  handler = typeof selector === 'function' ? selector : handler;
443
452
  let eventPair;
@@ -452,7 +461,7 @@ class TokenCollection extends Array {
452
461
  } else {
453
462
  for (const [event, listener] of eventPair) {
454
463
  if (typeof event !== 'string' || !['function', 'undefined'].includes(typeof listener)) {
455
- typeError('String', 'Function');
464
+ this.typeError('off', 'String', 'Function');
456
465
  } else if (listener) {
457
466
  token.removeEventListener(event, listener);
458
467
  } else {
@@ -503,7 +512,7 @@ class TokenCollection extends Array {
503
512
  } else if (Array.isArray(result)) {
504
513
  token[method](...result);
505
514
  } else {
506
- typeError('String', 'Token');
515
+ this.typeError(method, 'String', 'Token');
507
516
  }
508
517
  }
509
518
  }
@@ -604,7 +613,7 @@ class TokenCollection extends Array {
604
613
  }
605
614
  }
606
615
  } else {
607
- typeError('Token', 'Array');
616
+ this.typeError(method, 'Token', 'Array');
608
617
  }
609
618
  return this;
610
619
  }
@@ -648,7 +657,7 @@ class TokenCollection extends Array {
648
657
  } else if (Array.isArray(value)) {
649
658
  toValue = i => value[i];
650
659
  } else {
651
- typeError('String', 'Array', 'Function');
660
+ this.typeError('val', 'String', 'Array', 'Function');
652
661
  }
653
662
  for (const [i, token] of this.entries()) {
654
663
  if (token instanceof Token && typeof token.setValue === 'function' && token.setValue.length === 1) {
@@ -730,7 +739,7 @@ class TokenCollection extends Array {
730
739
  */
731
740
  wrapAll(wrapper) {
732
741
  if (typeof wrapper !== 'function' && !Array.isArray(wrapper)) {
733
- typeError('Array', 'Function');
742
+ this.typeError('wrapAll', 'Array', 'Function');
734
743
  }
735
744
  const firstToken = this._find(),
736
745
  error = new Error('wrapAll 的主体应为同一个父节点下的连续子节点!');
@@ -757,7 +766,7 @@ class TokenCollection extends Array {
757
766
  config = firstToken.getRootNode().getAttribute('config'),
758
767
  token = new Token(`${pre}${this.toString()}${post}`, config).parse();
759
768
  if (token.childNodes.length !== 1) {
760
- throw new RangeError(`非法的 wrapper:\n${pre}\n${post}`);
769
+ throw new RangeError(`非法的 wrapper:\n${noWrap(pre)}\n${noWrap(post)}`);
761
770
  }
762
771
  for (let j = i + this.length - 1; j >= i; j--) {
763
772
  parentNode.removeAt(j);
@@ -772,7 +781,7 @@ class TokenCollection extends Array {
772
781
  */
773
782
  _wrap(method, wrapper) {
774
783
  if (typeof wrapper !== 'function' && !Array.isArray(wrapper)) {
775
- typeError('Array', 'Function');
784
+ this.typeError(method, 'Array', 'Function');
776
785
  }
777
786
  return this[method](
778
787
  /**
@@ -788,7 +797,7 @@ class TokenCollection extends Array {
788
797
  config = this.getRootNode().getAttribute('config'),
789
798
  token = new Token(`${pre}${string}${post}`, config).parse();
790
799
  if (token.childNodes.length !== 1) {
791
- throw new RangeError(`非法的 wrapper:\n${pre}\n${post}`);
800
+ throw new RangeError(`非法的 wrapper:\n${noWrap(pre)}\n${noWrap(post)}`);
792
801
  }
793
802
  return token.firstChild;
794
803
  },
@@ -852,36 +861,37 @@ const $ = tokens => {
852
861
  },
853
862
  });
854
863
  };
855
- $.hasData = /** @param {Token} element */ element => {
864
+ /* eslint-disable func-names */
865
+ $.hasData = /** @param {Token} element */ function hasData(element) {
856
866
  if (!(element instanceof Token)) {
857
- typeError('Token');
867
+ typeError(this, 'hasData', 'Token');
858
868
  }
859
- return $.dataStore.has(element);
869
+ return this.dataStore.has(element);
860
870
  };
861
- $.data = /** @type {(element: Token, key?: string, value?: any) => any} */ (element, key, value) => {
871
+ $.data = /** @type {function(Token, string, any): any} */ function data(element, key, value) {
862
872
  if (!(element instanceof Token)) {
863
- typeError('Token');
873
+ typeError(this, 'data', 'Token');
864
874
  } else if (key === undefined) {
865
- return $.dataStore.get(element);
875
+ return this.dataStore.get(element);
866
876
  } else if (typeof key !== 'string') {
867
- typeError('String');
877
+ typeError(this, 'data', 'String');
868
878
  } else if (value === undefined) {
869
- return $.dataStore.get(element)?.[key];
870
- } else if (!$.dataStore.has(element)) {
871
- $.dataStore.set(element, {});
879
+ return this.dataStore.get(element)?.[key];
880
+ } else if (!this.dataStore.has(element)) {
881
+ this.dataStore.set(element, {});
872
882
  }
873
- $.dataStore.get(element)[key] = value;
883
+ this.dataStore.get(element)[key] = value;
874
884
  return value;
875
885
  };
876
- $.removeData = /** @type {(element: Token, name?: string) => void} */ (element, name) => {
886
+ $.removeData = /** @type {function(Token, string): void} */ function removeData(element, name) {
877
887
  if (!(element instanceof Token)) {
878
- typeError('Token');
888
+ typeError(this, 'removeData', 'Token');
879
889
  } else if (name === undefined) {
880
- $.dataStore.delete(element);
890
+ this.dataStore.delete(element);
881
891
  } else if (typeof name !== 'string') {
882
- typeError('String');
883
- } else if ($.dataStore.has(element)) {
884
- const data = $.dataStore.get(element);
892
+ typeError(this, 'removeData', 'String');
893
+ } else if (this.dataStore.has(element)) {
894
+ const data = this.dataStore.get(element);
885
895
  delete data[name];
886
896
  }
887
897
  };
@@ -41,6 +41,7 @@ declare global {
41
41
 
42
42
  readonly MAX_STAGE: number;
43
43
  parse(wikitext: string|Token, include?: boolean, maxStage?: number, config?: ParserConfig): Token;
44
+ reparse(date: string): Token;
44
45
 
45
46
  getTool(): typeof $;
46
47
  }
package/util/debug.js CHANGED
@@ -1,12 +1,12 @@
1
1
  'use strict';
2
2
 
3
3
  /**
4
- * @param {ObjectConstructor} constructor
4
+ * @param {function}
5
5
  * @param {string} method
6
6
  * @param {...string} args
7
7
  */
8
- const typeError = ({constructor}, method, ...args) => {
9
- throw new TypeError(`${constructor.name}.${method} 方法仅接受 ${args.join('、')} 作为输入参数!`);
8
+ const typeError = ({name}, method, ...args) => {
9
+ throw new TypeError(`${name}.${method} 方法仅接受 ${args.join('、')} 作为输入参数!`);
10
10
  };
11
11
 
12
12
  /**
package/util/string.js CHANGED
@@ -54,7 +54,10 @@ const explode = (start, end, separator, str) => {
54
54
  return exploded;
55
55
  };
56
56
 
57
+ /** @param {string} str */
58
+ const noWrap = str => str.replaceAll('\n', '\\n');
59
+
57
60
  const extUrlChar = '(?:[\\d.]+|\\[[\\da-f:.]+\\]|[^[\\]<>"\\x00-\\x20\\x7f\\p{Zs}\\ufffd])'
58
61
  + '[^[\\]<>"\\x00-\\x20\\x7f\\p{Zs}\\ufffd]*';
59
62
 
60
- module.exports = {toCase, removeComment, ucfirst, escapeRegExp, text, explode, extUrlChar};
63
+ module.exports = {toCase, removeComment, ucfirst, escapeRegExp, text, explode, noWrap, extUrlChar};