a CSS selector compiler/engine
npm install css-selectA CSS selector compiler and engine
As a compiler, css-select turns CSS selectors into functions that tests if
elements match them.
As an engine, css-select looks through a DOM tree, searching for elements.
Elements are tested "from the top", similar to how browsers execute CSS
selectors.
In its default configuration, css-select queries the DOM structure of thedomhandler module (also known as
htmlparser2 DOM). To query alternative DOM structures, see Options
below.
Features:
- ๐ฌ Full implementation of CSS3 selectors, as well as most CSS4 selectors
- ๐งช Partial implementation of jQuery/Sizzle extensions (see
cheerio-select for the
remaining selectors)
- ๐งโ๐ฌ High test coverage, including the full test suites from
Sizzle,
Qwery and
NWMatcher and .
- ๐ฅผ Reliably great performance
Most CSS engines written in JavaScript execute selectors left-to-right. That
means they execute every component of the selector in order, from left to right.
As an example: For the selector a b, these engines will first query for a
elements, then search these for b elements. (That's the approach of eg.Sizzle,Qwery andNWMatcher.)
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 superiora once, if they are bs. Using
Big O notation, that would beO(n^(k+1)), where k is the number of descendant selectors (that's the space
in the example above).
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 beO(n). That's called right-to-left execution.
And that's what css-select does โ and why it's quite performant.
By building a stack of functions.
_Wait, what?_
Okay, so let's suppose we want to compile the selector a b, for right-to-left
execution. We start by _parsing_ the selector. This turns the selector into an
array of the building blocks. That's what thecss-what module is for, if you want to
have a look.
Anyway, after parsing, we end up with an array like this one:
``js`
[
{ type: "tag", name: "a" },
{ type: "descendant" },
{ type: "tag", name: "b" },
];
(Actually, this array is wrapped in another array, but that's another story,
involving commas in selectors.)
Now that we know the meaning of every part of the selector, we can compile it.
That is where things become interesting.
The basic idea is to turn every part of the selector into a function, which
takes an element as its only argument. The function checks whether a passed
element matches its part of the selector: If it does, the element is passed to
the next function representing the next part of the selector. That function does
the same. If an element is accepted by all parts of the selector, it _matches_
the selector and double rainbow ALL THE WAY.
As said before, we want to do right-to-left execution with all the big O
improvements. That means elements are passed from the rightmost part of the
selector (b in our example) to the leftmost (~~which would be c~~ of coursea).
For traversals, such as the _descendant_ operating the space between a andb, we walk up the DOM tree, starting from the element passed as argument.
_//TODO: More in-depth description. Implementation details. Build a spaceship._
`js`
const CSSselect = require("css-select");
Note: css-select throws errors when invalid selectors are passed to it. This
is done to aid with writing css selectors, but can be unexpected when processing
arbitrary strings.
#### CSSselect.selectAll(query, elems, options)
Queries elems, returns an array containing all matches.
- query can be either a CSS selector or a function.elems
- can be either an array of elements, or a single element. If it is anoptions
element, its children will be queried.
- is described below.
Aliases: default export, CSSselect.iterate(query, elems).
#### CSSselect.compile(query, options)
Compiles the query, returns a function.
#### CSSselect.is(elem, query, options)
Tests whether or not an element is matched by query. query can be either a
CSS selector or a function.
#### CSSselect.selectOne(query, elems, options)
Arguments are the same as for CSSselect.selectAll(query, elems). Only returnsnull
the first match, or if there was no match.
All options are optional.
- xmlMode: When enabled, tag names will be case-sensitive. Default: false.rootFunc
- : The last function in the stack, will be called with the lastadapter
element that's looked at.
- : The adapter to use when interacting with the backing DOM structure.domutils
By default it uses the module.context
- : The context of the current query. Used to limit the scope of:scope
searches. Can be matched directly using the pseudo-class.relativeSelector
- : By default, selectors are relative to the context, whicha b c
means that no parent elements of the context will be matched. (Eg. b
with context will never give any results.) If relativeSelector is set tofalse
, selectors won't becontext
absolutized and selectors can
test for parent elements outside of the .cacheResults
- : Allow css-select to cache results for some selectors,true
sometimes greatly improving querying performance. Disable this if your
document can change in between queries with the same compiled selector.
Default: .pseudos
- : A map of pseudo-class names to functions or strings.
#### Custom Adapters
A custom adapter must match the interface described
here.
You may want to have a look at domutils to
see the default implementation, or at
css-select-browser-adapter
for an implementation backed by the DOM.
_As defined by CSS 4 and / or jQuery._
- Type
(): Selects elements by their tag name.
- Descendant
(): Selects elements that are descendants of the specified element.>
- Child
(): Selects elements that are direct children of the specified element.<
- Parent (): Selects elements that are direct parents of the specified:has()
element. This follows an
old proposal
that has been made obsolete by the pseudo-class.+
- Adjacent sibling
(): Selects elements that are the next sibling of the specified element.~
- General sibling
(): Selects elements that are siblings of the specified element.[attr=foo]
- Attribute
(), with supported comparisons:[attr]
- (existential): Selects elements with the specified attribute,=
whatever its value.
- : Selects elements with the specified attribute and value.~=
- : Selects elements with the specified attribute and value, separated|=
by spaces.
- : Selects elements with the specified attribute and value, separated*=
by hyphens.
- : Selects elements with the specified attribute and value, anywhere in^=
the attribute value.
- : Selects elements with the specified attribute and value, beginning$=
at the beginning of the attribute value.
- : Selects elements with the specified attribute and value, ending at!=
the end of the attribute value.
- : Selects elements with the specified attribute and value, not equali
to the specified value.
- and s can be added after the comparison to make the comparison[attr=foo i]
case-insensitive or case-sensitive (eg. ). If neither is,
supplied, css-select will follow the HTML spec's
case-sensitivity rules.
- Selector lists
(): Selects elements that match any of the specified selectors.*
- Universal
(): Selects all elements.:not
- Pseudos:
- : Selects:contains
elements that do not match the specified selector.
- : Selects elements:icontains
that contain the specified text.
- : Selects elements that contain the specified text,:has
case-insensitively.
- : Selects:root
elements that have descendants that match the specified selector.
- : Selects:empty
the root element.
- ::first-child
Selects elements that have no children.
- ::last-child
Selects elements that are the first element child of their parent.
- ::first-of-type
Selects elements that are the last element child of their parent.
- ::last-of-type
Selects elements that are the first element of their type.
- ::only-of-type
Selects elements that are the last element of their type.
- ::only-child
Selects elements that are the only element of their type.
- ::nth-child
Selects elements that are the only element child of their parent.
- ::nth-last-child
Selects elements that are the nth element child of their parent.
- ::nth-of-type
Selects elements that are the nth element child of their parent, counting
from the last child.
- ::nth-last-of-type
Selects elements that are the nth element of their type.
- ::any-link
Selects elements that are the nth element of their type, counting from the
last child.
- ::link
Selects elements that are links.
- : Selects:visited
elements that are links and have not been visited.
- ,:hover
,:active
Adapter
(these depend on optional methods, so these will only matchAdapter
elements if implemented in ):checked
- :input
Selects elements that are checked, or option elements that are:disabled
selected.
- ::enabled
Selects input elements that are disabled.
- ::required
Selects input elements that are not disabled.
- ::optional
Selects input elements that are required.
- ::parent
Selects input elements that are not required.
- jQuery extensions:
- : Selects elements:header
that have at least one child.
- : Selects header:selected
elements.
- : Selectsoption
elements that are selected.:button
- : Selects buttoninput
elements, and elements of type button.:input
- : Selects input,textarea
, select, and button elements.:text
- : Selects inputtext
elements of type .:checkbox
- : Selectsinput
elements of type checkbox.:file
- : Selects inputfile
elements of type .:password
- : Selectsinput
elements of type password.:reset
- : Selects inputreset
elements of type .:radio
- : Selects inputradio
elements of type .:is
- , as well as:where
the aliases
, and:matches
the legacy alias : Selects elements that match any of the given:scope
selectors.
- :
Selects elements that are part of the scope of the current selector. This
uses the context from the passed options.
---
License: BSD-2-Clause
To report a security vulnerability, please use the
Tidelift security contact. Tidelift will
coordinate the fix and disclosure.
for enterpriseAvailable as part of the Tidelift Subscription
The maintainers of css-select` and thousands of other packages are working with
Tidelift to deliver commercial support and maintenance for the open source
dependencies you use to build your applications. Save time, reduce risk, and
improve code health, while paying the maintainers of the exact dependencies you
use.
Learn more.