wikiparser-node 1.5.4 → 1.5.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.
@@ -33,6 +33,7 @@ const matchesLang = ({ attributes }, regex) => {
33
33
  const lang = attributes?.['lang'];
34
34
  return typeof lang === 'string' && regex.test(lang);
35
35
  };
36
+ const primitives = new Set(['string', 'number', 'boolean', 'undefined']);
36
37
  /* NOT FOR BROWSER END */
37
38
  /** 类似HTMLElement */
38
39
  class AstElement extends node_1.AstNode {
@@ -316,9 +317,6 @@ class AstElement extends node_1.AstNode {
316
317
  if (!(key in this) && (!isAttr || !this.hasAttr(key))) {
317
318
  return equal === '!=';
318
319
  }
319
- else if (!equal) {
320
- return true;
321
- }
322
320
  const v = toCase(val, i);
323
321
  let thisVal = this.getAttribute(key);
324
322
  if (isAttr) {
@@ -327,7 +325,10 @@ class AstElement extends node_1.AstNode {
327
325
  thisVal = attr === true ? '' : attr;
328
326
  }
329
327
  }
330
- if (thisVal instanceof RegExp) {
328
+ if (!equal) {
329
+ return thisVal !== undefined && thisVal !== false;
330
+ }
331
+ else if (thisVal instanceof RegExp) {
331
332
  thisVal = thisVal.source;
332
333
  }
333
334
  if (equal === '~=') {
@@ -335,10 +336,10 @@ class AstElement extends node_1.AstNode {
335
336
  return Boolean(thisVals?.[Symbol.iterator])
336
337
  && [...thisVals].some(w => typeof w === 'string' && toCase(w, i) === v);
337
338
  }
338
- else if (typeof thisVal !== 'string') {
339
+ else if (!primitives.has(typeof thisVal) && !(thisVal instanceof title_1.Title)) {
339
340
  throw new RangeError(`复杂属性 ${key} 不能用于选择器!`);
340
341
  }
341
- const stringVal = toCase(thisVal, i);
342
+ const stringVal = toCase(String(thisVal), i);
342
343
  switch (equal) {
343
344
  case '|=':
344
345
  return stringVal === v || stringVal.startsWith(`${v}-`);
package/dist/lib/node.js CHANGED
@@ -111,9 +111,7 @@ class AstNode {
111
111
  return new Set(this.#optional);
112
112
  /* NOT FOR BROWSER END */
113
113
  }
114
- return key in this
115
- ? String(this[key])
116
- : undefined;
114
+ return this[key];
117
115
  }
118
116
  /** @private */
119
117
  setAttribute(key, value) {
@@ -79,7 +79,7 @@ let HeadingToken = (() => {
79
79
  }
80
80
  /** 内部wikitext */
81
81
  get innerText() {
82
- return this.firstChild.text();
82
+ return this.firstChild.text().trim();
83
83
  }
84
84
  /** @throws `Error` 首尾包含`=` */
85
85
  set innerText(text) {
@@ -26,8 +26,6 @@ export declare abstract class ImagemapToken extends Token {
26
26
  abstract get parentElement(): ExtToken | undefined;
27
27
  /** 图片 */
28
28
  get image(): GalleryImageToken | undefined;
29
- /** 链接 */
30
- get links(): ImagemapLinkToken[];
31
29
  /** @param inner 标签内部wikitext */
32
30
  constructor(inner?: string, config?: Parser.Config, accum?: Token[]);
33
31
  /** @override */
@@ -21,12 +21,6 @@ class ImagemapToken extends index_2.Token {
21
21
  get image() {
22
22
  return this.childNodes.find((0, debug_1.isToken)('imagemap-image'));
23
23
  }
24
- /* NOT FOR BROWSER */
25
- /** 链接 */
26
- get links() {
27
- return this.childNodes.filter((0, debug_1.isToken)('imagemap-link'));
28
- }
29
- /* NOT FOR BROWSER END */
30
24
  /** @param inner 标签内部wikitext */
31
25
  constructor(inner, config = index_1.default.getConfig(), accum = []) {
32
26
  super(undefined, config, accum, {
@@ -6,7 +6,7 @@ import { AstText } from '../lib/text';
6
6
  import type { LintError } from '../base';
7
7
  import type { Range } from '../lib/ranges';
8
8
  import type { Title } from '../lib/title';
9
- import type { AstNodes, IncludeToken, HtmlToken, ExtToken, TranscludeToken, CommentToken, FileToken, LinkToken, ExtLinkToken, MagicLinkToken, ImageParameterToken, ImagemapLinkToken } from '../internal';
9
+ import type { AstNodes, IncludeToken, HtmlToken, ExtToken, TranscludeToken, CommentToken, FileToken, LinkToken, ExtLinkToken, MagicLinkToken, ImageParameterToken } from '../internal';
10
10
  import type { CaretPosition } from '../lib/node';
11
11
  import type { TokenTypes } from '../util/constants';
12
12
  declare type TagToken = IncludeToken | ExtToken | HtmlToken;
@@ -20,7 +20,7 @@ export declare class Token extends AstElement {
20
20
  /** 所有图片,包括图库 */
21
21
  get images(): FileToken[];
22
22
  /** 所有内链、外链和自由外链 */
23
- get links(): (LinkToken | ExtLinkToken | MagicLinkToken | ImageParameterToken | ImagemapLinkToken)[];
23
+ get links(): (LinkToken | ExtLinkToken | MagicLinkToken | ImageParameterToken)[];
24
24
  /** 所有模板和模块 */
25
25
  get embeds(): TranscludeToken[];
26
26
  /** @class */
@@ -12,7 +12,7 @@ class LinkToken extends base_1.LinkBaseToken {
12
12
  type = 'link';
13
13
  /** 链接显示文字 */
14
14
  get innerText() {
15
- return this.length > 1 ? this.lastChild.text() : this.firstChild.text().replace(/^\s*:/u, '');
15
+ return this.length > 1 ? this.lastChild.text() : this.firstChild.text().replace(/^\s*:?/u, '');
16
16
  }
17
17
  set innerText(text) {
18
18
  this.setLinkText(text);
@@ -75,14 +75,26 @@ let ListToken = (() => {
75
75
  /** 获取列表行的范围 */
76
76
  getRange() {
77
77
  const range = this.createRange();
78
+ let nDt = this.dt ? this.innerText.split(';').length - 2 : 0;
78
79
  range.setStartBefore(this);
79
80
  let { nextSibling } = this;
80
81
  while (nextSibling && (nextSibling.type !== 'text' || !nextSibling.data.includes('\n'))) {
82
+ if (nextSibling.type === 'dd') {
83
+ nDt -= nextSibling.indent;
84
+ }
85
+ if (nDt < 0) {
86
+ break;
87
+ }
81
88
  ({ nextSibling } = nextSibling);
82
89
  }
83
90
  if (nextSibling) {
84
- const i = nextSibling.data.indexOf('\n');
85
- range.setEnd(nextSibling, i);
91
+ if (nDt < 0) {
92
+ range.setEndBefore(nextSibling);
93
+ }
94
+ else {
95
+ const i = nextSibling.data.indexOf('\n');
96
+ range.setEnd(nextSibling, i);
97
+ }
86
98
  }
87
99
  else {
88
100
  const { parentNode } = this;
@@ -89,7 +89,7 @@ let TdToken = (() => {
89
89
  }
90
90
  /** 内部wikitext */
91
91
  get innerText() {
92
- return this.lastChild.text();
92
+ return this.lastChild.text().trim();
93
93
  }
94
94
  set innerText(text) {
95
95
  this.lastChild.replaceChildren(...index_1.default.parse(text, true, undefined, this.getAttribute('config')).childNodes);
@@ -245,7 +245,7 @@ let TdToken = (() => {
245
245
  /** @private */
246
246
  setAttribute(key, value) {
247
247
  if (key === 'innerSyntax') {
248
- this.#innerSyntax = value ?? '';
248
+ this.#innerSyntax = (value ?? '');
249
249
  }
250
250
  else {
251
251
  super.setAttribute(key, value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wikiparser-node",
3
- "version": "1.5.4",
3
+ "version": "1.5.5",
4
4
  "description": "A Node.js parser for MediaWiki markup with AST",
5
5
  "keywords": [
6
6
  "mediawiki",