Selector (EN) - bhsd-harry/wikiparser-node GitHub Wiki

Table of Contents

Other Languages

Introduction

The design of Token selector is inspired by CSS and jQuery.

✅ Available in the Mini and Browser versions.

type

✅ Expand

Similar to CSS tag name selector.

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

name

Expand

Similar to CSS id selector.

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

attribute

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

combinator

Expand

Similar to CSS combinator.

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

pseudo selector

Similar to CSS and jQuery pseudo selector.

root

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

first-child

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

first-of-type

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

last-child

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

last-of-type

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

only-child

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

only-of-type

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

empty

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

parent

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

header

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

hidden

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

visible

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

only-whitespace

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

any-link

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

local-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'));

invalid

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

required

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

optional

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

is

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

not

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

nth-child

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

nth-of-type

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

nth-last-child

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

nth-last-of-type

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

contains

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

has

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

lang

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

regex selector

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