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