2 * Ext JS Library 2.2.1
\r
3 * Copyright(c) 2006-2009, Ext JS, LLC.
\r
4 * licensing@extjs.com
\r
6 * http://extjs.com/license
\r
10 * This is code is also distributed under MIT license for use
\r
11 * with jQuery and prototype JavaScript libraries.
\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
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
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
22 <h4>Element Selectors:</h4>
\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
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
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
42 <h4>Pseudo Classes:</h4>
\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
63 <h4>CSS Value Selectors:</h4>
\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
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
83 function child(p, index){
\r
85 var n = p.firstChild;
\r
87 if(n.nodeType == 1){
\r
98 while((n = n.nextSibling) && n.nodeType != 1);
\r
103 while((n = n.previousSibling) && n.nodeType != 1);
\r
107 function children(d){
\r
108 var n = d.firstChild, ni = -1;
\r
110 var nx = n.nextSibling;
\r
111 if(n.nodeType == 3 && !nonSpace.test(n.nodeValue)){
\r
114 n.nodeIndex = ++ni;
\r
121 function byClassName(c, a, v){
\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
134 function attrValue(n, attr){
\r
135 if(!n.tagName && typeof n.length != "undefined"){
\r
144 if(attr == "class" || attr == "className"){
\r
145 return n.className;
\r
147 return n.getAttribute(attr) || n[attr];
\r
151 function getNodes(ns, mode, tagName){
\r
152 var result = [], ri = -1, cs;
\r
156 tagName = tagName || "*";
\r
157 if(typeof ns.getElementsByTagName != "undefined"){
\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
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
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
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
196 function concat(a, b){
\r
198 return a.concat(b);
\r
200 for(var i = 0, l = b.length; i < l; i++){
\r
201 a[a.length] = b[i];
\r
206 function byTag(cs, tagName){
\r
207 if(cs.tagName || cs == document){
\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
223 function byId(cs, attr, id){
\r
224 if(cs.tagName || cs == document){
\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
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
246 a = Ext.DomQuery.getStyle(ci, attr);
\r
248 else if(attr == "class" || attr == "className"){
\r
250 }else if(attr == "for"){
\r
252 }else if(attr == "href"){
\r
253 a = ci.getAttribute("href", 2);
\r
255 a = ci.getAttribute(attr);
\r
257 if((f && f(a, value)) || (!f && a)){
\r
264 function byPseudo(cs, name, value){
\r
265 return Ext.DomQuery.pseudos[name](cs, value);
\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
273 // this eval is stop the compressor from
\r
274 // renaming the variable to something shorter
\r
275 eval("var batch = 30803;");
\r
279 function nodupIEXml(cs){
\r
281 cs[0].setAttribute("_nodup", d);
\r
283 for(var i = 1, len = cs.length; i < len; i++){
\r
285 if(!c.getAttribute("_nodup") != d){
\r
286 c.setAttribute("_nodup", d);
\r
290 for(var i = 0, len = cs.length; i < len; i++){
\r
291 cs[i].removeAttribute("_nodup");
\r
296 function nodup(cs){
\r
300 var len = cs.length, c, i, r = cs, cj, ri = -1;
\r
301 if(!len || typeof cs.nodeType != "undefined" || len == 1){
\r
304 if(isIE && typeof cs[0].selectSingleNode != "undefined"){
\r
305 return nodupIEXml(cs);
\r
309 for(i = 1; c = cs[i]; i++){
\r
314 for(var j = 0; j < i; j++){
\r
317 for(j = i+1; cj = cs[j]; j++){
\r
318 if(cj._nodup != d){
\r
329 function quickDiffIEXml(c1, c2){
\r
331 for(var i = 0, len = c1.length; i < len; i++){
\r
332 c1[i].setAttribute("_qdiff", d);
\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
340 for(var i = 0, len = c1.length; i < len; i++){
\r
341 c1[i].removeAttribute("_qdiff");
\r
346 function quickDiff(c1, c2){
\r
347 var len1 = c1.length;
\r
351 if(isIE && c1[0].selectSingleNode){
\r
352 return quickDiffIEXml(c1, c2);
\r
355 for(var i = 0; i < len1; i++){
\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
367 function quickId(ns, mode, root, id){
\r
369 var d = root.ownerDocument || root;
\r
370 return d.getElementById(id);
\r
372 ns = getNodes(ns, mode, "*");
\r
373 return byId(ns, null, id);
\r
377 getStyle : function(el, name){
\r
378 return Ext.fly(el).getStyle(name);
\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
387 compile : function(path, type){
\r
388 type = type || "select";
\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
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
402 // strip leading slashes
\r
403 while(path.substr(0, 1)=="/"){
\r
404 path = path.substr(1);
\r
407 while(q && lq != q){
\r
409 var tm = q.match(tagTokenRe);
\r
410 if(type == "select"){
\r
413 fn[fn.length] = 'n = quickId(n, mode, root, "'+tm[2]+'");';
\r
415 fn[fn.length] = 'n = getNodes(n, mode, "'+tm[2]+'");';
\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
424 fn[fn.length] = 'n = byId(n, null, "'+tm[2]+'");';
\r
426 fn[fn.length] = 'n = byTag(n, "'+tm[2]+'");';
\r
428 q = q.replace(tm[0], "");
\r
431 while(!(mm = q.match(modeRe))){
\r
432 var matched = false;
\r
433 for(var j = 0; j < tklen; j++){
\r
435 var m = q.match(t.re);
\r
437 fn[fn.length] = t.select.replace(tplRe, function(x, i){
\r
440 q = q.replace(m[0], "");
\r
445 // prevent infinite loop on bad selector
\r
447 throw 'Error parsing selector, parsing failed at "' + q + '"';
\r
451 fn[fn.length] = 'mode="'+mm[1].replace(trimRe, "")+'";';
\r
452 q = q.replace(mm[1], "");
\r
455 fn[fn.length] = "return nodup(n);\n}";
\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
467 select : function(path, root, type){
\r
468 if(!root || root == document){
\r
471 if(typeof root == "string"){
\r
472 root = document.getElementById(root);
\r
474 var paths = path.split(",");
\r
476 for(var i = 0, len = paths.length; i < len; i++){
\r
477 var p = paths[i].replace(trimRe, "");
\r
479 cache[p] = Ext.DomQuery.compile(p);
\r
481 throw p + " is not a valid selector";
\r
484 var result = cache[p](root);
\r
485 if(result && result != document){
\r
486 results = results.concat(result);
\r
489 if(paths.length > 1){
\r
490 return nodup(results);
\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
501 selectNode : function(path, root){
\r
502 return Ext.DomQuery.select(path, root)[0];
\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
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
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
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
530 selectNumber : function(path, root, defaultValue){
\r
531 var v = Ext.DomQuery.selectValue(path, root, defaultValue || 0);
\r
532 return parseFloat(v);
\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
541 is : function(el, ss){
\r
542 if(typeof el == "string"){
\r
543 el = document.getElementById(el);
\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
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
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
564 var result = simpleCache[ss](els);
\r
565 return nonMatches ? quickDiff(result, els) : result;
\r
569 * Collection of matching regular expressions and code snippets.
\r
573 select: 'n = byClassName(n, null, " {1} ");'
\r
575 re: /^\:([\w-]+)(?:\(((?:[^\s>\/]*|.*?))\))?/,
\r
576 select: 'n = byPseudo(n, "{1}", "{2}");'
\r
578 re: /^(?:([\[\{])(?:@)?([\w-]+)\s?(?:(=|.=)\s?['"]?(.*?)["']?)?[\]\}])/,
\r
579 select: 'n = byAttribute(n, "{2}", "{4}", "{3}", "{1}");'
\r
582 select: 'n = byId(n, null, "{1}");'
\r
585 select: 'return {firstChild:{nodeValue:attrValue(n, "{1}")}};'
\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, > <.
\r
594 "=" : function(a, v){
\r
597 "!=" : function(a, v){
\r
600 "^=" : function(a, v){
\r
601 return a && a.substr(0, v.length) == v;
\r
603 "$=" : function(a, v){
\r
604 return a && a.substr(a.length-v.length) == v;
\r
606 "*=" : function(a, v){
\r
607 return a && a.indexOf(v) !== -1;
\r
609 "%=" : function(a, v){
\r
610 return (a % v) == 0;
\r
612 "|=" : function(a, v){
\r
613 return a && (a == v || a.substr(0, v.length+1) == v+'-');
\r
615 "~=" : function(a, v){
\r
616 return a && (' '+a+' ').indexOf(' '+v+' ') != -1;
\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
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
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
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
655 for(var cn = pn.firstChild; cn; cn = cn.nextSibling){
\r
656 if(cn.nodeType == 1){
\r
657 cn.nodeIndex = ++j;
\r
663 if (l == 0 || n.nodeIndex == l){
\r
666 } else if ((n.nodeIndex + l) % f == 0){
\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
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
690 if(cn.nodeType == 1 || cn.nodeType == 3){
\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
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
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
732 "not" : function(c, ss){
\r
733 return Ext.DomQuery.filter(c, ss, true);
\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
750 "odd" : function(c){
\r
751 return this["nth-child"](c, "odd");
\r
754 "even" : function(c){
\r
755 return this["nth-child"](c, "even");
\r
758 "nth" : function(c, a){
\r
759 return c[a-1] || [];
\r
762 "first" : function(c){
\r
766 "last" : function(c){
\r
767 return c[c.length-1] || [];
\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
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
786 if(n && is(n, ss)){
\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
798 if(n && is(n, ss)){
\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
816 Ext.query = Ext.DomQuery.select;
\r