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