a CSS selector compiler/engine
CSSselect turns CSS selectors into functions that tests if elements match them. When searching for elements, testing is executed "from the top", similar to how browsers execute CSS selectors.
In its default configuration, CSSselect queries the DOM structure of the
var CSSselect = ;
querycan be either a function or a string. If it's a string, the string is compiled as a CSS selector.
elemscan be either an array of elements, or a single element. If it is an element, its children will be used (so we're working with an array again).
optionsis described below.
elems, returns an array containing all matches.
Compiles the query, returns the function.
Tests whether or not an element is matched by
query can be either a CSS selector or a function.
Arguments are the same as for
CSSselect(query, elems). Only returns the first match, or
null if there was no match.
xmlMode: When enabled, tag names will be case-sensitive. Default:
strict: Limits the module to only use CSS3 selectors. Default:
rootFunc: The last function in the stack. Will be called with the last element that's looked at. Should return
trueif it shouldn't be called again for every matching subselector.
The common approach of executing CSS selectors (used eg. by
qwery) is to execute every component of the selector in order, from left to right. The selector
a b for example will first look for
a elements, then search these for
While this works, it has some downsides: Children of
as will be checked multiple times, first, to check if they are also
as, then, for every superior
a once, if they are
bs. Using Big O notation, that would be
The far more efficient approach is to first look for
b elements, then check if they have superior
a elements: Using big O notation again, that would be
And that's exactly what CSSselect does.
By stacking functions!
//TODO: Better explanation. For now, if you're interested, have a look at the source code.
[attr=foo]), with supported comparisons:
ican be added after the comparison to make the comparison case-insensitive (eg.
[attr=foo i]) *
*: Non-standard extensions