Qwery - The Tiny Selector Engine
Qwery is a small blazing fast query selector engine allowing you to select elements with CSS1|2|3 queries
Acceptable selectors
#foo {}
.bar {}
a#foo.bar {}
#foo a[href] {}
#foo a[href=bar] {}
#foo a[lang|=en] {}
#foo a[title~=hello] {}
#foo a[href^="http://"] {}
#foo a[href$=com] {}
#foo a[href*=twitter]
#foo a {}
ul#list > li {}
span ~ strong {}
p + p {}
div,p {}
#foo.bar.baz {}
div#baz.thunk a[-data-info*="hello world"] span + strong {}
#thunk[title$='huzza'] {}
Contexts
Each query can optionally pass in a context
qwery('div', node);
qwery('div', '#foo');
pseudo selector API
Optionally, Qwery provides a pseudo selector interface allowing you to extend into advanced CSS3 matchers. It looks like this:
qwery.pseudos['first-child'] = function (el, val) {
var p;
return el.parentNode && (p = el.parentNode) && (childs = p.getElementsByTagName('*')) && childs[0] == el;
};
To create a new pseudo matcher you must set a property on qwery.psuedos
with a boolean method that is passed back a candidate element, and a value (if any). For example:
qwery('#content p.surprise:foo(bar)')
qwery.pseudos.foo = function (el, val) {
return el.getAttribute(val)
}
Configuring Qwery
The configure()
method takes an options object allowing you to adjust the way that Qwery works internally. Currently only the useNativeQSA
option is available to turn on and off the use of native querySelectorAll()
where available.
qwery.configure({
useNativeQSA: false
})
Browser Support
Qwery attempts to stay up to date with Yahoo's Grade A Browser Support in addition to future browser candidates.
- IE6+
- Chrome 1+
- Safari 3+
- Firefox 2+
- Opera
Dev Env & Testing
$ npm install smoosh sink-test
$ make
$ open tests/index.html
Note
Qwery uses querySelectorAll
when available. All querySelectorAll
default behavior then applies.
Ender support
Qwery is the recommended selector engine for Ender. If you don't have Ender, install it, and don't ever look back.
$ npm install ender -g
To include Query in a custom build of Ender you can include it as such:
$ ender build qwery[,mod2,mod3,...]
Or add it to an existing Ender installation
$ ender add qwery
Ender bridge additions
Assuming you already know the happs on Ender -- Qwery provides some additional niceties when included with Ender:
$('div').find('p')
$('div').and('p')
$('#foo').is('div.bar');
Recommended sibling modules
In most cases, if you're hunting for a selector engine, you probably want to pair Qwery with a DOM module. In that case qwery pairs quite nicely with Bonzo (a DOM util) and Bean (an event util). Add them to your Ender installation as such:
$ ender build qwery bonzo bean
Then write code like a boss:
$('<p>hello world</p>')
.css({
color: 'red',
background: 'white'
})
.after('√')
.bind({
'click.button': function () {
$(this).hide().unbind('click.button')
}
})
.appendTo('body')
Qwery Mobile!
If you're building a Webkit (iPhone / Android / Chrome OS) application, you may be interested in qwery-mobile! Include this (instead of qwery) in your Ender build and get a full qwery interface for just 600 bytes :)
$ ender add qwery-mobile
Contributors
Giving back
Are you using this library in production? Consider leaving a tip to show your appreciation.