Roo/DomQuery.js
[roojs1] / Roo / DomQuery.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11  
12
13 /*
14  * This is code is also distributed under MIT license for use
15  * with jQuery and prototype JavaScript libraries.
16  */
17 /**
18  * @class Roo.DomQuery
19 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).
20 <p>
21 DomQuery supports most of the <a href="http://www.w3.org/TR/2005/WD-css3-selectors-20051215/">CSS3 selectors spec</a>, along with some custom selectors and basic XPath.</p>
22
23 <p>
24 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.
25 </p>
26 <h4>Element Selectors:</h4>
27 <ul class="list">
28     <li> <b>*</b> any element</li>
29     <li> <b>E</b> an element with the tag E</li>
30     <li> <b>E F</b> All descendent elements of E that have the tag F</li>
31     <li> <b>E > F</b> or <b>E/F</b> all direct children elements of E that have the tag F</li>
32     <li> <b>E + F</b> all elements with the tag F that are immediately preceded by an element with the tag E</li>
33     <li> <b>E ~ F</b> all elements with the tag F that are preceded by a sibling element with the tag E</li>
34 </ul>
35 <h4>Attribute Selectors:</h4>
36 <p>The use of @ and quotes are optional. For example, div[@foo='bar'] is also a valid attribute selector.</p>
37 <ul class="list">
38     <li> <b>E[foo]</b> has an attribute "foo"</li>
39     <li> <b>E[foo=bar]</b> has an attribute "foo" that equals "bar"</li>
40     <li> <b>E[foo^=bar]</b> has an attribute "foo" that starts with "bar"</li>
41     <li> <b>E[foo$=bar]</b> has an attribute "foo" that ends with "bar"</li>
42     <li> <b>E[foo*=bar]</b> has an attribute "foo" that contains the substring "bar"</li>
43     <li> <b>E[foo%=2]</b> has an attribute "foo" that is evenly divisible by 2</li>
44     <li> <b>E[foo!=bar]</b> has an attribute "foo" that does not equal "bar"</li>
45 </ul>
46 <h4>Pseudo Classes:</h4>
47 <ul class="list">
48     <li> <b>E:first-child</b> E is the first child of its parent</li>
49     <li> <b>E:last-child</b> E is the last child of its parent</li>
50     <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>
51     <li> <b>E:nth-child(odd)</b> E is an odd child of its parent</li>
52     <li> <b>E:nth-child(even)</b> E is an even child of its parent</li>
53     <li> <b>E:only-child</b> E is the only child of its parent</li>
54     <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>
55     <li> <b>E:first</b> the first E in the resultset</li>
56     <li> <b>E:last</b> the last E in the resultset</li>
57     <li> <b>E:nth(<i>n</i>)</b> the <i>n</i>th E in the resultset (1 based)</li>
58     <li> <b>E:odd</b> shortcut for :nth-child(odd)</li>
59     <li> <b>E:even</b> shortcut for :nth-child(even)</li>
60     <li> <b>E:contains(foo)</b> E's innerHTML contains the substring "foo"</li>
61     <li> <b>E:nodeValue(foo)</b> E contains a textNode with a nodeValue that equals "foo"</li>
62     <li> <b>E:not(S)</b> an E element that does not match simple selector S</li>
63     <li> <b>E:has(S)</b> an E element that has a descendent that matches simple selector S</li>
64     <li> <b>E:next(S)</b> an E element whose next sibling matches simple selector S</li>
65     <li> <b>E:prev(S)</b> an E element whose previous sibling matches simple selector S</li>
66 </ul>
67 <h4>CSS Value Selectors:</h4>
68 <ul class="list">
69     <li> <b>E{display=none}</b> css value "display" that equals "none"</li>
70     <li> <b>E{display^=none}</b> css value "display" that starts with "none"</li>
71     <li> <b>E{display$=none}</b> css value "display" that ends with "none"</li>
72     <li> <b>E{display*=none}</b> css value "display" that contains the substring "none"</li>
73     <li> <b>E{display%=2}</b> css value "display" that is evenly divisible by 2</li>
74     <li> <b>E{display!=none}</b> css value "display" that does not equal "none"</li>
75 </ul>
76  * @singleton
77  */
78 Roo.DomQuery = function(){
79     var cache = {}, simpleCache = {}, valueCache = {};
80     var nonSpace = /\S/;
81     var trimRe = /^\s+|\s+$/g;
82     var tplRe = /\{(\d+)\}/g;
83     var modeRe = /^(\s?[\/>+~]\s?|\s|$)/;
84     var tagTokenRe = /^(#)?([\w-\*]+)/;
85     var nthRe = /(\d*)n\+?(\d*)/, nthRe2 = /\D/;
86
87     function child(p, index){
88         var i = 0;
89         var n = p.firstChild;
90         while(n){
91             if(n.nodeType == 1){
92                if(++i == index){
93                    return n;
94                }
95             }
96             n = n.nextSibling;
97         }
98         return null;
99     };
100
101     function next(n){
102         while((n = n.nextSibling) && n.nodeType != 1);
103         return n;
104     };
105
106     function prev(n){
107         while((n = n.previousSibling) && n.nodeType != 1);
108         return n;
109     };
110
111     function children(d){
112         var n = d.firstChild, ni = -1;
113             while(n){
114                 var nx = n.nextSibling;
115                 if(n.nodeType == 3 && !nonSpace.test(n.nodeValue)){
116                     d.removeChild(n);
117                 }else{
118                     n.nodeIndex = ++ni;
119                 }
120                 n = nx;
121             }
122             return this;
123         };
124
125     function byClassName(c, a, v){
126         if(!v){
127             return c;
128         }
129         var r = [], ri = -1, cn;
130         for(var i = 0, ci; ci = c[i]; i++){
131             if((' '+ci.className+' ').indexOf(v) != -1){
132                 r[++ri] = ci;
133             }
134         }
135         return r;
136     };
137
138     function attrValue(n, attr){
139         if(!n.tagName && typeof n.length != "undefined"){
140             n = n[0];
141         }
142         if(!n){
143             return null;
144         }
145         if(attr == "for"){
146             return n.htmlFor;
147         }
148         if(attr == "class" || attr == "className"){
149             return n.className;
150         }
151         return n.getAttribute(attr) || n[attr];
152
153     };
154
155     function getNodes(ns, mode, tagName){
156         var result = [], ri = -1, cs;
157         if(!ns){
158             return result;
159         }
160         tagName = tagName || "*";
161         if(typeof ns.getElementsByTagName != "undefined"){
162             ns = [ns];
163         }
164         if(!mode){
165             for(var i = 0, ni; ni = ns[i]; i++){
166                 cs = ni.getElementsByTagName(tagName);
167                 for(var j = 0, ci; ci = cs[j]; j++){
168                     result[++ri] = ci;
169                 }
170             }
171         }else if(mode == "/" || mode == ">"){
172             var utag = tagName.toUpperCase();
173             for(var i = 0, ni, cn; ni = ns[i]; i++){
174                 cn = ni.children || ni.childNodes;
175                 for(var j = 0, cj; cj = cn[j]; j++){
176                     if(cj.nodeName == utag || cj.nodeName == tagName  || tagName == '*'){
177                         result[++ri] = cj;
178                     }
179                 }
180             }
181         }else if(mode == "+"){
182             var utag = tagName.toUpperCase();
183             for(var i = 0, n; n = ns[i]; i++){
184                 while((n = n.nextSibling) && n.nodeType != 1);
185                 if(n && (n.nodeName == utag || n.nodeName == tagName || tagName == '*')){
186                     result[++ri] = n;
187                 }
188             }
189         }else if(mode == "~"){
190             for(var i = 0, n; n = ns[i]; i++){
191                 while((n = n.nextSibling) && (n.nodeType != 1 || (tagName == '*' || n.tagName.toLowerCase()!=tagName)));
192                 if(n){
193                     result[++ri] = n;
194                 }
195             }
196         }
197         return result;
198     };
199
200     function concat(a, b){
201         if(b.slice){
202             return a.concat(b);
203         }
204         for(var i = 0, l = b.length; i < l; i++){
205             a[a.length] = b[i];
206         }
207         return a;
208     }
209
210     function byTag(cs, tagName){
211         if(cs.tagName || cs == document){
212             cs = [cs];
213         }
214         if(!tagName){
215             return cs;
216         }
217         var r = [], ri = -1;
218         tagName = tagName.toLowerCase();
219         for(var i = 0, ci; ci = cs[i]; i++){
220             if(ci.nodeType == 1 && ci.tagName.toLowerCase()==tagName){
221                 r[++ri] = ci;
222             }
223         }
224         return r;
225     };
226
227     function byId(cs, attr, id){
228         if(cs.tagName || cs == document){
229             cs = [cs];
230         }
231         if(!id){
232             return cs;
233         }
234         var r = [], ri = -1;
235         for(var i = 0,ci; ci = cs[i]; i++){
236             if(ci && ci.id == id){
237                 r[++ri] = ci;
238                 return r;
239             }
240         }
241         return r;
242     };
243
244     function byAttribute(cs, attr, value, op, custom){
245         var r = [], ri = -1, st = custom=="{";
246         var f = Roo.DomQuery.operators[op];
247         for(var i = 0, ci; ci = cs[i]; i++){
248             var a;
249             if(st){
250                 a = Roo.DomQuery.getStyle(ci, attr);
251             }
252             else if(attr == "class" || attr == "className"){
253                 a = ci.className;
254             }else if(attr == "for"){
255                 a = ci.htmlFor;
256             }else if(attr == "href"){
257                 a = ci.getAttribute("href", 2);
258             }else{
259                 a = ci.getAttribute(attr);
260             }
261             if((f && f(a, value)) || (!f && a)){
262                 r[++ri] = ci;
263             }
264         }
265         return r;
266     };
267
268     function byPseudo(cs, name, value){
269         return Roo.DomQuery.pseudos[name](cs, value);
270     };
271
272     // This is for IE MSXML which does not support expandos.
273     // IE runs the same speed using setAttribute, however FF slows way down
274     // and Safari completely fails so they need to continue to use expandos.
275     var isIE = window.ActiveXObject ? true : false;
276
277     // this eval is stop the compressor from
278     // renaming the variable to something shorter
279     
280     /** eval:var:batch */
281     var batch = 30803; 
282
283     var key = 30803;
284
285     function nodupIEXml(cs){
286         var d = ++key;
287         cs[0].setAttribute("_nodup", d);
288         var r = [cs[0]];
289         for(var i = 1, len = cs.length; i < len; i++){
290             var c = cs[i];
291             if(!c.getAttribute("_nodup") != d){
292                 c.setAttribute("_nodup", d);
293                 r[r.length] = c;
294             }
295         }
296         for(var i = 0, len = cs.length; i < len; i++){
297             cs[i].removeAttribute("_nodup");
298         }
299         return r;
300     }
301
302     function nodup(cs){
303         if(!cs){
304             return [];
305         }
306         var len = cs.length, c, i, r = cs, cj, ri = -1;
307         if(!len || typeof cs.nodeType != "undefined" || len == 1){
308             return cs;
309         }
310         if(isIE && typeof cs[0].selectSingleNode != "undefined"){
311             return nodupIEXml(cs);
312         }
313         var d = ++key;
314         cs[0]._nodup = d;
315         for(i = 1; c = cs[i]; i++){
316             if(c._nodup != d){
317                 c._nodup = d;
318             }else{
319                 r = [];
320                 for(var j = 0; j < i; j++){
321                     r[++ri] = cs[j];
322                 }
323                 for(j = i+1; cj = cs[j]; j++){
324                     if(cj._nodup != d){
325                         cj._nodup = d;
326                         r[++ri] = cj;
327                     }
328                 }
329                 return r;
330             }
331         }
332         return r;
333     }
334
335     function quickDiffIEXml(c1, c2){
336         var d = ++key;
337         for(var i = 0, len = c1.length; i < len; i++){
338             c1[i].setAttribute("_qdiff", d);
339         }
340         var r = [];
341         for(var i = 0, len = c2.length; i < len; i++){
342             if(c2[i].getAttribute("_qdiff") != d){
343                 r[r.length] = c2[i];
344             }
345         }
346         for(var i = 0, len = c1.length; i < len; i++){
347            c1[i].removeAttribute("_qdiff");
348         }
349         return r;
350     }
351
352     function quickDiff(c1, c2){
353         var len1 = c1.length;
354         if(!len1){
355             return c2;
356         }
357         if(isIE && c1[0].selectSingleNode){
358             return quickDiffIEXml(c1, c2);
359         }
360         var d = ++key;
361         for(var i = 0; i < len1; i++){
362             c1[i]._qdiff = d;
363         }
364         var r = [];
365         for(var i = 0, len = c2.length; i < len; i++){
366             if(c2[i]._qdiff != d){
367                 r[r.length] = c2[i];
368             }
369         }
370         return r;
371     }
372
373     function quickId(ns, mode, root, id){
374         if(ns == root){
375            var d = root.ownerDocument || root;
376            return d.getElementById(id);
377         }
378         ns = getNodes(ns, mode, "*");
379         return byId(ns, null, id);
380     }
381
382     return {
383         getStyle : function(el, name){
384             return Roo.fly(el).getStyle(name);
385         },
386         /**
387          * Compiles a selector/xpath query into a reusable function. The returned function
388          * takes one parameter "root" (optional), which is the context node from where the query should start.
389          * @param {String} selector The selector/xpath query
390          * @param {String} type (optional) Either "select" (the default) or "simple" for a simple selector match
391          * @return {Function}
392          */
393         compile : function(path, type){
394             type = type || "select";
395             
396             var fn = ["var f = function(root){\n var mode; ++batch; var n = root || document;\n"];
397             var q = path, mode, lq;
398             var tk = Roo.DomQuery.matchers;
399             var tklen = tk.length;
400             var mm;
401
402             // accept leading mode switch
403             var lmode = q.match(modeRe);
404             if(lmode && lmode[1]){
405                 fn[fn.length] = 'mode="'+lmode[1].replace(trimRe, "")+'";';
406                 q = q.replace(lmode[1], "");
407             }
408             // strip leading slashes
409             while(path.substr(0, 1)=="/"){
410                 path = path.substr(1);
411             }
412
413             while(q && lq != q){
414                 lq = q;
415                 var tm = q.match(tagTokenRe);
416                 if(type == "select"){
417                     Roo.log('tm');
418                     Roo.log(tm);
419                     if(tm){
420                         if(tm[1] == "#"){
421                             fn[fn.length] = 'n = quickId(n, mode, root, "'+tm[2]+'");';
422                         }else{
423                             fn[fn.length] = 'n = getNodes(n, mode, "'+tm[2]+'");';
424                         }
425                         q = q.replace(tm[0], "");
426                         Roo.log('fn');
427                         Roo.log(fn);
428                         Roo.log(q);
429                     }else if(q.substr(0, 1) != '@'){
430                         fn[fn.length] = 'n = getNodes(n, mode, "*");';
431                     }
432                 }else{
433                     if(tm){
434                         if(tm[1] == "#"){
435                             fn[fn.length] = 'n = byId(n, null, "'+tm[2]+'");';
436                         }else{
437                             fn[fn.length] = 'n = byTag(n, "'+tm[2]+'");';
438                         }
439                         q = q.replace(tm[0], "");
440                     }
441                 }
442                 while(!(mm = q.match(modeRe))){
443                     var matched = false;
444                     for(var j = 0; j < tklen; j++){
445                         var t = tk[j];
446                         var m = q.match(t.re);
447                         if(m){
448                             fn[fn.length] = t.select.replace(tplRe, function(x, i){
449                                                     return m[i];
450                                                 });
451                             q = q.replace(m[0], "");
452                             matched = true;
453                             break;
454                         }
455                     }
456                     // prevent infinite loop on bad selector
457                     if(!matched){
458                         throw 'Error parsing selector, parsing failed at "' + q + '"';
459                     }
460                 }
461                 if(mm[1]){
462                     fn[fn.length] = 'mode="'+mm[1].replace(trimRe, "")+'";';
463                     q = q.replace(mm[1], "");
464                 }
465             }
466             fn[fn.length] = "return nodup(n);\n}";
467             
468              /** 
469               * list of variables that need from compression as they are used by eval.
470              *  eval:var:batch 
471              *  eval:var:nodup
472              *  eval:var:byTag
473              *  eval:var:ById
474              *  eval:var:getNodes
475              *  eval:var:quickId
476              *  eval:var:mode
477              *  eval:var:root
478              *  eval:var:n
479              *  eval:var:byClassName
480              *  eval:var:byPseudo
481              *  eval:var:byAttribute
482              *  eval:var:attrValue
483              * 
484              **/ 
485             eval(fn.join(""));
486             return f;
487         },
488
489         /**
490          * Selects a group of elements.
491          * @param {String} selector The selector/xpath query (can be a comma separated list of selectors)
492          * @param {Node} root (optional) The start of the query (defaults to document).
493          * @return {Array}
494          */
495         select : function(path, root, type){
496             if(!root || root == document){
497                 root = document;
498             }
499             if(typeof root == "string"){
500                 root = document.getElementById(root);
501             }
502             var paths = path.split(",");
503             var results = [];
504             for(var i = 0, len = paths.length; i < len; i++){
505                 var p = paths[i].replace(trimRe, "");
506                 if(!cache[p]){
507                     cache[p] = Roo.DomQuery.compile(p);
508                     if(!cache[p]){
509                         throw p + " is not a valid selector";
510                     }
511                 }
512                 var result = cache[p](root);
513                 if(result && result != document){
514                     results = results.concat(result);
515                 }
516             }
517             if(paths.length > 1){
518                 return nodup(results);
519             }
520             return results;
521         },
522
523         /**
524          * Selects a single element.
525          * @param {String} selector The selector/xpath query
526          * @param {Node} root (optional) The start of the query (defaults to document).
527          * @return {Element}
528          */
529         selectNode : function(path, root){
530             return Roo.DomQuery.select(path, root)[0];
531         },
532
533         /**
534          * Selects the value of a node, optionally replacing null with the defaultValue.
535          * @param {String} selector The selector/xpath query
536          * @param {Node} root (optional) The start of the query (defaults to document).
537          * @param {String} defaultValue
538          */
539         selectValue : function(path, root, defaultValue){
540             path = path.replace(trimRe, "");
541             if(!valueCache[path]){
542                 valueCache[path] = Roo.DomQuery.compile(path, "select");
543             }
544             var n = valueCache[path](root);
545             n = n[0] ? n[0] : n;
546             var v = (n && n.firstChild ? n.firstChild.nodeValue : null);
547             return ((v === null||v === undefined||v==='') ? defaultValue : v);
548         },
549
550         /**
551          * Selects the value of a node, parsing integers and floats.
552          * @param {String} selector The selector/xpath query
553          * @param {Node} root (optional) The start of the query (defaults to document).
554          * @param {Number} defaultValue
555          * @return {Number}
556          */
557         selectNumber : function(path, root, defaultValue){
558             var v = Roo.DomQuery.selectValue(path, root, defaultValue || 0);
559             return parseFloat(v);
560         },
561
562         /**
563          * Returns true if the passed element(s) match the passed simple selector (e.g. div.some-class or span:first-child)
564          * @param {String/HTMLElement/Array} el An element id, element or array of elements
565          * @param {String} selector The simple selector to test
566          * @return {Boolean}
567          */
568         is : function(el, ss){
569             if(typeof el == "string"){
570                 el = document.getElementById(el);
571             }
572             var isArray = (el instanceof Array);
573             var result = Roo.DomQuery.filter(isArray ? el : [el], ss);
574             return isArray ? (result.length == el.length) : (result.length > 0);
575         },
576
577         /**
578          * Filters an array of elements to only include matches of a simple selector (e.g. div.some-class or span:first-child)
579          * @param {Array} el An array of elements to filter
580          * @param {String} selector The simple selector to test
581          * @param {Boolean} nonMatches If true, it returns the elements that DON'T match
582          * the selector instead of the ones that match
583          * @return {Array}
584          */
585         filter : function(els, ss, nonMatches){
586             ss = ss.replace(trimRe, "");
587             if(!simpleCache[ss]){
588                 simpleCache[ss] = Roo.DomQuery.compile(ss, "simple");
589             }
590             var result = simpleCache[ss](els);
591             return nonMatches ? quickDiff(result, els) : result;
592         },
593
594         /**
595          * Collection of matching regular expressions and code snippets.
596          */
597         matchers : [{
598                 re: /^\.([\w-]+)/,
599                 select: 'n = byClassName(n, null, " {1} ");'
600             }, {
601                 re: /^\:([\w-]+)(?:\(((?:[^\s>\/]*|.*?))\))?/,
602                 select: 'n = byPseudo(n, "{1}", "{2}");'
603             },{
604                 re: /^(?:([\[\{])(?:@)?([\w-]+)\s?(?:(=|.=)\s?['"]?(.*?)["']?)?[\]\}])/,
605                 select: 'n = byAttribute(n, "{2}", "{4}", "{3}", "{1}");'
606             }, {
607                 re: /^#([\w-]+)/,
608                 select: 'n = byId(n, null, "{1}");'
609             },{
610                 re: /^@([\w-]+)/,
611                 select: 'return {firstChild:{nodeValue:attrValue(n, "{1}")}};'
612             }
613         ],
614
615         /**
616          * Collection of operator comparison functions. The default operators are =, !=, ^=, $=, *=, %=, |= and ~=.
617          * 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, &gt; &lt;.
618          */
619         operators : {
620             "=" : function(a, v){
621                 return a == v;
622             },
623             "!=" : function(a, v){
624                 return a != v;
625             },
626             "^=" : function(a, v){
627                 return a && a.substr(0, v.length) == v;
628             },
629             "$=" : function(a, v){
630                 return a && a.substr(a.length-v.length) == v;
631             },
632             "*=" : function(a, v){
633                 return a && a.indexOf(v) !== -1;
634             },
635             "%=" : function(a, v){
636                 return (a % v) == 0;
637             },
638             "|=" : function(a, v){
639                 return a && (a == v || a.substr(0, v.length+1) == v+'-');
640             },
641             "~=" : function(a, v){
642                 return a && (' '+a+' ').indexOf(' '+v+' ') != -1;
643             }
644         },
645
646         /**
647          * Collection of "pseudo class" processors. Each processor is passed the current nodeset (array)
648          * and the argument (if any) supplied in the selector.
649          */
650         pseudos : {
651             "first-child" : function(c){
652                 var r = [], ri = -1, n;
653                 for(var i = 0, ci; ci = n = c[i]; i++){
654                     while((n = n.previousSibling) && n.nodeType != 1);
655                     if(!n){
656                         r[++ri] = ci;
657                     }
658                 }
659                 return r;
660             },
661
662             "last-child" : function(c){
663                 var r = [], ri = -1, n;
664                 for(var i = 0, ci; ci = n = c[i]; i++){
665                     while((n = n.nextSibling) && n.nodeType != 1);
666                     if(!n){
667                         r[++ri] = ci;
668                     }
669                 }
670                 return r;
671             },
672
673             "nth-child" : function(c, a) {
674                 var r = [], ri = -1;
675                 var m = nthRe.exec(a == "even" && "2n" || a == "odd" && "2n+1" || !nthRe2.test(a) && "n+" + a || a);
676                 var f = (m[1] || 1) - 0, l = m[2] - 0;
677                 for(var i = 0, n; n = c[i]; i++){
678                     var pn = n.parentNode;
679                     if (batch != pn._batch) {
680                         var j = 0;
681                         for(var cn = pn.firstChild; cn; cn = cn.nextSibling){
682                             if(cn.nodeType == 1){
683                                cn.nodeIndex = ++j;
684                             }
685                         }
686                         pn._batch = batch;
687                     }
688                     if (f == 1) {
689                         if (l == 0 || n.nodeIndex == l){
690                             r[++ri] = n;
691                         }
692                     } else if ((n.nodeIndex + l) % f == 0){
693                         r[++ri] = n;
694                     }
695                 }
696
697                 return r;
698             },
699
700             "only-child" : function(c){
701                 var r = [], ri = -1;;
702                 for(var i = 0, ci; ci = c[i]; i++){
703                     if(!prev(ci) && !next(ci)){
704                         r[++ri] = ci;
705                     }
706                 }
707                 return r;
708             },
709
710             "empty" : function(c){
711                 var r = [], ri = -1;
712                 for(var i = 0, ci; ci = c[i]; i++){
713                     var cns = ci.childNodes, j = 0, cn, empty = true;
714                     while(cn = cns[j]){
715                         ++j;
716                         if(cn.nodeType == 1 || cn.nodeType == 3){
717                             empty = false;
718                             break;
719                         }
720                     }
721                     if(empty){
722                         r[++ri] = ci;
723                     }
724                 }
725                 return r;
726             },
727
728             "contains" : function(c, v){
729                 var r = [], ri = -1;
730                 for(var i = 0, ci; ci = c[i]; i++){
731                     if((ci.textContent||ci.innerText||'').indexOf(v) != -1){
732                         r[++ri] = ci;
733                     }
734                 }
735                 return r;
736             },
737
738             "nodeValue" : function(c, v){
739                 var r = [], ri = -1;
740                 for(var i = 0, ci; ci = c[i]; i++){
741                     if(ci.firstChild && ci.firstChild.nodeValue == v){
742                         r[++ri] = ci;
743                     }
744                 }
745                 return r;
746             },
747
748             "checked" : function(c){
749                 var r = [], ri = -1;
750                 for(var i = 0, ci; ci = c[i]; i++){
751                     if(ci.checked == true){
752                         r[++ri] = ci;
753                     }
754                 }
755                 return r;
756             },
757
758             "not" : function(c, ss){
759                 return Roo.DomQuery.filter(c, ss, true);
760             },
761
762             "odd" : function(c){
763                 return this["nth-child"](c, "odd");
764             },
765
766             "even" : function(c){
767                 return this["nth-child"](c, "even");
768             },
769
770             "nth" : function(c, a){
771                 return c[a-1] || [];
772             },
773
774             "first" : function(c){
775                 return c[0] || [];
776             },
777
778             "last" : function(c){
779                 return c[c.length-1] || [];
780             },
781
782             "has" : function(c, ss){
783                 var s = Roo.DomQuery.select;
784                 var r = [], ri = -1;
785                 for(var i = 0, ci; ci = c[i]; i++){
786                     if(s(ss, ci).length > 0){
787                         r[++ri] = ci;
788                     }
789                 }
790                 return r;
791             },
792
793             "next" : function(c, ss){
794                 var is = Roo.DomQuery.is;
795                 var r = [], ri = -1;
796                 for(var i = 0, ci; ci = c[i]; i++){
797                     var n = next(ci);
798                     if(n && is(n, ss)){
799                         r[++ri] = ci;
800                     }
801                 }
802                 return r;
803             },
804
805             "prev" : function(c, ss){
806                 var is = Roo.DomQuery.is;
807                 var r = [], ri = -1;
808                 for(var i = 0, ci; ci = c[i]; i++){
809                     var n = prev(ci);
810                     if(n && is(n, ss)){
811                         r[++ri] = ci;
812                     }
813                 }
814                 return r;
815             }
816         }
817     };
818 }();
819
820 /**
821  * Selects an array of DOM nodes by CSS/XPath selector. Shorthand of {@link Roo.DomQuery#select}
822  * @param {String} path The selector/xpath query
823  * @param {Node} root (optional) The start of the query (defaults to document).
824  * @return {Array}
825  * @member Roo
826  * @method query
827  */
828 Roo.query = Roo.DomQuery.select;