-
Notifications
You must be signed in to change notification settings - Fork 1
Selector (EN)
bhsd edited this page Aug 5, 2024
·
11 revisions
Table of Contents
The design of Token selector is inspired by CSS and jQuery.
✅ Available in the Mini and Browser versions.
✅ Expand
Similar to CSS tag name selector.
// type
var root = Parser.parse([[a]]{{{b}}}');
assert.strictEqual(root.querySelectorAll('link, arg').length, 2);
Expand
Similar to CSS id selector.
// name (main)
var {firstChild} = Parser.parse('<ref/>');
assert(firstChild.matches('ext#ref'));
Expand
Similar to CSS attribute selector.
// attribute (main)
var {firstChild} = Parser.parse('<p id="ab-c" class="c1 c2" style="top:0"/>');
assert.equal(firstChild, '<p id="ab-c" class="c1 c2" style="top:0"/>');
assert(firstChild.matches('[selfClosing]'));
assert(firstChild.matches('[id^=A i]'));
assert(firstChild.matches('[id$=C i]'));
assert(firstChild.matches('[style*=TOP i]'));
assert(firstChild.matches('[name!=P]'));
assert(firstChild.matches('[name=P i]'));
assert(firstChild.matches('[id|=AB i]'));
assert(firstChild.matches('[class~=C1 i]'));
Similar to CSS combinator.
Expand
// descendant combinator (main)
var template = Parser.parse('<poem>[[a]]{{{b}}}{{c}}</poem>')
.querySelector('template');
assert.equal(template, '{{c}}');
assert(template.matches('root template'));
Expand
// child combinator (main)
var template = Parser.parse('<poem>[[a]]{{{b}}}{{c}}</poem>')
.querySelector('template');
assert.equal(template, '{{c}}');
assert(template.matches('root > ext > ext-inner > template'));
Expand
// subsequent-sibling combinator (main)
var template = Parser.parse('<poem>[[a]]{{{b}}}{{c}}</poem>')
.querySelector('template');
assert.equal(template, '{{c}}');
assert(template.matches('link ~ template'));
Expand
// next-sibling combinator (main)
var template = Parser.parse('<poem>[[a]]{{{b}}}{{c}}</poem>')
.querySelector('template');
assert.equal(template, '{{c}}');
assert(template.matches('link + arg + template'));
Similar to CSS and jQuery pseudo selector.
Expand
// root (main)
var root = Parser.parse('');
assert(root.matches(':root'));
Expand
// first-child (main)
var {lastChild} = Parser.parse('a<ref/>');
assert(lastChild.matches(':first-child'));
Expand
// first-of-type (main)
var {lastChild} = Parser.parse([[a]]<ref/>');
assert(lastChild.matches(':first-of-type'));
Expand
// last-child (main)
var {firstChild} = Parser.parse('<ref/>a');
assert(firstChild.matches(':last-child'));
Expand
// last-of-type (main)
var {firstChild} = Parser.parse('<ref/>[[a]]');
assert(firstChild.matches(':last-of-type'));
Expand
// only-child (main)
var {firstChild} = Parser.parse('<ref/>a');
assert(firstChild.matches(':only-child'));
Expand
// only-of-type (main)
var {firstChild} = Parser.parse('<ref/>[[a]]');
assert(firstChild.matches(':only-of-type'));
Expand
// empty (main)
var {firstChild} = Parser.parse('<!---->');
assert(firstChild.matches(':empty'));
Expand
// parent (main)
var {firstChild} = Parser.parse('<!-- -->');
assert(firstChild.matches(':parent'));
Expand
// header (main)
var {firstChild} = Parser.parse('==a==');
assert(firstChild.matches(':header'));
Expand
// hidden (main)
var root = Parser.parse('<!-- -->__nocc__');
assert(root.matches(':hidden'));
展开
// visible (main)
var root = Parser.parse(' ');
assert(root.matches(':visible'));
Expand
// only-whitespace (main)
var root = Parser.parse(' ');
assert(root.matches(':only-whitespace'));
Expand
// any-link (main)
var [
{lastChild: a},
b,
c,
d,
e,
] = Parser.parse('#redirect [[a]]ftp://b PMID 0[//d][[file:e]]')
.children;
assert.equal(a, '[[a]]');
assert.equal(b, 'ftp://b');
assert.equal(c, 'PMID 0');
assert.equal(d, '[//d]');
assert.equal(e, '[[file:e]]');
assert(a.matches(':any-link'));
assert(b.matches(':any-link'));
assert(c.matches(':any-link'));
assert(d.matches(':any-link'));
assert(e.matches(':any-link'));
Expand
// local-link (main)
var [a, b] = Parser.parse([[#a]][[file:b|link=#b]]').childNodes;
assert.equal(a, '[[#a]]');
assert.equal(b, '[[file:b|link=#b]]');
assert(a.matches(':local-link'));
assert(b.matches(':local-link'));
Expand
// invalid (main)
var root = Parser.parse('{|\na\n|}<gallery>b|1px</gallery>'),
a = root.querySelector('table-inter'),
b = root.querySelector('image-parameter');
assert.equal(a, '\na');
assert.equal(b, '1px');
assert(a.matches(':invalid'));
assert(b.matches(':invalid'));
Expand
// required (main)
var a = Parser.parse('{{{a}}}').querySelector('arg-name');
assert.equal(a, 'a');
assert(a.matches(':required'));
Expand
// optional (main)
var a = Parser.parse('{{{|a}}}').querySelector('arg-default');
assert.equal(a, 'a');
assert(a.matches(':optional'));
Expand
// is (main)
var root = Parser.parse('');
assert(root.matches(':is(:root, * + *)'));
Expand
// not (main)
var root = Parser.parse('');
assert(root.matches(':not(arg, * + *)'));
Expand
// nth-child (main)
var {lastChild} = Parser.parse([[a]]<ref/>');
assert(lastChild.matches(':nth-child(2)'));
assert(lastChild.matches(':nth-child(even)'));
assert(lastChild.matches(':nth-child(2n)'));
assert(lastChild.matches(':nth-child(:4:2)'));
Expand
// nth-of-type (main)
var {lastChild} = Parser.parse([[a]]<ref/>');
assert(lastChild.matches(':nth-of-type(1)'));
assert(lastChild.matches(':nth-of-type(odd)'));
assert(lastChild.matches(':nth-of-type(-2n+3)'));
assert(lastChild.matches(':nth-of-type(1:)'));
Expand
// nth-last-child (main)
var {firstChild} = Parser.parse('<ref/>[[a]]');
assert(firstChild.matches(':nth-last-child(2)'));
assert(firstChild.matches(':nth-last-child(even)'));
assert(firstChild.matches(':nth-last-child(2n-2)'));
assert(firstChild.matches(':nth-last-child(:3)'));
Expand
// nth-last-of-type (main)
var {firstChild} = Parser.parse('<ref/>[[a]]');
assert(firstChild.matches(':nth-last-of-type(1)'));
assert(firstChild.matches(':nth-last-of-type(odd)'));
assert(firstChild.matches(':nth-last-of-type(3n+1)'));
assert(firstChild.matches(':nth-last-of-type(:)'));
Expand
// contains (main)
var root = Parser.parse('[[a]]');
assert(root.matches(':contains([[)'));
Expand
// has (main)
var root = Parser.parse('<ref>[[a]]</ref>');
assert(root.matches(':has(#A, #B)'));
Expand
// lang (main)
var [p, b] = Parser.parse('<poem lang="en"><p lang="zh-cn"><b></poem>')
.querySelectorAll('html');
assert.equal(p, '<p lang="zh-cn">');
assert.equal(b, '<b>');
assert(p.matches(':lang(zh)'));
assert(p.matches(':lang(zh-CN)'));
assert(!p.matches(':lang(en)'));
assert(b.matches(':lang(en)'));
Expand
The format is similar to pseudo selector.
// regex (main)
var {firstChild, lastChild} = Parser.parse([[aa]]<p id=段落>');
assert.equal(firstChild, '[[aa]]');
assert.equal(lastChild, '<p id=段落>');
assert(firstChild.matches(':regex("name, /^A{2,}$/i")#Aa'));
assert(lastChild.matches(String.raw`:regex("id, /\p{L}/u")`));
对维基文本批量执行语法检查的命令行工具
用于维基文本的 ESLint 插件
A command-line tool that performs linting on Wikitext in bulk
ESLint plugin for Wikitext