wikiparser-node 1.12.4 → 1.12.5

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.
@@ -69,7 +69,12 @@
69
69
  "link"
70
70
  ]
71
71
  ],
72
- "namespaces": {},
72
+ "namespaces": {
73
+ "0": "",
74
+ "6": "File",
75
+ "10": "Template",
76
+ "828": "Module"
77
+ },
73
78
  "nsid": {
74
79
  "file": 6,
75
80
  "category": 14
package/dist/lib/title.js CHANGED
@@ -56,7 +56,7 @@ class Title {
56
56
  }
57
57
  /* NOT FOR BROWSER */
58
58
  set extension(extension) {
59
- extension ||= '';
59
+ extension ??= '';
60
60
  const { main } = this, i = main.lastIndexOf('.');
61
61
  this.main = (i === -1 ? main : main.slice(0, i)) + (extension && '.') + extension;
62
62
  }
package/dist/src/arg.js CHANGED
@@ -73,6 +73,22 @@ class ArgToken extends index_2.Token {
73
73
  getGaps() {
74
74
  return 1;
75
75
  }
76
+ /** 设置name */
77
+ #setName() {
78
+ this.setAttribute('name', this.firstChild.toString(true).trim());
79
+ }
80
+ /** @private */
81
+ afterBuild() {
82
+ this.#setName();
83
+ super.afterBuild();
84
+ /* NOT FOR BROWSER */
85
+ const /** @implements */ argListener = ({ prevTarget }) => {
86
+ if (prevTarget === this.firstChild) {
87
+ this.#setName();
88
+ }
89
+ };
90
+ this.addEventListener(['remove', 'insert', 'replace', 'text'], argListener);
91
+ }
76
92
  /** @private */
77
93
  lint(start = this.getAbsoluteIndex(), re) {
78
94
  const { childNodes: [argName, argDefault, ...rest] } = this;
@@ -125,21 +141,6 @@ class ArgToken extends index_2.Token {
125
141
  return token;
126
142
  });
127
143
  }
128
- /** 设置name */
129
- #setName() {
130
- this.setAttribute('name', this.firstChild.toString(true).trim());
131
- }
132
- /** @private */
133
- afterBuild() {
134
- this.#setName();
135
- super.afterBuild();
136
- const /** @implements */ argListener = ({ prevTarget }) => {
137
- if (prevTarget === this.firstChild) {
138
- this.#setName();
139
- }
140
- };
141
- this.addEventListener(['remove', 'insert', 'replace', 'text'], argListener);
142
- }
143
144
  /** 移除无效部分 */
144
145
  removeRedundant() {
145
146
  debug_1.Shadow.run(() => {
@@ -102,10 +102,14 @@ const commonHtmlAttrs = new Set([
102
102
  time: new Set(['datetime']),
103
103
  meta: new Set(['itemprop', 'content']),
104
104
  link: new Set(['itemprop', 'href', 'title']),
105
- gallery: new Set(['mode', 'showfilename', 'caption', 'perrow', 'widths', 'heights', 'showthumbnails', 'type']),
106
- poem: new Set(['compact', 'align']),
105
+ gallery: typeAttrs,
106
+ poem: blockAttrs,
107
+ categorytree: blockAttrs,
108
+ combooption: blockAttrs,
109
+ }, empty = new Set(), extAttrs = {
110
+ gallery: new Set(['mode', 'showfilename', 'caption', 'perrow', 'widths', 'heights', 'showthumbnails']),
111
+ poem: new Set(['compact']),
107
112
  categorytree: new Set([
108
- 'align',
109
113
  'hideroot',
110
114
  'onlyroot',
111
115
  'depth',
@@ -115,8 +119,7 @@ const commonHtmlAttrs = new Set([
115
119
  'showcount',
116
120
  'notranslations',
117
121
  ]),
118
- combooption: new Set(['name', 'for', 'inline', 'align']),
119
- }, empty = new Set(), extAttrs = {
122
+ combooption: new Set(['name', 'for', 'inline']),
120
123
  nowiki: empty,
121
124
  indicator: new Set(['name']),
122
125
  langconvert: new Set(['from', 'to']),
@@ -349,11 +352,12 @@ let AttributeToken = (() => {
349
352
  errors.push(e);
350
353
  }
351
354
  const attrs = extAttrs[tag];
352
- if (attrs && !attrs.has(name)
353
- || (type === 'ext-attr' ? tag in htmlAttrs : !/\{\{[^{]+\}\}/u.test(name))
354
- && !htmlAttrs[tag]?.has(name)
355
- && !/^(?:xmlns:[\w:.-]+|data-(?!ooui|mw|parsoid)[^:]*)$/u.test(name)
356
- && (tag === 'meta' || tag === 'link' || !commonHtmlAttrs.has(name))) {
355
+ if (!(attrs && attrs.has(name))
356
+ && (type === 'ext-attr' || !/\{\{[^{]+\}\}/u.test(name))
357
+ && (type === 'ext-attr' && !(tag in htmlAttrs)
358
+ || !htmlAttrs[tag]?.has(name)
359
+ && !/^(?:xmlns:[\w:.-]+|data-(?!ooui|mw|parsoid)[^:]*)$/u.test(name)
360
+ && (tag === 'meta' || tag === 'link' || !commonHtmlAttrs.has(name)))) {
357
361
  errors.push((0, lint_1.generateForChild)(firstChild, rect, 'illegal-attr', 'illegal attribute name'));
358
362
  }
359
363
  else if (obsoleteAttrs[tag]?.has(name)) {
@@ -117,7 +117,7 @@ class AttributesToken extends index_2.Token {
117
117
  while (mt) {
118
118
  const { index, 0: full, 1: key, 2: equal, 3: quoteStart, 4: quoted, 5: quoteEnd, 6: unquoted } = mt;
119
119
  out += attr.slice(lastIndex, index);
120
- if (/^(?:[\w:]|\0\d+[t!~{}+-]\x7F)(?:[\w:.-]|\0\d+[t!~{}+-]\x7F)*$/u.test((0, string_1.removeComment)(key).trim())) {
120
+ if (/^(?:[\w:]|\0\d+t\x7F)(?:[\w:.-]|\0\d+t\x7F)*$/u.test((0, string_1.removeComment)(key).trim())) {
121
121
  const value = quoted ?? unquoted, quotes = [quoteStart, quoteEnd],
122
122
  // @ts-expect-error abstract class
123
123
  token = new attribute_1.AttributeToken(toAttributeType(type), name, key, equal, value, quotes, config, accum);
@@ -24,6 +24,10 @@ export declare abstract class GalleryToken extends Token {
24
24
  abstract get previousElementSibling(): AttributesToken;
25
25
  abstract get parentElement(): ExtToken | undefined;
26
26
  get type(): 'ext-inner';
27
+ /** 图片宽度 */
28
+ get widths(): number;
29
+ /** 图片高度 */
30
+ get heights(): number;
27
31
  /** 所有图片 */
28
32
  get images(): GalleryImageToken[];
29
33
  /** @param inner 标签内部wikitext */
@@ -8,6 +8,7 @@ const noinclude_1 = require("./nowiki/noinclude");
8
8
  /* NOT FOR BROWSER */
9
9
  const debug_1 = require("../util/debug");
10
10
  const constants_1 = require("../util/constants");
11
+ const html_1 = require("../util/html");
11
12
  /* NOT FOR BROWSER END */
12
13
  /**
13
14
  * gallery标签
@@ -19,6 +20,14 @@ class GalleryToken extends index_2.Token {
19
20
  return 'ext-inner';
20
21
  }
21
22
  /* NOT FOR BROWSER */
23
+ /** 图片宽度 */
24
+ get widths() {
25
+ return this.#getSize('widths');
26
+ }
27
+ /** 图片高度 */
28
+ get heights() {
29
+ return this.#getSize('heights');
30
+ }
22
31
  /** 所有图片 */
23
32
  get images() {
24
33
  return this.childNodes.filter((0, debug_1.isToken)('gallery-image'));
@@ -140,6 +149,18 @@ class GalleryToken extends index_2.Token {
140
149
  }
141
150
  return super.insertAt(token, i);
142
151
  }
152
+ /**
153
+ * 获取图片的宽度或高度
154
+ * @param key `widths` 或 `heights`
155
+ */
156
+ #getSize(key) {
157
+ const widths = this.parentNode?.getAttr(key), mt = typeof widths === 'string' && /^(\d+)\s*(?:px)?$/u.exec(widths)?.[1];
158
+ return mt && Number(mt) || 120;
159
+ }
160
+ /** @private */
161
+ toHtmlInternal() {
162
+ return (0, html_1.html)(this.childNodes.filter((0, debug_1.isToken)('gallery-image')), '\n');
163
+ }
143
164
  }
144
165
  exports.GalleryToken = GalleryToken;
145
166
  constants_1.classes['GalleryToken'] = __filename;
@@ -77,7 +77,9 @@ class FileToken extends base_1.LinkBaseToken {
77
77
  }
78
78
  /** 图片宽度 */
79
79
  get width() {
80
- return this.size?.width;
80
+ return this.type === 'gallery-image'
81
+ ? this.parentNode?.widths.toString()
82
+ : this.size?.width;
81
83
  }
82
84
  set width(width) {
83
85
  const arg = this.getArg('width');
@@ -90,7 +92,9 @@ class FileToken extends base_1.LinkBaseToken {
90
92
  }
91
93
  /** 图片高度 */
92
94
  get height() {
93
- return this.size?.height;
95
+ return this.type === 'gallery-image'
96
+ ? this.parentNode?.heights.toString()
97
+ : this.size?.height;
94
98
  }
95
99
  set height(height) {
96
100
  const arg = this.getArg('width');
@@ -306,7 +310,7 @@ class FileToken extends base_1.LinkBaseToken {
306
310
  toHtmlInternal(_, nocc) {
307
311
  /** @ignore */
308
312
  const isInteger = (n) => Boolean(n && /^\d+$/u.test(n));
309
- const { link, width, height } = this, file = this.getAttribute('title'), fr = this.getFrame(), manual = fr instanceof title_1.Title, visibleCaption = manual || fr === 'thumbnail' || fr === 'framed', caption = this.getArg('caption')?.toHtmlInternal(true, nocc) ?? '', titleFromCaption = visibleCaption ? '' : (0, string_1.sanitizeAlt)(caption), hasLink = manual || link !== file, title = titleFromCaption || (hasLink && typeof link !== 'string' ? link.getTitleAttr() : ''), titleAttr = title && ` title="${title}"`, alt = (0, string_1.sanitizeAlt)(this.getArg('alt')?.toHtmlInternal(true)) ?? titleFromCaption, horiz = this.getHorizAlign() ?? '', vert = this.getVertAlign() ?? '', className = `${horiz ? `mw-halign-${horiz}` : vert && `mw-valign-${vert}`}${this.getValue('border') ? ' mw-image-border' : ''} ${(0, string_1.sanitizeAlt)(this.getValue('class')) ?? ''}`.trim(), classAttr = className && ` class="${className}"`, img = `<img${alt && ` alt="${alt}"`} src="${(manual ? fr : file).getUrl()}" class="mw-file-element"${isInteger(width) ? ` width="${width}"` : ''}${isInteger(height) ? ` height="${height}"` : ''}>`;
313
+ const { link, width, height, type } = this, file = this.getAttribute('title'), fr = this.getFrame(), manual = fr instanceof title_1.Title, visibleCaption = manual || fr === 'thumbnail' || fr === 'framed' || type === 'gallery-image', caption = this.getArg('caption')?.toHtmlInternal(true, nocc) ?? '', titleFromCaption = visibleCaption && type !== 'gallery-image' ? '' : (0, string_1.sanitizeAlt)(caption), hasLink = manual || link !== file, title = titleFromCaption || (hasLink && typeof link !== 'string' ? link.getTitleAttr() : ''), titleAttr = title && ` title="${title}"`, alt = (0, string_1.sanitizeAlt)(this.getArg('alt')?.toHtmlInternal(true)) ?? titleFromCaption, horiz = this.getHorizAlign() ?? '', vert = this.getVertAlign() ?? '', className = `${horiz ? `mw-halign-${horiz}` : vert && `mw-valign-${vert}`}${this.getValue('border') ? ' mw-image-border' : ''} ${(0, string_1.sanitizeAlt)(this.getValue('class')) ?? ''}`.trim(), classAttr = className && ` class="${className}"`, img = `<img${alt && ` alt="${alt}"`} src="${(manual ? fr : file).getUrl()}" class="mw-file-element"${isInteger(width) ? ` width="${width}"` : ''}${isInteger(height) ? ` height="${height}"` : ''}>`;
310
314
  let href = '';
311
315
  if (link) {
312
316
  try {
@@ -326,9 +330,15 @@ class FileToken extends base_1.LinkBaseToken {
326
330
  const a = link
327
331
  ? `<a${href && ` href="${href}"`}${hasLink ? '' : ` class="mw-file-description"`}${titleAttr}${typeof link === 'string' ? ` rel="nofollow"` : ''}>${img}</a>`
328
332
  : `<span${titleAttr}>${img}</span>`;
329
- return (0, html_1.font)(this, horiz || vert || visibleCaption
330
- ? `<figure${classAttr} typeof="mw:File${fr ? `/${manual ? 'Thumb' : frame.get(fr)}` : ''}">${a}<figcaption>${caption}</figcaption></figure>`
331
- : `<span${classAttr}>${a}</span>`);
333
+ if (type !== 'gallery-image') {
334
+ return (0, html_1.font)(this, horiz || vert || visibleCaption
335
+ ? `<figure${classAttr} typeof="mw:File${fr ? `/${manual ? 'Thumb' : frame.get(fr)}` : ''}">${a}<figcaption>${caption}</figcaption></figure>`
336
+ : `<span${classAttr}>${a}</span>`);
337
+ }
338
+ const parent = this.parentNode, mode = parent?.parentNode?.getAttr('mode'), nolines = typeof mode === 'string' && mode.toLowerCase() === 'nolines', padding = nolines ? 0 : 30, showfilename = parent?.parentNode?.getAttr('showfilename') !== undefined;
339
+ return `\t<li class="gallerybox" style="width: ${Number(width) + padding + 5}px">\n\t\t<div class="thumb" style="width: ${Number(width) + padding}px${nolines ? '' : `; height: ${Number(height) + padding}px`}"><span>${a}</span></div>\n\t\t<div class="gallerytext">${showfilename
340
+ ? `<a href="${file.getUrl()}" class="galleryfilename galleryfilename-truncate" title="${file.title}">${file.main}</a>\n`
341
+ : ''}${caption}</div>\n\t</li>`;
332
342
  }
333
343
  }
334
344
  exports.FileToken = FileToken;
@@ -206,6 +206,18 @@ let ExtToken = (() => {
206
206
  firstChild.classList.add('poem');
207
207
  return (0, html_1.font)(this, `<div${firstChild.toHtmlInternal()}>${lastChild.toHtmlInternal(false, nocc).replace(/(?<!^|<hr>)\n(?!$)/gu, '<br>\n')
208
208
  .replace(/^ +/gmu, p => '&nbsp;'.repeat(p.length))}</div>`);
209
+ case 'gallery': {
210
+ const caption = firstChild.getAttrToken('caption'), perrow = parseInt(String(firstChild.getAttr('perrow'))), mode = firstChild.getAttr('mode'), nolines = typeof mode === 'string' && mode.toLowerCase() === 'nolines', padding = nolines ? 9 : 43;
211
+ firstChild.classList.add('gallery');
212
+ if (nolines) {
213
+ firstChild.classList.add('mw-gallery-nolines');
214
+ }
215
+ if (perrow > 0) {
216
+ const style = firstChild.getAttr('style');
217
+ firstChild.setAttr('style', `max-width: ${(lastChild.widths + padding) * perrow}px;${typeof style === 'string' ? style : ''}`);
218
+ }
219
+ return (0, html_1.font)(this, `<ul${firstChild.toHtmlInternal()}>\n${caption ? `\t<li class="gallerycaption">${caption.lastChild.toHtmlInternal(true)}</li>\n` : ''}${lastChild.toHtmlInternal()}\n</ul>`);
220
+ }
209
221
  default:
210
222
  return '';
211
223
  }
@@ -164,12 +164,12 @@ class TranscludeToken extends index_2.Token {
164
164
  this.setAttribute('modifier', this.buildFromStr(this.modifier, constants_1.BuildMethod.String));
165
165
  }
166
166
  super.afterBuild();
167
- /* NOT FOR BROWSER */
168
167
  if (this.isTemplate()) {
169
168
  const isTemplate = this.type === 'template';
170
169
  if (isTemplate) {
171
170
  this.setAttribute('name', this.#getTitle().title);
172
171
  }
172
+ /* NOT FOR BROWSER */
173
173
  /**
174
174
  * 当事件bubble到`parameter`时,将`oldKey`和`newKey`保存进AstEventData。
175
175
  * 当继续bubble到`template`时,处理并删除`oldKey`和`newKey`。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wikiparser-node",
3
- "version": "1.12.4",
3
+ "version": "1.12.5",
4
4
  "description": "A Node.js parser for MediaWiki markup with AST",
5
5
  "keywords": [
6
6
  "mediawiki",