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