2 * Sizzle CSS Selector Engine - v1.0
3 * Copyright 2009, The Dojo Foundation
4 * Released under the MIT, BSD, and GPL Licenses.
5 * More information: http://sizzlejs.com/
9 var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|['"][^'"]*['"]|[^[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?/g,
11 toString = Object.prototype.toString;
13 var Sizzle = function(selector, context, results, seed) {
14 results = results || [];
15 var origContext = context = context || document;
17 if ( context.nodeType !== 1 && context.nodeType !== 9 ) {
21 if ( !selector || typeof selector !== "string" ) {
25 var parts = [], m, set, checkSet, check, mode, extra, prune = true, contextXML = isXML(context);
27 // Reset the position of the chunker regexp (start from head)
28 chunker.lastIndex = 0;
30 while ( (m = chunker.exec(selector)) !== null ) {
34 extra = RegExp.rightContext;
39 if ( parts.length > 1 && origPOS.exec( selector ) ) {
40 if ( parts.length === 2 && Expr.relative[ parts[0] ] ) {
41 set = posProcess( parts[0] + parts[1], context );
43 set = Expr.relative[ parts[0] ] ?
45 Sizzle( parts.shift(), context );
47 while ( parts.length ) {
48 selector = parts.shift();
50 if ( Expr.relative[ selector ] )
51 selector += parts.shift();
53 set = posProcess( selector, set );
57 // Take a shortcut and set the context if the root selector is an ID
58 // (but not if it'll be faster if the inner selector is an ID)
59 if ( !seed && parts.length > 1 && context.nodeType === 9 && !contextXML &&
60 Expr.match.ID.test(parts[0]) && !Expr.match.ID.test(parts[parts.length - 1]) ) {
61 var ret = Sizzle.find( parts.shift(), context, contextXML );
62 context = ret.expr ? Sizzle.filter( ret.expr, ret.set )[0] : ret.set[0];
67 { expr: parts.pop(), set: makeArray(seed) } :
68 Sizzle.find( parts.pop(), parts.length === 1 && context.parentNode ? context.parentNode : context, contextXML );
69 set = ret.expr ? Sizzle.filter( ret.expr, ret.set ) : ret.set;
71 if ( parts.length > 0 ) {
72 checkSet = makeArray(set);
77 while ( parts.length ) {
78 var cur = parts.pop(), pop = cur;
80 if ( !Expr.relative[ cur ] ) {
90 Expr.relative[ cur ]( checkSet, pop, contextXML );
93 checkSet = parts = [];
102 throw "Syntax error, unrecognized expression: " + (cur || selector);
105 if ( toString.call(checkSet) === "[object Array]" ) {
107 results.push.apply( results, checkSet );
108 } else if ( context && context.nodeType === 1 ) {
109 for ( var i = 0; checkSet[i] != null; i++ ) {
110 if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && contains(context, checkSet[i])) ) {
111 results.push( set[i] );
115 for ( var i = 0; checkSet[i] != null; i++ ) {
116 if ( checkSet[i] && checkSet[i].nodeType === 1 ) {
117 results.push( set[i] );
122 makeArray( checkSet, results );
126 Sizzle( extra, origContext, results, seed );
127 Sizzle.uniqueSort( results );
133 Sizzle.uniqueSort = function(results){
135 hasDuplicate = false;
136 results.sort(sortOrder);
138 if ( hasDuplicate ) {
139 for ( var i = 1; i < results.length; i++ ) {
140 if ( results[i] === results[i-1] ) {
141 results.splice(i--, 1);
148 Sizzle.matches = function(expr, set){
149 return Sizzle(expr, null, null, set);
152 Sizzle.find = function(expr, context, isXML){
159 for ( var i = 0, l = Expr.order.length; i < l; i++ ) {
160 var type = Expr.order[i], match;
162 if ( (match = Expr.match[ type ].exec( expr )) ) {
163 var left = RegExp.leftContext;
165 if ( left.substr( left.length - 1 ) !== "\\" ) {
166 match[1] = (match[1] || "").replace(/\\/g, "");
167 set = Expr.find[ type ]( match, context, isXML );
169 expr = expr.replace( Expr.match[ type ], "" );
177 set = context.getElementsByTagName("*");
180 return {set: set, expr: expr};
183 Sizzle.filter = function(expr, set, inplace, not){
184 var old = expr, result = [], curLoop = set, match, anyFound,
185 isXMLFilter = set && set[0] && isXML(set[0]);
187 while ( expr && set.length ) {
188 for ( var type in Expr.filter ) {
189 if ( (match = Expr.match[ type ].exec( expr )) != null ) {
190 var filter = Expr.filter[ type ], found, item;
193 if ( curLoop == result ) {
197 if ( Expr.preFilter[ type ] ) {
198 match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not, isXMLFilter );
201 anyFound = found = true;
202 } else if ( match === true ) {
208 for ( var i = 0; (item = curLoop[i]) != null; i++ ) {
210 found = filter( item, match, i, curLoop );
211 var pass = not ^ !!found;
213 if ( inplace && found != null ) {
227 if ( found !== undefined ) {
232 expr = expr.replace( Expr.match[ type ], "" );
243 // Improper expression
245 if ( anyFound == null ) {
246 throw "Syntax error, unrecognized expression: " + expr;
258 var Expr = Sizzle.selectors = {
259 order: [ "ID", "NAME", "TAG" ],
261 ID: /#((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
262 CLASS: /\.((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
263 NAME: /\[name=['"]*((?:[\w\u00c0-\uFFFF_-]|\\.)+)['"]*\]/,
264 ATTR: /\[\s*((?:[\w\u00c0-\uFFFF_-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\s*\]/,
265 TAG: /^((?:[\w\u00c0-\uFFFF\*_-]|\\.)+)/,
266 CHILD: /:(only|nth|last|first)-child(?:\((even|odd|[\dn+-]*)\))?/,
267 POS: /:(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^-]|$)/,
268 PSEUDO: /:((?:[\w\u00c0-\uFFFF_-]|\\.)+)(?:\((['"]*)((?:\([^\)]+\)|[^\2\(\)]*)+)\2\))?/
271 "class": "className",
275 href: function(elem){
276 return elem.getAttribute("href");
280 "+": function(checkSet, part, isXML){
281 var isPartStr = typeof part === "string",
282 isTag = isPartStr && !/\W/.test(part),
283 isPartStrNotTag = isPartStr && !isTag;
285 if ( isTag && !isXML ) {
286 part = part.toUpperCase();
289 for ( var i = 0, l = checkSet.length, elem; i < l; i++ ) {
290 if ( (elem = checkSet[i]) ) {
291 while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {}
293 checkSet[i] = isPartStrNotTag || elem && elem.nodeName === part ?
299 if ( isPartStrNotTag ) {
300 Sizzle.filter( part, checkSet, true );
303 ">": function(checkSet, part, isXML){
304 var isPartStr = typeof part === "string";
306 if ( isPartStr && !/\W/.test(part) ) {
307 part = isXML ? part : part.toUpperCase();
309 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
310 var elem = checkSet[i];
312 var parent = elem.parentNode;
313 checkSet[i] = parent.nodeName === part ? parent : false;
317 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
318 var elem = checkSet[i];
320 checkSet[i] = isPartStr ?
322 elem.parentNode === part;
327 Sizzle.filter( part, checkSet, true );
331 "": function(checkSet, part, isXML){
332 var doneName = done++, checkFn = dirCheck;
334 if ( !part.match(/\W/) ) {
335 var nodeCheck = part = isXML ? part : part.toUpperCase();
336 checkFn = dirNodeCheck;
339 checkFn("parentNode", part, doneName, checkSet, nodeCheck, isXML);
341 "~": function(checkSet, part, isXML){
342 var doneName = done++, checkFn = dirCheck;
344 if ( typeof part === "string" && !part.match(/\W/) ) {
345 var nodeCheck = part = isXML ? part : part.toUpperCase();
346 checkFn = dirNodeCheck;
349 checkFn("previousSibling", part, doneName, checkSet, nodeCheck, isXML);
353 ID: function(match, context, isXML){
354 if ( typeof context.getElementById !== "undefined" && !isXML ) {
355 var m = context.getElementById(match[1]);
359 NAME: function(match, context, isXML){
360 if ( typeof context.getElementsByName !== "undefined" ) {
361 var ret = [], results = context.getElementsByName(match[1]);
363 for ( var i = 0, l = results.length; i < l; i++ ) {
364 if ( results[i].getAttribute("name") === match[1] ) {
365 ret.push( results[i] );
369 return ret.length === 0 ? null : ret;
372 TAG: function(match, context){
373 return context.getElementsByTagName(match[1]);
377 CLASS: function(match, curLoop, inplace, result, not, isXML){
378 match = " " + match[1].replace(/\\/g, "") + " ";
384 for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) {
386 if ( not ^ (elem.className && (" " + elem.className + " ").indexOf(match) >= 0) ) {
389 } else if ( inplace ) {
398 return match[1].replace(/\\/g, "");
400 TAG: function(match, curLoop){
401 for ( var i = 0; curLoop[i] === false; i++ ){}
402 return curLoop[i] && isXML(curLoop[i]) ? match[1] : match[1].toUpperCase();
404 CHILD: function(match){
405 if ( match[1] == "nth" ) {
406 // parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6'
407 var test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec(
408 match[2] == "even" && "2n" || match[2] == "odd" && "2n+1" ||
409 !/\D/.test( match[2] ) && "0n+" + match[2] || match[2]);
411 // calculate the numbers (first)n+(last) including if they are negative
412 match[2] = (test[1] + (test[2] || 1)) - 0;
413 match[3] = test[3] - 0;
416 // TODO: Move to normal caching system
421 ATTR: function(match, curLoop, inplace, result, not, isXML){
422 var name = match[1].replace(/\\/g, "");
424 if ( !isXML && Expr.attrMap[name] ) {
425 match[1] = Expr.attrMap[name];
428 if ( match[2] === "~=" ) {
429 match[4] = " " + match[4] + " ";
434 PSEUDO: function(match, curLoop, inplace, result, not){
435 if ( match[1] === "not" ) {
436 // If we're dealing with a complex expression, or a simple one
437 if ( match[3].match(chunker).length > 1 || /^\w/.test(match[3]) ) {
438 match[3] = Sizzle(match[3], null, null, curLoop);
440 var ret = Sizzle.filter(match[3], curLoop, inplace, true ^ not);
442 result.push.apply( result, ret );
446 } else if ( Expr.match.POS.test( match[0] ) || Expr.match.CHILD.test( match[0] ) ) {
452 POS: function(match){
453 match.unshift( true );
458 enabled: function(elem){
459 return elem.disabled === false && elem.type !== "hidden";
461 disabled: function(elem){
462 return elem.disabled === true;
464 checked: function(elem){
465 return elem.checked === true;
467 selected: function(elem){
468 // Accessing this property makes selected-by-default
469 // options in Safari work properly
470 elem.parentNode.selectedIndex;
471 return elem.selected === true;
473 parent: function(elem){
474 return !!elem.firstChild;
476 empty: function(elem){
477 return !elem.firstChild;
479 has: function(elem, i, match){
480 return !!Sizzle( match[3], elem ).length;
482 header: function(elem){
483 return /h\d/i.test( elem.nodeName );
485 text: function(elem){
486 return "text" === elem.type;
488 radio: function(elem){
489 return "radio" === elem.type;
491 checkbox: function(elem){
492 return "checkbox" === elem.type;
494 file: function(elem){
495 return "file" === elem.type;
497 password: function(elem){
498 return "password" === elem.type;
500 submit: function(elem){
501 return "submit" === elem.type;
503 image: function(elem){
504 return "image" === elem.type;
506 reset: function(elem){
507 return "reset" === elem.type;
509 button: function(elem){
510 return "button" === elem.type || elem.nodeName.toUpperCase() === "BUTTON";
512 input: function(elem){
513 return /input|select|textarea|button/i.test(elem.nodeName);
517 first: function(elem, i){
520 last: function(elem, i, match, array){
521 return i === array.length - 1;
523 even: function(elem, i){
526 odd: function(elem, i){
529 lt: function(elem, i, match){
530 return i < match[3] - 0;
532 gt: function(elem, i, match){
533 return i > match[3] - 0;
535 nth: function(elem, i, match){
536 return match[3] - 0 == i;
538 eq: function(elem, i, match){
539 return match[3] - 0 == i;
543 PSEUDO: function(elem, match, i, array){
544 var name = match[1], filter = Expr.filters[ name ];
547 return filter( elem, i, match, array );
548 } else if ( name === "contains" ) {
549 return (elem.textContent || elem.innerText || "").indexOf(match[3]) >= 0;
550 } else if ( name === "not" ) {
553 for ( var i = 0, l = not.length; i < l; i++ ) {
554 if ( not[i] === elem ) {
562 CHILD: function(elem, match){
563 var type = match[1], node = elem;
567 while (node = node.previousSibling) {
568 if ( node.nodeType === 1 ) return false;
570 if ( type == 'first') return true;
573 while (node = node.nextSibling) {
574 if ( node.nodeType === 1 ) return false;
578 var first = match[2], last = match[3];
580 if ( first == 1 && last == 0 ) {
584 var doneName = match[0],
585 parent = elem.parentNode;
587 if ( parent && (parent.sizcache !== doneName || !elem.nodeIndex) ) {
589 for ( node = parent.firstChild; node; node = node.nextSibling ) {
590 if ( node.nodeType === 1 ) {
591 node.nodeIndex = ++count;
594 parent.sizcache = doneName;
597 var diff = elem.nodeIndex - last;
601 return ( diff % first == 0 && diff / first >= 0 );
605 ID: function(elem, match){
606 return elem.nodeType === 1 && elem.getAttribute("id") === match;
608 TAG: function(elem, match){
609 return (match === "*" && elem.nodeType === 1) || elem.nodeName === match;
611 CLASS: function(elem, match){
612 return (" " + (elem.className || elem.getAttribute("class")) + " ")
613 .indexOf( match ) > -1;
615 ATTR: function(elem, match){
617 result = Expr.attrHandle[ name ] ?
618 Expr.attrHandle[ name ]( elem ) :
619 elem[ name ] != null ?
621 elem.getAttribute( name ),
626 return result == null ?
631 value.indexOf(check) >= 0 :
633 (" " + value + " ").indexOf(check) >= 0 :
635 value && result !== false :
639 value.indexOf(check) === 0 :
641 value.substr(value.length - check.length) === check :
643 value === check || value.substr(0, check.length + 1) === check + "-" :
646 POS: function(elem, match, i, array){
647 var name = match[2], filter = Expr.setFilters[ name ];
650 return filter( elem, i, match, array );
656 var origPOS = Expr.match.POS;
658 for ( var type in Expr.match ) {
659 Expr.match[ type ] = RegExp( Expr.match[ type ].source + /(?![^\[]*\])(?![^\(]*\))/.source );
662 var makeArray = function(array, results) {
663 array = Array.prototype.slice.call( array );
666 results.push.apply( results, array );
673 // Perform a simple check to determine if the browser is capable of
674 // converting a NodeList to an array using builtin methods.
676 Array.prototype.slice.call( document.documentElement.childNodes );
678 // Provide a fallback method if it does not work
680 makeArray = function(array, results) {
681 var ret = results || [];
683 if ( toString.call(array) === "[object Array]" ) {
684 Array.prototype.push.apply( ret, array );
686 if ( typeof array.length === "number" ) {
687 for ( var i = 0, l = array.length; i < l; i++ ) {
688 ret.push( array[i] );
691 for ( var i = 0; array[i]; i++ ) {
692 ret.push( array[i] );
703 if ( document.documentElement.compareDocumentPosition ) {
704 sortOrder = function( a, b ) {
705 var ret = a.compareDocumentPosition(b) & 4 ? -1 : a === b ? 0 : 1;
711 } else if ( "sourceIndex" in document.documentElement ) {
712 sortOrder = function( a, b ) {
713 var ret = a.sourceIndex - b.sourceIndex;
719 } else if ( document.createRange ) {
720 sortOrder = function( a, b ) {
721 var aRange = a.ownerDocument.createRange(), bRange = b.ownerDocument.createRange();
722 aRange.selectNode(a);
723 aRange.collapse(true);
724 bRange.selectNode(b);
725 bRange.collapse(true);
726 var ret = aRange.compareBoundaryPoints(Range.START_TO_END, bRange);
734 // Check to see if the browser returns elements by name when
735 // querying by getElementById (and provide a workaround)
737 // We're going to inject a fake input element with a specified name
738 var form = document.createElement("form"),
739 id = "script" + (new Date).getTime();
740 form.innerHTML = "<input name='" + id + "'/>";
742 // Inject it into the root element, check its status, and remove it quickly
743 var root = document.documentElement;
744 root.insertBefore( form, root.firstChild );
746 // The workaround has to do additional checks after a getElementById
747 // Which slows things down for other browsers (hence the branching)
748 if ( !!document.getElementById( id ) ) {
749 Expr.find.ID = function(match, context, isXML){
750 if ( typeof context.getElementById !== "undefined" && !isXML ) {
751 var m = context.getElementById(match[1]);
752 return m ? m.id === match[1] || typeof m.getAttributeNode !== "undefined" && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
756 Expr.filter.ID = function(elem, match){
757 var node = typeof elem.getAttributeNode !== "undefined" && elem.getAttributeNode("id");
758 return elem.nodeType === 1 && node && node.nodeValue === match;
762 root.removeChild( form );
766 // Check to see if the browser returns only elements
767 // when doing getElementsByTagName("*")
769 // Create a fake element
770 var div = document.createElement("div");
771 div.appendChild( document.createComment("") );
773 // Make sure no comments are found
774 if ( div.getElementsByTagName("*").length > 0 ) {
775 Expr.find.TAG = function(match, context){
776 var results = context.getElementsByTagName(match[1]);
778 // Filter out possible comments
779 if ( match[1] === "*" ) {
782 for ( var i = 0; results[i]; i++ ) {
783 if ( results[i].nodeType === 1 ) {
784 tmp.push( results[i] );
795 // Check to see if an attribute returns normalized href attributes
796 div.innerHTML = "<a href='#'></a>";
797 if ( div.firstChild && typeof div.firstChild.getAttribute !== "undefined" &&
798 div.firstChild.getAttribute("href") !== "#" ) {
799 Expr.attrHandle.href = function(elem){
800 return elem.getAttribute("href", 2);
805 if ( document.querySelectorAll ) (function(){
806 var oldSizzle = Sizzle, div = document.createElement("div");
807 div.innerHTML = "<p class='TEST'></p>";
809 // Safari can't handle uppercase or unicode characters when
811 if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) {
815 Sizzle = function(query, context, extra, seed){
816 context = context || document;
818 // Only use querySelectorAll on non-XML documents
819 // (ID selectors don't work in non-HTML documents)
820 if ( !seed && context.nodeType === 9 && !isXML(context) ) {
822 return makeArray( context.querySelectorAll(query), extra );
826 return oldSizzle(query, context, extra, seed);
829 Sizzle.find = oldSizzle.find;
830 Sizzle.filter = oldSizzle.filter;
831 Sizzle.selectors = oldSizzle.selectors;
832 Sizzle.matches = oldSizzle.matches;
835 if ( document.getElementsByClassName && document.documentElement.getElementsByClassName ) (function(){
836 var div = document.createElement("div");
837 div.innerHTML = "<div class='test e'></div><div class='test'></div>";
839 // Opera can't find a second classname (in 9.6)
840 if ( div.getElementsByClassName("e").length === 0 )
843 // Safari caches class attributes, doesn't catch changes (in 3.2)
844 div.lastChild.className = "e";
846 if ( div.getElementsByClassName("e").length === 1 )
849 Expr.order.splice(1, 0, "CLASS");
850 Expr.find.CLASS = function(match, context, isXML) {
851 if ( typeof context.getElementsByClassName !== "undefined" && !isXML ) {
852 return context.getElementsByClassName(match[1]);
857 function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
858 var sibDir = dir == "previousSibling" && !isXML;
859 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
860 var elem = checkSet[i];
862 if ( sibDir && elem.nodeType === 1 ){
863 elem.sizcache = doneName;
870 if ( elem.sizcache === doneName ) {
871 match = checkSet[elem.sizset];
875 if ( elem.nodeType === 1 && !isXML ){
876 elem.sizcache = doneName;
880 if ( elem.nodeName === cur ) {
893 function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
894 var sibDir = dir == "previousSibling" && !isXML;
895 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
896 var elem = checkSet[i];
898 if ( sibDir && elem.nodeType === 1 ) {
899 elem.sizcache = doneName;
906 if ( elem.sizcache === doneName ) {
907 match = checkSet[elem.sizset];
911 if ( elem.nodeType === 1 ) {
913 elem.sizcache = doneName;
916 if ( typeof cur !== "string" ) {
917 if ( elem === cur ) {
922 } else if ( Sizzle.filter( cur, [elem] ).length > 0 ) {
936 var contains = document.compareDocumentPosition ? function(a, b){
937 return a.compareDocumentPosition(b) & 16;
939 return a !== b && (a.contains ? a.contains(b) : true);
942 var isXML = function(elem){
943 return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
944 !!elem.ownerDocument && elem.ownerDocument.documentElement.nodeName !== "HTML";
947 var posProcess = function(selector, context){
948 var tmpSet = [], later = "", match,
949 root = context.nodeType ? [context] : context;
951 // Position selectors must be done after the filter
952 // And so must :not(positional) so we move all PSEUDOs to the end
953 while ( (match = Expr.match.PSEUDO.exec( selector )) ) {
955 selector = selector.replace( Expr.match.PSEUDO, "" );
958 selector = Expr.relative[selector] ? selector + "*" : selector;
960 for ( var i = 0, l = root.length; i < l; i++ ) {
961 Sizzle( selector, root[i], tmpSet );
964 return Sizzle.filter( later, tmpSet );
968 jQuery.find = Sizzle;
969 jQuery.expr = Sizzle.selectors;
970 jQuery.expr[":"] = jQuery.expr.filters;
972 Sizzle.selectors.filters.hidden = function(elem){
973 return elem.offsetWidth === 0 || elem.offsetHeight === 0;
976 Sizzle.selectors.filters.visible = function(elem){
977 return elem.offsetWidth > 0 || elem.offsetHeight > 0;
980 Sizzle.selectors.filters.animated = function(elem){
981 return jQuery.grep(jQuery.timers, function(fn){
982 return elem === fn.elem;
986 jQuery.filter = jQuery.multiFilter = function( expr, elems, not ) {
988 expr = ":not(" + expr + ")";
991 return Sizzle.matches(expr, elems);
994 jQuery.dir = function( elem, dir ){
995 var matched = [], cur = elem[dir];
996 while ( cur && cur != document ) {
997 if ( cur.nodeType == 1 )
1004 jQuery.nth = function(cur, result, dir, elem){
1005 result = result || 1;
1008 for ( ; cur; cur = cur[dir] )
1009 if ( cur.nodeType == 1 && ++num == result )
1015 jQuery.sibling = function(n, elem){
1018 for ( ; n; n = n.nextSibling ) {
1019 if ( n.nodeType == 1 && n != elem )
1028 window.Sizzle = Sizzle;