Skip to content

Commit

Permalink
Merge branch 'mini' into noprint
Browse files Browse the repository at this point in the history
  • Loading branch information
bhsd-harry committed Sep 21, 2024
2 parents 73887e5 + d7c6401 commit 713be1b
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 @@ -740,7 +740,9 @@
"静态重定向",
"靜態重新導向",
"NOGLOBAL",
"EXPECTED_UNCONNECTED_PAGE"
"禁用全域用户页",
"EXPECTED_UNCONNECTED_PAGE",
"EXPECTUNUSEDTEMPLATE"
],
{
"forcetoc": "forcetoc",
Expand Down Expand Up @@ -768,7 +770,9 @@
"目录": "toc",
"目錄": "toc",
"archivedtalk": "archivedtalk",
"notalk": "notalk"
"已存档讨论": "archivedtalk",
"notalk": "notalk",
"禁用讨论": "notalk"
}
],
"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 @@ -357,7 +361,8 @@
"NONEWSECTIONLINK",
"STATICREDIRECT",
"NOGLOBAL",
"EXPECTED_UNCONNECTED_PAGE"
"EXPECTED_UNCONNECTED_PAGE",
"EXPECTUNUSEDTEMPLATE"
],
{
"forcetoc": "forcetoc",
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 @@ -682,7 +686,9 @@
"静态重定向",
"靜態重新導向",
"NOGLOBAL",
"EXPECTED_UNCONNECTED_PAGE"
"禁用全域用户页",
"EXPECTED_UNCONNECTED_PAGE",
"EXPECTUNUSEDTEMPLATE"
],
{
"forcetoc": "forcetoc",
Expand All @@ -708,7 +714,11 @@
"無目錄": "notoc",
"toc": "toc",
"目录": "toc",
"目錄": "toc"
"目錄": "toc",
"archivedtalk": "archivedtalk",
"已存档讨论": "archivedtalk",
"notalk": "notalk",
"禁用讨论": "notalk"
}
],
"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 @@ -2,26 +2,14 @@ import {
text,
} from '../util/string';
import {setChildNodes} from '../util/debug';
import {getCondition} from '../parser/selector';
import {AstNode} from './node';
import type {
LintError,
} 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 @@ -82,7 +70,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 @@ -117,7 +105,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 @@ -143,7 +131,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 @@ -336,7 +336,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 @@ -350,7 +350,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 713be1b

Please sign in to comment.