wikiparser-node 1.0.0-beta.2 → 1.0.0-beta.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 (127) hide show
  1. package/config/minimum.json +136 -0
  2. package/dist/index.d.ts +1 -1
  3. package/dist/index.js +4 -4
  4. package/dist/internal.d.ts +1 -1
  5. package/dist/lib/element.d.ts +12 -8
  6. package/dist/lib/element.js +72 -57
  7. package/dist/lib/node.js +4 -5
  8. package/dist/lib/range.js +5 -8
  9. package/dist/lib/ranges.js +2 -2
  10. package/dist/lib/text.d.ts +2 -2
  11. package/dist/lib/text.js +5 -5
  12. package/dist/lib/title.d.ts +3 -2
  13. package/dist/lib/title.js +11 -10
  14. package/dist/mixin/attributesParent.js +2 -2
  15. package/dist/mixin/fixed.js +3 -3
  16. package/dist/mixin/hidden.js +2 -2
  17. package/dist/mixin/singleLine.js +4 -4
  18. package/dist/mixin/sol.js +6 -6
  19. package/dist/parser/braces.js +4 -9
  20. package/dist/parser/commentAndExt.js +24 -18
  21. package/dist/parser/converter.js +8 -7
  22. package/dist/parser/externalLinks.js +3 -4
  23. package/dist/parser/hrAndDoubleUnderscore.js +4 -7
  24. package/dist/parser/html.js +4 -7
  25. package/dist/parser/links.js +4 -5
  26. package/dist/parser/list.js +3 -6
  27. package/dist/parser/magicLinks.js +3 -3
  28. package/dist/parser/quotes.js +4 -5
  29. package/dist/parser/selector.js +4 -7
  30. package/dist/parser/table.js +6 -15
  31. package/dist/src/arg.d.ts +6 -5
  32. package/dist/src/arg.js +31 -27
  33. package/dist/src/atom.d.ts +2 -1
  34. package/dist/src/atom.js +4 -4
  35. package/dist/src/attribute.d.ts +14 -13
  36. package/dist/src/attribute.js +41 -31
  37. package/dist/src/attributes.d.ts +4 -3
  38. package/dist/src/attributes.js +15 -21
  39. package/dist/src/converter.d.ts +4 -3
  40. package/dist/src/converter.js +10 -17
  41. package/dist/src/converterFlags.d.ts +9 -8
  42. package/dist/src/converterFlags.js +14 -15
  43. package/dist/src/converterRule.d.ts +4 -3
  44. package/dist/src/converterRule.js +20 -22
  45. package/dist/src/extLink.d.ts +5 -4
  46. package/dist/src/extLink.js +16 -17
  47. package/dist/src/gallery.d.ts +7 -5
  48. package/dist/src/gallery.js +15 -19
  49. package/dist/src/heading.d.ts +12 -10
  50. package/dist/src/heading.js +17 -18
  51. package/dist/src/hidden.d.ts +4 -3
  52. package/dist/src/hidden.js +4 -4
  53. package/dist/src/html.d.ts +17 -15
  54. package/dist/src/html.js +25 -23
  55. package/dist/src/imageParameter.d.ts +5 -4
  56. package/dist/src/imageParameter.js +22 -23
  57. package/dist/src/imagemap.d.ts +7 -5
  58. package/dist/src/imagemap.js +14 -19
  59. package/dist/src/imagemapLink.d.ts +8 -7
  60. package/dist/src/imagemapLink.js +4 -8
  61. package/dist/src/index.d.ts +2 -1
  62. package/dist/src/index.js +48 -46
  63. package/dist/src/link/base.d.ts +5 -4
  64. package/dist/src/link/base.js +26 -28
  65. package/dist/src/link/category.d.ts +1 -1
  66. package/dist/src/link/category.js +3 -2
  67. package/dist/src/link/file.d.ts +5 -4
  68. package/dist/src/link/file.js +22 -20
  69. package/dist/src/link/galleryImage.d.ts +5 -5
  70. package/dist/src/link/galleryImage.js +11 -10
  71. package/dist/src/link/index.d.ts +1 -1
  72. package/dist/src/link/index.js +8 -6
  73. package/dist/src/magicLink.d.ts +3 -2
  74. package/dist/src/magicLink.js +10 -11
  75. package/dist/src/nested.d.ts +4 -2
  76. package/dist/src/nested.js +5 -9
  77. package/dist/src/nowiki/base.d.ts +6 -5
  78. package/dist/src/nowiki/base.js +4 -4
  79. package/dist/src/nowiki/comment.d.ts +6 -5
  80. package/dist/src/nowiki/comment.js +8 -10
  81. package/dist/src/nowiki/dd.d.ts +1 -1
  82. package/dist/src/nowiki/dd.js +3 -2
  83. package/dist/src/nowiki/doubleUnderscore.d.ts +8 -6
  84. package/dist/src/nowiki/doubleUnderscore.js +13 -13
  85. package/dist/src/nowiki/hr.d.ts +2 -8
  86. package/dist/src/nowiki/hr.js +4 -11
  87. package/dist/src/nowiki/index.d.ts +2 -1
  88. package/dist/src/nowiki/index.js +6 -5
  89. package/dist/src/nowiki/list.d.ts +4 -4
  90. package/dist/src/nowiki/list.js +3 -2
  91. package/dist/src/nowiki/noinclude.d.ts +2 -2
  92. package/dist/src/nowiki/noinclude.js +3 -2
  93. package/dist/src/nowiki/quote.d.ts +1 -8
  94. package/dist/src/nowiki/quote.js +25 -17
  95. package/dist/src/onlyinclude.d.ts +5 -4
  96. package/dist/src/onlyinclude.js +13 -13
  97. package/dist/src/paramTag/index.d.ts +5 -3
  98. package/dist/src/paramTag/index.js +7 -7
  99. package/dist/src/paramTag/inputbox.d.ts +3 -2
  100. package/dist/src/paramTag/inputbox.js +6 -5
  101. package/dist/src/parameter.d.ts +11 -10
  102. package/dist/src/parameter.js +29 -30
  103. package/dist/src/pre.d.ts +4 -2
  104. package/dist/src/pre.js +19 -14
  105. package/dist/src/syntax.d.ts +2 -1
  106. package/dist/src/syntax.js +7 -7
  107. package/dist/src/table/base.d.ts +6 -5
  108. package/dist/src/table/base.js +6 -8
  109. package/dist/src/table/index.d.ts +9 -8
  110. package/dist/src/table/index.js +23 -24
  111. package/dist/src/table/td.d.ts +12 -10
  112. package/dist/src/table/td.js +19 -20
  113. package/dist/src/table/tr.d.ts +3 -2
  114. package/dist/src/table/tr.js +4 -3
  115. package/dist/src/table/trBase.d.ts +3 -3
  116. package/dist/src/table/trBase.js +10 -10
  117. package/dist/src/tagPair/ext.d.ts +7 -6
  118. package/dist/src/tagPair/ext.js +5 -17
  119. package/dist/src/tagPair/include.d.ts +5 -5
  120. package/dist/src/tagPair/include.js +4 -5
  121. package/dist/src/tagPair/index.d.ts +7 -7
  122. package/dist/src/tagPair/index.js +13 -19
  123. package/dist/src/transclude.d.ts +10 -9
  124. package/dist/src/transclude.js +41 -46
  125. package/dist/util/lint.js +14 -4
  126. package/dist/util/string.js +21 -20
  127. package/package.json +11 -9
@@ -0,0 +1,136 @@
1
+ {
2
+ "ext": [],
3
+ "html": [
4
+ [
5
+ "b",
6
+ "bdi",
7
+ "del",
8
+ "i",
9
+ "ins",
10
+ "u",
11
+ "font",
12
+ "big",
13
+ "small",
14
+ "sub",
15
+ "sup",
16
+ "h1",
17
+ "h2",
18
+ "h3",
19
+ "h4",
20
+ "h5",
21
+ "h6",
22
+ "cite",
23
+ "code",
24
+ "em",
25
+ "s",
26
+ "strike",
27
+ "strong",
28
+ "tt",
29
+ "var",
30
+ "div",
31
+ "center",
32
+ "blockquote",
33
+ "ol",
34
+ "ul",
35
+ "dl",
36
+ "table",
37
+ "caption",
38
+ "pre",
39
+ "ruby",
40
+ "rb",
41
+ "rp",
42
+ "rt",
43
+ "rtc",
44
+ "p",
45
+ "span",
46
+ "abbr",
47
+ "dfn",
48
+ "kbd",
49
+ "samp",
50
+ "data",
51
+ "time",
52
+ "mark",
53
+ "tr",
54
+ "td",
55
+ "th",
56
+ "q",
57
+ "bdo"
58
+ ],
59
+ [
60
+ "li",
61
+ "dt",
62
+ "dd"
63
+ ],
64
+ [
65
+ "br",
66
+ "wbr",
67
+ "hr",
68
+ "meta",
69
+ "link",
70
+ "img"
71
+ ]
72
+ ],
73
+ "namespaces": {
74
+ "6": "File",
75
+ "10": "Template"
76
+ },
77
+ "nsid": {},
78
+ "parserFunction": [
79
+ {
80
+ "#language": "language",
81
+ "#special": "special",
82
+ "#speciale": "speciale",
83
+ "#tag": "tag",
84
+ "#formatdate": "formatdate",
85
+ "#invoke": "invoke",
86
+ "#while": "while",
87
+ "#dowhile": "dowhile",
88
+ "#loop": "loop",
89
+ "#forargs": "forargs",
90
+ "#fornumargs": "fornumargs",
91
+ "#if": "if",
92
+ "#ifeq": "ifeq",
93
+ "#switch": "switch",
94
+ "#ifexist": "ifexist",
95
+ "#ifexpr": "ifexpr",
96
+ "#iferror": "iferror",
97
+ "#time": "time",
98
+ "#timel": "timel",
99
+ "#expr": "expr",
100
+ "#rel2abs": "rel2abs",
101
+ "#titleparts": "titleparts",
102
+ "#categorytree": "categorytree",
103
+ "#urldecode": "urldecode",
104
+ "#choose": "choose",
105
+ "#var": "var",
106
+ "#varexists": "varexists",
107
+ "#var_final": "var_final",
108
+ "#vardefine": "vardefine",
109
+ "#vardefineecho": "vardefineecho",
110
+ "#widget": "widget",
111
+ "#regex": "regex",
112
+ "#related": "related",
113
+ "#cscore": "cscore"
114
+ },
115
+ [],
116
+ [
117
+ "msg",
118
+ "原始",
119
+ "raw"
120
+ ],
121
+ [
122
+ "替代",
123
+ "subst",
124
+ "安全替代",
125
+ "safesubst"
126
+ ]
127
+ ],
128
+ "doubleUnderscore": [
129
+ [],
130
+ []
131
+ ],
132
+ "protocol": "bitcoin:|ftp://|ftps://|geo:|git://|gopher://|http://|https://|irc://|ircs://|magnet:|mailto:|mms://|news:|nntp://|redis://|sftp://|sip:|sips:|sms:|ssh://|svn://|tel:|telnet://|urn:|worldwind://|xmpp:",
133
+ "interwiki": [],
134
+ "img": {},
135
+ "variants": []
136
+ }
package/dist/index.d.ts CHANGED
@@ -58,5 +58,5 @@ declare interface Parser {
58
58
  isInterwiki(title: string, config?: Config): [string, string] | null;
59
59
  }
60
60
  declare const Parser: Parser;
61
- export default Parser;
61
+ export = Parser;
62
62
  export type * from './internal';
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+ /* eslint n/exports-style: 0 */
3
3
  const fs = require("fs");
4
4
  const path = require("path");
5
5
  /**
@@ -7,13 +7,13 @@ const path = require("path");
7
7
  * @param file 文件名
8
8
  * @param dir 子路径
9
9
  */
10
- const rootRequire = (file, dir = '') => require(`../${file.includes('/') ? '' : dir}${file}`);
10
+ const rootRequire = (file, dir) => require(file.startsWith('/') ? file : `../${file.includes('/') ? '' : dir}${file}`);
11
11
  // eslint-disable-next-line @typescript-eslint/no-redeclare
12
12
  const Parser = {
13
13
  config: 'default',
14
+ MAX_STAGE: 11,
14
15
  conversionTable: new Map(),
15
16
  redirects: new Map(),
16
- MAX_STAGE: 11,
17
17
  warning: true,
18
18
  debugging: false,
19
19
  running: false,
@@ -302,4 +302,4 @@ for (const key in Parser) {
302
302
  }
303
303
  }
304
304
  Object.defineProperties(Parser, def);
305
- exports.default = Parser;
305
+ module.exports = Parser;
@@ -6,7 +6,7 @@ export type * from './src/nowiki/noinclude';
6
6
  export type * from './src/tagPair/include';
7
7
  export type * from './src/nowiki/comment';
8
8
  export type * from './src/atom';
9
- export type * from './src/attribute';
9
+ export type { AttributeToken } from './src/attribute';
10
10
  export type * from './src/attributes';
11
11
  export type * from './src/tagPair/ext';
12
12
  export type * from './src/hidden';
@@ -88,10 +88,10 @@ export declare abstract class AstElement extends AstNode {
88
88
  /**
89
89
  * 还原为wikitext
90
90
  * @browser
91
- * @param selector
91
+ * @param omit 忽略的节点类型
92
92
  * @param separator 子节点间的连接符
93
93
  */
94
- toString(selector?: string, separator?: string): string;
94
+ toString(omit?: Set<string>, separator?: string): string;
95
95
  /**
96
96
  * Linter
97
97
  * @browser
@@ -106,7 +106,6 @@ export declare abstract class AstElement extends AstNode {
106
106
  print(opt?: PrintOpt): string;
107
107
  /**
108
108
  * 保存为JSON
109
- * @browser
110
109
  * @param file 文件名
111
110
  */
112
111
  json(file?: string): unknown;
@@ -123,15 +122,20 @@ export declare abstract class AstElement extends AstNode {
123
122
  */
124
123
  querySelector(selector: string): Token | undefined;
125
124
  /**
126
- * 符合选择器的所有后代节点
127
- * @param selector 选择器
125
+ * 类型选择器
126
+ * @param types
128
127
  */
129
- querySelectorAll(selector: string): Token[];
128
+ getElementByTypes(types: string): Token | undefined;
130
129
  /**
131
130
  * id选择器
132
131
  * @param id id名
133
132
  */
134
133
  getElementById(id: string): Token | undefined;
134
+ /**
135
+ * 符合选择器的所有后代节点
136
+ * @param selector 选择器
137
+ */
138
+ querySelectorAll(selector: string): Token[];
135
139
  /**
136
140
  * 类选择器
137
141
  * @param className 类名之一
@@ -139,9 +143,9 @@ export declare abstract class AstElement extends AstNode {
139
143
  getElementsByClassName(className: string): Token[];
140
144
  /**
141
145
  * 标签名选择器
142
- * @param name 标签名
146
+ * @param tag 标签名
143
147
  */
144
- getElementsByTagName(name: string): Token[];
148
+ getElementsByTagName(tag: string): Token[];
145
149
  /**
146
150
  * 获取某一行的wikitext
147
151
  * @param n 行号
@@ -7,7 +7,7 @@ const string_1 = require("../util/string");
7
7
  const selector_1 = require("../parser/selector");
8
8
  const ranges_1 = require("./ranges");
9
9
  const title_1 = require("./title");
10
- const index_1 = require("../index");
10
+ const Parser = require("../index");
11
11
  const node_1 = require("./node");
12
12
  const lintIgnoredExt = new Set([
13
13
  'nowiki',
@@ -32,6 +32,16 @@ const lintIgnoredExt = new Set([
32
32
  * @param i 待检查的下标
33
33
  */
34
34
  const nth = (str, i) => new ranges_1.Ranges(str.split(',')).applyTo(i + 1).includes(i);
35
+ /**
36
+ * 检测:lang()伪选择器
37
+ * @param node 节点
38
+ * @param node.attributes 节点属性
39
+ * @param regex 语言正则
40
+ */
41
+ const matchesLang = ({ attributes }, regex) => {
42
+ const lang = attributes?.['lang'];
43
+ return typeof lang === 'string' && regex.test(lang);
44
+ };
35
45
  /** 类似HTMLElement */
36
46
  class AstElement extends node_1.AstNode {
37
47
  /** @browser */
@@ -106,7 +116,7 @@ class AstElement extends node_1.AstNode {
106
116
  * @browser
107
117
  * @param separator 子节点间的连接符
108
118
  */
109
- text(separator = '') {
119
+ text(separator) {
110
120
  return (0, string_1.text)(this.childNodes, separator);
111
121
  }
112
122
  /**
@@ -152,11 +162,11 @@ class AstElement extends node_1.AstNode {
152
162
  */
153
163
  insertAt(node, i = this.length) {
154
164
  if (node.contains(this)) {
155
- index_1.default.error('不能插入祖先节点!', node);
165
+ Parser.error('不能插入祖先节点!', node);
156
166
  throw new RangeError('不能插入祖先节点!');
157
167
  }
158
168
  this.verifyChild(i, 1);
159
- const childNodes = [...this.childNodes], e = new Event('insert', { bubbles: true }), j = index_1.default.running ? -1 : childNodes.indexOf(node);
169
+ const childNodes = [...this.childNodes], e = new Event('insert', { bubbles: true }), j = Parser.running ? -1 : childNodes.indexOf(node);
160
170
  if (j === -1) {
161
171
  node.parentNode?.removeChild(node);
162
172
  node.setAttribute('parentNode', this);
@@ -175,10 +185,17 @@ class AstElement extends node_1.AstNode {
175
185
  * @param selector 选择器
176
186
  */
177
187
  closest(selector) {
178
- let { parentNode } = this;
179
- const stack = (0, selector_1.parseSelector)(selector);
188
+ let { parentNode } = this, condition;
189
+ if (/[^a-z\-,\s]/u.test(selector)) {
190
+ const stack = (0, selector_1.parseSelector)(selector);
191
+ condition = /** @implements */ (token) => token.#matchesStack(stack);
192
+ }
193
+ else {
194
+ const types = new Set(selector.split(',').map(str => str.trim()));
195
+ condition = /** @implements */ ({ type }) => types.has(type);
196
+ }
180
197
  while (parentNode) {
181
- if (parentNode.#matchesStack(stack)) {
198
+ if (condition(parentNode)) {
182
199
  return parentNode;
183
200
  }
184
201
  ({ parentNode } = parentNode);
@@ -226,13 +243,13 @@ class AstElement extends node_1.AstNode {
226
243
  /**
227
244
  * 还原为wikitext
228
245
  * @browser
229
- * @param selector
246
+ * @param omit 忽略的节点类型
230
247
  * @param separator 子节点间的连接符
231
248
  */
232
- toString(selector, separator = '') {
233
- return selector && this.matches(selector)
249
+ toString(omit, separator = '') {
250
+ return omit && this.matchesTypes(omit)
234
251
  ? ''
235
- : this.childNodes.map(child => child.toString(selector)).join(separator);
252
+ : this.childNodes.map(child => child.toString(omit)).join(separator);
236
253
  }
237
254
  /**
238
255
  * Linter
@@ -253,19 +270,20 @@ class AstElement extends node_1.AstNode {
253
270
  }
254
271
  return errors;
255
272
  }
273
+ /** @private */
274
+ matchesTypes(types) {
275
+ return types.has(this.type);
276
+ }
256
277
  /**
257
278
  * 以HTML格式打印
258
279
  * @browser
259
280
  * @param opt 选项
260
281
  */
261
282
  print(opt = {}) {
262
- return String(this)
263
- ? `<span class="wpb-${opt.class ?? this.type}">${(0, string_1.print)(this.childNodes, opt)}</span>`
264
- : '';
283
+ return String(this) ? `<span class="wpb-${opt.class ?? this.type}">${(0, string_1.print)(this.childNodes, opt)}</span>` : '';
265
284
  }
266
285
  /**
267
286
  * 保存为JSON
268
- * @browser
269
287
  * @param file 文件名
270
288
  */
271
289
  json(file) {
@@ -391,7 +409,7 @@ class AstElement extends node_1.AstNode {
391
409
  return this.#isProtected() === false;
392
410
  default: {
393
411
  const [t, n] = selector.split('#');
394
- return (!t || t === type || Boolean(index_1.default.typeAliases[type]?.includes(t)))
412
+ return (!t || t === type || Boolean(Parser.typeAliases[type]?.includes(t)))
395
413
  && (!n || n === name);
396
414
  }
397
415
  }
@@ -428,8 +446,7 @@ class AstElement extends node_1.AstNode {
428
446
  throw new SyntaxError('错误的伪选择器用法。请使用形如 ":regex(\'attr, /re/i\')" 的格式。');
429
447
  }
430
448
  try {
431
- const regex = new RegExp(mt[2], mt[3]);
432
- return regex.test(String(this.getAttribute(mt[1].trim())));
449
+ return new RegExp(mt[2], mt[3]).test(String(this.getAttribute(mt[1].trim())));
433
450
  }
434
451
  catch {
435
452
  throw new SyntaxError(`错误的正则表达式:/${mt[2]}/${mt[3]}`);
@@ -483,15 +500,15 @@ class AstElement extends node_1.AstNode {
483
500
  return selector === undefined || this.#matchesStack((0, selector_1.parseSelector)(selector));
484
501
  }
485
502
  /**
486
- * 符合组合选择器的第一个后代节点
487
- * @param stack 解析后的一组选择器
503
+ * 符合条件的第一个后代节点
504
+ * @param condition 条件
488
505
  */
489
- #queryStack(stack) {
506
+ #getElementBy(condition) {
490
507
  for (const child of this.children) {
491
- if (child.#matchesStack(stack)) {
508
+ if (condition(child)) {
492
509
  return child;
493
510
  }
494
- const descendant = child.#queryStack(stack);
511
+ const descendant = child.#getElementBy(condition);
495
512
  if (descendant) {
496
513
  return descendant;
497
514
  }
@@ -503,19 +520,35 @@ class AstElement extends node_1.AstNode {
503
520
  * @param selector 选择器
504
521
  */
505
522
  querySelector(selector) {
506
- return this.#queryStack((0, selector_1.parseSelector)(selector));
523
+ const stack = (0, selector_1.parseSelector)(selector);
524
+ return this.#getElementBy(token => token.#matchesStack(stack));
507
525
  }
508
526
  /**
509
- * 符合组合选择器的所有后代节点
510
- * @param stack 解析后的一组选择器
527
+ * 类型选择器
528
+ * @param types
529
+ */
530
+ getElementByTypes(types) {
531
+ const typeSet = new Set(types.split(',').map(str => str.trim()));
532
+ return this.#getElementBy(({ type }) => typeSet.has(type));
533
+ }
534
+ /**
535
+ * id选择器
536
+ * @param id id名
537
+ */
538
+ getElementById(id) {
539
+ return this.#getElementBy(token => 'id' in token && token.id === id);
540
+ }
541
+ /**
542
+ * 符合条件的所有后代节点
543
+ * @param condition 条件
511
544
  */
512
- #queryStackAll(stack) {
545
+ #getElementsBy(condition) {
513
546
  const descendants = [];
514
547
  for (const child of this.children) {
515
- if (child.#matchesStack(stack)) {
548
+ if (condition(child)) {
516
549
  descendants.push(child);
517
550
  }
518
- descendants.push(...child.#queryStackAll(stack));
551
+ descendants.push(...child.#getElementsBy(condition));
519
552
  }
520
553
  return descendants;
521
554
  }
@@ -524,30 +557,22 @@ class AstElement extends node_1.AstNode {
524
557
  * @param selector 选择器
525
558
  */
526
559
  querySelectorAll(selector) {
527
- return this.#queryStackAll((0, selector_1.parseSelector)(selector));
528
- }
529
- /**
530
- * id选择器
531
- * @param id id名
532
- */
533
- getElementById(id) {
534
- const eid = id.replace(/(?<!\\)"/gu, '\\"');
535
- return this.querySelector(`ext[id="${eid}"], html[id="${eid}"]`);
560
+ const stack = (0, selector_1.parseSelector)(selector);
561
+ return this.#getElementsBy(token => token.#matchesStack(stack));
536
562
  }
537
563
  /**
538
564
  * 类选择器
539
565
  * @param className 类名之一
540
566
  */
541
567
  getElementsByClassName(className) {
542
- return this.querySelectorAll(`[className~="${className.replace(/(?<!\\)"/gu, '\\"')}"]`);
568
+ return this.#getElementsBy(token => 'classList' in token && token.classList.has(className));
543
569
  }
544
570
  /**
545
571
  * 标签名选择器
546
- * @param name 标签名
572
+ * @param tag 标签名
547
573
  */
548
- getElementsByTagName(name) {
549
- const ename = name.replace(/(?<!\\)"/gu, '\\"');
550
- return this.querySelectorAll(`ext[name="${ename}"], html[name="${ename}"]`);
574
+ getElementsByTagName(tag) {
575
+ return this.#getElementsBy(({ type, name }) => name === tag && (type === 'html' || type === 'ext'));
551
576
  }
552
577
  /**
553
578
  * 获取某一行的wikitext
@@ -573,7 +598,7 @@ class AstElement extends node_1.AstNode {
573
598
  #getChildIndex(node) {
574
599
  const i = this.childNodes.indexOf(node);
575
600
  if (i === -1) {
576
- index_1.default.error('找不到子节点!', node);
601
+ Parser.error('找不到子节点!', node);
577
602
  throw new RangeError('找不到子节点!');
578
603
  }
579
604
  return i;
@@ -617,7 +642,7 @@ class AstElement extends node_1.AstNode {
617
642
  console.log(`${indent}\x1B[32m<%s>\x1B[0m${(0, string_1.noWrap)(str)}\x1B[32m</%s>\x1B[0m`, type, type);
618
643
  return;
619
644
  }
620
- index_1.default.info(`${indent}<${type}>`);
645
+ Parser.info(`${indent}<${type}>`);
621
646
  let i = this.getPadding();
622
647
  if (i) {
623
648
  console.log(`${indent} ${(0, string_1.noWrap)(str.slice(0, i))}`);
@@ -642,18 +667,8 @@ class AstElement extends node_1.AstNode {
642
667
  if (i < str.length) {
643
668
  console.log(`${indent} ${(0, string_1.noWrap)(str.slice(i))}`);
644
669
  }
645
- index_1.default.info(`${indent}</${type}>`);
670
+ Parser.info(`${indent}</${type}>`);
646
671
  }
647
672
  }
648
673
  exports.AstElement = AstElement;
649
- /**
650
- * 检测:lang()伪选择器
651
- * @param node 节点
652
- * @param node.attributes 节点属性
653
- * @param regex 语言正则
654
- */
655
- const matchesLang = ({ attributes }, regex) => {
656
- const lang = attributes?.['lang'];
657
- return typeof lang === 'string' && regex.test(lang);
658
- };
659
- index_1.default.classes['AstElement'] = __filename;
674
+ Parser.classes['AstElement'] = __filename;
package/dist/lib/node.js CHANGED
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AstNode = void 0;
4
4
  const assert = require("assert/strict");
5
5
  const EventEmitter = require("events");
6
- const index_1 = require("../index");
6
+ const Parser = require("../index");
7
7
  /**
8
8
  * 定制TypeError消息
9
9
  * @param {Function} Constructor 类
@@ -254,7 +254,7 @@ class AstNode {
254
254
  */
255
255
  isEqualNode(node) {
256
256
  try {
257
- assert.deepStrictEqual(this, node);
257
+ assert.deepEqual(this, node);
258
258
  }
259
259
  catch (e) {
260
260
  if (e instanceof assert.AssertionError) {
@@ -431,8 +431,7 @@ class AstNode {
431
431
  throw new Error('不在同一个语法树!');
432
432
  }
433
433
  const aAncestors = [...this.getAncestors().reverse(), this], bAncestors = [...other.getAncestors().reverse(), other], depth = aAncestors.findIndex((ancestor, i) => bAncestors[i] !== ancestor), commonAncestor = aAncestors[depth - 1], { childNodes } = commonAncestor;
434
- return childNodes.indexOf(aAncestors[depth])
435
- - childNodes.indexOf(bAncestors[depth]);
434
+ return childNodes.indexOf(aAncestors[depth]) - childNodes.indexOf(bAncestors[depth]);
436
435
  }
437
436
  /**
438
437
  * 将行列号转换为字符位置
@@ -455,4 +454,4 @@ class AstNode {
455
454
  }
456
455
  }
457
456
  exports.AstNode = AstNode;
458
- index_1.default.classes['AstNode'] = __filename;
457
+ Parser.classes['AstNode'] = __filename;
package/dist/lib/range.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AstRange = void 0;
4
- const index_1 = require("../index");
4
+ const Parser = require("../index");
5
5
  /**
6
6
  * 计算绝对位置
7
7
  * @param referenceNode 容器
@@ -33,8 +33,7 @@ class AstRange {
33
33
  }
34
34
  /** 起点绝对位置 */
35
35
  get startIndex() {
36
- const { startContainer, startOffset } = this;
37
- return getIndex(startContainer, startOffset);
36
+ return getIndex(this.startContainer, this.startOffset);
38
37
  }
39
38
  /** 终点容器 */
40
39
  get endContainer() {
@@ -46,8 +45,7 @@ class AstRange {
46
45
  }
47
46
  /** 终点绝对位置 */
48
47
  get endIndex() {
49
- const { endContainer, endOffset } = this;
50
- return getIndex(endContainer, endOffset);
48
+ return getIndex(this.endContainer, this.endOffset);
51
49
  }
52
50
  /** 起始和终止位置是否重合 */
53
51
  get collapsed() {
@@ -379,9 +377,8 @@ class AstRange {
379
377
  }
380
378
  /** 范围内的全部文本 */
381
379
  toString() {
382
- const { startContainer, startIndex, endIndex } = this;
383
- return String(startContainer.getRootNode()).slice(startIndex, endIndex);
380
+ return String(this.startContainer.getRootNode()).slice(this.startIndex, this.endIndex);
384
381
  }
385
382
  }
386
383
  exports.AstRange = AstRange;
387
- index_1.default.classes['AstRange'] = __filename;
384
+ Parser.classes['AstRange'] = __filename;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Ranges = exports.Range = void 0;
4
- const index_1 = require("../index");
4
+ const Parser = require("../index");
5
5
  /** 模拟Python的Range对象。除`step`至少为`1`外,允许负数、小数或`end < start`的情形。 */
6
6
  class Range {
7
7
  start;
@@ -122,4 +122,4 @@ class Ranges extends Array {
122
122
  }
123
123
  }
124
124
  exports.Ranges = Ranges;
125
- index_1.default.classes['Ranges'] = __filename;
125
+ Parser.classes['Ranges'] = __filename;
@@ -14,7 +14,7 @@ export declare class AstText extends AstNode {
14
14
  * @browser
15
15
  * @param text 包含文本
16
16
  */
17
- constructor(text?: string);
17
+ constructor(text: string);
18
18
  /**
19
19
  * 输出字符串
20
20
  * @browser
@@ -36,7 +36,7 @@ export declare class AstText extends AstNode {
36
36
  * @browser
37
37
  * @param text 替换的字符串
38
38
  */
39
- replaceData(text?: string): void;
39
+ replaceData(text: string): void;
40
40
  /** 复制 */
41
41
  cloneNode(): AstText;
42
42
  /**
package/dist/lib/text.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AstText = void 0;
4
- const index_1 = require("../index");
4
+ const Parser = require("../index");
5
5
  const node_1 = require("./node");
6
6
  const errorSyntax = /https?:\/\/|\{+|\}+|\[{2,}|\[(?![^[]*\])|(?<=^|\])([^[]*?)\]+|\]{2,}|<\s*\/?([a-z]\w*)/giu, errorSyntaxUrl = /\{+|\}+|\[{2,}|\[(?![^[]*\])|(?<=^|\])([^[]*?)\]+|\]{2,}|<\s*\/?([a-z]\w*)/giu, disallowedTags = [
7
7
  'html',
@@ -69,7 +69,7 @@ class AstText extends node_1.AstNode {
69
69
  * @browser
70
70
  * @param text 包含文本
71
71
  */
72
- constructor(text = '') {
72
+ constructor(text) {
73
73
  super();
74
74
  Object.defineProperties(this, {
75
75
  data: { value: text, writable: false },
@@ -118,7 +118,7 @@ class AstText extends node_1.AstNode {
118
118
  ? 'error'
119
119
  : 'warning';
120
120
  return (char !== 'h' || index > 0) && (char !== '<' || tags.has(tag.toLowerCase())) && {
121
- message: index_1.default.msg('lonely "$1"', char === 'h' ? error : char),
121
+ message: Parser.msg('lonely "$1"', char === 'h' ? error : char),
122
122
  severity,
123
123
  startIndex,
124
124
  endIndex,
@@ -149,7 +149,7 @@ class AstText extends node_1.AstNode {
149
149
  * @browser
150
150
  * @param text 替换的字符串
151
151
  */
152
- replaceData(text = '') {
152
+ replaceData(text) {
153
153
  this.#setData(text);
154
154
  }
155
155
  /** 复制 */
@@ -224,4 +224,4 @@ class AstText extends node_1.AstNode {
224
224
  }
225
225
  }
226
226
  exports.AstText = AstText;
227
- index_1.default.classes['AstText'] = __filename;
227
+ Parser.classes['AstText'] = __filename;
@@ -1,3 +1,4 @@
1
+ import * as Parser from '../index';
1
2
  /** MediaWiki页面标题对象 */
2
3
  export declare class Title {
3
4
  /** @browser */
@@ -17,12 +18,12 @@ export declare class Title {
17
18
  get title(): string;
18
19
  /**
19
20
  * @browser
20
- * @param str 标题(含或不含命名空间前缀)
21
+ * @param title 标题(含或不含命名空间前缀)
21
22
  * @param defaultNs 命名空间
22
23
  * @param decode 是否需要解码
23
24
  * @param selfLink 是否允许selfLink
24
25
  */
25
- constructor(str: string, defaultNs?: number, config?: import("../index").Config, decode?: boolean, selfLink?: boolean);
26
+ constructor(title: string, defaultNs?: number, config?: Parser.Config, decode?: boolean, selfLink?: boolean);
26
27
  /** 完整链接 */
27
28
  toString(): string;
28
29
  /**