selector-engine
is a JavaScript based DOM selector engine. It provides methods for selecting elements from a DOM and inquiring if an element matches a selector by using standard Selectors API selectors. These methods may serve as alternatives or polyfills to their standard counterparts.
selector-engine | standard | behavior |
---|---|---|
Query.one |
querySelector |
Return the first element matching the provided selector from the given root. |
Query.all |
querySelectorAll |
Return all elements matching the provided selector from the given root. |
Query.matches |
matches |
Return true if the given element matches the selector, otherwise false . |
In each selector-engine
method, the first argument is the root from which the query is performed and the second argument is the selector string. If only one argument is passed, it is assumed to be the selector string, and the document
will be used as the root.
To use this project, simply include one of the two files in your project. Use the "legacy" version if support for old browsers like IE6 is needed.
selector-engine.min.js | selector-engine-legacy.min.js |
---|---|
3.45kb gzipped | 3.67kb gzipped |
Takes advantage of modern features, providing better performance. | Ensures compatibility at a minor performance cost. |
Support:
|
Support:
|
// Select the first .comments div.
var div = Query.one(document, "div.comments")
// Check if the comments section is empty
if (!Query.matches(div, ":empty")) {
// Select all highlighted spans in new and updated comments.
Query.all(div, "p.comment:matches(.new, .updated) > span.highlighted").forEach(function(s) {
console.log(s.textContent)
})
}
(feature support table coming soon)
selector-engine
needs some real-world testing, but aside from that, it's ready for use. Be sure to file an issue if you find bugs.
That's the goal. Generally if one or two major browsers implement a feature described in the Working Draft or Editor's Draft of the Selectors API, it'll be given consideration.
There are some rare cases where legacy support at that level is genuinely needed. Adding support for them is fairly simple, so the decision was easy.
- String character access uses
.charAt()
instead of direct indexes. - Custom tags are selectable.
- Element traversal uses techniques like
.nextSibling
in a loop instead of.nextElementSibling
. - Checking attributes, like
"class"
, has compatibility patches.
- In legacy version of IE, properties on elements automatically become available as attributes. Because of this, if you use attribute selectors to fetch elements by a custom attribute that also appears as a custom property, you may get false positives. For example, in IE6, doing
element.foo = "bar"
will cause a[foo="bar"]
selector to match that element, becausefoo
will appear as an attribute on the element. This is not the case in modern browsers.
No, for the following reasons:
- Adhering to standards makes code more portable and future proof. Code written for non-standard features can only be used where that library will be accepted as a dependency, which isn't always the case.
- For the same reason that developers avoid extending host prototypes, we avoid it with selectors. If custom selectors are added, they may conflict with future implementations of new standards. They may also conflict with additions by other libraries that use this library.
- The standard selectors provide quite a lot of functionality, and like regular expressions, they're useful and powerful but don't need to be the solution to every problem.
Please review the contributing guide for information on contributing to this project.