initial import
[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                     if(tm){
418                         if(tm[1] == "#"){
419                             fn[fn.length] = 'n = quickId(n, mode, root, "'+tm[2]+'");';
420                         }else{
421                             fn[fn.length] = 'n = getNodes(n, mode, "'+tm[2]+'");';
422                         }
423                         q = q.replace(tm[0], "");
424                     }else if(q.substr(0, 1) != '@'){
425                         fn[fn.length] = 'n = getNodes(n, mode, "*");';
426                     }
427                 }else{
428                     if(tm){
429                         if(tm[1] == "#"){
430                             fn[fn.length] = 'n = byId(n, null, "'+tm[2]+'");';
431                         }else{
432                             fn[fn.length] = 'n = byTag(n, "'+tm[2]+'");';
433                         }
434                         q = q.replace(tm[0], "");
435                     }
436                 }
437                 while(!(mm = q.match(modeRe))){
438                     var matched = false;
439                     for(var j = 0; j < tklen; j++){
440                         var t = tk[j];
441                         var m = q.match(t.re);
442                         if(m){
443                             fn[fn.length] = t.select.replace(tplRe, function(x, i){
444                                                     return m[i];
445                                                 });
446                             q = q.replace(m[0], "");
447                             matched = true;
448                             break;
449                         }
450                     }
451                     // prevent infinite loop on bad selector
452                     if(!matched){
453                         throw 'Error parsing selector, parsing failed at "' + q + '"';
454                     }
455                 }
456                 if(mm[1]){
457                     fn[fn.length] = 'mode="'+mm[1].replace(trimRe, "")+'";';
458                     q = q.replace(mm[1], "");
459                 }
460             }
461             fn[fn.length] = "return nodup(n);\n}";
462             
463              /** 
464               * list of variables that need from compression as they are used by eval.
465              *  eval:var:batch 
466              *  eval:var:nodup
467              *  eval:var:byTag
468              *  eval:var:ById
469              *  eval:var:getNodes
470              *  eval:var:quickId
471              *  eval:var:mode
472              *  eval:var:root
473              *  eval:var:n
474              *  eval:var:byClassName
475              *  eval:var:byPseudo
476              *  eval:var:byAttribute
477              *  eval:var:attrValue
478              * 
479              **/ 
480             eval(fn.join(""));
481             return f;
482         },
483
484         /**
485          * Selects a group of elements.
486          * @param {String} selector The selector/xpath query (can be a comma separated list of selectors)
487          * @param {Node} root (optional) The start of the query (defaults to document).
488          * @return {Array}
489          */
490         select : function(path, root, type){
491             if(!root || root == document){
492                 root = document;
493             }
494             if(typeof root == "string"){
495                 root = document.getElementById(root);
496             }
497             var paths = path.split(",");
498             var results = [];
499             for(var i = 0, len = paths.length; i < len; i++){
500                 var p = paths[i].replace(trimRe, "");
501                 if(!cache[p]){
502                     cache[p] = Roo.DomQuery.compile(p);
503                     if(!cache[p]){
504                         throw p + " is not a valid selector";
505                     }
506                 }
507                 var result = cache[p](root);
508                 if(result && result != document){
509                     results = results.concat(result);
510                 }
511             }
512             if(paths.length > 1){
513                 return nodup(results);
514             }
515             return results;
516         },
517
518         /**
519          * Selects a single element.
520          * @param {String} selector The selector/xpath query
521          * @param {Node} root (optional) The start of the query (defaults to document).
522          * @return {Element}
523          */
524         selectNode : function(path, root){
525             return Roo.DomQuery.select(path, root)[0];
526         },
527
528         /**
529          * Selects the value of a node, optionally replacing null with the defaultValue.
530          * @param {String} selector The selector/xpath query
531          * @param {Node} root (optional) The start of the query (defaults to document).
532          * @param {String} defaultValue
533          */
534         selectValue : function(path, root, defaultValue){
535             path = path.replace(trimRe, "");
536             if(!valueCache[path]){
537                 valueCache[path] = Roo.DomQuery.compile(path, "select");
538             }
539             var n = valueCache[path](root);
540             n = n[0] ? n[0] : n;
541             var v = (n && n.firstChild ? n.firstChild.nodeValue : null);
542             return ((v === null||v === undefined||v==='') ? defaultValue : v);
543         },
544
545         /**
546          * Selects the value of a node, parsing integers and floats.
547          * @param {String} selector The selector/xpath query
548          * @param {Node} root (optional) The start of the query (defaults to document).
549          * @param {Number} defaultValue
550          * @return {Number}
551          */
552         selectNumber : function(path, root, defaultValue){
553             var v = Roo.DomQuery.selectValue(path, root, defaultValue || 0);
554             return parseFloat(v);
555         },
556
557         /**
558          * Returns true if the passed element(s) match the passed simple selector (e.g. div.some-class or span:first-child)
559          * @param {String/HTMLElement/Array} el An element id, element or array of elements
560          * @param {String} selector The simple selector to test
561          * @return {Boolean}
562          */
563         is : function(el, ss){
564             if(typeof el == "string"){
565                 el = document.getElementById(el);
566             }
567             var isArray = (el instanceof Array);
568             var result = Roo.DomQuery.filter(isArray ? el : [el], ss);
569             return isArray ? (result.length == el.length) : (result.length > 0);
570         },
571
572         /**
573          * Filters an array of elements to only include matches of a simple selector (e.g. div.some-class or span:first-child)
574          * @param {Array} el An array of elements to filter
575          * @param {String} selector The simple selector to test
576          * @param {Boolean} nonMatches If true, it returns the elements that DON'T match
577          * the selector instead of the ones that match
578          * @return {Array}
579          */
580         filter : function(els, ss, nonMatches){
581             ss = ss.replace(trimRe, "");
582             if(!simpleCache[ss]){
583                 simpleCache[ss] = Roo.DomQuery.compile(ss, "simple");
584             }
585             var result = simpleCache[ss](els);
586             return nonMatches ? quickDiff(result, els) : result;
587         },
588
589         /**
590          * Collection of matching regular expressions and code snippets.
591          */
592         matchers : [{
593                 re: /^\.([\w-]+)/,
594                 select: 'n = byClassName(n, null, " {1} ");'
595             }, {
596                 re: /^\:([\w-]+)(?:\(((?:[^\s>\/]*|.*?))\))?/,
597                 select: 'n = byPseudo(n, "{1}", "{2}");'
598             },{
599                 re: /^(?:([\[\{])(?:@)?([\w-]+)\s?(?:(=|.=)\s?['"]?(.*?)["']?)?[\]\}])/,
600                 select: 'n = byAttribute(n, "{2}", "{4}", "{3}", "{1}");'
601             }, {
602                 re: /^#([\w-]+)/,
603                 select: 'n = byId(n, null, "{1}");'
604             },{
605                 re: /^@([\w-]+)/,
606                 select: 'return {firstChild:{nodeValue:attrValue(n, "{1}")}};'
607             }
608         ],
609
610         /**
611          * Collection of operator comparison functions. The default operators are =, !=, ^=, $=, *=, %=, |= and ~=.
612          * 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;.
613          */
614         operators : {
615             "=" : function(a, v){
616                 return a == v;
617             },
618             "!=" : function(a, v){
619                 return a != v;
620             },
621             "^=" : function(a, v){
622                 return a && a.substr(0, v.length) == v;
623             },
624             "$=" : function(a, v){
625                 return a && a.substr(a.length-v.length) == v;
626             },
627             "*=" : function(a, v){
628                 return a && a.indexOf(v) !== -1;
629             },
630             "%=" : function(a, v){
631                 return (a % v) == 0;
632             },
633             "|=" : function(a, v){
634                 return a && (a == v || a.substr(0, v.length+1) == v+'-');
635             },
636             "~=" : function(a, v){
637                 return a && (' '+a+' ').indexOf(' '+v+' ') != -1;
638             }
639         },
640
641         /**
642          * Collection of "pseudo class" processors. Each processor is passed the current nodeset (array)
643          * and the argument (if any) supplied in the selector.
644          */
645         pseudos : {
646             "first-child" : function(c){
647                 var r = [], ri = -1, n;
648                 for(var i = 0, ci; ci = n = c[i]; i++){
649                     while((n = n.previousSibling) && n.nodeType != 1);
650                     if(!n){
651                         r[++ri] = ci;
652                     }
653                 }
654                 return r;
655             },
656
657             "last-child" : function(c){
658                 var r = [], ri = -1, n;
659                 for(var i = 0, ci; ci = n = c[i]; i++){
660                     while((n = n.nextSibling) && n.nodeType != 1);
661                     if(!n){
662                         r[++ri] = ci;
663                     }
664                 }
665                 return r;
666             },
667
668             "nth-child" : function(c, a) {
669                 var r = [], ri = -1;
670                 var m = nthRe.exec(a == "even" && "2n" || a == "odd" && "2n+1" || !nthRe2.test(a) && "n+" + a || a);
671                 var f = (m[1] || 1) - 0, l = m[2] - 0;
672                 for(var i = 0, n; n = c[i]; i++){
673                     var pn = n.parentNode;
674                     if (batch != pn._batch) {
675                         var j = 0;
676                         for(var cn = pn.firstChild; cn; cn = cn.nextSibling){
677                             if(cn.nodeType == 1){
678                                cn.nodeIndex = ++j;
679                             }
680                         }
681                         pn._batch = batch;
682                     }
683                     if (f == 1) {
684                         if (l == 0 || n.nodeIndex == l){
685                             r[++ri] = n;
686                         }
687                     } else if ((n.nodeIndex + l) % f == 0){
688                         r[++ri] = n;
689                     }
690                 }
691
692                 return r;
693             },
694
695             "only-child" : function(c){
696                 var r = [], ri = -1;;
697                 for(var i = 0, ci; ci = c[i]; i++){
698                     if(!prev(ci) && !next(ci)){
699                         r[++ri] = ci;
700                     }
701                 }
702                 return r;
703             },
704
705             "empty" : function(c){
706                 var r = [], ri = -1;
707                 for(var i = 0, ci; ci = c[i]; i++){
708                     var cns = ci.childNodes, j = 0, cn, empty = true;
709                     while(cn = cns[j]){
710                         ++j;
711                         if(cn.nodeType == 1 || cn.nodeType == 3){
712                             empty = false;
713                             break;
714                         }
715                     }
716                     if(empty){
717                         r[++ri] = ci;
718                     }
719                 }
720                 return r;
721             },
722
723             "contains" : function(c, v){
724                 var r = [], ri = -1;
725                 for(var i = 0, ci; ci = c[i]; i++){
726                     if((ci.textContent||ci.innerText||'').indexOf(v) != -1){
727                         r[++ri] = ci;
728                     }
729                 }
730                 return r;
731             },
732
733             "nodeValue" : function(c, v){
734                 var r = [], ri = -1;
735                 for(var i = 0, ci; ci = c[i]; i++){
736                     if(ci.firstChild && ci.firstChild.nodeValue == v){
737                         r[++ri] = ci;
738                     }
739                 }
740                 return r;
741             },
742
743             "checked" : function(c){
744                 var r = [], ri = -1;
745                 for(var i = 0, ci; ci = c[i]; i++){
746                     if(ci.checked == true){
747                         r[++ri] = ci;
748                     }
749                 }
750                 return r;
751             },
752
753             "not" : function(c, ss){
754                 return Roo.DomQuery.filter(c, ss, true);
755             },
756
757             "odd" : function(c){
758                 return this["nth-child"](c, "odd");
759             },
760
761             "even" : function(c){
762                 return this["nth-child"](c, "even");
763             },
764
765             "nth" : function(c, a){
766                 return c[a-1] || [];
767             },
768
769             "first" : function(c){
770                 return c[0] || [];
771             },
772
773             "last" : function(c){
774                 return c[c.length-1] || [];
775             },
776
777             "has" : function(c, ss){
778                 var s = Roo.DomQuery.select;
779                 var r = [], ri = -1;
780                 for(var i = 0, ci; ci = c[i]; i++){
781                     if(s(ss, ci).length > 0){
782                         r[++ri] = ci;
783                     }
784                 }
785                 return r;
786             },
787
788             "next" : function(c, ss){
789                 var is = Roo.DomQuery.is;
790                 var r = [], ri = -1;
791                 for(var i = 0, ci; ci = c[i]; i++){
792                     var n = next(ci);
793                     if(n && is(n, ss)){
794                         r[++ri] = ci;
795                     }
796                 }
797                 return r;
798             },
799
800             "prev" : function(c, ss){
801                 var is = Roo.DomQuery.is;
802                 var r = [], ri = -1;
803                 for(var i = 0, ci; ci = c[i]; i++){
804                     var n = prev(ci);
805                     if(n && is(n, ss)){
806                         r[++ri] = ci;
807                     }
808                 }
809                 return r;
810             }
811         }
812     };
813 }();
814
815 /**
816  * Selects an array of DOM nodes by CSS/XPath selector. Shorthand of {@link Roo.DomQuery#select}
817  * @param {String} path The selector/xpath query
818  * @param {Node} root (optional) The start of the query (defaults to document).
819  * @return {Array}
820  * @member Roo
821  * @method query
822  */
823 Roo.query = Roo.DomQuery.select;