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