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