Skip to content

Commit

Permalink
Merge branch 'mini' into browser
Browse files Browse the repository at this point in the history
  • Loading branch information
bhsd-harry committed Sep 21, 2024
2 parents 26d681c + d7c6401 commit d60a926
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 24 deletions.
8 changes: 6 additions & 2 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,9 @@
"目录",
"目錄",
"archivedtalk",
"notalk"
"已存档讨论",
"notalk",
"禁用讨论"
],
[
"EXPECTUNUSEDCATEGORY",
Expand All @@ -767,7 +769,9 @@
"静态重定向",
"靜態重新導向",
"NOGLOBAL",
"EXPECTED_UNCONNECTED_PAGE"
"禁用全域用户页",
"EXPECTED_UNCONNECTED_PAGE",
"EXPECTUNUSEDTEMPLATE"
]
],
"protocol": "bitcoin:|ftp://|ftps://|geo:|git://|gopher://|http://|https://|irc://|ircs://|magnet:|mailto:|matrix:|mms://|news:|nntp://|redis://|sftp://|sip:|sips:|sms:|ssh://|svn://|tel:|telnet://|urn:|worldwind://|xmpp:",
Expand Down
7 changes: 6 additions & 1 deletion config/enwiki.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@
"101": "Portal talk",
"118": "Draft",
"119": "Draft talk",
"126": "MOS",
"127": "MOS talk",
"710": "TimedText",
"711": "TimedText talk",
"828": "Module",
Expand Down Expand Up @@ -158,6 +160,8 @@
"portal talk": 101,
"draft": 118,
"draft talk": 119,
"mos": 126,
"mos talk": 127,
"timedtext": 710,
"timedtext talk": 711,
"module": 828,
Expand Down Expand Up @@ -369,7 +373,8 @@
"NONEWSECTIONLINK",
"STATICREDIRECT",
"NOGLOBAL",
"EXPECTED_UNCONNECTED_PAGE"
"EXPECTED_UNCONNECTED_PAGE",
"EXPECTUNUSEDTEMPLATE"
]
],
"protocol": "bitcoin:|ftp://|ftps://|geo:|git://|gopher://|http://|https://|irc://|ircs://|magnet:|mailto:|matrix:|mms://|news:|nntp://|redis://|sftp://|sip:|sips:|sms:|ssh://|svn://|tel:|telnet://|urn:|worldwind://|xmpp:",
Expand Down
14 changes: 12 additions & 2 deletions config/zhwiki.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@
"103": "WikiProject talk",
"118": "Draft",
"119": "Draft talk",
"126": "MOS",
"127": "MOS talk",
"710": "TimedText",
"711": "TimedText talk",
"828": "Module",
Expand Down Expand Up @@ -260,6 +262,8 @@
"draft talk": 119,
"草稿讨论": 119,
"草稿討論": 119,
"mos": 126,
"mos talk": 127,
"timedtext": 710,
"timedtext talk": 711,
"module": 828,
Expand Down Expand Up @@ -686,7 +690,11 @@
"無目錄",
"toc",
"目录",
"目錄"
"目錄",
"archivedtalk",
"已存档讨论",
"notalk",
"禁用讨论"
],
[
"EXPECTUNUSEDCATEGORY",
Expand All @@ -707,7 +715,9 @@
"静态重定向",
"靜態重新導向",
"NOGLOBAL",
"EXPECTED_UNCONNECTED_PAGE"
"禁用全域用户页",
"EXPECTED_UNCONNECTED_PAGE",
"EXPECTUNUSEDTEMPLATE"
]
],
"protocol": "bitcoin:|ftp://|ftps://|geo:|git://|gopher://|http://|https://|irc://|ircs://|magnet:|mailto:|matrix:|mms://|news:|nntp://|redis://|sftp://|sip:|sips:|sms:|ssh://|svn://|tel:|telnet://|urn:|worldwind://|xmpp:",
Expand Down
22 changes: 5 additions & 17 deletions lib/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,15 @@ import {
print,
} from '../util/string';
import {setChildNodes} from '../util/debug';
import {getCondition} from '../parser/selector';
import {AstNode} from './node';
import type {
LintError,
AST,
} from '../base';
import type {TokenPredicate} from '../parser/selector';
import type {AstNodes, AstText, Token} from '../internal';

// @ts-expect-error unconstrained predicate
declare type TokenPredicate<T = Token> = (token: Token) => token is T;

/**
* 将选择器转化为类型谓词
* @param selector 选择器
*/
const getCondition = <T>(selector: string): TokenPredicate<T> => (
({type, name}): boolean => selector.split(',').some(str => {
const [t, ...ns] = str.trim().split('#');
return (!t || t === type) && ns.every(n => n === name);
})
) as TokenPredicate<T>;

/** 类似HTMLElement */
export abstract class AstElement extends AstNode {
declare readonly name?: string;
Expand Down Expand Up @@ -84,7 +72,7 @@ export abstract class AstElement extends AstNode {
* @param selector 选择器
*/
closest<T = Token>(selector: string): T | undefined {
const condition = getCondition<T>(selector);
const condition = getCondition<T>(selector, this);
let {parentNode} = this;
while (parentNode) {
if (condition(parentNode)) {
Expand Down Expand Up @@ -119,7 +107,7 @@ export abstract class AstElement extends AstNode {
* @param selector 选择器
*/
querySelector<T = Token>(selector: string): T | undefined {
const condition = getCondition<T>(selector);
const condition = getCondition<T>(selector, this);
return this.#getElementBy(condition);
}

Expand All @@ -145,7 +133,7 @@ export abstract class AstElement extends AstNode {
* @param selector 选择器
*/
querySelectorAll<T = Token>(selector: string): T[] {
const condition = getCondition<T>(selector);
const condition = getCondition<T>(selector, this);
return this.#getElementsBy(condition);
}

Expand Down
20 changes: 20 additions & 0 deletions parser/selector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type {AstElement} from '../lib/element';
import type {
Token,
} from '../internal';

// @ts-expect-error unconstrained predicate
export type TokenPredicate<T = Token> = (token: AstElement) => token is T;

/**
* 将选择器转化为类型谓词
* @param selector 选择器
* @param scope 作用对象
* @param has `:has()`伪选择器
*/
export const getCondition = <T>(selector: string, scope: AstElement, has?: Token): TokenPredicate<T> => (
({type, name}): boolean => selector.split(',').some(str => {
const [t, ...ns] = str.trim().split('#');
return (!t || t === type) && ns.every(n => n === name);
})
) as TokenPredicate<T>;
4 changes: 2 additions & 2 deletions src/transclude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export abstract class TranscludeToken extends Token {
getPossibleValues(): Token[] {
const {type, name, childNodes} = this;
if (type === 'template') {
throw new Error(`TranscludeToken.getPossibleValues method is only for specific magic words!`);
throw new Error('TranscludeToken.getPossibleValues method is only for specific magic words!');
}
let start: number;
switch (name) {
Expand All @@ -352,7 +352,7 @@ export abstract class TranscludeToken extends Token {
start = 3;
break;
default:
throw new Error(`TranscludeToken.getPossibleValues method is only for specific magic words!`);
throw new Error('TranscludeToken.getPossibleValues method is only for specific magic words!');
}
const queue = (childNodes.slice(start, start + 2) as ParameterToken[]).map(({childNodes: [, value]}) => value);
for (let i = 0; i < queue.length;) {
Expand Down

0 comments on commit d60a926

Please sign in to comment.