wikiparser-node 0.2.0 → 0.2.1

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 (2) hide show
  1. package/README.md +29 -2140
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,2150 +1,39 @@
1
1
  [![npm version](https://badge.fury.io/js/wikiparser-node.svg)](https://www.npmjs.com/package/wikiparser-node)
2
- # 目录
3
- <details>
4
- <summary>展开</summary>
5
2
 
6
- 1. [Parser](#parser)
7
- 1. [方法](#parser.methods)
8
- 1. [parse](#parser.parse)
9
- 2. [isInterwiki](#parser.isinterwiki)
10
- 3. [normalizeTitle](#parser.normalizetitle)
11
- 4. [getTool](#parser.gettool)
12
- 2. [属性](#parser.properties)
13
- 1. [config](#parser.config)
14
- 2. [AstElement](#astelement)
15
- 1. [原型方法](#astelement.prototype.methods)
16
- 2. [实例属性](#astelement.instance.properties)
17
- 3. [原型属性](#astelement.prototype.properties)
18
- 3. [Token](#token)
19
- 1. [原型方法](#token.prototype.methods)
20
- 1. [destroy](#token.destroy)
21
- 2. [getAncestors](#token.getancestors)
22
- 3. [isPlain](#token.isplain)
23
- 4. [toString](#token.tostring)
24
- 5. [text](#token.text)
25
- 6. [safeReplaceWith](#token.safereplacewith)
26
- 7. [setText](#token.settext)
27
- 8. [sections](#token.sections)
28
- 9. [section](#token.section)
29
- 10. [findEnclosingHtml](#token.findenclosinghtml)
30
- 11. [getCategories](#token.getcategories)
31
- 12. [redoQuotes](#token.redoquotes)
32
- 13. [print](#token.print)
33
- 2. [实例属性](#token.instance.properties)
34
- 1. [type](#token.type)
35
- 3. [原型属性](#token.prototype.properties)
36
- 4. [CommentToken](#commenttoken)
37
- 1. [实例属性](#commenttoken.instance.properties)
38
- 1. [closed](#commenttoken.closed)
39
- 5. [ExtToken](#exttoken)
40
- 1. [实例属性](#exttoken.instance.properties)
41
- 1. [selfClosing](#exttoken.selfclosing)
42
- 2. [name](#exttoken.name)
43
- 6. [AttributeToken](#attributetoken)
44
- 1. [原型方法](#attributetoken.prototype.methods)
45
- 1. [hasAttr](#attributetoken.hasattr)
46
- 2. [getAttr](#attributetoken.getattr)
47
- 3. [getAttrNames](#attributetoken.getattrnames)
48
- 4. [hasAttrs](#attributetoken.hasattrs)
49
- 5. [setAttr](#attributetoken.setattr)
50
- 6. [removeAttr](#attributetoken.removeattr)
51
- 7. [toggleAttr](#attributetoken.toggleattr)
52
- 8. [sanitize](#attributetoken.sanitize)
53
- 2. [实例属性](#attributetoken.instance.properties)
54
- 1. [name](#attributetoken.name)
55
- 7. [HeadingToken](#headingtoken)
56
- 1. [原型方法](#headingtoken.prototype.methods)
57
- 1. [setLevel](#headingtoken.setlevel)
58
- 2. [实例属性](#headingtoken.instance.properties)
59
- 1. [name](#headingtoken.name)
60
- 8. [ArgToken](#argtoken)
61
- 1. [原型方法](#argtoken.prototype.methods)
62
- 1. [setName](argtoken.setname)
63
- 2. [setDefault](argtoken.setdefault)
64
- 2. [实例属性](argtoken.instance.properties)
65
- 1. [name](argtoken.name)
66
- 9. [TranscludeToken](#transcludetoken)
67
- 1. [原型方法](#transcludetoken.prototype.methods)
68
- 1. [subst](#transcludetoken.subst)
69
- 2. [safesubst](#transcludetoken.safesubst)
70
- 3. [getAllArgs](#transcludetoken.getallargs)
71
- 4. [getAnonArgs](#transcludetoken.getanonargs)
72
- 5. [getArgs](#transcludetoken.getargs)
73
- 6. [hasArg](#transcludetoken.hasarg)
74
- 7. [getArg](#transcludetoken.getarg)
75
- 8. [removeArg](#transcludetoken.removearg)
76
- 9. [getKeys](#transcludetoken.getkeys)
77
- 10. [getValues](#transcludetoken.getvalues)
78
- 11. [getValue](#transcludetoken.getvalue)
79
- 12. [newAnonArg](#transcludetoken.newanonarg)
80
- 13. [setValue](#transcludetoken.setvalue)
81
- 14. [anonToNamed](#transcludetoken.anontonamed)
82
- 15. [replaceTemplate](#transcludetoken.replacetemplate)
83
- 16. [hasDuplicatedArgs](#transcludetoken.hasduplicatedargs)
84
- 17. [getDuplicatedArgs](#transcludetoken.getduplicatedargs)
85
- 18. [fixDuplication](#transcludetoken.fixduplication)
86
- 19. [escapeTables](#transcludetoken.escapetables)
87
- 2. [实例属性](#transcludetoken.instance.properties)
88
- 1. [name](#transcludetoken.name)
89
- 2. [modifier](#transcludetoken.modifier)
90
- 10. [ParameterToken](#parametertoken)
91
- 1. [原型方法](#parametertoken.prototype.methods)
92
- 1. [getValue](#parametertoken.getvalue)
93
- 2. [setValue](#parametertoken.setvalue)
94
- 3. [rename](#parametertoken.rename)
95
- 2. [实例属性](#parametertoken.instance.properties)
96
- 1. [name](#parametertoken.name)
97
- 2. [anon](#parametertoken.anon)
98
- 11. [HtmlToken](#htmltoken)
99
- 1. [原型方法](#htmltoken.prototype.methods)
100
- 1. [replaceTag](#htmltoken.replacetag)
101
- 2. [findMatchingTag](#htmltoken.findmatchingtag)
102
- 3. [fix](#htmltoken.fix)
103
- 2. [实例属性](#htmltoken.instance.properties)
104
- 1. [name](#htmltoken.name)
105
- 2. [closing](#htmltoken.closing)
106
- 3. [selfClosing](#htmltoken.selfclosing)
107
- 12. [TableToken](#tabletoken)
108
- 1. [原型方法](#tabletoken.prototype.methods)
109
- 1. [getRowCount](#tabletoken.getrowcount)
110
- 2. [getNthRow](#tabletoken.getnthrow)
111
- 3. [getNthCell](#tabletoken.getnthcell)
112
- 4. [getFullRow](#tabletoken.getfullrow)
113
- 5. [getFullCol](#tabletoken.getfullcol)
114
- 6. [formatTableRow](#tabletoken.formattablerow)
115
- 7. [formatTableCol](#tabletoken.formattablecol)
116
- 8. [insertTableRow](#tabletoken.inserttablerow)
117
- 9. [insertTableCol](#tabletoken.inserttablecol)
118
- 10. [insertTableCell](#tabletoken.inserttablecell)
119
- 11. [removeTableRow](#tabletoken.removetablerow)
120
- 12. [removeTableCol](#tabletoken.removetablecol)
121
- 13. [mergeCells](#tabletoken.mergecells)
122
- 14. [splitIntoRows](#tabletoken.splitintorows)
123
- 15. [splitIntoCols](#tabletoken.splitintocols)
124
- 16. [splitIntoCells](#tabletoken.splitintocells)
125
- 17. [replicateTableRow](#tabletoken.replicatetablerow)
126
- 18. [replicateTableCol](#tabletoken.replicatetablecol)
127
- 19. [moveTableRowBefore](#tabletoken.movetablerowbefore)
128
- 20. [moveTableRowAfter](#tabletoken.movetablerowafter)
129
- 21. [moveTableColBefore](#tabletoken.movetablecolbefore)
130
- 22. [moveTableColAfter](#tabletoken.movetablecolafter)
131
- 13. [TdToken](#tdtoken)
132
- 1. [原型属性](#tdtoken.prototype.properties)
133
- 1. [subtype](#tdtoken.subtype)
134
- 2. [rowspan](#tdtoken.rowspan)
135
- 3. [colspan](#tdtoken.colspan)
136
- 14. [DoubleUnderscoreToken](#doubleunderscoretoken)
137
- 1. [实例属性](#doubleunderscoretoken.instance.properties)
138
- 1. [name](#doubleunderscoretoken.name)
139
- 15. [LinkToken](#linktoken)
140
- 1. [原型方法](#linktoken.prototype.methods)
141
- 1. [setTarget](#linktoken.settarget)
142
- 2. [setFragment](#linktoken.setfragment)
143
- 3. [asSelfLink](#linktoken.asselflink)
144
- 4. [setLinkText](#linktoken.setlinktext)
145
- 5. [pipeTrick](#linktoken.pipetrick)
146
- 2. [实例属性](#linktoken.instance.properties)
147
- 1. [name](#linktoken.name)
148
- 2. [selfLink](#linktoken.selflink)
149
- 3. [fragment](#linktoken.fragment)
150
- 4. [interwiki](#linktoken.interwiki)
151
- 16. [CategoryToken](#categorytoken)
152
- 1. [原型方法](#categorytoken.prototype.methods)
153
- 1. [setSortkey](#categorytoken.setsortkey)
154
- 2. [实例属性](#categorytoken.instance.properties)
155
- 1. [sortkey](#categorytoken.sortkey)
156
- 17. [FileToken](#filetoken)
157
- 1. [原型方法](#filetoken.prototype.methods)
158
- 1. [getAllArgs](#filetoken.getallargs)
159
- 2. [getArgs](#filetoken.getargs)
160
- 3. [hasArg](#filetoken.hasarg)
161
- 4. [getArg](#filetoken.getarg)
162
- 5. [removeArg](#filetoken.removearg)
163
- 6. [getKeys](#filetoken.getkeys)
164
- 7. [getValue](#filetoken.getvalue)
165
- 8. [setValue](#filetoken.setvalue)
166
- 2. [原型属性](#filetoken.prototype.properties)
167
- 1. [link](#filetoken.link)
168
- 2. [size](#filetoken.size)
169
- 3. [width](#filetoken.width)
170
- 4. [height](#filetoken.height)
171
- 18. [ImageParameterToken](#imageparametertoken)
172
- 1. [原型方法](#imageparametertoken.prototype.methods)
173
- 1. [getValue](#imageparametertoken.getvalue)
174
- 2. [setValue](#imageparametertoken.setvalue)
175
- 19. [ExtLinkToken](#extlinktoken)
176
- 1. [原型方法](#extlinktoken.prototype.methods)
177
- 1. [setLinkText](#extlinktoken.setlinktext)
178
- 20. [MagicLinkToken](#magiclinktoken)
179
- 1. [原型方法](#magiclinktoken.prototype.methods)
180
- 1. [getUrl](#magiclinktoken.geturl)
181
- 2. [setTarget](#magiclinktoken.settarget)
182
- 2. [原型属性](#magiclinktoken.prototype.properties)
183
- 1. [protocol](#magiclinktoken.protocol)
184
- 21. [ConverterToken](#convertertoken)
185
- 1. [原型方法](#convertertoken.prototype.methods)
186
- 1. [getAllFlags](#convertertoken.getallflags)
187
- 2. [getEffectiveFlags](#convertertoken.geteffectiveflags)
188
- 3. [hasFlag](#convertertoken.hasflag)
189
- 4. [hasEffectiveFlag](#convertertoken.haseffectiveflag)
190
- 5. [removeFlag](#convertertoken.removeflag)
191
- 6. [setFlag](#convertertoken.setflag)
192
- 7. [toggleFlag](#convertertoken.toggleflag)
193
- 2. [原型属性](#convertertoken.prototype.properties)
194
- 1. [noConvert](#convertertoken.noconvert)
195
- 22. [ConverterRuleToken](#converterruletoken)
196
- 1. [原型方法](#converterruletoken.prototype.methods)
197
- 1. [noConvert](#converterruletoken.noconvert)
198
- 2. [setTo](#converterruletoken.setto)
199
- 3. [setVariant](#converterruletoken.setvariant)
200
- 4. [setFrom](#converterruletoken.setfrom)
201
- 5. [makeUnidirectional](#converterruletoken.makeunidirectional)
202
- 6. [makeBidirectional](#converterruletoken.makebidirectional)
203
- 2. [实例属性](#converterruletoken.instance.properties)
204
- 1. [variant](#converterruletoken.variant)
205
- 2. [unidirectional](#converterruletoken.unidirectional)
206
- 3. [bidirectional](#converterruletoken.bidirectional)
207
- 23. [选择器](#选择器)
208
- 1. [type](#selector.type)
209
- 2. [name](#selector.name)
210
- 3. [属性](#selector.attribute)
211
- 4. [伪选择器](#selector.pseudo)
212
- 24. [$ (TokenCollection)](#-tokencollection)
213
- </details>
3
+ # 简介
4
+ wikiparser-node 是一款由 Bhsd 开发的基于 [Node.js](https://nodejs.org/en/) 环境的离线[维基文本](https://www.mediawiki.org/wiki/Wikitext)语法解析器,可以解析绝大部分的维基语法并生成[语法树](https://en.wikipedia.org/wiki/Abstract_syntax_tree),还可以很方便地对语法树进行查询和修改,最后返回修改后的维基文本。语法树的每个节点对应一个仿照 [HTMLElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement) 类设计的类 [Token](https://github.com/bhsd-harry/wikiparser-node/wiki/01.-Token)。
214
5
 
215
- # Parser
216
- 这是解析工具的入口。
6
+ # 使用方法
217
7
 
218
8
  ```js
219
9
  var Parser = require('wikiparser-node');
220
10
  ```
221
11
 
222
- ## 方法<a id="parser.methods"></a>
223
-
224
- **parse**(wikitext: string, include?: boolean = false): [Token](#token)<a id="parser.parse"></a>
225
- - 解析维基文本。
226
-
227
- ```js
228
- var wikitext = '<includeonly>include</includeonly><noinclude>noinclude</noinclude>',
229
- include = Parser.parse(wikitext, true),
230
- noinclude = Parser.parse(wikitext);
231
- assert(include.text() === 'include'); // Token.text()方法只保留有效部分,详见后文Token章节
232
- assert(noinclude.text() === 'noinclude');
233
- ```
234
-
235
- **isInterwiki**(title: string): RegExpMatchArray<a id="parser.isinterwiki"></a>
236
- - 指定的标题是否是跨维基。
237
-
238
- ```js
239
- assert(Boolean(Parser.isInterwiki('zhwiki:首页')));
240
- ```
241
-
242
- **normalizeTitle**(title: string, defaultNs?: number = 0): string<a id="parser.normalizetitle"></a>
243
- - 规范化页面标题。
244
-
245
- ```js
246
- assert(Parser.normalizeTitle('lj', 10) === 'Template:Lj');
247
- ```
248
-
249
- **getTool**(): typeof [$](#-tokencollection)<a id="parser.gettool"></a>
250
- - 加载[批量操作工具](#-tokencollection)。
251
-
252
- ## 属性<a id="parser.properties"></a>
253
-
254
- **config**: string<a id="parser.config"></a>
255
- - 指定解析设置JSON文件的相对或绝对路径。
256
-
257
- ```js
258
- assert(Parser.config === './config/default'); // 这是默认设置的相对路径
259
- ```
260
-
261
- [返回目录](#目录)
262
-
263
- # AstElement
264
- 语法树的节点均为字符串或一个仿 HTMLElement 的类 AstElement,这里仅列举这些方法和属性。
265
-
266
- ## 原型方法<a id="astelement.prototype.methods"></a>
267
- <details>
268
- <summary>展开</summary>
269
-
270
- **isEqualNode**(node: this): boolean
271
- **cloneNode**(): this
272
- **hasAttribute**(key: PropertyKey): boolean
273
- **getAttribute**(key: PropertyKey): string\|undefined
274
- **getAttributeNames**(): string[]
275
- **hasAttributes**(): boolean
276
- **setAttribte**(key: PropertyKey, value: any): this
277
- **removeAttribute**(key: PropertyKey): void
278
- **toggleAttribute**(key: PropertyKey, force?: boolean): void
279
- **hasChildNodes**(): boolean
280
- **contains**(node: this): boolean
281
- **removeChild**(node: this): this
282
- **appendChild**(node: string\|this): string\|this
283
- **append**(...elements: string\|this): void
284
- **insertBefore**(node: string\|this, reference: this): string\|this
285
- **prepend**(...elements: string\|this): void
286
- **replaceChild**(newChild: string\|this, oldChild: this): this
287
- **replaceChildren**(...elements: string\|this): void
288
- **after**(...element: string\|this): void
289
- **before**(...element: string\|this): void
290
- **remove**(): void
291
- **replaceWith**(...elements: string\|this): void
292
- **normalize**(): void
293
- **getRootNode**(): this
294
- **addEventListener**(type: string, listener: (e: event, data: any) => void, options?: {once: boolean}): void
295
- **removeEventListener**(type: string, listener: (e: event, data: any) => void): void
296
- **dispatchEvent**(e: event, data: any): void
297
- **matches**(selector: string): boolean
298
- **comparePosition**(other: this): number
299
- **closest**(selector: string): this\|undefined
300
- **querySelector**(selector: string): this\|undefined
301
- **querySelectorAll**(selector: string): this[]
302
- **getBoundingClientRect**(): {height: number, width: number, top: number, left: number}
303
- **splitText**(i: number, offset: number): string
304
- </details>
305
-
306
- ## 实例属性<a id="astelement.instance.properties"></a>
307
- <details>
308
- <summary>展开</summary>
309
-
310
- **childNodes**: (string\|this)[]
311
- </details>
312
-
313
- ## 原型属性<a id="astelement.prototype.properties"></a>
314
- <details>
315
- <summary>展开</summary>
316
-
317
- **children**: this[]
318
- **firstChild**: string\|this\|undefined
319
- **firstElementChild**: this\|undefined
320
- **lastChild** string\|this\|undefined
321
- **lastElementChild**: this\|undefined
322
- **isConnected**: boolean
323
- **parentNode**: this\|undefined
324
- **parentElement**: this\|undefined
325
- **nextSibling**: string\|this\|undefined
326
- **nextElementSibling**: this\|undefined
327
- **previousSibling**: string\|this\|undefined
328
- **previousElementSibling**: this\|undefined
329
- **hidden**: boolean
330
- **offsetHeight**: number
331
- **offsetWidth**: number
332
- **offsetTop**: number
333
- **offsetLeft**: number
334
- **style**: {top: number, left: number, height: number, width: number, padding: number}
335
- </details>
336
-
337
- [返回目录](#目录)
338
-
339
- # Token
340
- 这是所有解析后的维基文本的基础类。
341
-
342
- ## 原型方法<a id="token.prototype.methods"></a>
343
- <details>
344
- <summary>展开</summary>
345
-
346
- **destroy**(): void<a id="token.destroy"></a>
347
- - 销毁节点,只能对根节点使用。
348
-
349
- **getAncestors**(): Token[]<a id="token.getancestors"></a>
350
- - 获取所有祖先节点。
351
-
352
- ```js
353
- var root = Parser.parse('<ref/>'),
354
- attr = root.querySelector('ext-attr');
355
- assert.deepStrictEqual(attr.getAncestors(), [attr.parentElement, root]);
356
- ```
357
-
358
- **isPlain**(): boolean<a id="token.isplain"></a>
359
- - 是否是基础类(即未拓展的 Token)。
360
-
361
- ```js
362
- var root = Parser.parse(wikitext);
363
- assert(root.isPlain() === true); // 根节点总是基础类
364
- ```
365
-
366
- **toString**(): string<a id="token.tostring"></a>
367
- - 还原为完整的维基文本。
368
-
369
- ```js
370
- var root = Parser.parse(wikitext);
371
- assert(root.toString() === wikitext); // 解析是可逆的
372
- ```
373
-
374
- **text**(): string<a id="token.text"></a>
375
- - 移除不可见的维基文本,包括 HTML 注释、仅用于嵌入的文字(即 `<includeonly>` 或 `<onlyinclude>` 标签内部)、无效的标签属性等。
376
-
377
- ```js
378
- var root = Parser.parse('<!-- a -->{{{||b}}}<br */>');
379
- assert(root.text() === '{{{|}}}<br/>');
380
- ```
381
-
382
- **setText**(text: string, i: number): void<a id="token.settext"></a>
383
- - 只能通过这个方法修改指定位置上的纯字符串节点。
384
-
385
- ```js
386
- var root = Parser.parse('');
387
- root.setText('string', 0);
388
- assert(root.toString() === 'string');
389
- ```
390
-
391
- **safeReplaceWith**(token: Token): void<a id="token.safereplacewith"></a>
392
- - 将节点替换为一个同类节点,相当于[replaceWith](#astelement.prototype.methods),但适用于父节点有规定的子节点顺序时。
393
-
394
- **sections**(): Token\[\]\[\]<a id="token.sections"></a>
395
- - 将页面分割为章节,每个章节对应一个 Token 数组。
396
-
397
- ```js
398
- var root = Parser.parse('a\n==b==\nc\n===d===\n'),
399
- {childNodes} = root,
400
- sections = root.sections();
401
- assert.deepStrictEqual(sections, [childNodes.slice(0, 1), childNodes.slice(1), childNodes.slice(3)]);
402
- ```
403
-
404
- **section**(n: number): Token\[\]<a id="token.section"></a>
405
- - 仅获取指定章节。
406
-
407
- ```js
408
- var root = Parser.parse('a\n==b==\nc\n===d===\n'),
409
- section = root.section(0); // 序言对应的编号为 0
410
- assert.deepStrictEqual(section, [root.firstChild]);
411
- ```
412
-
413
- **findEnclosingHtml**(tag?: string): [Token, Token]<a id="token.findenclosinghtml"></a>
414
- - 搜索包裹当前Token的HTML标签对,不指定`tag`参数时会搜索任意HTML标签。
415
-
416
- ```js
417
- var root = Parser.parse('<p>{{a}}</p>'),
418
- template = root.querySelector('template');
419
- assert.deepStrictEqual(template.findEnclosingHtml('p'), [root.firstChild, root.lastChild]);
420
- ```
421
-
422
- **getCategories**(): [string, string][]<a id="token.getcategories"></a>
423
- - 获取所有分类和对应的排序关键字。
424
-
425
- ```js
426
- var root = Parser.parse('[[category:a|*]]');
427
- assert.deepStrictEqual(root.getCategories(), [['Category:A', '*']]);
428
- ```
429
-
430
- **redoQuotes**(): void<a id="token.redoquotes"></a>
431
- - 经过一系列编辑操作后,重新局部解析`'`。注意这个方法会忽略所有 Token,只解析当前节点的直接纯文本子节点。
432
-
433
- ```js
434
- var root = Parser.parse("'''a'''");
435
- root.lastElementChild.setText("''");
436
- assert(root.toString() === "'''a''");
437
- root.redoQuotes();
438
- assert.deepStrictEqual(root.childNodes, ["'", root.firstElementChild, "a", root.lastElementChild]);
439
- ```
440
- </details>
441
-
442
- ## 实例属性<a id="token.instance.properties"></a>
443
- <details>
444
- <summary>展开</summary>
445
-
446
- **type**: string<a id="token.type"></a>
447
- - 根节点的值为 `root`,其他的基础类节点一般为 `plain`。
448
-
449
- ```js
450
- var root = Parser.parse(wikitext);
451
- assert(root.type === 'root');
452
- ```
453
-
454
- **print**(format?: 'markup'\|'json' = 'markup'): void\|object<a id="token.print"></a>
455
- - 打印解析生成的 AST。
456
-
457
- ```js
458
- var root = Parser.parse("<ref>{{T|<br>\n----\n[[File:F|thumb|''[//example.net]'']]}}</ref>");
459
- root.print();
460
- root.print('json', 'example'); // JSON格式的输出结果将保存至 /printed/example.json 文件
461
- ```
462
- </details>
463
-
464
- ## 原型属性<a id="token.prototype.properties"></a>
465
- <details>
466
- <summary>展开</summary>
467
-
468
- **previousVisibleSibling**: string\|Token\|undefined
469
- **nextVisibleSibling**: string\|Token\|undefined
470
- </details>
471
-
472
- [返回目录](#目录)
473
-
474
- # CommentToken
475
- HTML 注释。
476
-
477
- ## 实例属性<a id="commenttoken.instance.properties"></a>
478
- <details>
479
- <summary>展开</summary>
480
-
481
- **closed**: boolean<a id="commenttoken.closed"></a>
482
- - 是否闭合。
483
-
484
- ```js
485
- var root = Parser.parse('<!-- text'),
486
- comment = root.firstChild;
487
- assert(comment.closed === false);
488
- ```
489
- </details>
490
-
491
- [返回目录](#目录)
492
-
493
- # ExtToken
494
- 扩展标签。这个类同时混合了 [AttributeToken](#attributetoken) 类的方法。
495
-
496
- ## 实例属性<a id="exttoken.instance.properties"></a>
497
- <details>
498
- <summary>展开</summary>
499
-
500
- **selfClosing**: boolean<a id="exttoken.selfclosing"></a>
501
- - 是否自封闭。
502
-
503
- ```js
504
- var root = Parser.parse('<ref/>'),
505
- ref = root.firstChild;
506
- assert(ref.selfClosing === true);
507
- ```
508
-
509
- **name**: string<a id="exttoken.name"></a>
510
- - 小写的标签名。
511
-
512
- ```js
513
- var root = Parser.parse('<REF/>'),
514
- ref = root.firstChild;
515
- assert(ref.name === 'ref');
516
- ```
517
- </details>
518
-
519
- [返回目录](#目录)
520
-
521
- # AttributeToken
522
- 扩展和 HTML 标签及表格的属性。
523
-
524
- ## 原型方法<a id="attributetoken.prototype.methods"></a>
525
- <details>
526
- <summary>展开</summary>
527
-
528
- **hasAttr**(key: string): boolean<a id="attributetoken.hasattr"></a>
529
- - 是否带有指定属性。
530
-
531
- ```js
532
- var root = Parser.parse('<choose uncached before="a"></choose>'),
533
- attr = root.querySelector('ext-attr'); // 扩展标签属性的 type 值为 'ext-attr'
534
- assert(attr.hasAttr('uncached') === true);
535
- assert(attr.hasAttr('before') === true);
536
- ```
537
-
538
- **getAttr**(key: string): string\|boolean<a id="attributetoken.getattr"></a>
539
- - 获取指定属性。
540
-
541
- ```js
542
- var root = Parser.parse('<choose uncached before="a"></choose>'),
543
- attr = root.querySelector('ext-attr');
544
- assert(attr.getAttr('uncached') === true);
545
- assert(attr.getAttr('before') === 'a');
546
- ```
547
-
548
- **getAttrNames**(): string\[\]<a id="attributetoken.getattrnames"></a>
549
- - 获取属性名列表。
550
-
551
- ```js
552
- var root = Parser.parse('<ref name="a"/>'),
553
- attr = root.querySelector('ext-attr');
554
- assert.deepStrictEqual(attr.getAttrNames(), ['name']);
555
- ```
556
-
557
- **hasAttrs**(): boolean<a id="attributetoken.hasattrs"></a>
558
- - 是否带有至少一条属性。
559
-
560
- ```js
561
- var root = Parser.parse('<ref/>'),
562
- attr = root.querySelector('ext-attr');
563
- assert(attr.hasAttrs() === false);
564
- ```
565
-
566
- **setAttr**(key: string, value: string\|boolean): boolean<a id="attributetoken.setattr"></a>
567
- - 设置属性。
568
-
569
- ```js
570
- var root = Parser.parse('<choose></choose>'),
571
- attr = root.querySelector('ext-attr');
572
- assert(attr.setAttr('before', 'a') === true);
573
- assert(attr.setAttr('uncached', true) === true);
574
- assert(root.toString() === '<choose before="a" uncached></choose>');
575
- ```
576
-
577
- **removeAttr**(key: string): void<a id="attributetoken.removeattr"></a>
578
- - 移除指定属性。
579
-
580
- ```js
581
- var root = Parser.parse('<ref name="a"/>'),
582
- attr = root.querySelector('ext-attr');
583
- attr.removeAttr('name');
584
- assert(root.toString() === '<ref/>');
585
- ```
586
-
587
- **toggleAttr**(key: string): void<a id="attributetoken.toggleattr"></a>
588
- - 切换某 Boolean 属性。
589
-
590
- ```js
591
- var root = Parser.parse('<choose uncached></choose>'),
592
- attr = root.querySelector('ext-attr');
593
- attr.toggleAttr('uncached');
594
- assert(root.toString() === '<choose></choose>');
595
- attr.toggleAttr('uncached');
596
- assert(root.toString() === '<choose uncached></choose>');
597
- ```
598
-
599
- **sanitize**(): void<a id="attributetoken.sanitize"></a>
600
- - 清理无效属性。
601
-
602
- ```js
603
- var root = Parser.parse('<p ">'),
604
- attr = root.querySelector('html-attr');
605
- attr.sanitize();
606
- assert(root.toString() === '<p>');
607
- ```
608
- </details>
609
-
610
- ## 实例属性<a id="attributetoken.instance.properties"></a>
611
- <details>
612
- <summary>展开</summary>
613
-
614
- **name**: string<a id="attributetoken.name"></a>
615
- - 小写的标签名。
616
-
617
- ```js
618
- var root = Parser.parse('<REF/>'),
619
- attr = root.querySelector('ext-attr'); // 即使没有设置属性,扩展和 HTML 标签的第一个子节点也总是 AttributeToken
620
- assert(attr.name === 'ref');
621
- ```
622
- </details>
623
-
624
- [返回目录](#目录)
625
-
626
- # HeadingToken
627
- 章节标题。
628
-
629
- ## 原型方法<a id="headingtoken.prototype.methods"></a>
630
- <details>
631
- <summary>展开</summary>
632
-
633
- **setLevel**(n: number): void<a id="headingtoken.setlevel"></a>
634
- - 修改标题层级。
635
-
636
- ```js
637
- var root = Parser.parse('==a=='),
638
- header = root.firstChild;
639
- header.setLevel(3);
640
- assert(root.toString() === '===a===');
641
- ```
642
- </details>
643
-
644
- ## 实例属性<a id="headingtoken.instance.properties"></a>
645
- <details>
646
- <summary>展开</summary>
647
-
648
- **name**: string<a id="headingtoken.name"></a>
649
- - 字符串格式的标题层级。
650
-
651
- ```js
652
- var root = Parser.parse('==a=='),
653
- header = root.firstChild;
654
- assert(header.name === '2');
655
- ```
656
- </details>
657
-
658
- [返回目录](#目录)
659
-
660
- # ArgToken
661
- 被 `{{{}}}` 包裹的模板参数。
662
-
663
- ## 原型方法<a id="argtoken.prototype.methods"></a>
664
- <details>
665
- <summary>展开</summary>
666
-
667
- **setName**(name: any): void<a id="argtoken.setname"></a>
668
- - 修改参数名。
669
-
670
- ```js
671
- var root = Parser.parse('{{{a}}}'),
672
- arg = root.firstChild;
673
- arg.setName('b');
674
- assert(root.toString() === '{{{b}}}');
675
- ```
676
-
677
- **setDefault**(value: any): void<a id="argtoken.setdefault"></a>
678
- - 设置或修改参数预设值。
679
-
680
- ```js
681
- var root = Parser.parse('{{{a}}}'),
682
- arg = root.firstChild;
683
- arg.setDefault('b');
684
- assert(root.toString() === '{{{a|b}}}');
685
- ```
686
- </details>
687
-
688
- ## 实例属性<a id="argtoken.instance.properties"></a>
689
- <details>
690
- <summary>展开</summary>
691
-
692
- **name**: string<a id="argtoken.name"></a>
693
- - 参数名。
694
-
695
- ```js
696
- var root = Parser.parse('{{{a}}}'),
697
- arg = root.firstChild;
698
- assert(arg.name === 'a');
699
- ```
700
- </details>
701
-
702
- [返回目录](#目录)
703
-
704
- # TranscludeToken
705
- 模板或魔术字。
706
-
707
- ## 原型方法<a id="transcludetoken.prototype.methods"></a>
708
- <details>
709
- <summary>展开</summary>
710
-
711
- **subst**(): void<a id="transcludetoken.subst"></a>
712
- **safesubst**(): void<a id="transcludetoken.safesubst"></a>
713
- - 将引用方式修改为替换引用。
714
-
715
- ```js
716
- var root = Parser.parse('{{a}}{{!}}'),
717
- template = root.firstChild,
718
- magicWord = root.lastChild;
719
- template.subst();
720
- magicWord.safesubst();
721
- assert(root.toString() === '{{subst:a}}{{safesubst:!}}');
722
- ```
723
-
724
- **getAllArgs**(): [ParameterToken](#parametertoken)[]<a id="transcludetoken.getallargs"></a>
725
- - 获取所有参数。
726
-
727
- ```js
728
- var root = Parser.parse('{{a|b|c=1}}{{#invoke:d|e|f|g=1}}'),
729
- template = root.firstChild,
730
- invoke = root.lastChild;
731
- assert.deepStrictEqual(template.getAllArgs(), template.children.slice(1));
732
- assert.deepStrictEqual(invoke.getAllArgs(), invoke.children.slice(3));
733
- ```
734
-
735
- **getAnonArgs**(): [ParameterToken](#parametertoken)[]<a id="transcludetoken.getanonargs"></a>
736
- - 获取所有匿名参数。
737
-
738
- ```js
739
- var root = Parser.parse('{{a|b|c=1}}{{#invoke:d|e|f|g=1}}{{#if:x|y|z}}'),
740
- [template, invoke, magicWord] = root.children;
741
- assert.deepStrictEqual(template.getAnonArgs(), template.children.slice(1, 2));
742
- assert.deepStrictEqual(invoke.getAnonArgs(), invoke.children.slice(3, 4));
743
- assert.deepStrictEqual(magicWord.getAnonArgs(), magicWord.children.slice(1)); // 除#invoke外的魔术字的参数总是视为匿名参数
744
- ```
745
-
746
- **getArgs**(key: string\|number): Set\<[ParameterToken](#parametertoken)><a id="transcludetoken.getargs"></a>
747
- - 获取指定名称的参数(含重复),注意顺序可能不固定。
748
-
749
- ```js
750
- var root = Parser.parse('{{a|b|1=c}}{{#invoke:d|e|f|1=g}}'),
751
- template = root.firstChild,
752
- invoke = root.lastChild;
753
- assert.deepStrictEqual(template.getArgs(1), new Set(template.children.slice(1)));
754
- assert.deepStrictEqual(invoke.getArgs(1), new Set(invoke.children.slice(3)));
755
- ```
756
-
757
- **hasArg**(key: string\|number): boolean<a id="transcludetoken.hasarg"></a>
758
- - 是否带有指定参数。
759
-
760
- ```js
761
- var root = Parser.parse('{{a|b|c=1}}{{#invoke:d|e|f|g=1}}'),
762
- template = root.firstChild,
763
- invoke = root.lastChild;
764
- assert(template.hasArg(1) === true);
765
- assert(template.hasArg('c') === true);
766
- assert(invoke.hasArg(1) === true);
767
- assert(invoke.hasArg('g') === true);
768
- ```
769
-
770
- **getArg**(key: string\|number): [ParameterToken](#parametertoken)<a id="transcludetoken.getarg"></a>
771
- - 获取指定名称的有效参数(即最后一个)。
772
-
773
- ```js
774
- var root = Parser.parse('{{a|b|1=c}}{{#invoke:d|e|1=f|g}}'),
775
- template = root.firstChild,
776
- invoke = root.lastChild;
777
- assert(template.getArg(1) === template.lastChild);
778
- assert(invoke.getArg(1) === invoke.lastChild);
779
- ```
780
-
781
- **removeArg**(key: string\|number): void<a id="transcludetoken.removearg"></a>
782
- - 移除指定名称的参数(含重复)。
783
-
784
- ```js
785
- var root = Parser.parse('{{a|b|1=c}}{{#invoke:d|e|f|1=g}}'),
786
- template = root.firstChild,
787
- invoke = root.lastChild;
788
- template.removeArg(1);
789
- invoke.removeArg(1);
790
- assert(root.toString() === '{{a}}{{#invoke:d|e}}');
791
- ```
792
-
793
- **getKeys**(): string[]<a id="transcludetoken.getkeys"></a>
794
- - 获取所有参数名。
795
-
796
- ```js
797
- var root = Parser.parse('{{a|b=1|c=2}}{{#invoke:d|e|f=1|g=2}}'),
798
- template = root.firstChild,
799
- invoke = root.lastChild;
800
- assert.deepStrictEqual(template.getKeys(), ['b', 'c']);
801
- assert.deepStrictEqual(invoke.getKeys(), ['f', 'g']);
802
- ```
803
-
804
- **getValues**(key: string\|number): string[]<a id="transcludetoken.getvalues"></a>
805
- - 获取指定名称的参数值(含重复)。
806
-
807
- ```js
808
- var root = Parser.parse('{{a|b|1=c}}{{#invoke:d|e|f|1=g}}'),
809
- template = root.firstChild,
810
- invoke = root.lastChild;
811
- assert.deepStrictEqual(template.getValues(1), ['b', 'c']);
812
- assert.deepStrictEqual(invoke.getValues(1), ['f', 'g']);
813
- ```
814
-
815
- **getValue**(key: string\|number): string<a id="transcludetoken.getvalue"></a>
816
- - 获取指定名称的有效参数值(即最后一个)。
817
-
818
- ```js
819
- var root = Parser.parse('{{a|b|1= c }}{{#invoke:d|e|1=f| g }}'),
820
- template = root.firstChild,
821
- invoke = root.lastChild;
822
- assert(template.getValue(1) === 'c'); // 模板的命名参数不保留首尾的空白字符
823
- assert(invoke.getValue(1) === ' g '); // #invoke魔术字保留匿名参数首位的空白字符
824
- ```
825
-
826
- **newAnonArg**(val: any): void<a id="transcludetoken.newanonarg"></a>
827
- - 在末尾添加新的匿名参数。
828
-
829
- ```js
830
- var root = Parser.parse('{{a|b}}{{#invoke:d|e}}'),
831
- template = root.firstChild,
832
- invoke = root.lastChild;
833
- template.newAnonArg(' c ');
834
- invoke.newAnonArg(' f ');
835
- assert(root.toString() === '{{a|b| c }}{{#invoke:d|e| f }}');
836
- ```
837
-
838
- **setValue**(key: string, value: any): void<a id="transcludetoken.setvalue"></a>
839
- - 修改或新增参数。
840
-
841
- ```js
842
- var root = Parser.parse('{{a|b}}{{#invoke:e|f|g=3}}'),
843
- template = root.firstChild,
844
- invoke = root.lastChild;
845
- template.setValue('1', ' c ');
846
- template.setValue('d', ' 2 ');
847
- invoke.setValue('g', ' 4 ');
848
- assert(root.toString() === '{{a| c |d= 2 }}{{#invoke:e|f|g= 4 }}');
849
- ```
850
-
851
- **anonToNamed**(): void<a id="transcludetoken.anontonamed"></a>
852
- - 将所有匿名参数修改为对应的命名参数。
853
-
854
- ```js
855
- var root = Parser.parse('{{a| b | c }}{{#invoke:d|e| f }}'),
856
- template = root.firstChild,
857
- invoke = root.lastChild;
858
- template.anonToNamed();
859
- invoke.anonToNamed();
860
- assert(root.toString() === '{{a|1= b |2= c }}{{#invoke:d|e|1= f }}'); // 注意改成命名参数后会参数值的首尾空白字符失效
861
- ```
862
-
863
- **replaceTemplate**(title: string): void<a id="transcludetoken.replacetemplate"></a>
864
- - 更换模板,但保留参数。
865
-
866
- ```js
867
- var root = Parser.parse('{{a|b|c=1}}'),
868
- template = root.firstChild;
869
- template.replaceTemplate('aa');
870
- assert(root.toString() === '{{aa|b|c=1}}');
871
- ```
872
-
873
- **hasDuplicatedArgs**(): number<a id="transcludetoken.hasduplicatedargs"></a>
874
- - 重复参数计数。
875
-
876
- ```js
877
- var root = Parser.parse('{{a||1=}}'),
878
- template = root.firstChild;
879
- assert(template.hasDuplicatedArgs() === 1);
880
- ```
881
-
882
- **getDuplicatedArgs**(): \[string, Set\<[ParameterToken](#parametertoken)>][]<a id="transcludetoken.getduplicatedargs"></a>
883
- - 获取全部重复参数,注意顺序可能不固定。
884
-
885
- ```js
886
- var root = Parser.parse('{{a||1=}}'),
887
- template = root.firstChild;
888
- assert.deepStrictEqual(template.getDuplicatedArgs(), [['1', new Set(template.getAllArgs())]]);
889
- ```
890
-
891
- **fixDuplication**(): string[]<a id="transcludetoken.fixduplication"></a>
892
- - 尝试修复重复参数,返回值为无法修复的参数名列表。
893
-
894
- ```js
895
- var root = Parser.parse('{{a|b|1=|1=c}}'),
896
- template = root.firstChild;
897
- assert.deepStrictEqual(template.fixDuplication(), ['1']);
898
- assert(root.toString() === '{{a|b|1=c}}');
899
- ```
900
-
901
- **escapeTables**(): this<a id="transcludetoken.escapetables"></a>
902
- - 如果内部包含疑似未转义的表格语法且因此造成了重复参数,则对这些表格进行转义。
903
-
904
- ```js
905
- var root = Parser.parse('{{a|b=c\n{|\n|rowspan=2|d\n|rowspan=2|e\n|}}}'),
906
- template = root.firstChild;
907
- template.escapeTables();
908
- assert(root.toString() === '{{a|b=c\n{{(!}}\n{{!}}rowspan=2{{!}}d\n{{!}}rowspan=2{{!}}e\n{{!}}}}}');
909
- ```
910
- </details>
911
-
912
- ## 实例属性<a id="transcludetoken.instance.properties"></a>
913
- <details>
914
- <summary>展开</summary>
915
-
916
- **name**: string<a id="transcludetoken.name"></a>
917
- - 模板名(含名字空间)或魔术字。
918
-
919
- ```js
920
- var root = Parser.parse('{{a}}{{!}}'),
921
- template = root.firstChild,
922
- magicWord = root.lastChild;
923
- assert(template.name === 'Template:A');
924
- assert(magicWord.name === '!');
925
- ```
926
-
927
- **modifier**: string<a id="transcludetoken.modifier"></a>
928
- - subst 和 safesubst 等。
929
-
930
- ```js
931
- var root = Parser.parse('<includeonly>{{subst:REVISIONUSER}}</includeonly>', true),
932
- magicWord = root.querySelector('magic-word');
933
- assert(magicWord.modifier === 'subst');
934
- ```
935
- </details>
936
-
937
- [返回目录](#目录)
938
-
939
- # ParameterToken
940
- 模板或魔术字的参数。
941
-
942
- ## 原型方法<a id="parametertoken.prototype.methods"></a>
943
- <details>
944
- <summary>展开</summary>
12
+ 更多文档请查阅 [Wiki](https://github.com/bhsd-harry/wikiparser-node/wiki)。
945
13
 
946
- **getValue**(): string<a id="parametertoken.getvalue"></a>
947
- - 获取参数值。
948
-
949
- ```js
950
- var root = Parser.parse('{{a| b | c = 1 }}'),
951
- [anonymous, named] = root.querySelectorAll('parameter');
952
- assert(anonymous.getValue() === ' b '); // 模板的匿名参数保留首尾的空白字符
953
- assert(named.getValue() === '1'); // 模板的命名参数不保留首尾的空白字符
954
- ```
955
-
956
- **setValue**(value: any): void<a id="parametertoken.setvalue"></a>
957
- - 设置参数值。
958
-
959
- ```js
960
- var root = Parser.parse('{{a|b=1}}'),
961
- param = root.querySelector('parameter');
962
- param.setValue(' 2 ');
963
- assert(root.toString() === '{{a|b= 2 }}'); // setValue方法总是保留空白字符,哪怕是无效的
964
- ```
965
-
966
- **rename**(key: string, force?: boolean = false): void<a id="parametertoken.rename"></a>
967
- - 重命名参数,可选是否在导致重复参数时抛出错误。
968
-
969
- ```js
970
- var root = Parser.parse('{{a|b=1|c=2}}'),
971
- param = root.querySelector('parameter');
972
- try {
973
- param.rename('c');
974
- throw new Error();
975
- } catch (e) {
976
- assert(e.message === '参数更名造成重复参数:c');
977
- }
978
- ```
979
- </details>
980
-
981
- ## 实例属性<a id="parametertoken.instance.properties"></a>
982
- <details>
983
- <summary>展开</summary>
984
-
985
- **name**: string<a id="parametertoken.name"></a>
986
- - 参数名。
987
-
988
- ```js
989
- var root = Parser.parse('{{a|b| c = 1}}'),
990
- [anonymous, named] = root.querySelectorAll('parameter');
991
- assert(anonymous.name === '1');
992
- assert(named.name === 'c');
993
- ```
994
-
995
- **anon**: boolean<a id="parametertoken.anon"></a>
996
- - 是否是匿名参数。
997
-
998
- ```js
999
- var root = Parser.parse('{{a|b| c = 1}}'),
1000
- [anonymous, named] = root.querySelectorAll('parameter');
1001
- assert(anonymous.anon === true);
1002
- assert(named.anon === false);
1003
- ```
1004
- </details>
1005
-
1006
- [返回目录](#目录)
1007
-
1008
- # HtmlToken
1009
- HTML标签,未进行匹配。这个类同时混合了 [AttributeToken](#attributetoken) 类的方法。
1010
-
1011
- ## 原型方法<a id="htmltoken.prototype.methods"></a>
1012
- <details>
1013
- <summary>展开</summary>
1014
-
1015
- **replaceTag**(tag: string): void<a id="htmltoken.replacetag"></a>
1016
- - 修改标签。
1017
-
1018
- ```js
1019
- var root = Parser.parse('<b>'),
1020
- html = root.firstChild;
1021
- html.replaceTag('i');
1022
- assert(root.toString() === '<i>');
1023
- ```
1024
-
1025
- **findMatchingTag**(): HtmlToken<a id="htmltoken.findmatchingtag"></a>
1026
- - 搜索匹配的另一个标签,找不到或无效自封闭时会抛出不同错误。
1027
-
1028
- ```js
1029
- var root = Parser.parse('<p><b/></i><u></u><br>'),
1030
- [p, b, i, u, u2, br] = root.children;
1031
- try {
1032
- p.findMatchingTag();
1033
- throw new Error();
1034
- } catch (e) {
1035
- assert(e.message === '未闭合的标签:<p>');
1036
- }
1037
- try {
1038
- b.findMatchingTag();
1039
- throw new Error();
1040
- } catch (e) {
1041
- assert(e.message === '无效自封闭标签:<b/>');
1042
- }
1043
- try {
1044
- i.findMatchingTag();
1045
- throw new Error();
1046
- } catch (e) {
1047
- assert(e.message === '未匹配的闭合标签:</i>');
1048
- }
1049
- assert(u.findMatchingTag() === u2);
1050
- assert(br.findMatchingTag() === br);
1051
- ```
1052
-
1053
- **fix**(): void<a id="htmltoken.fix"></a>
1054
- - 尝试修复无效自封闭标签,无法修复时会抛出错误。
1055
-
1056
- ```js
1057
- var root = Parser.parse('<b>a<b/><div style="height:1em"/>');
1058
- for (const html of root.querySelectorAll('html')) {
1059
- html.fix();
1060
- }
1061
- assert(root.toString() === '<b>a</b><div style="height:1em"></div>');
1062
- ```
1063
- </details>
1064
-
1065
- ## 实例属性<a id="htmltoken.instance.properties"></a>
1066
- <details>
1067
- <summary>展开</summary>
1068
-
1069
- **name**: string<a id="htmltoken.name"></a>
1070
- - 小写的标签名。
1071
-
1072
- ```js
1073
- var root = Parser.parse('<b>'),
1074
- html = root.firstChild;
1075
- assert(html.name === 'b');
1076
- ```
1077
-
1078
- **closing**: boolean<a id="htmltoken.closing"></a>
1079
- - 是否是闭合标签。
1080
-
1081
- ```js
1082
- var root = Parser.parse('</b>'),
1083
- html = root.firstChild;
1084
- assert(html.closing === true);
1085
- ```
1086
-
1087
- **selfClosing**: boolean<a id="htmltoken.selfclosing"></a>
1088
- - 是否是自闭合标签(可能不符合HTML5规范)。
1089
-
1090
- ```js
1091
- var root = Parser.parse('<b/>'),
1092
- html = root.firstChild;
1093
- assert(html.selfClosing === true);
1094
- ```
1095
- </details>
1096
-
1097
- [返回目录](#目录)
1098
-
1099
- # TableToken
1100
- 表格。
1101
-
1102
- ## 原型方法<a id="tabletoken.prototype.methods"></a>
1103
- <details>
1104
- <summary>展开</summary>
1105
-
1106
- **getRowCount**(): number<a id="tabletoken.getrowcount"></a>
1107
- - 表格的有效行数。
1108
-
1109
- ```js
1110
- var root = Parser.parse('{|\n|\n|-\n|}'),
1111
- table = root.firstChild;
1112
- assert(table.getRowCount() === 1); // 表格首行可以直接写在 TableToken 下,没有内容的表格行是无效行。
1113
- ```
1114
-
1115
- **getNthRow**(n: number): TrToken<a id="tabletoken.getnthrow"></a>
1116
- - 第 `n` 个有效行。
1117
-
1118
- ```js
1119
- var root = Parser.parse('{|\n|rowspan=2| ||\n|-\n|\n|}'),
1120
- table = root.firstChild;
1121
- assert(table.getNthRow(0) === table);
1122
- assert(table.getNthRow(1) === table.querySelector('tr'));
1123
- ```
1124
-
1125
- |表格示意|
1126
- |:-:|
1127
- |<table><tr><td rowspan=2>第 0 行</td><td>第 0 行</td></tr><tr><td>第 1 行</td></tr></table>|
1128
-
1129
- **getNthCell**(coords: {row: number, column: number}\|{x: number, y: number}): [TdToken](#tdtoken)<a id="tabletoken.getnthcell"></a>
1130
- - 获取指定位置的单元格,指定位置可以基于 raw HTML 或渲染后的表格。
1131
-
1132
- ```js
1133
- var root = Parser.parse('{|\n|rowspan=2 colspan=2| ||\n|-\n|\n|-\n| || ||\n|}'),
1134
- table = root.firstChild,
1135
- td = table.querySelector('td');
1136
- assert(table.getNthCell({row: 0, column: 0}) === td);
1137
- assert(table.getNthCell({x: 1, y: 1}) === td); // 该单元格跨了 2 行 2 列
1138
- ```
1139
-
1140
- |(row, column)|(y, x)|
1141
- |:-:|:-:|
1142
- |<table><tr><td rowspan=2 colspan=2 align="center">(0, 0)</td><td>(0, 1)</td></tr><tr><td>(1, 0)</td></tr><tr><td>(2, 0)</td><td>(2, 1)</td><td>(2, 2)</td></table>|<table><tr><td rowspan=2 colspan=2 align="center">(0, 0), (0, 1)<br>(1, 0), (1, 1)</td><td>(0, 2)</td></tr><tr><td>(1, 2)</td></tr><tr><td>(2, 0)</td><td>(2, 1)</td><td>(2, 2)</td></tr></table>|
1143
-
1144
- **getFullRow**(y: number): Map\<[TdToken](#tdtoken), boolean><a id="tabletoken.getfullrow"></a>
1145
- - 获取一行的所有单元格。
1146
-
1147
- ```js
1148
- var root = Parser.parse('{|\n|rowspan=2| ||\n|-\n|\n|}'),
1149
- table = root.firstChild,
1150
- [a,, c] = table.querySelectorAll('td');
1151
- assert.deepStrictEqual(table.getFullRow(1), new Map([[a, false], [c, true]])); // 起始位置位于该行的单元格值为 `true`
1152
- ```
1153
-
1154
- |表格|选中第 1 行|
1155
- |:-:|:-:|
1156
- |<table><tr><td rowspan=2>&nbsp;</td><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr></table>|<table><tr><td rowspan=2>`false`</td><td>&nbsp;</td></tr><tr><td>`true`</td></tr></table>|
1157
-
1158
- **getFullCol**(x: number): Map\<[TdToken](#tdtoken), boolean><a id="tabletoken.getfullcol"></a>
1159
- - 获取一列的所有单元格。
1160
-
1161
- ```js
1162
- var root = Parser.parse('{|\n|colspan=2|\n|-\n| ||\n|}'),
1163
- table = root.firstChild,
1164
- [a,, c] = table.querySelectorAll('td');
1165
- assert.deepStrictEqual(table.getFullCol(1), new Map([[a, false], [c, true]])); // 起始位置位于该列的单元格值为 `true`
1166
- ```
1167
-
1168
- |表格|选中第 1 列|
1169
- |:-:|:-:|
1170
- |<table><tr><td colspan=2>&nbsp;</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td></tr></table>|<table><tr><td colspan=2 align="center">`false`</td></tr><tr><td>&nbsp;</td><td>`true`</td></tr></table>|
1171
-
1172
- **formatTableRow**(y: number, attr: string\|Record\<string, string\|boolean>, multiRow?: boolean = false): void<a id="tabletoken.formattablerow"></a>
1173
- - 批量设置一行单元格的属性。
1174
-
1175
- ```js
1176
- var root = Parser.parse('{|\n|rowspan=2| ||\n|-\n|\n|}'),
1177
- table = root.firstChild;
1178
- table.formatTableRow(1, 'th'); // `multiRow` 为假时忽略起始位置不在该行的单元格
1179
- assert(root.toString() === '{|\n|rowspan=2| ||\n|-\n!\n|}');
1180
- ```
1181
-
1182
- |原表格|将第 1 行设为`<th>`<br>`multiRow = false`|将第 1 行设为`<th>`<br>`multiRow = true`|
1183
- |:-:|:-:|:-:|
1184
- |<table><tr><td rowspan=2 align="center">td</td><td>td</td></tr><tr><td>td</td></tr></table>|<table><tr><td rowspan=2>td</td><td>td</td></tr><tr><th>th</th></tr></table>|<table><tr><th rowspan=2>th</th><td>td</td></tr><tr><th>th</th></tr></table>|
1185
-
1186
- **formatTableCol**(x: number, attr: string\|Record\<string, string\|boolean>, multiCol?: boolean = false): void<a id="tabletoken.formattablecol"></a>
1187
- - 批量设置一列单元格的属性。
1188
-
1189
- ```js
1190
- var root = Parser.parse('{|\n|colspan=2|\n|-\n| ||\n|}'),
1191
- table = root.firstChild;
1192
- table.formatTableCol(1, 'th', true); // `multiCol` 为真时包含起始位置不在该列的单元格
1193
- assert(root.toString() === '{|\n!colspan=2|\n|-\n| \n!\n|}');
1194
- ```
1195
-
1196
- |原表格|将第 1 列设为`<th>`<br>`multiCol = false`|将第 1 列设为`<th>`<br>`multiCol = true`|
1197
- |:-:|:-:|:-:|
1198
- |<table><tr><td colspan=2>td</td></tr><tr><td>td</td><td>td</td></tr></table>|<table><tr><td colspan=2>td</td></tr><tr><td>td</td><th>th</th></tr></table>|<table><tr><th colspan=2>th</th></tr><tr><td>td</td><th>th</th></tr></table>|
1199
-
1200
- **insertTableRow**(row: number, attr: Record\<string, string\|boolean>, inner?: string, subtype?: 'td'\|'th' = 'td', innerAttr?: Record\<string, string\|boolean>): TrToken<a id="tabletoken.inserttablerow"></a>
1201
- - 插入空行或一行单元格。
1202
-
1203
- ```js
1204
- var root = Parser.parse('{|\n|a||rowspan=2|b||c\n|-\n|d||e\n|}'),
1205
- table = root.firstChild;
1206
- table.insertTableRow(1, {class: 'tr'}); // 不填写 `inner` 等后续参数时会插入一个空行,且此时是无效行。
1207
- table.insertTableRow(1, {}, 'f', 'th', {class: 'th'});
1208
- assert(root.toString() === '{|\n|a|| rowspan="3"|b||c\n|- class="tr"\n|-\n! class="th"|f\n! class="th"|f\n|-\n|d||e\n|}');
1209
- ```
1210
-
1211
- |原表格|插入 1 行|
1212
- |:-:|:-:|
1213
- |<table><tr><td>a</td><td rowspan=2>b</td><td>c</td></tr><tr><td>d</td><td>e</td></tr></table>|<table><tr><td>a</td><td rowspan=3>b</td><td>c</td></tr><tr><th>f</th><th>f</th></tr><tr><td>d</td><td>e</td></tr></table>|
1214
-
1215
- **insertTableCol**(x: number, inner: string, subtype?: 'td'\|'th' = 'td', attr?: Record\<string, string\|boolean>): void<a id="tabletoken.inserttablecol"></a>
1216
- - 插入一列单元格。
1217
-
1218
- ```js
1219
- var root = Parser.parse('{|\n|colspan=2|a\n|-\n|b||c\n|}'),
1220
- table = root.firstChild;
1221
- table.insertTableCol(1, 'd', 'th', {class: 'th'});
1222
- assert(root.toString() === '{|\n| colspan="3"|a\n|-\n|b\n! class="th"|d\n|c\n|}');
1223
- ```
1224
-
1225
- |原表格|插入 1 列|
1226
- |:-:|:-:|
1227
- |<table><tr><td colspan=2 align="center">a</td></tr><tr><td>b</td><td>c</td></tr></table>|<table><tr><td colspan=3 align="center">a</td></tr><tr><td>b</td><th>d</th><td>c</td></tr></table>|
1228
-
1229
- **insertTableCell**(inner: string, coords: {row: number, column: number}\|{x: number, y: number}, subtype?: 'td'\|'th' = 'td', attr?: Record\<string, string\|boolean>): [TdToken](#tdtoken)<a id="tabletoken.inserttablecell"></a>
1230
- - 插入一个单元格。
1231
-
1232
- ```js
1233
- var root = Parser.parse('{|\n|rowspan=2 colspan=2|a||b\n|-\n|c\n|-\n|d||e||f\n|}'),
1234
- table = root.firstChild;
1235
- table.insertTableCell('g', {row: 0, column: 2}, 'th');
1236
- table.insertTableCell('h', {x: 2, y: 1}, 'th', {rowspan: 2});
1237
- assert(root.toString() === '{|\n|rowspan=2 colspan=2|a||b\n!g\n|-\n! rowspan="2"|h\n|c\n|-\n|d||e||f\n|}');
1238
- ```
1239
-
1240
- |原表格|插入 2 格|
1241
- |:-:|:-:|
1242
- |<table><tr><td rowspan=2 colspan=2 align="center">a</td><td>b</td></tr><tr><td>c</td></tr><tr><td>d</td><td>e</td><td>f</td></tr></table>|<table><tr><td rowspan=2 colspan=2 align="center">a</td><td>b</td><th>g</th></tr><tr><th rowspan=2>h</th><td>c</td></tr><tr><td>d</td><td>e</td><td>f</td></tr></table>
1243
-
1244
- **removeTableRow**(y: number): TrToken<a id="tabletoken.removetablerow"></a>
1245
- - 删除一行。
1246
-
1247
- ```js
1248
- var root = Parser.parse('{|\n|rowspan=2|a||b||c\n|-\n!rowspan=2 class="th"|d||e\n|-\n|f||g\n|}'),
1249
- table = root.firstChild;
1250
- table.removeTableRow(1);
1251
- assert(root.toString() === '{|\n|a||b||c\n|-\n|f\n! class="th"|\n|g\n|}'); // 自动调整跨行单元格的 rowspan
1252
- ```
1253
-
1254
- |原表格|删除第 1 行|
1255
- |:-:|:-:|
1256
- |<table><tr><td rowspan=2>a</td><td>b</td><td>c</td></tr><tr><th rowspan=2>d</th><th>e</th></tr><tr><td>f</td><td>g</td></tr></table>|<table><tr><td>a</td><td>b</td><td>c</td></tr><tr><td>f</td><td></td><td>g</td></tr></table>|
1257
-
1258
- **removeTableCol**(x: number): void<a id="tabletoken.removetablecol"></a>
1259
- - 删除一列。
1260
-
1261
- ```js
1262
- var root = Parser.parse('{|\n|colspan=2|a||b\n|-\n|c\n!colspan=2 class="th"|d\n|-\n|e\n!f\n|g\n|}'),
1263
- table = root.firstChild;
1264
- table.removeTableCol(1);
1265
- assert(root.toString() === '{|\n|a||b\n|-\n|c\n! class="th"|\n|-\n|e\n|g\n|}'); // 自动调整跨列单元格的 colspan
1266
- ```
1267
-
1268
- |原表格|删除第 1 列|
1269
- |:-:|:-:|
1270
- |<table><tr><td colspan=2 align="center">a</td><td>b</td></tr><tr><td>c</td><th colspan=2>d</th></tr><tr><td>e</td><th>f</th><td>g</td></tr></table>|<table><tr><td>a</td><td>b</td></tr><tr><th>c</th><th></th></tr><tr><td>e</td><td>g</td></tr></table>|
1271
-
1272
- **mergeCells**(xlim: [number, number], ylim: [number, number]): [TdToken](#tdtoken)<a id="tabletoken.mergecells"></a>
1273
- - 合并单元格。
1274
-
1275
- ```js
1276
- var root = Parser.parse('{|\n!a\n|b||c\n|-\n|d||e||f\n|-\n|g||h||i\n|}'),
1277
- table = root.firstChild;
1278
- table.mergeCells([0, 2], [0, 2]); // 被合并的单元格的属性和内部文本均会丢失
1279
- assert(root.toString() === '{|\n! rowspan="2" colspan="2"|a\n|c\n|-\n|f\n|-\n|g||h||i\n|}');
1280
- ```
1281
-
1282
- |原表格|合并单元格|
1283
- |:-:|:-:|
1284
- |<table><tr><th>a</th><td>b</td><td>c</td></tr><tr><td>d</td><td>e</td><td>f</td></tr><tr><td>g</td><td>h</td><td>i</td></tr></table>|<table><tr><th rowspan=2 colspan=2>a</th><td>c</td></tr><tr><td>f</td></tr><tr><td>g</td><td>h</td><td>i</td></tr></table>|
1285
-
1286
- **splitIntoRows**(coords: {row: number, column: number}\|{x: number, y: number}): void<a id="tabletoken.splitintorows"></a>
1287
- - 将单元格分裂到不同行。
1288
-
1289
- ```js
1290
- var root = Parser.parse('{|\n|a||b\n!rowspan=3|c\n|-\n|d\n|-\n|e||f\n|}'),
1291
- table = root.firstChild;
1292
- table.splitIntoRows({x: 2, y: 0});
1293
- assert(root.toString() === '{|\n|a||b\n!c\n|-\n|d\n|-\n|e||f\n!\n|}'); // 第 1 行由于缺失第 1 列的单元格,分裂后的第 2 列不会保留
1294
- ```
1295
-
1296
- |原表格|按行分裂单元格|
1297
- |:-:|:-:|
1298
- |<table><tr><td>a</td><td>b</td><th rowspan=3>c</th></tr><tr><td>d</td></tr><tr><td>e</td><td>f</td></tr></table>|<table><tr><td>a</td><td>b</td><th>c</th></tr><tr><td>d</td></tr><tr><td>e</td><td>f</td><th></th></tr></table>|
1299
-
1300
- **splitIntoCols**(coords: {row: number, column: number}\|{x: number, y: number}): void<a id="tabletoken.splitintocols"></a>
1301
- - 将单元格分裂到不同列。
1302
-
1303
- ```js
1304
- var root = Parser.parse('{|\n!colspan=2 class="th"|a\n|}'),
1305
- table = root.firstChild;
1306
- table.splitIntoCols({x: 1, y: 0});
1307
- assert(root.toString() === '{|\n! class="th"|a\n! class="th"|\n|}'); // 分裂继承属性
1308
- ```
1309
-
1310
- |原表格|按列分裂单元格|
1311
- |:-:|:-:|
1312
- |<table><tr><th colspan=2>a</th></tr></table>|<table><tr><th>a</th><th>&nbsp;</th></tr></table>|
1313
-
1314
- **splitIntoCells**(coords: {row: number, column: number}\|{x: number, y: number}): void<a id="tabletoken.splitintocells"></a>
1315
- - 将单元格分裂成最小单元格。
1316
-
1317
- ```js
1318
- var root = Parser.parse('{|\n!rowspan=2 colspan=2|a\n|b\n|-\n|c\n|-\n|d||e||f\n|}'),
1319
- table = root.firstChild;
1320
- table.splitIntoCells({x: 0, y: 0});
1321
- assert(root.toString() === '{|\n!a\n!\n|b\n|-\n!\n!\n|c\n|-\n|d||e||f\n|}');
1322
- ```
1323
-
1324
- |原表格|分裂单元格|
1325
- |:-:|:-:|
1326
- |<table><tr><th rowspan=2 colspan=2>a</th><td>b</td></tr><tr><td>c</td></tr><tr><td>d</td><td>e</td><td>f</td></tr></table>|<table><tr><th>a</th><th></th><td>b</td></tr><tr><th></th><th></th><td>c</td></tr><tr><td>d</td><td>e</td><td>f</td></tr></table>|
1327
-
1328
- **replicateTableRow**(row: number): TrToken<a id="tabletoken.replicatetablerow"></a>
1329
- - 复制一行并插入该行之前
1330
-
1331
- ```js
1332
- var root = Parser.parse('{|\n|rowspan=2|a||b||c\n|-\n!rowspan=2|d||e\n|-\n|f||g\n|}'),
1333
- table = root.firstChild;
1334
- table.replicateTableRow(1);
1335
- assert(root.toString() === '{|\n| rowspan="3"|a||b||c\n|-\n!d||e\n|-\n!rowspan=2|d||e\n|-\n|f||g\n|}'); // 复制行内的单元格`rowspan`总是为1
1336
- ```
1337
-
1338
- |原表格|复制第 1 行|
1339
- |:-:|:-:|
1340
- |<table><tr><td rowspan=2>a</td><td>b</td><td>c</td></tr><tr><th rowspan=2>d</th><th>e</th></tr><tr><td>f</td><td>g</td></tr></table>|<table><tr><td rowspan=3>a</td><td>b</td><td>c</td></tr><tr><th>d</th><th>e</th></tr><tr><th rowspan=2>d</th><th>e</th></tr><tr><td>f</td><td>g</td></tr></table>|
1341
-
1342
- **replicateTableCol**(x: number): [TdToken](#tdtoken)[]<a id="tabletoken.replicatetablecol"></a>
1343
- - 复制一列并插入该列之前
1344
-
1345
- ```js
1346
- var root = Parser.parse('{|\n|colspan=2|a||b\n|-\n|c\n!colspan=2|d\n|-\n|e\n!f\n|g\n|}'),
1347
- table = root.firstChild;
1348
- table.replicateTableCol(1);
1349
- assert(root.toString() === '{|\n| colspan="3"|a||b\n|-\n|c\n!d\n!colspan=2|d\n|-\n|e\n!f\n!f\n|g\n|}'); // 复制列内的单元格`colspan`总是为1
1350
- ```
1351
-
1352
- |原表格|复制第 1 列|
1353
- |:-:|:-:|
1354
- |<table><tr><td colspan=2 align="center">a</td><td>b</td></tr><tr><td>c</td><th colspan=2>d</th></tr><tr><td>e</td><th>f</th><td>g</td></tr></table>|<table><tr><td colspan=3 align="center">a</td><td>b</td></tr><tr><td>c</td><th>d</th><th colspan=2>d</th></tr><tr><td>e</td><th>f</th><th>f</th><td>g</td></tr></table>|
1355
-
1356
- **moveTableRowBefore**(y: number, before: number): TrToken<a id="tabletoken.movetablerowbefore"></a>
1357
- - 移动表格行。
1358
-
1359
- ```js
1360
- var root = Parser.parse('{|\n|rowspan=2|a||b||c||d\n|-\n|colspan=2|e||f\n|-\n|rowspan=2|g||h||i||j\n|-\n!rowspan=2|k||colspan=2|l\n|-\n|m||n||o\n|}'),
1361
- table = root.firstChild;
1362
- table.moveTableRowBefore(3, 1);
1363
- assert(root.toString() === '{|\n| rowspan="3"|a||b||c||d\n|-\n!k||colspan=2|l\n|-\n|colspan=2|e||f\n|-\n|g||h||i||j\n|-\n|m\n!\n|n||o\n|}');
1364
- ```
1365
-
1366
- |原表格|移动第 3 行至第 1 行前|
1367
- |:-:|:-:|
1368
- |<table><tr><td rowspan=2>a</td><td>b</td><td>c</td><td>d</td></tr><tr><td colspan=2 align="center">e</td><td>f</td></tr><tr><td rowspan=2>g</td><td>h</td><td>i</td><td>j</td></tr><tr><th rowspan=2>k</th><th colspan=2>l</th></tr><tr><td>m</td><td>n</td><td>o</td></tr></table>|<table><tr><td rowspan=3>a</td><td>b</td><td>c</td><td>d</td></tr><tr><th>k</th><th colspan=2>l</th></tr><tr><td colspan=2 align="center">e</td><td>f</td></tr><tr><td>g</td><td>h</td><td>i</td><td>j</td></tr><tr><td>m</td><th></th><td>n</td><td>o</td></tr></table>|
1369
-
1370
- **moveTableRowAfter**(y: number, after: number): TrToken<a id="tabletoken.movetablerowafter"></a>
1371
- - 移动表格行。
1372
-
1373
- ```js
1374
- var root = Parser.parse('{|\n|rowspan=2|a||colspan=2|b||c\n|-\n|d||e||f\n|-\n|rowspan=2|g||h||i||j\n|-\n!rowspan=2|k||colspan=2|l\n|-\n|m||n||o\n|}'),
1375
- table = root.firstChild;
1376
- table.moveTableRowAfter(3, 0);
1377
- assert(root.toString() === '{|\n| rowspan="3"|a||colspan=2|b||c\n|-\n!k||colspan=2|l\n|-\n|d||e||f\n|-\n|g||h||i||j\n|-\n|m\n!\n|n||o\n|}');
1378
- ```
1379
-
1380
- |原表格|移动第 3 行至第 0 行后|
1381
- |:-:|:-:|
1382
- |<table><tr><td rowspan=2>a</td><td colspan=2 align="center">b</td><td>c</td></tr><tr><td>d</td><td>e</td><td>f</td></tr><tr><td rowspan=2>g</td><td>h</td><td>i</td><td>j</td></tr><tr><th rowspan=2>k</th><th colspan=2>l</th></tr><tr><td>m</td><td>n</td><td>o</td></tr></table>|<table><tr><td rowspan=3>a</td><td colspan=2 align="center">b</td><td>c</td></tr><tr><th>k</th><th colspan=2>l</th></tr><tr><td>d</td><td>e</td><td>f</td></tr><tr><td>g</td><td>h</td><td>i</td><td>j</td></tr><tr><td>m</td><th></th><td>n</td><td>o</td></tr></table>|
1383
-
1384
- **moveTableColBefore**(x: number, before: number): void<a id="tabletoken.movetablecolbefore"></a>
1385
- - 移动表格列。
1386
-
1387
- ```js
1388
- var root = Parser.parse('{|\n|colspan=2|a||colspan=2|b||c\n|-\n|d||rowspan=2|e||f\n!colspan=2|g\n|-\n|h||i\n!rowspan=2|j\n|k\n|-\n|l||m||n||o\n|}'),
1389
- table = root.firstChild;
1390
- table.moveTableColBefore(3, 1);
1391
- assert(root.toString() === '{|\n| colspan="3"|a||b||c\n|-\n|d\n!g\n|rowspan=2|e||f\n!\n|-\n|h\n!rowspan=2|j\n|i\n|k\n|-\n|l||m||n||o\n|}');
1392
- ```
1393
-
1394
- |原表格|移动第 3 列至第 1 列前|
1395
- |:-:|:-:|
1396
- |<table><tr><td colspan=2 align="center">a</td><td colspan=2 align="center">b</td><td>c</td></tr><tr><td>d</td><td rowspan=2>e</td><td>f</td><th colspan=2>g</th></tr><tr><td>h</td><td>i</td><th rowspan=2>j</th><td>k</td></tr><tr><td>l</td><td>m</td><td>n</td><td>o</td></tr></table>|<table><tr><td colspan=3 align="center">a</td><td>b</td><td>c</td></tr><tr><td>d</td><th>g</th><td rowspan=2>e</td><td>f</td><th></th></tr><tr><td>h</td><th rowspan=2>j</th><td>i</td><td>k</td></tr><tr><td>l</td><td>m</td><td>n</td><td>o</td></tr></table>|
1397
-
1398
- **moveTableColAfter**(x: number, after: number): void<a id="tabletoken.movetablecolafter"></a>
1399
- - 移动表格列。
1400
-
1401
- ```js
1402
- var root = Parser.parse('{|\n|colspan=2|a||colspan=2|b||c\n|-\n|rowspan=2|d||e||f\n!colspan=2|g\n|-\n|h||i\n!rowspan=2|j\n|k\n|-\n|l||m||n||o\n|}'),
1403
- table = root.firstChild;
1404
- table.moveTableColAfter(3, 0);
1405
- assert(root.toString() === '{|\n| colspan="3"|a||b||c\n|-\n|rowspan=2|d\n!g\n|e||f\n!\n|-\n!rowspan=2|j\n|h||i\n|k\n|-\n|l||m||n||o\n|}');
1406
- ```
1407
-
1408
- |原表格|移动第 3 列至第 0 列后|
1409
- |:-:|:-:|
1410
- |<table><tr><td colspan=2 align="center">a</td><td colspan=2 align="center">b</td><td>c</td></tr><tr><td rowspan=2>d</td><td>e</td><td>f</td><th colspan=2>g</th></tr><tr><td>h</td><td>i</td><th rowspan=2>j</th><td>k</td></tr><tr><td>l</td><td>m</td><td>n</td><td>o</td></tr></table>|<table><tr><td colspan=3 align="center">a</td><td>b</td><td>c</td></tr><tr><td rowspan=2>d</td><th>g</th><td>e</td><td>f</td><th></th></tr><tr><th rowspan=2>j</th><td>h</td><td>i</td><td>k</td></tr><tr><td>l</td><td>m</td><td>n</td><td>o</td></tr></table>|
1411
- </details>
1412
-
1413
- [返回目录](#目录)
1414
-
1415
- # TdToken
1416
- 表格单元格。
1417
-
1418
- ## 原型属性<a id="tdtoken.prototype.properties"></a>
1419
- <details>
1420
- <summary>展开</summary>
1421
-
1422
- **subtype**: 'td'\|'th'\|'caption'<a id="tdtoken.subtype"></a>
1423
-
1424
- ```js
1425
- var root = Parser.parse('{|\n|+\n!\n|\n|}'),
1426
- [caption, th, td] = root.querySelectorAll('td');
1427
- assert(caption.subtype === 'caption');
1428
- assert(th.subtype === 'th');
1429
- assert(td.subtype === 'td');
1430
- ```
1431
-
1432
- **rowspan**: number<a id="tdtoken.rowspan"></a>
1433
-
1434
- ```js
1435
- var root = Parser.parse('{|\n|\n|}'),
1436
- td = root.querySelector('td');
1437
- assert(td.rowspan === 1);
1438
- ```
1439
-
1440
- **colspan**: number<a id="tdtoken.colspan"></a>
1441
-
1442
- ```js
1443
- var root = Parser.parse('{|\n|\n|}'),
1444
- td = root.querySelector('td');
1445
- assert(td.colspan === 1);
1446
- ```
1447
- </details>
1448
-
1449
- [返回目录](#目录)
1450
-
1451
- # DoubleUnderscoreToken
1452
- 状态开关。
1453
-
1454
- ## 实例属性<a id="doubleunderscoretoken.instance.properties"></a>
1455
- <details>
1456
- <summary>展开</summary>
1457
-
1458
- **name**: string<a id="doubleunderscoretoken.name"></a>
1459
- - 小写的状态开关名。
1460
-
1461
- ```js
1462
- var root = Parser.parse('__NOTOC__'),
1463
- doubleUnderscore = root.firstChild;
1464
- assert(doubleUnderscore.name === 'notoc');
1465
- ```
1466
- </details>
1467
-
1468
- [返回目录](#目录)
1469
-
1470
- # LinkToken
1471
- 内链,包括跨维基链接。
1472
-
1473
- ## 原型方法<a id="linktoken.prototype.methods"></a>
1474
- <details>
1475
- <summary>展开</summary>
1476
-
1477
- **setTarget**(link: string): void<a id="linktoken.settarget"></a>
1478
- - 修改内链目标。
1479
-
1480
- ```js
1481
- var root = Parser.parse('[[a]]'),
1482
- link = root.firstChild;
1483
- link.setTarget('b');
1484
- assert(root.toString() === '[[:b]]'); // 自动在开头添加':'
1485
- ```
1486
-
1487
- **setFragment**(fragment: string): void<a id="linktoken.setfragment"></a>
1488
- - 不改变目标页面,仅修改 fragment。
1489
-
1490
- ```js
1491
- var root = Parser.parse('[[:file:a]]'),
1492
- link = root.firstChild;
1493
- link.setFragment('b');
1494
- assert(root.toString() === '[[:File:A#b]]'); // 这个方法会同时规范化页面名
1495
- ```
1496
-
1497
- **asSelfLink**(fragment?: string): void<a id="linktoken.asselflink"></a>
1498
- - 当原链接带有或指定 fragment 时,将内链修改为 selfLink 格式。
1499
-
1500
- ```js
1501
- var root = Parser.parse('[[a#b]]'),
1502
- link = root.firstChild;
1503
- link.asSelfLink();
1504
- assert(root.toString() === '[[#b]]');
1505
- ```
1506
-
1507
- **setLinkText**(linkText: string)<a id="linktoken.setlinktext"></a>
1508
- - 修改链接文本。
1509
-
1510
- ```js
1511
- var root = Parser.parse('[[a]]'),
1512
- link = root.firstChild;
1513
- link.setLinkText('b');
1514
- assert(root.toString() === '[[a|b]]');
1515
- link.removeAt(1); // 若要移除链接文本,直接使用removeAt方法即可
1516
- assert(root.toString() === '[[a]]');
1517
- ```
1518
-
1519
- **pipeTrick**(): void<a id="linktoken.pipetrick"></a>
1520
- - 模拟解析器预转换的 pipe trick。
1521
-
1522
- ```js
1523
- var root = Parser.parse('[[help:a (b)]]'),
1524
- link = root.firstChild;
1525
- link.pipeTrick();
1526
- assert(root.toString() === '[[help:a (b)|a]]');
1527
- ```
1528
- </details>
1529
-
1530
- ## 实例属性<a id="linktoken.instance.properties"></a>
1531
- <details>
1532
- <summary>展开</summary>
1533
-
1534
- **name**: string<a id="linktoken.name"></a>
1535
- - 规范化的目标页面名称。
1536
-
1537
- ```js
1538
- var root = Parser.parse('[[:文件:a]]'),
1539
- link = root.firstChild;
1540
- assert(link.name === 'File:A');
1541
- ```
1542
-
1543
- **selfLink**: boolean<a id="linktoken.selflink"></a>
1544
- - 是否是 selfLink。
1545
-
1546
- ```js
1547
- var root = Parser.parse('[[#a]]'),
1548
- link = root.firstChild;
1549
- assert(link.selfLink === true);
1550
- ```
1551
-
1552
- **fragment**: string<a id="linktoken.fragment"></a>
1553
- - URL 解码后的 fragment。
1554
-
1555
- ```js
1556
- var root = Parser.parse('[[#.7B.7D]]'), // 兼容 MediaWiki 式的 fragment 编码
1557
- link = root.firstChild;
1558
- assert(link.fragment === '{}');
1559
- ```
1560
-
1561
- **interwiki**: string<a id="linktoken.interwiki"></a>
1562
- - 跨维基前缀。
1563
-
1564
- ```js
1565
- var root = Parser.parse('[[zhwiki:a]]'),
1566
- link = root.firstChild;
1567
- assert(link.interwiki === 'zhwiki');
1568
- ```
1569
- </details>
1570
-
1571
- [返回目录](#目录)
1572
-
1573
- # CategoryToken
1574
- 分类。这个类继承了 [LinkToken](#linktoken) 类。
1575
-
1576
- ## 原型方法<a id="categorytoken.prototype.methods"></a>
1577
- <details>
1578
- <summary>展开</summary>
1579
-
1580
- **setSortkey**(text: string): void<a id="categorytoken.setsortkey"></a>
1581
- - 修改分类关键字,实际上就是 [setLinkText](#linktoken.setlinktext) 方法的别名。
1582
-
1583
- ```js
1584
- var root = Parser.parse('[[category:a]]'),
1585
- category = root.firstChild;
1586
- category.setSortkey('*');
1587
- assert(root.toString() === '[[category:a|*]]');
1588
- ````
1589
- </details>
1590
-
1591
- ## 实例属性<a id="categorytoken.instance.properties"></a>
1592
- <details>
1593
- <summary>展开</summary>
1594
-
1595
- **sortkey**: string<a id="categorytoken.sortkey"></a>
1596
- - 分类关键字。
1597
-
1598
- ```js
1599
- var root = Parser.parse('[[category:a|*]]'),
1600
- category = root.firstChild;
1601
- assert(category.sortkey === '*');
1602
- ```
1603
- </details>
1604
-
1605
- [返回目录](#目录)
1606
-
1607
- # FileToken
1608
- 文件。这个类继承了 [LinkToken](#linktoken) 类。
1609
-
1610
- ## 原型方法<a id="filetoken.prototype.methods"></a>
1611
- <details>
1612
- <summary>展开</summary>
1613
-
1614
- **getAllArgs**(): [ImageParameterToken](#imageparametertoken)[]<a id="filetoken.getallargs"></a>
1615
- - 获取所有图片参数,类似 [TranscludeToken.getAllArgs](#transcludetoken.getallargs) 方法。
1616
-
1617
- ```js
1618
- var root = Parser.parse('[[file:a|thumb|1px|link=b|alt=c|d]]'),
1619
- file = root.firstChild;
1620
- assert.deepStrictEqual(file.getAllArgs(), file.children.slice(1));
1621
- ```
1622
-
1623
- **getArgs**(key: string): Set\<[ImageParameterToken](#imageparametertoken)><a id="filetoken.getargs"></a>
1624
- - 获取指定的图片参数,类似 [TranscludeToken.getArgs](#transcludetoken.getargs) 方法。
1625
-
1626
- ```js
1627
- var root = Parser.parse('[[file:a|link=b|链接=c]]'), // 这里故意使用一个错误语法的例子,请勿模仿
1628
- file = root.firstChild;
1629
- assert.deepStrictEqual(file.getArgs('link'), new Set(file.children.slice(1)));
1630
- ```
1631
-
1632
- **hasArg**(key: string): boolean<a id="filetoken.hasarg"></a>
1633
- - 是否带有指定的图片参数,类似 [TranscludeToken.hasArg](#transcludetoken.hasarg) 方法。
1634
-
1635
- ```js
1636
- var root = Parser.parse('[[file:a|b]]'),
1637
- file = root.firstChild;
1638
- assert(file.hasArg('caption') === true);
1639
- ```
1640
-
1641
- **getArg**(key: string): [ImageParameterToken](#imageparametertoken)<a id="filetoken.getarg"></a>
1642
- - 获取最后一个指定的图片参数,类似 [TranscludeToken.getArg](#transcludetoken.getarg) 方法。
1643
-
1644
- ```js
1645
- var root = Parser.parse('[[file:a|link=b|链接=c]]'), // 这里故意使用一个错误语法的例子,请勿模仿
1646
- file = root.firstChild;
1647
- assert(file.getArg('link'), file.lastChild);
1648
- ```
1649
-
1650
- **removeArg**(key: string): void<a id="filetoken.removearg"></a>
1651
- - 移除指定的图片参数,类似 [TranscludeToken.removeArg](#transcludetoken.removearg) 方法。
1652
-
1653
- ```js
1654
- var root = Parser.parse('[[file:a|link=b|链接=c]]'), // 这里故意使用一个错误语法的例子,请勿模仿
1655
- file = root.firstChild;
1656
- file.removeArg('link');
1657
- assert(root.toString() === '[[file:a]]');
1658
- ```
1659
-
1660
- **getKeys**(): string[]<a id="filetoken.getkeys"></a>
1661
- - 获取所有图片参数名,类似 [TranscludeToken.getKeys](#transcludetoken.getkeys) 方法。
1662
-
1663
- ```js
1664
- var root = Parser.parse('[[file:a|thumb|1px|link=b|alt=c|d]]'),
1665
- file = root.firstChild;
1666
- assert.deepStrictEqual(file.getKeys(), ['thumbnail', 'width', 'link', 'alt', 'caption']);
1667
- ```
1668
-
1669
- **getValue**(key: string): string\|true<a id="filetoken.getvalue"></a>
1670
- - 获取指定的图片参数值,类似 [TranscludeToken.getValue](#transcludetoken.getvalue) 方法。
1671
-
1672
- ```js
1673
- var root = Parser.parse('[[file:a|thumb|100px]]'),
1674
- file = root.firstChild;
1675
- assert(file.getValue('thumbnail') === true);
1676
- assert(file.getValue('width') === '100');
1677
- ```
1678
-
1679
- **setValue**(key: string, value: string\|boolean): void<a id="filetoken.setvalue"></a>
1680
- - 修改或设置指定的图片参数,类似 [TranscludeToken.setValue](#transcludetoken.setvalue) 方法。
1681
-
1682
- ```js
1683
- var root = Parser.parse('[[file:a|thumb]]'),
1684
- file = root.firstChild;
1685
- file.setValue('thumbnail', false);
1686
- file.setValue('width', '100');
1687
- file.setValue('framed', true);
1688
- assert(root.toString() === '[[file:a|100px|framed]]');
1689
- ```
1690
- </details>
1691
-
1692
- ## 原型属性<a id="filetoken.prototype.properties"></a>
1693
- <details>
1694
- <summary>展开</summary>
1695
-
1696
- **link**: string\|symbol<a id="filetoken.link"></a>
1697
- - 链接目标。
1698
-
1699
- ```js
1700
- var root = Parser.parse('[[file:a|link=[[talk:b]]]]'),
1701
- file = root.firstChild;
1702
- assert(file.link === 'Talk:B');
1703
- file.link = '//c';
1704
- assert(root.toString() === '[[file:a|link=//c]]');
1705
- ```
1706
-
1707
- **size**: {width: string, height: string}<a id="filetoken.size"></a>
1708
- - 图片尺寸。
1709
-
1710
- ```js
1711
- var root = Parser.parse('[[file:a|x1px]]'),
1712
- file = root.firstChild;
1713
- assert.deepStrictEqual(file.size, {width: '', height: '1'});
1714
- ```
1715
-
1716
- **width**: number<a id="filetoken.width"></a>
1717
- - 图片宽度。
1718
-
1719
- ```js
1720
- var root = Parser.parse('[[file:a|1x1px]]'),
1721
- file = root.firstChild;
1722
- assert(file.width === '1');
1723
- file.width = undefined;
1724
- assert(root.toString() === '[[file:a|x1px]]');
1725
- ```
1726
-
1727
- **height**: number<a id="filetoken.height"></a>
1728
- - 图片高度。
1729
-
1730
- ```js
1731
- var root = Parser.parse('[[file:a|1x1px]]'),
1732
- file = root.firstChild;
1733
- assert(file.height === '1');
1734
- file.height = undefined;
1735
- assert(root.toString() === '[[file:a|1px]]');
1736
- ```
1737
- </details>
1738
-
1739
- [返回目录](#目录)
1740
-
1741
- # ImageParameterToken
1742
- 图片参数。
1743
-
1744
- ## 原型方法<a id="imageparametertoken.prototype.methods"></a>
1745
- <details>
1746
- <summary>展开</summary>
1747
-
1748
- **getValue**(): string\|true<a id="imageparametertoken.getvalue"></a>
1749
- - 获取图片参数值,类似 [ParameterToken.getValue](#parametertoken.getvalue) 方法。
1750
-
1751
- ```js
1752
- var root = Parser.parse('[[file:a|thumb|100px]]'),
1753
- [thumbnail, width] = root.querySelectorAll('image-parameter');
1754
- assert(thumbnail.getValue() === true);
1755
- assert(width.getValue() === '100');
1756
- ```
1757
-
1758
- **setValue**(value: string\|boolean): void<a id="imageparametertoken.setvalue"></a>
1759
- - 修改或移除图片参数,类似 [ParameterToken.setValue](#parametertoken.setvalue) 方法。
1760
-
1761
- ```js
1762
- var root = Parser.parse('[[file:a|thumb|100px]]'),
1763
- [thumbnail, width] = root.querySelectorAll('image-parameter');
1764
- thumbnail.setValue(false);
1765
- width.setValue('x100');
1766
- assert(root.toString() === '[[file:a|x100px]]');
1767
- ```
1768
- </details>
1769
-
1770
- [返回目录](#目录)
1771
-
1772
- # ExtLinkToken
1773
- `[]`内的外部链接。这个类同时混合了 [MagicLinkToken](#magiclinktoken) 类的方法和原型属性。
1774
-
1775
- ## 原型方法<a id="extlinktoken.prototype.methods"></a>
1776
- <details>
1777
- <summary>展开</summary>
1778
-
1779
- **setLinkText**(text: string): void<a id="extlinktoken.setlinktext"></a>
1780
- - 修改外链文本。
1781
-
1782
- ```js
1783
- var root = Parser.parse('[//example.org example]'),
1784
- extlink = root.firstChild;
1785
- extlink.setLinkText(''); // 清空外链文本
1786
- assert(root.toString() === '[//example.org]');
1787
- ```
1788
- </details>
1789
-
1790
- [返回目录](#目录)
1791
-
1792
- # MagicLinkToken
1793
- 自由外链。
1794
-
1795
- ## 原型方法<a id="magiclinktoken.prototype.methods"></a>
1796
- <details>
1797
- <summary>展开</summary>
1798
-
1799
- **getUrl**(): URL<a id="magiclinktoken.geturl"></a>
1800
- - 生成一个 URL 对象,以方便解析和修改外链目标。
1801
-
1802
- **setTarget**(url: string\|URL): void<a id="magiclinktoken.settarget"></a>
1803
- - 修改外链目标。可以和 [getUrl](#magiclinktoken.geturl) 方法联合使用(见以下示例)。
1804
-
1805
- ```js
1806
- var root = Parser.parse('https://www.mediawiki.org/wiki/Manual:Parser.php'),
1807
- magiclink = root.firstChild,
1808
- url = magiclink.getUrl();
1809
- url.searchParams.set('action', 'info');
1810
- magiclink.setTarget(url);
1811
- assert(root.toString() === 'https://www.mediawiki.org/wiki/Manual:Parser.php?action=info');
1812
- ```
1813
- </details>
1814
-
1815
- ## 原型属性<a id="magiclinktoken.prototype.properties"></a>
1816
- <details>
1817
- <summary>展开</summary>
1818
-
1819
- **protocol**: string<a id="magiclinktoken.protocol"></a>
1820
- - 外链协议。
1821
-
1822
- ```js
1823
- var root = Parser.parse('ftp://example.org'),
1824
- magiclink = root.firstChild;
1825
- assert(magiclink.protocol === 'ftp://');
1826
- magiclink.protocol = 'https://';
1827
- assert(root.toString() === 'https://example.org');
1828
- ```
1829
- </details>
1830
-
1831
- [返回目录](#目录)
1832
-
1833
- # ConverterToken
1834
- 转换。
1835
-
1836
- ## 原型方法<a id="convertertoken.prototype.methods"></a>
1837
- <details>
1838
- <summary>展开</summary>
1839
-
1840
- **getAllFlags**(): Set\<string><a id="convertertoken.getallflags"></a>
1841
- - 获取所有转换 flag,包括无效的。
1842
-
1843
- ```js
1844
- var root = Parser.parse('-{zh-hans;zh-hant|}-'),
1845
- converter = root.firstChild;
1846
- assert.deepStrictEqual(converter.getAllFlags(), new Set(['zh-hans', 'zh-hant']));
1847
- ```
1848
-
1849
- **getEffectiveFlags**(): Set\<string><a id="convertertoken.geteffectiveflags"></a>
1850
- - 解析有效的转换 flag。
1851
-
1852
- ```js
1853
- var root = Parser.parse('-{}-'),
1854
- converter = root.firstChild;
1855
- assert.deepStrictEqual(converter.getEffectiveFlags(), new Set(['S']));
1856
- ```
1857
-
1858
- **hasFlag**(flag: string): boolean<a id="convertertoken.hasflag"></a>
1859
- - 是否包含 flag,即使可能是无效 flag。
1860
-
1861
- ```js
1862
- var root = Parser.parse('-{R|}-'),
1863
- converter = root.firstChild;
1864
- assert(converter.hasFlag('R') === true);
1865
- ```
1866
-
1867
- **hasEffectiveFlag**(flag: string): boolean<a id="convertertoken.haseffectiveflag"></a>
1868
- - 是否包含某有效 flag。
1869
-
1870
- ```js
1871
- var root = Parser.parse('-{}-'),
1872
- converter = root.firstChild;
1873
- assert(converter.hasEffectiveFlag('S') === true);
1874
- ```
1875
-
1876
- **removeFlag**(flag: string): void<a id="convertertoken.removeflag"></a>
1877
- - 移除 flag,可能原本就是无效的 flag。
1878
-
1879
- ```js
1880
- var root = Parser.parse('-{R|}-'),
1881
- converter = root.firstChild;
1882
- converter.removeFlag('R');
1883
- assert(root.toString() === '-{}-');
1884
- ```
1885
-
1886
- **setFlag**(flag: string): void<a id="convertertoken.setflag"></a>
1887
- - 添加指定 flag,可能是无效 flag。
1888
-
1889
- ```js
1890
- var root = Parser.parse('-{}-'),
1891
- converter = root.firstChild;
1892
- converter.setFlag('R');
1893
- assert(root.toString() === '-{R|}-');
1894
- ```
1895
-
1896
- **toggleFlag**(flag: string): void<a id="convertertoken.toggleflag"></a>
1897
- - 改变某 flag 的有无,可能是无效 flag。
1898
-
1899
- ```js
1900
- var root = Parser.parse('-{R|}-'),
1901
- converter = root.firstChild;
1902
- converter.toggleFlag('R');
1903
- converter.toggleFlag('D');
1904
- assert(root.toString() === '-{D|}-');
1905
- ```
1906
- </details>
1907
-
1908
- ## 原型属性<a id="convertertoken.prototype.properties"></a>
1909
- <details>
1910
- <summary>展开</summary>
1911
-
1912
- **noConvert**: boolean<a id="convertertoken.noconvert"></a>
1913
- - 是否不会转换。
1914
-
1915
- ```js
1916
- var root = Parser.parse('-{简体}-'),
1917
- converter = root.firstChild;
1918
- assert(converter.noConvert === true);
1919
- ```
1920
- </details>
1921
-
1922
- [返回目录](#目录)
1923
-
1924
- # ConverterRuleToken
1925
- 单条转换规则。
1926
-
1927
- ## 原型方法<a id="converterruletoken.prototype.methods"></a>
1928
- <details>
1929
- <summary>展开</summary>
1930
-
1931
- **noConvert**(): void<a id="converterruletoken.noconvert"></a>
1932
- - 更改规则使得内容不会转换。
1933
-
1934
- ```js
1935
- var root = Parser.parse('-{繁體=>zh-hans:繁體}-'),
1936
- converterRule = root.querySelector('converter-rule');
1937
- converterRule.noConvert();
1938
- assert(root.toString() === '-{繁體}-');
1939
- ```
1940
-
1941
- **setTo**(to: string): void<a id="converterruletoken.setto"></a>
1942
- - 修改转换结果。
1943
-
1944
- ```js
1945
- var root = Parser.parse('-{繁體=>zh-hans:繁體}-'),
1946
- converterRule = root.querySelector('converter-rule');
1947
- converterRule.setTo('繁体');
1948
- assert(root.toString() === '-{繁體=>zh-hans:繁体}-');
1949
- ```
1950
-
1951
- **setVariant**(variant: string): void<a id="converterruletoken.setvariant"></a>
1952
- - 修改转换规则对应的语言变体。
1953
-
1954
- ```js
1955
- var root = Parser.parse('-{繁體=>zh-hans:繁體}-'),
1956
- converterRule = root.querySelector('converter-rule');
1957
- converterRule.setVariant('zh-cn');
1958
- assert(root.toString() === '-{繁體=>zh-cn:繁體}-');
1959
- ```
1960
-
1961
- **setFrom**(from: string): void<a id="converterruletoken.setfrom"></a>
1962
- - 对单项转换规则,修改待转换的内容。
1963
-
1964
- ```js
1965
- var root = Parser.parse('-{繁體=>zh-hans:繁体}-'),
1966
- converterRule = root.querySelector('converter-rule');
1967
- converterRule.setFrom('正體');
1968
- assert(root.toString() === '-{正體=>zh-hans:繁体}-');
1969
- ```
1970
-
1971
- **makeUnidirectional**(from: string): void<a id="converterruletoken.makeunidirectional"></a>
1972
- - [ConverterRuleToken.setFrom](#converterruletoken.setfrom) 方法的別名。
1973
-
1974
- **makeBidirectional**(): void<a id="converterruletoken.makebidirectional"></a>
1975
- - 将规则改为双向转换。
1976
-
1977
- ```js
1978
- var root = Parser.parse('-{繁體=>zh-hans:繁体}-'),
1979
- converterRule = root.querySelector('converter-rule');
1980
- converterRule.makeBidirectional();
1981
- assert(root.toString() === '-{zh-hans:繁体}-');
1982
- ```
1983
- </details>
1984
-
1985
- ## 原型属性<a id="convertertoken.prototype.properties"></a>
1986
- <details>
1987
- <summary>展开</summary>
1988
-
1989
- **variant**: string<a id="converterruletoken.variant"></a>
1990
- - 规则对应的语言变体。
1991
-
1992
- ```js
1993
- var root = Parser.parse('-{繁體=>zh-hans:繁体}-'),
1994
- converterRule = root.querySelector('converter-rule');
1995
- assert(converterRule.variant === 'zh-hans');
1996
- ```
1997
-
1998
- **unidirectional**: boolean<a id="converterruletoken.unidirectional"></a>
1999
- - 是否是单向转换。
2000
-
2001
- ```js
2002
- var root = Parser.parse('-{繁體=>zh-hans:繁体}-'),
2003
- converterRule = root.querySelector('converter-rule');
2004
- assert(converterRule.unidirectional === true);
2005
- ```
2006
-
2007
- **bidirectional**: boolean<a id="converterruletoken.bidirectional"></a>
2008
- - 是否是双向转换。
2009
-
2010
- ```js
2011
- var root = Parser.parse('-{zh-hans:繁体}-'),
2012
- converterRule = root.querySelector('converter-rule');
2013
- assert(converterRule.bidirectional === true);
2014
- ```
2015
- </details>
2016
-
2017
- [返回目录](#目录)
2018
-
2019
- # 选择器
2020
- Token 选择器的设计仿照了 CSS 和 jQuery 的选择器。
2021
- <details>
2022
- <summary>展开</summary>
2023
-
2024
- **type**<a id="selector.type"></a>
2025
- - 类比 CSS tag 选择器。
2026
-
2027
- ```js
2028
- var root = Parser.parse(wikitext);
2029
- assert(root.matches('root') === true)
2030
- ```
2031
-
2032
- **name**<a id="selector.name"></a>
2033
- - 类比 CSS id 选择器。
2034
-
2035
- ```js
2036
- var root = Parser.parse('<ref/>'),
2037
- ref = root.firstChild;
2038
- assert(ref.matches('#ref') === true);
2039
- ```
2040
-
2041
- **属性**<a id="selector.attribute"></a>
2042
- - 类比 CSS 属性选择器。
2043
-
2044
- ```js
2045
- var root = Parser.parse('<!-- --><ref name="abc"/>'),
2046
- comment = root.firstChild,
2047
- attr = root.querySelector('ext-attr');
2048
- assert(comment.matches('[closed]') === true); // 非 AttributeToken 的属性选择器对应自身属性
2049
- assert(attr.matches('[name^=a]') === true); // AttributeToken 的属性选择器对应维基文本中的属性
2050
- assert(attr.matches('[name$=c]') === true);
2051
- assert(attr.matches('[name*=b]') === true);
2052
- assert(attr.matches('[name!=x]') === true);
2053
- assert(attr.matches('[name=abc]') === true);
2054
- ```
2055
-
2056
- **伪选择器**<a id="selector.pseudo"></a>
2057
- - 类比 CSS 和 jQuery 伪选择器。
2058
-
2059
- ```js
2060
- var root = Parser.parse('text <!--'),
2061
- comment = root.lastChild;
2062
- assert(root.matches(':root') === true);
2063
- assert(root.matches(':is(root)') === true);
2064
- assert(comment.matches(':not(root)') === true);
2065
- assert(comment.matches(':nth-child(2)') === true);
2066
- assert(comment.matches(':nth-last-of-type(1)') === true);
2067
- assert(comment.matches(':last-child') === true);
2068
- assert(comment.matches(':first-of-type') === true);
2069
- assert(root.matches(':only-child') === true);
2070
- assert(comment.matches(':only-of-type') === true);
2071
- assert(root.matches(':contains(text)') === true);
2072
- assert(root.matches(':has(comment)') === true);
2073
- assert(root.matches(':parent') === true);
2074
- assert(comment.matches(':empty') === true);
2075
- assert(comment.matches(':hidden') === true);
2076
- assert(root.matches(':visible') === true);
2077
- ```
2078
- </details>
2079
-
2080
- [返回目录](#目录)
2081
-
2082
- # $ (TokenCollection)
2083
- 这是一个仿 jQuery 的批量操作工具,这里仅列举方法和属性。
2084
- <details>
2085
- <summary>展开</summary>
2086
-
2087
- **toArray**(): (string\|Token)[]
2088
- **get**(num: number): string\|Token
2089
- **each**(callback: function(this: string\|Token, number, string\|Token): void): this
2090
- **map**(callback: function(this: string\|Token, number, string\|Token): any): any[]\|TokenCollection
2091
- **slice**(start: number, end: number): TokenCollection
2092
- **first**(): TokenCollection
2093
- **last**(): TokenCollection
2094
- **eq**(i: number): TokenCollection
2095
- **toString**(): string
2096
- **text**(text?: string): string\|this
2097
- **is**(selector: string): boolean
2098
- **filter**(selector: string\|function(this: string\|Token, number, string\|Token): boolean): TokenCollection
2099
- **not**(selector: string\|function(this: string\|Token, number, string\|Token): boolean): TokenCollection
2100
- **find**(selector: string): TokenCollection
2101
- **has**(selector: string): boolean
2102
- **closest**(selector?: string): TokenCollection
2103
- **index**(): number
2104
- **add**(elements: string\|Token\|TokenCollection\|(string\|Token)[]): TokenCollection
2105
- **addBack**(selector?: string): TokenCollection
2106
- **parent**(selector?: string): TokenCollection
2107
- **parents**(selector?: string): TokenCollection
2108
- **parentsUntil**(selector?: string, filter?: string): TokenCollection
2109
- **next**(selector?: string): TokenCollection
2110
- **nextAll**(selector?: string): TokenCollection
2111
- **nextUntil**(selector?: string, filter?: string): TokenCollection
2112
- **prev**(selector?: string): TokenCollection
2113
- **prevAll**(selector?: string): TokenCollection
2114
- **prevUntil**(selector?: string, filter?: string): TokenCollection
2115
- **siblings**(selector?: string): TokenCollection
2116
- **children**(selector?: string): TokenCollection
2117
- **contents**(): TokenCollection
2118
- **data**(key: string\|Record\<string, any>, value?: any): this\|any
2119
- **removeData**(name: string\|string[]): this
2120
- **on**(events: string\|Record\<string, (e: Event, data: any) => any, [selector: string], handler: (e: Event, data: any) => any): this
2121
- **one**(events: string\|Record\<string, (e: Event, data: any) => any, [selector: string], handler: (e: Event, data: any) => any): this
2122
- **off**(events: string\|Record\<string, (e: Event, data: any) => any, [selector: string], [handler: (e: Event, data: any) => any]): this
2123
- **trigger**(event: Event, data?: any): this
2124
- **tiggerHandler**(event: Event, data?: any): any
2125
- **append**(content: string\|Token\|TokenCollection\|(string\|Token)[]\|function(this: Token, number, string): string\|Token\|TokenCollection\|(string\|Token)[]): this
2126
- **prepend**(content: string\|Token\|TokenCollection\|(string\|Token)[]\|function(this: Token, number, string): string\|Token\|TokenCollection\|(string\|Token)[]): this
2127
- **before**(content: string\|Token\|TokenCollection\|(string\|Token)[]\|function(this: Token, number, string): string\|Token\|TokenCollection\|(string\|Token)[]): this
2128
- **after**(content: string\|Token\|TokenCollection\|(string\|Token)[]\|function(this: Token, number, string): string\|Token\|TokenCollection\|(string\|Token)[]): this
2129
- **html**(content: string\|Token\|TokenCollection\|(string\|Token)[]\|function(this: Token, number, string): string\|Token\|TokenCollection\|(string\|Token)[]): this
2130
- **replaceWith**(content: string\|Token\|TokenCollection\|(string\|Token)[]\|function(this: Token, number, string): string\|Token\|TokenCollection\|(string\|Token)[]): this
2131
- **remove**(selector?: string): this
2132
- **detach**(selector?: string): this
2133
- **empty**(): this
2134
- **appendTo**(target: Token\|Token[]): this
2135
- **prependTo**(target: Token\|Token[]): this
2136
- **insertBefore**(target: Token\|Token[]): this
2137
- **insertAfter**(target: Token\|Token[]): this
2138
- **replaceAll**(target: Token\|Token[]): this
2139
- **val**(value: string\|string[]\|function(this: Token, number, string): string): this
2140
- **attr**(name: string\|Record\<string, string>, value?: string): this\|string
2141
- **removeAttr**(name: string): this
2142
- **prop**(name: string\|Record\<string, any>, value?: any): this\|any
2143
- **removeProp**(name: string): this
2144
- **wrapAll**(wrapper: string[]\|function(this: Token, string): string[]): this
2145
- **wrapInner**(wrapper: string[]\|function(this: Token, string): string[]): this
2146
- **wrap**(wrapper: string[]\|function(this: Token, string): string[]): this
2147
-
2148
- </details>
14
+ # 目录
2149
15
 
2150
- [返回目录](#目录)
16
+ 1. [Parser](https://github.com/bhsd-harry/wikiparser-node/wiki/Home#parser)
17
+ 2. [AstElement](https://github.com/bhsd-harry/wikiparser-node/wiki/01.-Token#astelement)
18
+ 3. [Token](https://github.com/bhsd-harry/wikiparser-node/wiki/01.-Token#token)
19
+ 4. [CommentToken](https://github.com/bhsd-harry/wikiparser-node/wiki/02.-CommentToken等#commenttoken)
20
+ 5. [ExtToken](https://github.com/bhsd-harry/wikiparser-node/wiki/03.-ExtToken)
21
+ 6. [AttributeToken](https://github.com/bhsd-harry/wikiparser-node/wiki/04.-AttributeToken)
22
+ 7. [HeadingToken](https://github.com/bhsd-harry/wikiparser-node/wiki/05.-HeadingToken)
23
+ 8. [ArgToken](https://github.com/bhsd-harry/wikiparser-node/wiki/06.-ArgToken)
24
+ 9. [TranscludeToken](https://github.com/bhsd-harry/wikiparser-node/wiki/07.-TranscludeToken)
25
+ 10. [ParameterToken](https://github.com/bhsd-harry/wikiparser-node/wiki/08.-ParameterToken)
26
+ 11. [HtmlToken](https://github.com/bhsd-harry/wikiparser-node/wiki/09.-HtmlToken)
27
+ 12. [TableToken](https://github.com/bhsd-harry/wikiparser-node/wiki/10.-TableToken)
28
+ 13. [TdToken](https://github.com/bhsd-harry/wikiparser-node/wiki/11.-TdToken)
29
+ 14. [DoubleUnderscoreToken](https://github.com/bhsd-harry/wikiparser-node/wiki/12.-DoubleUnderscoreToken)
30
+ 15. [LinkToken](https://github.com/bhsd-harry/wikiparser-node/wiki/13.-LinkToken)
31
+ 16. [CategoryToken](https://github.com/bhsd-harry/wikiparser-node/wiki/14.-CategoryToken)
32
+ 17. [FileToken](https://github.com/bhsd-harry/wikiparser-node/wiki/15.-FileToken)
33
+ 18. [ImageParameterToken](https://github.com/bhsd-harry/wikiparser-node/wiki/16.-ImageParameterToken)
34
+ 19. [ExtLinkToken](https://github.com/bhsd-harry/wikiparser-node/wiki/17.-ExtLinkToken和MagicLinkToken#extlinktoken)
35
+ 20. [MagicLinkToken](https://github.com/bhsd-harry/wikiparser-node/wiki/17.-ExtLinkToken和MagicLinkToken#magiclinktoken)
36
+ 21. [ConverterToken](https://github.com/bhsd-harry/wikiparser-node/wiki/18.-ConverterToken)
37
+ 22. [ConverterRuleToken](https://github.com/bhsd-harry/wikiparser-node/wiki/19.-ConverterRuleToken)
38
+ 23. [选择器](https://github.com/bhsd-harry/wikiparser-node/wiki/20.-选择器)
39
+ 24. [$ (TokenCollection)](https://github.com/bhsd-harry/wikiparser-node/wiki/21.-$-(TokenCollection))