Skip to content

Commit

Permalink
fix(AstElement): :regex()
Browse files Browse the repository at this point in the history
  • Loading branch information
bhsd-harry committed Jun 9, 2024
1 parent e1bd2df commit dfc202c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
**Fixed**

- [`AstNode.font`](https://github.com/bhsd-harry/wikiparser-node/wiki/AstNode#font), [`AstNode.bold`](https://github.com/bhsd-harry/wikiparser-node/wiki/AstNode#bold) and [`AstNode.italic`](https://github.com/bhsd-harry/wikiparser-node/wiki/AstNode#italic) for external links which have a lower precedence than apostrophes
- [Pseudo selector](https://github.com/bhsd-harry/wikiparser-node/wiki/Selector#伪选择器) `:any-link` for [`FileToken`](https://github.com/bhsd-harry/wikiparser-node/wiki/FileToken)
- [Pseudo selector](https://github.com/bhsd-harry/wikiparser-node/wiki/Selector#伪选择器) `:lang()`
- Pseudo selector [`:any-link`](https://github.com/bhsd-harry/wikiparser-node/wiki/Selector#any-link) for [`FileToken`](https://github.com/bhsd-harry/wikiparser-node/wiki/FileToken)
- Pseudo selector [`:lang()`](https://github.com/bhsd-harry/wikiparser-node/wiki/Selector#lang)
- Pseudo selector [`:regex()`](https://github.com/bhsd-harry/wikiparser-node/wiki/Selector#正则选择器) for [`AttributesToken`](https://github.com/bhsd-harry/wikiparser-node/wiki/AttributesToken)

**Changed**

Expand Down Expand Up @@ -379,7 +380,7 @@
**Changed**

- `HiddenToken` child nodes of [`GalleryToken`](https://github.com/bhsd-harry/wikiparser-node/wiki/GalleryToken) are replaced by [`NoincludeToken`](https://github.com/bhsd-harry/wikiparser-node/wiki/NoincludeToken)
- The pseudo selector [`:invalid`](https://github.com/bhsd-harry/wikiparser-node/wiki/Selector#伪选择器) now reports invalid [`ImageParameterToken`](https://github.com/bhsd-harry/wikiparser-node/wiki/ImageParameterToken) instead of redundant child nodes of [`ArgToken`](https://github.com/bhsd-harry/wikiparser-node/wiki/ArgToken)
- The pseudo selector [`:invalid`](https://github.com/bhsd-harry/wikiparser-node/wiki/Selector#invalid) now reports invalid [`ImageParameterToken`](https://github.com/bhsd-harry/wikiparser-node/wiki/ImageParameterToken) instead of redundant child nodes of [`ArgToken`](https://github.com/bhsd-harry/wikiparser-node/wiki/ArgToken)

**Removed**

Expand Down
35 changes: 21 additions & 14 deletions lib/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,21 @@ export abstract class AstElement extends AstNode {
.includes(childNodes.indexOf(this as AstElement as Token));
}

/**
* 获取属性
* @param key 属性键
*/
#getAttr(this: AstElement & Partial<AttributesParentBase>, key: string): unknown {
if (typeof this.getAttr === 'function') {
const attr = this.getAttr(key);
if (attr !== undefined) {
return attr;
}
}
const val = this.getAttribute(key);
return val instanceof RegExp ? val.source : val;
}

/**
* 检查是否符合属性选择器
* @param key 属性键
Expand All @@ -427,18 +442,10 @@ export abstract class AstElement extends AstNode {
if (!(key in this) && (!isAttr || !this.hasAttr!(key))) {
return equal === '!=';
}
const v = toCase(val, i);
let thisVal = this.getAttribute(key);
if (isAttr) {
const attr = this.getAttr!(key);
if (attr !== undefined) {
thisVal = attr === true ? '' : attr;
}
}
const v = toCase(val, i),
thisVal = this.#getAttr(key);
if (!equal) {
return thisVal !== undefined && thisVal !== false;
} else if (thisVal instanceof RegExp) {
thisVal = thisVal.source;
}
if (equal === '~=') {
const thisVals = typeof thisVal === 'string' ? thisVal.split(/\s/u) : thisVal;
Expand Down Expand Up @@ -470,7 +477,7 @@ export abstract class AstElement extends AstNode {
* @throws `SyntaxError` 错误的正则伪选择器
* @throws `SyntaxError` 未定义的伪选择器
*/
#matches(step: SelectorArray): boolean {
#matches(this: AstElement & Partial<AttributesParentBase>, step: SelectorArray): boolean {
const {parentNode, type, name, childNodes, link} = this as AstElement & {link?: string | Title},
children = parentNode?.children,
childrenOfType = children?.filter(({type: t}) => t === type),
Expand Down Expand Up @@ -555,8 +562,8 @@ export abstract class AstElement extends AstNode {
return Boolean(this.querySelector<Token>(s));
case 'lang': {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
/^zh(?:-|$)/u;
const regex = new RegExp(`^${s}(?:-|$)`, 'u');
/^zh(?:-|$)/iu;
const regex = new RegExp(`^${s}(?:-|$)`, 'iu');
for (let node: AstElement | undefined = this as AstElement; node; node = node.parentNode) {
const result = matchesLang(node, regex);
if (result !== undefined) {
Expand All @@ -573,7 +580,7 @@ export abstract class AstElement extends AstNode {
);
}
try {
return new RegExp(mt[2], mt[3]).test(String(this.getAttribute(mt[1].trim())));
return new RegExp(mt[2], mt[3]).test(String(this.#getAttr(mt[1].trim())));
} catch {
throw new SyntaxError(`Invalid regular expression: /${mt[2]}/${mt[3]}`);
}
Expand Down

0 comments on commit dfc202c

Please sign in to comment.