Selector - bhsd-harry/wikiparser-node GitHub Wiki

目录

Other Languages

简介

Token 选择器的设计仿照了 CSSjQuery 的选择器。

✅ 在 MiniBrowser 版本中可用。

type

✅ 展开

类比 CSS 元素选择器。

// type
var root = Parser.parse([[a]]{{{b}}}');
assert.strictEqual(root.querySelectorAll('link, arg').length, 2);

name

展开

类比 CSS id 选择器。

// name (main)
var {firstChild} = Parser.parse('<ref/>');
assert(firstChild.matches('ext#ref'));

属性

展开

类比 CSS 属性选择器。

// 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]'));

组合器

展开

类比 CSS 组合器。

// combinator (main)
var template = Parser.parse('<poem>[[a]]{{{b}}}{{c}}</poem>')
	.querySelector('template');
assert.equal(template, '{{c}}');
assert(template.matches(':root template'));
assert(template.matches('arg + template'));
assert(template.matches('link ~ template'));
assert(template.matches('ext > * > template'));

伪选择器

类比 CSS 和 jQuery 伪选择器。

root

展开
// root (main)
var root = Parser.parse('');
assert(root.matches(':root'));

first-child

展开
// first-child (main)
var {lastChild} = Parser.parse('a<ref/>');
assert(lastChild.matches(':first-child'));

first-of-type

展开
// first-of-type (main)
var {lastChild} = Parser.parse([[a]]<ref/>');
assert(lastChild.matches(':first-of-type'));

last-child

展开
// last-child (main)
var {firstChild} = Parser.parse('<ref/>a');
assert(firstChild.matches(':last-child'));

last-of-type

展开
// last-of-type (main)
var {firstChild} = Parser.parse('<ref/>[[a]]');
assert(firstChild.matches(':last-of-type'));

only-child

展开
// only-child (main)
var {firstChild} = Parser.parse('<ref/>a');
assert(firstChild.matches(':only-child'));

only-of-type

展开
// only-of-type (main)
var {firstChild} = Parser.parse('<ref/>[[a]]');
assert(firstChild.matches(':only-of-type'));

empty

展开
// empty (main)
var {firstChild} = Parser.parse('<!---->');
assert(firstChild.matches(':empty'));

parent

展开
// parent (main)
var {firstChild} = Parser.parse('<!-- -->');
assert(firstChild.matches(':parent'));

header

展开
// header (main)
var {firstChild} = Parser.parse('==a==');
assert(firstChild.matches(':header'));

hidden

展开
// hidden (main)
var root = Parser.parse('<!-- -->__nocc__');
assert(root.matches(':hidden'));

visible

展开
// visible (main)
var root = Parser.parse(' ');
assert(root.matches(':visible'));

only-whitespace

展开
// only-whitespace (main)
var root = Parser.parse(' ');
assert(root.matches(':only-whitespace'));

any-link

展开
// any-link (main)
var [a, b, c, d, e] = Parser.parse([[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'));

local-link

展开
// 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'));

invalid

展开
// 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'));

required

展开
// required (main)
var a = Parser.parse('{{{a}}}').querySelector('arg-name');
assert.equal(a, 'a');
assert(a.matches(':required'));

optional

展开
// optional (main)
var a = Parser.parse('{{{|a}}}').querySelector('arg-default');
assert.equal(a, 'a');
assert(a.matches(':optional'));

is

展开
// is (main)
var root = Parser.parse('');
assert(root.matches(':is(:root, * + *)'));

not

展开
// not (main)
var root = Parser.parse('');
assert(root.matches(':not(arg, * + *)'));

nth-child

展开
// 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)'));

nth-of-type

展开
// 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:)'));

nth-last-child

展开
// 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)'));

nth-last-of-type

展开
// 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(:)'));

contains

展开
// contains (main)
var root = Parser.parse('[[a]]');
assert(root.matches(':contains([[)'));

has

展开
// has (main)
var root = Parser.parse('<ref>[[a]]</ref>');
assert(root.matches(':has(#A, #B)'));

lang

展开
// 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)'));

正则选择器

展开

格式类似伪选择器。

// 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")`));
⚠️ **GitHub.com Fallback** ⚠️