A small, fast, standard compilant, cross-browser implementation of an CSS3 Query Selector
A small, fast, standard compilant, cross-browser implementation of an CSS3 Query Selector.
Currently the Selector API
(querySelectorAll) is only available in browsers based on
WebKit (Safari and
Chrome) and the soon to be released Firefox 3.1 and
Internet Explorer 8. Similar selectors are also available in frameworks like
base2, Dojo,
Ext, jQuery,
MooTools and Prototype.
But, if you want to use query selection today without the bulky frameworks, our stand-alone selector is for you.
For fast queries, our implementation uses either DOM Level 3 XPath
or pre-compiled JavaScript DOM traversal.
The matched Element’s is always returned in a standard
Array so iteration functions like
forEach can be used.
And to ease integration with other frameworks, the Selector class has hook functions to extend the result
Element’s (patchElement) and Array (patchArray).
Selector.js supports all
Simple selectors
and Attribute selectors without namespace, all
Combinators,
Class selectors,
ID selectors and all
Pseudo-class selectors except the
Dynamic pseudo-classes :hover and :visited,
which isn’t possible with JavaScript/DOM. The other frameworks support a non-standard pseudo-selector
:contains() and attribute compare operator !=, ours don’t.
Intuitive usage, context node (document) optional:
// Normal constructor syntax:
new Selector('div > p').exec(document).forEach(function (e) {
e.style.color = 'red';
});
// Or, the shorthand syntax:
Selector('div > p', document).forEach(function (e) {
e.style.color = 'red';
});
compareDocumentPosition.RegEx for attribute comparison in Internet Explorer, it’s faster.:nth- indexing and lookup, maybe index elements when traversing.:active, :focus, :root and :target)selector speed by accessing them directly, avoiding traversal.
Fixed bug when matching * elements with -of-type pseudo selectors. Better syntax error reporting.#id, :active, :focus, :root and :target speed by exiting all ancestor selections when the match is found.patchElement and optional function, only calling it when defined.
Not using sourceIndex as node hash. Direct insert into array instead of using push.getElementById in
Internet Explorer since it also matches name attribute.The generated online documentation can be found here.
Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0>
Henrik Lindqvist <henrik.lindqvist@llamalab.com>