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