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