wikiparser-node 0.10.0 → 0.11.0-b

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 (93) hide show
  1. package/README.md +44 -32
  2. package/bundle/bundle.min.js +38 -0
  3. package/config/.schema.json +1 -19
  4. package/config/default.json +1 -2
  5. package/config/llwiki.json +1 -36
  6. package/config/minimum.json +135 -0
  7. package/config/moegirl.json +1 -45
  8. package/config/zhwiki.json +1 -467
  9. package/extensions/base.js +154 -0
  10. package/extensions/editor.css +63 -0
  11. package/extensions/editor.js +183 -0
  12. package/extensions/highlight.js +40 -0
  13. package/extensions/ui.css +119 -0
  14. package/i18n/zh-hans.json +1 -1
  15. package/i18n/zh-hant.json +1 -1
  16. package/package.json +16 -11
  17. package/index.js +0 -333
  18. package/lib/element.js +0 -611
  19. package/lib/node.js +0 -770
  20. package/lib/ranges.js +0 -130
  21. package/lib/text.js +0 -263
  22. package/lib/title.js +0 -83
  23. package/mixin/attributeParent.js +0 -117
  24. package/mixin/fixedToken.js +0 -40
  25. package/mixin/hidden.js +0 -21
  26. package/mixin/singleLine.js +0 -31
  27. package/mixin/sol.js +0 -54
  28. package/parser/brackets.js +0 -126
  29. package/parser/commentAndExt.js +0 -59
  30. package/parser/converter.js +0 -46
  31. package/parser/externalLinks.js +0 -33
  32. package/parser/hrAndDoubleUnderscore.js +0 -49
  33. package/parser/html.js +0 -42
  34. package/parser/links.js +0 -94
  35. package/parser/list.js +0 -59
  36. package/parser/magicLinks.js +0 -41
  37. package/parser/quotes.js +0 -64
  38. package/parser/selector.js +0 -177
  39. package/parser/table.js +0 -114
  40. package/src/arg.js +0 -207
  41. package/src/atom/hidden.js +0 -13
  42. package/src/atom/index.js +0 -43
  43. package/src/attribute.js +0 -470
  44. package/src/attributes.js +0 -453
  45. package/src/charinsert.js +0 -97
  46. package/src/converter.js +0 -176
  47. package/src/converterFlags.js +0 -284
  48. package/src/converterRule.js +0 -256
  49. package/src/extLink.js +0 -180
  50. package/src/gallery.js +0 -149
  51. package/src/hasNowiki/index.js +0 -44
  52. package/src/hasNowiki/pre.js +0 -40
  53. package/src/heading.js +0 -134
  54. package/src/html.js +0 -254
  55. package/src/imageParameter.js +0 -303
  56. package/src/imagemap.js +0 -199
  57. package/src/imagemapLink.js +0 -41
  58. package/src/index.js +0 -932
  59. package/src/link/category.js +0 -44
  60. package/src/link/file.js +0 -287
  61. package/src/link/galleryImage.js +0 -120
  62. package/src/link/index.js +0 -388
  63. package/src/magicLink.js +0 -149
  64. package/src/nested/choose.js +0 -24
  65. package/src/nested/combobox.js +0 -23
  66. package/src/nested/index.js +0 -93
  67. package/src/nested/references.js +0 -23
  68. package/src/nowiki/comment.js +0 -71
  69. package/src/nowiki/dd.js +0 -59
  70. package/src/nowiki/doubleUnderscore.js +0 -56
  71. package/src/nowiki/hr.js +0 -41
  72. package/src/nowiki/index.js +0 -56
  73. package/src/nowiki/list.js +0 -16
  74. package/src/nowiki/noinclude.js +0 -28
  75. package/src/nowiki/quote.js +0 -69
  76. package/src/onlyinclude.js +0 -64
  77. package/src/paramTag/index.js +0 -89
  78. package/src/paramTag/inputbox.js +0 -35
  79. package/src/parameter.js +0 -239
  80. package/src/syntax.js +0 -91
  81. package/src/table/index.js +0 -983
  82. package/src/table/td.js +0 -338
  83. package/src/table/tr.js +0 -319
  84. package/src/tagPair/ext.js +0 -145
  85. package/src/tagPair/include.js +0 -50
  86. package/src/tagPair/index.js +0 -126
  87. package/src/transclude.js +0 -843
  88. package/tool/index.js +0 -1202
  89. package/util/base.js +0 -17
  90. package/util/debug.js +0 -73
  91. package/util/diff.js +0 -76
  92. package/util/lint.js +0 -55
  93. package/util/string.js +0 -126
package/src/attribute.js DELETED
@@ -1,470 +0,0 @@
1
- 'use strict';
2
-
3
- const {generateForChild} = require('../util/lint'),
4
- {noWrap, removeComment} = require('../util/string'),
5
- fixedToken = require('../mixin/fixedToken'),
6
- Parser = require('..'),
7
- Token = require('.'),
8
- AtomToken = require('./atom');
9
-
10
- const stages = {'ext-attr': 0, 'html-attr': 2, 'table-attr': 3},
11
- pre = {'ext-attr': '<pre ', 'html-attr': '<p ', 'table-attr': '{|'},
12
- post = {'ext-attr': '/>', 'html-attr': '>', 'table-attr': ''},
13
- commonHtmlAttrs = new Set([
14
- 'id',
15
- 'class',
16
- 'style',
17
- 'lang',
18
- 'dir',
19
- 'title',
20
- 'tabindex',
21
- 'aria-describedby',
22
- 'aria-flowto',
23
- 'aria-hidden',
24
- 'aria-label',
25
- 'aria-labelledby',
26
- 'aria-owns',
27
- 'role',
28
- 'about',
29
- 'property',
30
- 'resource',
31
- 'datatype',
32
- 'typeof',
33
- 'itemid',
34
- 'itemprop',
35
- 'itemref',
36
- 'itemscope',
37
- 'itemtype',
38
- ]),
39
- blockAttrs = new Set(['align']),
40
- citeAttrs = new Set(['cite']),
41
- citeAndAttrs = new Set(['cite', 'datetime']),
42
- widthAttrs = new Set(['width']),
43
- tdAttrs = new Set(
44
- ['align', 'valign', 'abbr', 'axis', 'headers', 'scope', 'rowspan', 'colspan', 'width', 'height', 'bgcolor'],
45
- ),
46
- typeAttrs = new Set(['type']),
47
- htmlAttrs = {
48
- div: blockAttrs,
49
- h1: blockAttrs,
50
- h2: blockAttrs,
51
- h3: blockAttrs,
52
- h4: blockAttrs,
53
- h5: blockAttrs,
54
- h6: blockAttrs,
55
- blockquote: citeAttrs,
56
- q: citeAttrs,
57
- p: blockAttrs,
58
- br: new Set(['clear']),
59
- pre: widthAttrs,
60
- ins: citeAndAttrs,
61
- del: citeAndAttrs,
62
- ul: typeAttrs,
63
- ol: new Set(['type', 'start', 'reversed']),
64
- li: new Set(['type', 'value']),
65
- table: new Set(
66
- ['summary', 'width', 'border', 'frame', 'rules', 'cellspacing', 'cellpadding', 'align', 'bgcolor'],
67
- ),
68
- caption: blockAttrs,
69
- tr: new Set(['bgcolor', 'align', 'valign']),
70
- td: tdAttrs,
71
- th: tdAttrs,
72
- img: new Set(['alt', 'src', 'width', 'height', 'srcset']),
73
- font: new Set(['size', 'color', 'face']),
74
- hr: widthAttrs,
75
- rt: new Set(['rbspan']),
76
- data: new Set(['value']),
77
- time: new Set(['datetime']),
78
- meta: new Set(['itemprop', 'content']),
79
- link: new Set(['itemprop', 'href', 'title']),
80
- gallery: new Set(['mode', 'showfilename', 'caption', 'perrow', 'widths', 'heights', 'showthumbnails', 'type']),
81
- poem: new Set(['compact', 'align']),
82
- categorytree: new Set([
83
- 'align',
84
- 'hideroot',
85
- 'onlyroot',
86
- 'depth',
87
- 'mode',
88
- 'hideprefix',
89
- 'namespaces',
90
- 'showcount',
91
- 'notranslations',
92
- ]),
93
- combooption: new Set(['name', 'for', 'inline', 'align']),
94
- },
95
- empty = new Set(),
96
- extAttrs = {
97
- nowiki: empty,
98
- indicator: new Set(['name']),
99
- langconvert: new Set(['from', 'to']),
100
- ref: new Set(['group', 'name', 'extends', 'follow', 'dir']),
101
- references: new Set(['group', 'responsive']),
102
- charinsert: new Set(['label']),
103
- choose: new Set(['uncached', 'before', 'after']),
104
- option: new Set(['weight']),
105
- imagemap: empty,
106
- inputbox: empty,
107
- templatestyles: new Set(['src', 'wrapper']),
108
- dynamicpagelist: empty,
109
- poll: new Set(['id', 'show-results-before-voting']),
110
- sm2: typeAttrs,
111
- flashmp3: typeAttrs,
112
- score: new Set([
113
- 'line_width_inches',
114
- 'lang',
115
- 'override_midi',
116
- 'raw',
117
- 'note-language',
118
- 'override_audio',
119
- 'override_ogg',
120
- 'sound',
121
- 'vorbis',
122
- ]),
123
- seo: new Set([
124
- 'title',
125
- 'title_mode',
126
- 'title_separator',
127
- 'keywords',
128
- 'description',
129
- 'robots',
130
- 'google_bot',
131
- 'image',
132
- 'image_width',
133
- 'image_height',
134
- 'image_alt',
135
- 'type',
136
- 'site_name',
137
- 'locale',
138
- 'section',
139
- 'author',
140
- 'published_time',
141
- 'twitter_site',
142
- ]),
143
- tab: new Set([
144
- 'nested',
145
- 'name',
146
- 'index',
147
- 'class',
148
- 'block',
149
- 'inline',
150
- 'openname',
151
- 'closename',
152
- 'collapsed',
153
- 'dropdown',
154
- 'style',
155
- 'bgcolor',
156
- 'container',
157
- 'id',
158
- 'title',
159
- ]),
160
- tabs: new Set(['plain', 'class', 'container', 'id', 'title', 'style']),
161
- combobox: new Set(['placeholder', 'value', 'id', 'class', 'text', 'dropdown', 'style']),
162
- },
163
- insecureStyle = new RegExp(
164
- `${
165
- 'expression'
166
- }|${
167
- '(?:filter|accelerator|-o-link(?:-source)?|-o-replace)\\s*:'
168
- }|${
169
- '(?:url|image(?:-set)?)\\s*\\('
170
- }|${
171
- 'attr\\s*\\([^)]+[\\s,]url'
172
- }`,
173
- 'u',
174
- );
175
-
176
- /**
177
- * 扩展和HTML标签属性
178
- * @classdesc `{childNodes: [AtomToken, Token|AtomToken]}`
179
- */
180
- class AttributeToken extends fixedToken(Token) {
181
- #equal;
182
- #quotes;
183
- #tag;
184
-
185
- /** 引号是否匹配 */
186
- get balanced() {
187
- return !this.#equal || this.#quotes[0] === this.#quotes[1];
188
- }
189
-
190
- /** getValue()的getter */
191
- get value() {
192
- return this.getValue();
193
- }
194
-
195
- set value(value) {
196
- this.setValue(value);
197
- }
198
-
199
- /** 标签名 */
200
- get tag() {
201
- return this.#tag;
202
- }
203
-
204
- /**
205
- * @param {'ext-attr'|'html-attr'|'table-attr'} type 标签类型
206
- * @param {string} tag 标签名
207
- * @param {string} key 属性名
208
- * @param {string} equal 等号
209
- * @param {string} value 属性值
210
- * @param {string[]} quotes 引号
211
- * @param {accum} accum
212
- */
213
- constructor(type, tag, key, equal = '', value = '', quotes = [], config = Parser.getConfig(), accum = []) {
214
- const keyToken = new AtomToken(key, 'attr-key', config, accum, {
215
- [type === 'ext-attr' ? 'AstText' : 'Stage-1']: ':', ArgToken: ':', TranscludeToken: ':',
216
- });
217
- let valueToken;
218
- if (key === 'title') {
219
- valueToken = new Token(value, config, true, accum, {
220
- [`Stage-${stages[type]}`]: ':', ConverterToken: ':',
221
- }).setAttribute('type', 'attr-value').setAttribute('stage', Parser.MAX_STAGE - 1);
222
- } else if (tag === 'gallery' && key === 'caption') {
223
- /** @type {ParserConfig} */
224
- const newConfig = {...config, excludes: [...config.excludes, 'quote', 'extLink', 'magicLink', 'list']};
225
- valueToken = new Token(value, newConfig, true, accum, {
226
- AstText: ':', LinkToken: ':', FileToken: ':', CategoryToken: ':', ConverterToken: ':',
227
- }).setAttribute('type', 'attr-value').setAttribute('stage', 5);
228
- } else if (tag === 'choose' && (key === 'before' || key === 'after')) {
229
- /** @type {ParserConfig} */
230
- const newConfig = {...config, excludes: [...config.excludes, 'heading', 'html', 'table', 'hr', 'list']};
231
- valueToken = new Token(value, newConfig, true, accum, {
232
- ArgToken: ':',
233
- TranscludeToken: ':',
234
- LinkToken: ':',
235
- FileToken: ':',
236
- CategoryToken: ':',
237
- QuoteToken: ':',
238
- ExtLinkToken: ':',
239
- MagicLinkToken: ':',
240
- ConverterToken: ':',
241
- }).setAttribute('type', 'attr-value').setAttribute('stage', 1);
242
- } else {
243
- valueToken = new AtomToken(value, 'attr-value', config, accum, {
244
- [`Stage-${stages[type]}`]: ':',
245
- });
246
- }
247
- super(undefined, config, true, accum);
248
- this.type = type;
249
- this.append(keyToken, valueToken);
250
- this.#equal = equal;
251
- this.#quotes = quotes;
252
- this.#tag = tag;
253
- this.setAttribute('name', removeComment(key).trim().toLowerCase());
254
- }
255
-
256
- /** @override */
257
- afterBuild() {
258
- if (this.#equal.includes('\0')) {
259
- this.#equal = this.getAttribute('buildFromStr')(this.#equal, 'string');
260
- }
261
- if (this.parentNode) {
262
- this.#tag = this.parentNode.name;
263
- }
264
- this.setAttribute('name', this.firstChild.text().trim().toLowerCase());
265
- }
266
-
267
- /**
268
- * @override
269
- * @param {string} selector
270
- * @returns {string}
271
- */
272
- toString(selector) {
273
- if (selector && this.matches(selector)) {
274
- return '';
275
- }
276
- const [quoteStart = '', quoteEnd = ''] = this.#quotes;
277
- return this.#equal
278
- ? `${super.toString(selector, `${this.#equal}${quoteStart}`)}${quoteEnd}`
279
- : this.firstChild.toString(selector);
280
- }
281
-
282
- /**
283
- * @override
284
- * @returns {string}
285
- */
286
- text() {
287
- return this.#equal ? `${super.text(`${this.#equal.trim()}"`)}"` : this.firstChild.text();
288
- }
289
-
290
- /** @override */
291
- getGaps() {
292
- return this.#equal ? this.#equal.length + (this.#quotes[0]?.length ?? 0) : 0;
293
- }
294
-
295
- /** @override */
296
- print() {
297
- const [quoteStart = '', quoteEnd = ''] = this.#quotes;
298
- return this.#equal ? super.print({sep: `${this.#equal}${quoteStart}`, post: quoteEnd}) : super.print();
299
- }
300
-
301
- /**
302
- * @override
303
- * @param {number} start 起始位置
304
- */
305
- lint(start = this.getAbsoluteIndex()) {
306
- const errors = super.lint(start),
307
- {balanced, firstChild, lastChild, type, name, parentNode, value} = this,
308
- tagName = parentNode?.name;
309
- let rect;
310
- if (!balanced) {
311
- const root = this.getRootNode();
312
- rect = {start, ...root.posFromIndex(start)};
313
- const e = generateForChild(lastChild, rect, 'unclosed quotes', 'warning'),
314
- startIndex = e.startIndex - 1,
315
- startCol = e.startCol - 1;
316
- errors.push({...e, startIndex, startCol, excerpt: String(root).slice(startIndex, startIndex + 50)});
317
- }
318
- if (!/\{\{[^{]+\}\}/u.test(name) && (
319
- type === 'ext-attr' && !(tagName in htmlAttrs) && extAttrs[tagName] && !extAttrs[tagName].has(name)
320
- || (type === 'html-attr' || type === 'table-attr' || tagName in htmlAttrs) && !htmlAttrs[tagName]?.has(name)
321
- && !/^(?:xmlns:[\w:.-]+|data-[^:]*)$/u.test(name)
322
- && (tagName === 'meta' || tagName === 'link' || !commonHtmlAttrs.has(name))
323
- )) {
324
- rect ||= {start, ...this.getRootNode().posFromIndex(start)};
325
- errors.push(generateForChild(firstChild, rect, 'illegal attribute name'));
326
- } else if (name === 'style' && typeof value === 'string' && insecureStyle.test(value)) {
327
- rect ||= {start, ...this.getRootNode().posFromIndex(start)};
328
- errors.push(generateForChild(lastChild, rect, 'insecure style'));
329
- }
330
- return errors;
331
- }
332
-
333
- /** 获取属性值 */
334
- getValue() {
335
- if (this.#equal) {
336
- const value = this.lastChild.text();
337
- if (this.#quotes[1]) {
338
- return value;
339
- }
340
- return this.#quotes[0] ? value.trimEnd() : value.trim();
341
- }
342
- return true;
343
- }
344
-
345
- /**
346
- * @override
347
- * @template {string} T
348
- * @param {T} key 属性键
349
- * @returns {TokenAttribute<T>}
350
- */
351
- getAttribute(key) {
352
- if (key === 'equal') {
353
- return this.#equal;
354
- }
355
- return key === 'quotes' ? this.#quotes : super.getAttribute(key);
356
- }
357
-
358
- /**
359
- * @override
360
- * @param {PropertyKey} key 属性键
361
- */
362
- hasAttribute(key) {
363
- return key === 'equal' || key === 'quotes' || super.hasAttribute(key);
364
- }
365
-
366
- /** @override */
367
- cloneNode() {
368
- const [key, value] = this.cloneChildNodes(),
369
- config = this.getAttribute('config');
370
- return Parser.run(() => {
371
- const token = new AttributeToken(this.type, this.#tag, '', this.#equal, '', this.#quotes, config);
372
- token.firstChild.safeReplaceWith(key);
373
- token.lastChild.safeReplaceWith(value);
374
- token.afterBuild();
375
- return token;
376
- });
377
- }
378
-
379
- /** 转义等号 */
380
- escape() {
381
- this.#equal = '{{=}}';
382
- }
383
-
384
- /** 闭合引号 */
385
- close() {
386
- [this.#quotes[1]] = this.#quotes;
387
- }
388
-
389
- /**
390
- * 设置属性值
391
- * @param {string|boolean} value 参数值
392
- * @throws `SyntaxError` 非法的标签属性
393
- */
394
- setValue(value) {
395
- if (value === false) {
396
- this.remove();
397
- return;
398
- } else if (value === true) {
399
- this.#equal = '';
400
- return;
401
- }
402
- value = String(value);
403
- const {type} = this,
404
- key = this.name === 'title' ? 'title' : 'data',
405
- wikitext = `${pre[type]}${key}="${value}"${post[type]}`,
406
- root = Parser.parse(wikitext, this.getAttribute('include'), stages[type] + 1, this.getAttribute('config')),
407
- {length, firstChild: tag} = root;
408
- let attrs;
409
- if (length !== 1 || tag.type !== type.slice(0, -5)) {
410
- throw new SyntaxError(`非法的标签属性:${noWrap(value)}`);
411
- } else if (type === 'table-attr') {
412
- if (tag.length !== 2) {
413
- throw new SyntaxError(`非法的标签属性:${noWrap(value)}`);
414
- }
415
- attrs = tag.lastChild;
416
- } else {
417
- attrs = tag.firstChild;
418
- }
419
- const {length: attrsLength, firstChild} = attrs;
420
- if (attrsLength !== 1 || firstChild.type !== this.type || firstChild.name !== key) {
421
- throw new SyntaxError(`非法的标签属性:${noWrap(value)}`);
422
- }
423
- const {lastChild} = firstChild;
424
- firstChild.destroy(true);
425
- this.lastChild.safeReplaceWith(lastChild);
426
- if (this.#quotes[0]) {
427
- this.close();
428
- } else {
429
- this.#quotes = ['"', '"'];
430
- }
431
- }
432
-
433
- /**
434
- * 修改属性名
435
- * @param {string} key 新属性名
436
- * @throws `Error` title属性不能更名
437
- * @throws `SyntaxError` 非法的模板参数名
438
- */
439
- rename(key) {
440
- if (this.name === 'title') {
441
- throw new Error('title 属性不能更名!');
442
- }
443
- key = String(key);
444
- const {type} = this,
445
- wikitext = `${pre[type]}${key}${post[type]}`,
446
- root = Parser.parse(wikitext, this.getAttribute('include'), stages[type] + 1, this.getAttribute('config')),
447
- {length, firstChild: tag} = root;
448
- let attrs;
449
- if (length !== 1 || tag.type !== type.slice(0, -5)) {
450
- throw new SyntaxError(`非法的标签属性名:${noWrap(key)}`);
451
- } else if (type === 'table-attr') {
452
- if (tag.length !== 2) {
453
- throw new SyntaxError(`非法的标签属性名:${noWrap(key)}`);
454
- }
455
- attrs = tag.lastChild;
456
- } else {
457
- attrs = tag.firstChild;
458
- }
459
- const {length: attrsLength, firstChild: attr} = attrs;
460
- if (attrsLength !== 1 || attr.type !== this.type || attr.value !== true) {
461
- throw new SyntaxError(`非法的标签属性名:${noWrap(key)}`);
462
- }
463
- const {firstChild} = attr;
464
- attr.destroy(true);
465
- this.firstChild.safeReplaceWith(firstChild);
466
- }
467
- }
468
-
469
- Parser.classes.AttributeToken = __filename;
470
- module.exports = AttributeToken;