2
* Ext JS Library 3.0 RC2
3
* Copyright(c) 2006-2009, Ext JS, LLC.
6
* http://extjs.com/license
10
* This is code is also distributed under MIT license for use
11
* with jQuery and prototype JavaScript libraries.
15
Provides high performance selector/xpath processing by compiling queries into reusable functions. New pseudo classes and matchers can be plugged. It works on HTML and XML documents (if a content node is passed in).
17
DomQuery supports most of the <a href="http://www.w3.org/TR/2005/WD-css3-selectors-20051215/#selectors">CSS3 selectors spec</a>, along with some custom selectors and basic XPath.</p>
20
All selectors, attribute filters and pseudos below can be combined infinitely in any order. For example "div.foo:nth-child(odd)[@foo=bar].bar:first" would be a perfectly valid selector. Node filters are processed in the order in which they appear, which allows you to optimize your queries for your document structure.
22
<h4>Element Selectors:</h4>
24
<li> <b>*</b> any element</li>
25
<li> <b>E</b> an element with the tag E</li>
26
<li> <b>E F</b> All descendent elements of E that have the tag F</li>
27
<li> <b>E > F</b> or <b>E/F</b> all direct children elements of E that have the tag F</li>
28
<li> <b>E + F</b> all elements with the tag F that are immediately preceded by an element with the tag E</li>
29
<li> <b>E ~ F</b> all elements with the tag F that are preceded by a sibling element with the tag E</li>
31
<h4>Attribute Selectors:</h4>
32
<p>The use of @ and quotes are optional. For example, div[@foo='bar'] is also a valid attribute selector.</p>
34
<li> <b>E[foo]</b> has an attribute "foo"</li>
35
<li> <b>E[foo=bar]</b> has an attribute "foo" that equals "bar"</li>
36
<li> <b>E[foo^=bar]</b> has an attribute "foo" that starts with "bar"</li>
37
<li> <b>E[foo$=bar]</b> has an attribute "foo" that ends with "bar"</li>
38
<li> <b>E[foo*=bar]</b> has an attribute "foo" that contains the substring "bar"</li>
39
<li> <b>E[foo%=2]</b> has an attribute "foo" that is evenly divisible by 2</li>
40
<li> <b>E[foo!=bar]</b> has an attribute "foo" that does not equal "bar"</li>
42
<h4>Pseudo Classes:</h4>
44
<li> <b>E:first-child</b> E is the first child of its parent</li>
45
<li> <b>E:last-child</b> E is the last child of its parent</li>
46
<li> <b>E:nth-child(<i>n</i>)</b> E is the <i>n</i>th child of its parent (1 based as per the spec)</li>
47
<li> <b>E:nth-child(odd)</b> E is an odd child of its parent</li>
48
<li> <b>E:nth-child(even)</b> E is an even child of its parent</li>
49
<li> <b>E:only-child</b> E is the only child of its parent</li>
50
<li> <b>E:checked</b> E is an element that is has a checked attribute that is true (e.g. a radio or checkbox) </li>
51
<li> <b>E:first</b> the first E in the resultset</li>
52
<li> <b>E:last</b> the last E in the resultset</li>
53
<li> <b>E:nth(<i>n</i>)</b> the <i>n</i>th E in the resultset (1 based)</li>
54
<li> <b>E:odd</b> shortcut for :nth-child(odd)</li>
55
<li> <b>E:even</b> shortcut for :nth-child(even)</li>
56
<li> <b>E:contains(foo)</b> E's innerHTML contains the substring "foo"</li>
57
<li> <b>E:nodeValue(foo)</b> E contains a textNode with a nodeValue that equals "foo"</li>
58
<li> <b>E:not(S)</b> an E element that does not match simple selector S</li>
59
<li> <b>E:has(S)</b> an E element that has a descendent that matches simple selector S</li>
60
<li> <b>E:next(S)</b> an E element whose next sibling matches simple selector S</li>
61
<li> <b>E:prev(S)</b> an E element whose previous sibling matches simple selector S</li>
63
<h4>CSS Value Selectors:</h4>
65
<li> <b>E{display=none}</b> css value "display" that equals "none"</li>
66
<li> <b>E{display^=none}</b> css value "display" that starts with "none"</li>
67
<li> <b>E{display$=none}</b> css value "display" that ends with "none"</li>
68
<li> <b>E{display*=none}</b> css value "display" that contains the substring "none"</li>
69
<li> <b>E{display%=2}</b> css value "display" that is evenly divisible by 2</li>
70
<li> <b>E{display!=none}</b> css value "display" that does not equal "none"</li>
74
Ext.DomQuery = function(){
79
trimRe = /^\s+|\s+$/g,
81
modeRe = /^(\s?[\/>+~]\s?|\s|$)/,
82
tagTokenRe = /^(#)?([\w-\*]+)/,
83
nthRe = /(\d*)n\+?(\d*)/,
85
// This is for IE MSXML which does not support expandos.
86
// IE runs the same speed using setAttribute, however FF slows way down
87
// and Safari completely fails so they need to continue to use expandos.
88
isIE = window.ActiveXObject ? true : false,
89
isOpera = Ext.isOpera,
92
// this eval is stop the compressor from
93
// renaming the variable to something shorter
94
eval("var batch = 30803;");
96
function child(p, index){
111
while((n = n.nextSibling) && n.nodeType != 1);
116
while((n = n.previousSibling) && n.nodeType != 1);
120
function children(d){
121
var n = d.firstChild, ni = -1,
125
if(n.nodeType == 3 && !nonSpace.test(n.nodeValue)){
135
function byClassName(c, a, v){
139
var r = [], ri = -1, cn;
140
for(var i = 0, ci; ci = c[i]; i++){
141
if((' '+ci.className+' ').indexOf(v) != -1){
148
function attrValue(n, attr){
149
if(!n.tagName && typeof n.length != "undefined"){
158
if(attr == "class" || attr == "className"){
161
return n.getAttribute(attr) || n[attr];
165
function getNodes(ns, mode, tagName){
166
var result = [], ri = -1, cs;
170
tagName = tagName || "*";
171
if(typeof ns.getElementsByTagName != "undefined"){
175
for(var i = 0, ni; ni = ns[i]; i++){
176
cs = ni.getElementsByTagName(tagName);
177
for(var j = 0, ci; ci = cs[j]; j++){
181
}else if(mode == "/" || mode == ">"){
182
var utag = tagName.toUpperCase();
183
for(var i = 0, ni, cn; ni = ns[i]; i++){
184
cn = isOpera ? ni.childNodes : (ni.children || ni.childNodes);
185
for(var j = 0, cj; cj = cn[j]; j++){
186
if(cj.nodeName == utag || cj.nodeName == tagName || tagName == '*'){
191
}else if(mode == "+"){
192
var utag = tagName.toUpperCase();
193
for(var i = 0, n; n = ns[i]; i++){
194
while((n = n.nextSibling) && n.nodeType != 1);
195
if(n && (n.nodeName == utag || n.nodeName == tagName || tagName == '*')){
199
}else if(mode == "~"){
200
var utag = tagName.toUpperCase();
201
for(var i = 0, n; n = ns[i]; i++){
202
while((n = n.nextSibling)){
203
if (n.nodeName == utag || n.nodeName == tagName || tagName == '*'){
212
function concat(a, b){
216
for(var i = 0, l = b.length; i < l; i++){
222
function byTag(cs, tagName){
223
if(cs.tagName || cs == document){
230
tagName = tagName.toLowerCase();
231
for(var i = 0, ci; ci = cs[i]; i++){
232
if(ci.nodeType == 1 && ci.tagName.toLowerCase()==tagName){
239
function byId(cs, attr, id){
240
if(cs.tagName || cs == document){
247
for(var i = 0,ci; ci = cs[i]; i++){
248
if(ci && ci.id == id){
256
function byAttribute(cs, attr, value, op, custom){
260
f = Ext.DomQuery.operators[op];
261
for(var i = 0, ci; ci = cs[i]; i++){
262
if(ci.nodeType != 1){
267
a = Ext.DomQuery.getStyle(ci, attr);
269
else if(attr == "class" || attr == "className"){
271
}else if(attr == "for"){
273
}else if(attr == "href"){
274
a = ci.getAttribute("href", 2);
276
a = ci.getAttribute(attr);
278
if((f && f(a, value)) || (!f && a)){
285
function byPseudo(cs, name, value){
286
return Ext.DomQuery.pseudos[name](cs, value);
289
function nodupIEXml(cs){
292
cs[0].setAttribute("_nodup", d);
294
for(var i = 1, len = cs.length; i < len; i++){
296
if(!c.getAttribute("_nodup") != d){
297
c.setAttribute("_nodup", d);
301
for(var i = 0, len = cs.length; i < len; i++){
302
cs[i].removeAttribute("_nodup");
311
var len = cs.length, c, i, r = cs, cj, ri = -1;
312
if(!len || typeof cs.nodeType != "undefined" || len == 1){
315
if(isIE && typeof cs[0].selectSingleNode != "undefined"){
316
return nodupIEXml(cs);
320
for(i = 1; c = cs[i]; i++){
325
for(var j = 0; j < i; j++){
328
for(j = i+1; cj = cs[j]; j++){
340
function quickDiffIEXml(c1, c2){
343
for(var i = 0, len = c1.length; i < len; i++){
344
c1[i].setAttribute("_qdiff", d);
346
for(var i = 0, len = c2.length; i < len; i++){
347
if(c2[i].getAttribute("_qdiff") != d){
351
for(var i = 0, len = c1.length; i < len; i++){
352
c1[i].removeAttribute("_qdiff");
357
function quickDiff(c1, c2){
358
var len1 = c1.length,
364
if(isIE && c1[0].selectSingleNode){
365
return quickDiffIEXml(c1, c2);
367
for(var i = 0; i < len1; i++){
370
for(var i = 0, len = c2.length; i < len; i++){
371
if(c2[i]._qdiff != d){
378
function quickId(ns, mode, root, id){
380
var d = root.ownerDocument || root;
381
return d.getElementById(id);
383
ns = getNodes(ns, mode, "*");
384
return byId(ns, null, id);
388
getStyle : function(el, name){
389
return Ext.fly(el).getStyle(name);
392
* Compiles a selector/xpath query into a reusable function. The returned function
393
* takes one parameter "root" (optional), which is the context node from where the query should start.
394
* @param {String} selector The selector/xpath query
395
* @param {String} type (optional) Either "select" (the default) or "simple" for a simple selector match
398
compile : function(path, type){
399
type = type || "select";
401
var fn = ["var f = function(root){\n var mode; ++batch; var n = root || document;\n"],
403
tk = Ext.DomQuery.matchers,
406
// accept leading mode switch
407
lmode = q.match(modeRe);
409
if(lmode && lmode[1]){
410
fn[fn.length] = 'mode="'+lmode[1].replace(trimRe, "")+'";';
411
q = q.replace(lmode[1], "");
413
// strip leading slashes
414
while(path.substr(0, 1)=="/"){
415
path = path.substr(1);
420
var tm = q.match(tagTokenRe);
421
if(type == "select"){
424
fn[fn.length] = 'n = quickId(n, mode, root, "'+tm[2]+'");';
426
fn[fn.length] = 'n = getNodes(n, mode, "'+tm[2]+'");';
428
q = q.replace(tm[0], "");
429
}else if(q.substr(0, 1) != '@'){
430
fn[fn.length] = 'n = getNodes(n, mode, "*");';
435
fn[fn.length] = 'n = byId(n, null, "'+tm[2]+'");';
437
fn[fn.length] = 'n = byTag(n, "'+tm[2]+'");';
439
q = q.replace(tm[0], "");
442
while(!(mm = q.match(modeRe))){
444
for(var j = 0; j < tklen; j++){
446
var m = q.match(t.re);
448
fn[fn.length] = t.select.replace(tplRe, function(x, i){
451
q = q.replace(m[0], "");
456
// prevent infinite loop on bad selector
458
throw 'Error parsing selector, parsing failed at "' + q + '"';
462
fn[fn.length] = 'mode="'+mm[1].replace(trimRe, "")+'";';
463
q = q.replace(mm[1], "");
466
fn[fn.length] = "return nodup(n);\n}";
472
* Selects a group of elements.
473
* @param {String} selector The selector/xpath query (can be a comma separated list of selectors)
474
* @param {Node} root (optional) The start of the query (defaults to document).
475
* @return {Array} An Array of DOM elements which match the selector. If there are
476
* no matches, and empty Array is returned.
478
select : function(path, root, type){
479
if(!root || root == document){
482
if(typeof root == "string"){
483
root = document.getElementById(root);
485
var paths = path.split(","),
487
for(var i = 0, len = paths.length; i < len; i++){
488
var p = paths[i].replace(trimRe, "");
490
cache[p] = Ext.DomQuery.compile(p);
492
throw p + " is not a valid selector";
495
var result = cache[p](root);
496
if(result && result != document){
497
results = results.concat(result);
500
if(paths.length > 1){
501
return nodup(results);
507
* Selects a single element.
508
* @param {String} selector The selector/xpath query
509
* @param {Node} root (optional) The start of the query (defaults to document).
510
* @return {Element} The DOM element which matched the selector.
512
selectNode : function(path, root){
513
return Ext.DomQuery.select(path, root)[0];
517
* Selects the value of a node, optionally replacing null with the defaultValue.
518
* @param {String} selector The selector/xpath query
519
* @param {Node} root (optional) The start of the query (defaults to document).
520
* @param {String} defaultValue
523
selectValue : function(path, root, defaultValue){
524
path = path.replace(trimRe, "");
525
if(!valueCache[path]){
526
valueCache[path] = Ext.DomQuery.compile(path, "select");
528
var n = valueCache[path](root),
531
v = (n && n.firstChild ? n.firstChild.nodeValue : null);
532
return ((v === null||v === undefined||v==='') ? defaultValue : v);
536
* Selects the value of a node, parsing integers and floats. Returns the defaultValue, or 0 if none is specified.
537
* @param {String} selector The selector/xpath query
538
* @param {Node} root (optional) The start of the query (defaults to document).
539
* @param {Number} defaultValue
542
selectNumber : function(path, root, defaultValue){
543
var v = Ext.DomQuery.selectValue(path, root, defaultValue || 0);
544
return parseFloat(v);
548
* Returns true if the passed element(s) match the passed simple selector (e.g. div.some-class or span:first-child)
549
* @param {String/HTMLElement/Array} el An element id, element or array of elements
550
* @param {String} selector The simple selector to test
553
is : function(el, ss){
554
if(typeof el == "string"){
555
el = document.getElementById(el);
557
var isArray = Ext.isArray(el),
558
result = Ext.DomQuery.filter(isArray ? el : [el], ss);
559
return isArray ? (result.length == el.length) : (result.length > 0);
563
* Filters an array of elements to only include matches of a simple selector (e.g. div.some-class or span:first-child)
564
* @param {Array} el An array of elements to filter
565
* @param {String} selector The simple selector to test
566
* @param {Boolean} nonMatches If true, it returns the elements that DON'T match
567
* the selector instead of the ones that match
568
* @return {Array} An Array of DOM elements which match the selector. If there are
569
* no matches, and empty Array is returned.
571
filter : function(els, ss, nonMatches){
572
ss = ss.replace(trimRe, "");
573
if(!simpleCache[ss]){
574
simpleCache[ss] = Ext.DomQuery.compile(ss, "simple");
576
var result = simpleCache[ss](els);
577
return nonMatches ? quickDiff(result, els) : result;
581
* Collection of matching regular expressions and code snippets.
585
select: 'n = byClassName(n, null, " {1} ");'
587
re: /^\:([\w-]+)(?:\(((?:[^\s>\/]*|.*?))\))?/,
588
select: 'n = byPseudo(n, "{1}", "{2}");'
590
re: /^(?:([\[\{])(?:@)?([\w-]+)\s?(?:(=|.=)\s?['"]?(.*?)["']?)?[\]\}])/,
591
select: 'n = byAttribute(n, "{2}", "{4}", "{3}", "{1}");'
594
select: 'n = byId(n, null, "{1}");'
597
select: 'return {firstChild:{nodeValue:attrValue(n, "{1}")}};'
602
* Collection of operator comparison functions. The default operators are =, !=, ^=, $=, *=, %=, |= and ~=.
603
* New operators can be added as long as the match the format <i>c</i>= where <i>c</i> is any character other than space, > <.
606
"=" : function(a, v){
609
"!=" : function(a, v){
612
"^=" : function(a, v){
613
return a && a.substr(0, v.length) == v;
615
"$=" : function(a, v){
616
return a && a.substr(a.length-v.length) == v;
618
"*=" : function(a, v){
619
return a && a.indexOf(v) !== -1;
621
"%=" : function(a, v){
624
"|=" : function(a, v){
625
return a && (a == v || a.substr(0, v.length+1) == v+'-');
627
"~=" : function(a, v){
628
return a && (' '+a+' ').indexOf(' '+v+' ') != -1;
633
* Collection of "pseudo class" processors. Each processor is passed the current nodeset (array)
634
* and the argument (if any) supplied in the selector.
637
"first-child" : function(c){
638
var r = [], ri = -1, n;
639
for(var i = 0, ci; ci = n = c[i]; i++){
640
while((n = n.previousSibling) && n.nodeType != 1);
648
"last-child" : function(c){
649
var r = [], ri = -1, n;
650
for(var i = 0, ci; ci = n = c[i]; i++){
651
while((n = n.nextSibling) && n.nodeType != 1);
659
"nth-child" : function(c, a) {
661
m = nthRe.exec(a == "even" && "2n" || a == "odd" && "2n+1" || !nthRe2.test(a) && "n+" + a || a),
662
f = (m[1] || 1) - 0, l = m[2] - 0;
663
for(var i = 0, n; n = c[i]; i++){
664
var pn = n.parentNode;
665
if (batch != pn._batch) {
667
for(var cn = pn.firstChild; cn; cn = cn.nextSibling){
668
if(cn.nodeType == 1){
675
if (l == 0 || n.nodeIndex == l){
678
} else if ((n.nodeIndex + l) % f == 0){
686
"only-child" : function(c){
687
var r = [], ri = -1;;
688
for(var i = 0, ci; ci = c[i]; i++){
689
if(!prev(ci) && !next(ci)){
696
"empty" : function(c){
698
for(var i = 0, ci; ci = c[i]; i++){
699
var cns = ci.childNodes, j = 0, cn, empty = true;
702
if(cn.nodeType == 1 || cn.nodeType == 3){
714
"contains" : function(c, v){
716
for(var i = 0, ci; ci = c[i]; i++){
717
if((ci.textContent||ci.innerText||'').indexOf(v) != -1){
724
"nodeValue" : function(c, v){
726
for(var i = 0, ci; ci = c[i]; i++){
727
if(ci.firstChild && ci.firstChild.nodeValue == v){
734
"checked" : function(c){
736
for(var i = 0, ci; ci = c[i]; i++){
737
if(ci.checked == true){
744
"not" : function(c, ss){
745
return Ext.DomQuery.filter(c, ss, true);
748
"any" : function(c, selectors){
749
var ss = selectors.split('|'),
751
for(var i = 0, ci; ci = c[i]; i++){
752
for(var j = 0; s = ss[j]; j++){
753
if(Ext.DomQuery.is(ci, s)){
763
return this["nth-child"](c, "odd");
766
"even" : function(c){
767
return this["nth-child"](c, "even");
770
"nth" : function(c, a){
774
"first" : function(c){
778
"last" : function(c){
779
return c[c.length-1] || [];
782
"has" : function(c, ss){
783
var s = Ext.DomQuery.select,
785
for(var i = 0, ci; ci = c[i]; i++){
786
if(s(ss, ci).length > 0){
793
"next" : function(c, ss){
794
var is = Ext.DomQuery.is,
796
for(var i = 0, ci; ci = c[i]; i++){
805
"prev" : function(c, ss){
806
var is = Ext.DomQuery.is,
808
for(var i = 0, ci; ci = c[i]; i++){
821
* Selects an array of DOM nodes by CSS/XPath selector. Shorthand of {@link Ext.DomQuery#select}
822
* @param {String} path The selector/xpath query
823
* @param {Node} root (optional) The start of the query (defaults to document).
828
Ext.query = Ext.DomQuery.select;