2 * Sizzle CSS Selector Engine - v0.9.3
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 context = context || document;
17 if ( context.nodeType !== 1 && context.nodeType !== 9 )
20 if ( !selector || typeof selector !== "string" ) {
24 var parts = [], m, set, checkSet, check, mode, extra, prune = true;
26 // Reset the position of the chunker regexp (start from head)
27 chunker.lastIndex = 0;
29 while ( (m = chunker.exec(selector)) !== null ) {
33 extra = RegExp.rightContext;
38 if ( parts.length > 1 && origPOS.exec( selector ) ) {
39 if ( parts.length === 2 && Expr.relative[ parts[0] ] ) {
40 set = posProcess( parts[0] + parts[1], context );
42 set = Expr.relative[ parts[0] ] ?
44 Sizzle( parts.shift(), context );
46 while ( parts.length ) {
47 selector = parts.shift();
49 if ( Expr.relative[ selector ] )
50 selector += parts.shift();
52 set = posProcess( selector, set );
57 { expr: parts.pop(), set: makeArray(seed) } :
58 Sizzle.find( parts.pop(), parts.length === 1 && context.parentNode ? context.parentNode : context, isXML(context) );
59 set = Sizzle.filter( ret.expr, ret.set );
61 if ( parts.length > 0 ) {
62 checkSet = makeArray(set);
67 while ( parts.length ) {
68 var cur = parts.pop(), pop = cur;
70 if ( !Expr.relative[ cur ] ) {
80 Expr.relative[ cur ]( checkSet, pop, isXML(context) );
89 throw "Syntax error, unrecognized expression: " + (cur || selector);
92 if ( toString.call(checkSet) === "[object Array]" ) {
94 results.push.apply( results, checkSet );
95 } else if ( context.nodeType === 1 ) {
96 for ( var i = 0; checkSet[i] != null; i++ ) {
97 if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && contains(context, checkSet[i])) ) {
98 results.push( set[i] );
102 for ( var i = 0; checkSet[i] != null; i++ ) {
103 if ( checkSet[i] && checkSet[i].nodeType === 1 ) {
104 results.push( set[i] );
109 makeArray( checkSet, results );
113 Sizzle( extra, context, results, seed );
116 hasDuplicate = false;
117 results.sort(sortOrder);
119 if ( hasDuplicate ) {
120 for ( var i = 1; i < results.length; i++ ) {
121 if ( results[i] === results[i-1] ) {
122 results.splice(i--, 1);
132 Sizzle.matches = function(expr, set){
133 return Sizzle(expr, null, null, set);
136 Sizzle.find = function(expr, context, isXML){
143 for ( var i = 0, l = Expr.order.length; i < l; i++ ) {
144 var type = Expr.order[i], match;
146 if ( (match = Expr.match[ type ].exec( expr )) ) {
147 var left = RegExp.leftContext;
149 if ( left.substr( left.length - 1 ) !== "\\" ) {
150 match[1] = (match[1] || "").replace(/\\/g, "");
151 set = Expr.find[ type ]( match, context, isXML );
153 expr = expr.replace( Expr.match[ type ], "" );
161 set = context.getElementsByTagName("*");
164 return {set: set, expr: expr};
167 Sizzle.filter = function(expr, set, inplace, not){
168 var old = expr, result = [], curLoop = set, match, anyFound;
170 while ( expr && set.length ) {
171 for ( var type in Expr.filter ) {
172 if ( (match = Expr.match[ type ].exec( expr )) != null ) {
173 var filter = Expr.filter[ type ], found, item;
176 if ( curLoop == result ) {
180 if ( Expr.preFilter[ type ] ) {
181 match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not );
184 anyFound = found = true;
185 } else if ( match === true ) {
191 for ( var i = 0; (item = curLoop[i]) != null; i++ ) {
193 found = filter( item, match, i, curLoop );
194 var pass = not ^ !!found;
196 if ( inplace && found != null ) {
210 if ( found !== undefined ) {
215 expr = expr.replace( Expr.match[ type ], "" );
226 expr = expr.replace(/\s*,\s*/, "");
228 // Improper expression
230 if ( anyFound == null ) {
231 throw "Syntax error, unrecognized expression: " + expr;
243 var Expr = Sizzle.selectors = {
244 order: [ "ID", "NAME", "TAG" ],
246 ID: /#((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
247 CLASS: /\.((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
248 NAME: /\[name=['"]*((?:[\w\u00c0-\uFFFF_-]|\\.)+)['"]*\]/,
249 ATTR: /\[\s*((?:[\w\u00c0-\uFFFF_-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\s*\]/,
250 TAG: /^((?:[\w\u00c0-\uFFFF\*_-]|\\.)+)/,
251 CHILD: /:(only|nth|last|first)-child(?:\((even|odd|[\dn+-]*)\))?/,
252 POS: /:(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^-]|$)/,
253 PSEUDO: /:((?:[\w\u00c0-\uFFFF_-]|\\.)+)(?:\((['"]*)((?:\([^\)]+\)|[^\2\(\)]*)+)\2\))?/
256 "class": "className",
260 href: function(elem){
261 return elem.getAttribute("href");
265 "+": function(checkSet, part, isXML){
266 var isPartStr = typeof part === "string",
267 isTag = isPartStr && !/\W/.test(part),
268 isPartStrNotTag = isPartStr && !isTag;
269 if ( isTag && !isXML ) part = part.toUpperCase();
270 for ( var i = 0, l = checkSet.length, elem; i < l; i++ ) {
271 if ( elem = checkSet[i] ) {
272 while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {};
273 checkSet[i] = isPartStrNotTag || elem && elem.nodeName === part ?
274 elem : elem === part;
277 if (isPartStrNotTag) {
278 Sizzle.filter( part, checkSet, true );
281 ">": function(checkSet, part, isXML){
282 if ( typeof part === "string" && !/\W/.test(part) ) {
283 part = isXML ? part : part.toUpperCase();
285 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
286 var elem = checkSet[i];
288 var parent = elem.parentNode;
289 checkSet[i] = parent.nodeName === part ? parent : false;
293 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
294 var elem = checkSet[i];
296 checkSet[i] = typeof part === "string" ?
298 elem.parentNode === part;
302 if ( typeof part === "string" ) {
303 Sizzle.filter( part, checkSet, true );
307 "": function(checkSet, part, isXML){
308 var doneName = done++, checkFn = dirCheck;
310 if ( !part.match(/\W/) ) {
311 var nodeCheck = part = isXML ? part : part.toUpperCase();
312 checkFn = dirNodeCheck;
315 checkFn("parentNode", part, doneName, checkSet, nodeCheck, isXML);
317 "~": function(checkSet, part, isXML){
318 var doneName = done++, checkFn = dirCheck;
320 if ( typeof part === "string" && !part.match(/\W/) ) {
321 var nodeCheck = part = isXML ? part : part.toUpperCase();
322 checkFn = dirNodeCheck;
325 checkFn("previousSibling", part, doneName, checkSet, nodeCheck, isXML);
329 ID: function(match, context, isXML){
330 if ( typeof context.getElementById !== "undefined" && !isXML ) {
331 var m = context.getElementById(match[1]);
335 NAME: function(match, context, isXML){
336 if ( typeof context.getElementsByName !== "undefined" && !isXML ) {
337 return context.getElementsByName(match[1]);
340 TAG: function(match, context){
341 return context.getElementsByTagName(match[1]);
345 CLASS: function(match, curLoop, inplace, result, not){
346 match = " " + match[1].replace(/\\/g, "") + " ";
348 for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) {
350 if ( not ^ (elem.className && (" " + elem.className + " ").indexOf(match) >= 0) ) {
353 } else if ( inplace ) {
362 return match[1].replace(/\\/g, "");
364 TAG: function(match, curLoop){
365 for ( var i = 0; curLoop[i] === false; i++ ){}
366 return curLoop[i] && isXML(curLoop[i]) ? match[1] : match[1].toUpperCase();
368 CHILD: function(match){
369 if ( match[1] == "nth" ) {
370 // parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6'
371 var test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec(
372 match[2] == "even" && "2n" || match[2] == "odd" && "2n+1" ||
373 !/\D/.test( match[2] ) && "0n+" + match[2] || match[2]);
375 // calculate the numbers (first)n+(last) including if they are negative
376 match[2] = (test[1] + (test[2] || 1)) - 0;
377 match[3] = test[3] - 0;
380 // TODO: Move to normal caching system
385 ATTR: function(match){
386 var name = match[1].replace(/\\/g, "");
388 if ( Expr.attrMap[name] ) {
389 match[1] = Expr.attrMap[name];
392 if ( match[2] === "~=" ) {
393 match[4] = " " + match[4] + " ";
398 PSEUDO: function(match, curLoop, inplace, result, not){
399 if ( match[1] === "not" ) {
400 // If we're dealing with a complex expression, or a simple one
401 if ( match[3].match(chunker).length > 1 ) {
402 match[3] = Sizzle(match[3], null, null, curLoop);
404 var ret = Sizzle.filter(match[3], curLoop, inplace, true ^ not);
406 result.push.apply( result, ret );
410 } else if ( Expr.match.POS.test( match[0] ) ) {
416 POS: function(match){
417 match.unshift( true );
422 enabled: function(elem){
423 return elem.disabled === false && elem.type !== "hidden";
425 disabled: function(elem){
426 return elem.disabled === true;
428 checked: function(elem){
429 return elem.checked === true;
431 selected: function(elem){
432 // Accessing this property makes selected-by-default
433 // options in Safari work properly
434 elem.parentNode.selectedIndex;
435 return elem.selected === true;
437 parent: function(elem){
438 return !!elem.firstChild;
440 empty: function(elem){
441 return !elem.firstChild;
443 has: function(elem, i, match){
444 return !!Sizzle( match[3], elem ).length;
446 header: function(elem){
447 return /h\d/i.test( elem.nodeName );
449 text: function(elem){
450 return "text" === elem.type;
452 radio: function(elem){
453 return "radio" === elem.type;
455 checkbox: function(elem){
456 return "checkbox" === elem.type;
458 file: function(elem){
459 return "file" === elem.type;
461 password: function(elem){
462 return "password" === elem.type;
464 submit: function(elem){
465 return "submit" === elem.type;
467 image: function(elem){
468 return "image" === elem.type;
470 reset: function(elem){
471 return "reset" === elem.type;
473 button: function(elem){
474 return "button" === elem.type || elem.nodeName.toUpperCase() === "BUTTON";
476 input: function(elem){
477 return /input|select|textarea|button/i.test(elem.nodeName);
481 first: function(elem, i){
484 last: function(elem, i, match, array){
485 return i === array.length - 1;
487 even: function(elem, i){
490 odd: function(elem, i){
493 lt: function(elem, i, match){
494 return i < match[3] - 0;
496 gt: function(elem, i, match){
497 return i > match[3] - 0;
499 nth: function(elem, i, match){
500 return match[3] - 0 == i;
502 eq: function(elem, i, match){
503 return match[3] - 0 == i;
507 CHILD: function(elem, match){
508 var type = match[1], node = elem;
512 while (node = node.previousSibling) {
513 if ( node.nodeType === 1 ) return false;
515 if ( type == 'first') return true;
518 while (node = node.nextSibling) {
519 if ( node.nodeType === 1 ) return false;
523 var first = match[2], last = match[3];
525 if ( first == 1 && last == 0 ) {
529 var doneName = match[0],
530 parent = elem.parentNode;
532 if ( parent && (parent.sizcache !== doneName || !elem.nodeIndex) ) {
534 for ( node = parent.firstChild; node; node = node.nextSibling ) {
535 if ( node.nodeType === 1 ) {
536 node.nodeIndex = ++count;
539 parent.sizcache = doneName;
542 var diff = elem.nodeIndex - last;
546 return ( diff % first == 0 && diff / first >= 0 );
550 PSEUDO: function(elem, match, i, array){
551 var name = match[1], filter = Expr.filters[ name ];
554 return filter( elem, i, match, array );
555 } else if ( name === "contains" ) {
556 return (elem.textContent || elem.innerText || "").indexOf(match[3]) >= 0;
557 } else if ( name === "not" ) {
560 for ( var i = 0, l = not.length; i < l; i++ ) {
561 if ( not[i] === elem ) {
569 ID: function(elem, match){
570 return elem.nodeType === 1 && elem.getAttribute("id") === match;
572 TAG: function(elem, match){
573 return (match === "*" && elem.nodeType === 1) || elem.nodeName === match;
575 CLASS: function(elem, match){
576 return match.test( elem.className );
578 ATTR: function(elem, match){
580 result = Expr.attrHandle[ name ] ?
581 Expr.attrHandle[ name ]( elem ) :
582 elem[ name ] != null ?
584 elem.getAttribute( name ),
589 return result == null ?
594 value.indexOf(check) >= 0 :
596 (" " + value + " ").indexOf(check) >= 0 :
598 value && result !== false :
602 value.indexOf(check) === 0 :
604 value.substr(value.length - check.length) === check :
606 value === check || value.substr(0, check.length + 1) === check + "-" :
609 POS: function(elem, match, i, array){
610 var name = match[2], filter = Expr.setFilters[ name ];
613 return filter( elem, i, match, array );
619 var origPOS = Expr.match.POS;
621 for ( var type in Expr.match ) {
622 Expr.match[ type ] = RegExp( Expr.match[ type ].source + /(?![^\[]*\])(?![^\(]*\))/.source );
625 var makeArray = function(array, results) {
626 array = Array.prototype.slice.call( array );
629 results.push.apply( results, array );
636 // Perform a simple check to determine if the browser is capable of
637 // converting a NodeList to an array using builtin methods.
639 Array.prototype.slice.call( document.documentElement.childNodes );
641 // Provide a fallback method if it does not work
643 makeArray = function(array, results) {
644 var ret = results || [];
646 if ( toString.call(array) === "[object Array]" ) {
647 Array.prototype.push.apply( ret, array );
649 if ( typeof array.length === "number" ) {
650 for ( var i = 0, l = array.length; i < l; i++ ) {
651 ret.push( array[i] );
654 for ( var i = 0; array[i]; i++ ) {
655 ret.push( array[i] );
666 if ( Array.prototype.indexOf ) {
667 var indexOf = Array.prototype.indexOf,
668 allSort = document.getElementsByTagName("*");
670 sortOrder = function( a, b ) {
671 var ret = indexOf.call( allSort, a ) - indexOf.call( allSort, b );
677 } else if ( document.documentElement.sourceIndex === 1 ) {
678 sortOrder = function( a, b ) {
679 var ret = a.sourceIndex - b.sourceIndex;
687 // Check to see if the browser returns elements by name when
688 // querying by getElementById (and provide a workaround)
690 // We're going to inject a fake input element with a specified name
691 var form = document.createElement("form"),
692 id = "script" + (new Date).getTime();
693 form.innerHTML = "<input name='" + id + "'/>";
695 // Inject it into the root element, check its status, and remove it quickly
696 var root = document.documentElement;
697 root.insertBefore( form, root.firstChild );
699 // The workaround has to do additional checks after a getElementById
700 // Which slows things down for other browsers (hence the branching)
701 if ( !!document.getElementById( id ) ) {
702 Expr.find.ID = function(match, context, isXML){
703 if ( typeof context.getElementById !== "undefined" && !isXML ) {
704 var m = context.getElementById(match[1]);
705 return m ? m.id === match[1] || typeof m.getAttributeNode !== "undefined" && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
709 Expr.filter.ID = function(elem, match){
710 var node = typeof elem.getAttributeNode !== "undefined" && elem.getAttributeNode("id");
711 return elem.nodeType === 1 && node && node.nodeValue === match;
715 root.removeChild( form );
719 // Check to see if the browser returns only elements
720 // when doing getElementsByTagName("*")
722 // Create a fake element
723 var div = document.createElement("div");
724 div.appendChild( document.createComment("") );
726 // Make sure no comments are found
727 if ( div.getElementsByTagName("*").length > 0 ) {
728 Expr.find.TAG = function(match, context){
729 var results = context.getElementsByTagName(match[1]);
731 // Filter out possible comments
732 if ( match[1] === "*" ) {
735 for ( var i = 0; results[i]; i++ ) {
736 if ( results[i].nodeType === 1 ) {
737 tmp.push( results[i] );
748 // Check to see if an attribute returns normalized href attributes
749 div.innerHTML = "<a href='#'></a>";
750 if ( div.firstChild && typeof div.firstChild.getAttribute !== "undefined" &&
751 div.firstChild.getAttribute("href") !== "#" ) {
752 Expr.attrHandle.href = function(elem){
753 return elem.getAttribute("href", 2);
758 if ( document.querySelectorAll ) (function(){
759 var oldSizzle = Sizzle, div = document.createElement("div");
760 div.innerHTML = "<p class='TEST'></p>";
762 // Safari can't handle uppercase or unicode characters when
764 if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) {
768 Sizzle = function(query, context, extra, seed){
769 context = context || document;
771 // Only use querySelectorAll on non-XML documents
772 // (ID selectors don't work in non-HTML documents)
773 if ( !seed && context.nodeType === 9 && !isXML(context) ) {
775 return makeArray( context.querySelectorAll(query), extra );
779 return oldSizzle(query, context, extra, seed);
782 Sizzle.find = oldSizzle.find;
783 Sizzle.filter = oldSizzle.filter;
784 Sizzle.selectors = oldSizzle.selectors;
785 Sizzle.matches = oldSizzle.matches;
788 if ( document.getElementsByClassName && document.documentElement.getElementsByClassName ) (function(){
789 var div = document.createElement("div");
790 div.innerHTML = "<div class='test e'></div><div class='test'></div>";
792 // Opera can't find a second classname (in 9.6)
793 if ( div.getElementsByClassName("e").length === 0 )
796 // Safari caches class attributes, doesn't catch changes (in 3.2)
797 div.lastChild.className = "e";
799 if ( div.getElementsByClassName("e").length === 1 )
802 Expr.order.splice(1, 0, "CLASS");
803 Expr.find.CLASS = function(match, context) {
804 return context.getElementsByClassName(match[1]);
808 function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
809 var sibDir = dir == "previousSibling" && !isXML;
810 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
811 var elem = checkSet[i];
813 if ( sibDir && elem.nodeType === 1 ){
814 elem.sizcache = doneName;
821 if ( elem.sizcache === doneName ) {
822 match = checkSet[elem.sizset];
826 if ( elem.nodeType === 1 && !isXML ){
827 elem.sizcache = doneName;
831 if ( elem.nodeName === cur ) {
844 function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
845 var sibDir = dir == "previousSibling" && !isXML;
846 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
847 var elem = checkSet[i];
849 if ( sibDir && elem.nodeType === 1 ) {
850 elem.sizcache = doneName;
857 if ( elem.sizcache === doneName ) {
858 match = checkSet[elem.sizset];
862 if ( elem.nodeType === 1 ) {
864 elem.sizcache = doneName;
867 if ( typeof cur !== "string" ) {
868 if ( elem === cur ) {
873 } else if ( Sizzle.filter( cur, [elem] ).length > 0 ) {
887 var contains = document.compareDocumentPosition ? function(a, b){
888 return a.compareDocumentPosition(b) & 16;
890 return a !== b && (a.contains ? a.contains(b) : true);
893 var isXML = function(elem){
894 return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
895 !!elem.ownerDocument && isXML( elem.ownerDocument );
898 var posProcess = function(selector, context){
899 var tmpSet = [], later = "", match,
900 root = context.nodeType ? [context] : context;
902 // Position selectors must be done after the filter
903 // And so must :not(positional) so we move all PSEUDOs to the end
904 while ( (match = Expr.match.PSEUDO.exec( selector )) ) {
906 selector = selector.replace( Expr.match.PSEUDO, "" );
909 selector = Expr.relative[selector] ? selector + "*" : selector;
911 for ( var i = 0, l = root.length; i < l; i++ ) {
912 Sizzle( selector, root[i], tmpSet );
915 return Sizzle.filter( later, tmpSet );
919 jQuery.find = Sizzle;
920 jQuery.filter = Sizzle.filter;
921 jQuery.expr = Sizzle.selectors;
922 jQuery.expr[":"] = jQuery.expr.filters;
924 Sizzle.selectors.filters.hidden = function(elem){
925 return "hidden" === elem.type ||
926 jQuery.css(elem, "display") === "none" ||
927 jQuery.css(elem, "visibility") === "hidden";
930 Sizzle.selectors.filters.visible = function(elem){
931 return "hidden" !== elem.type &&
932 jQuery.css(elem, "display") !== "none" &&
933 jQuery.css(elem, "visibility") !== "hidden";
936 Sizzle.selectors.filters.animated = function(elem){
937 return jQuery.grep(jQuery.timers, function(fn){
938 return elem === fn.elem;
942 jQuery.multiFilter = function( expr, elems, not ) {
944 expr = ":not(" + expr + ")";
947 return Sizzle.matches(expr, elems);
950 jQuery.dir = function( elem, dir ){
951 var matched = [], cur = elem[dir];
952 while ( cur && cur != document ) {
953 if ( cur.nodeType == 1 )
960 jQuery.nth = function(cur, result, dir, elem){
961 result = result || 1;
964 for ( ; cur; cur = cur[dir] )
965 if ( cur.nodeType == 1 && ++num == result )
971 jQuery.sibling = function(n, elem){
974 for ( ; n; n = n.nextSibling ) {
975 if ( n.nodeType == 1 && n != elem )
984 window.Sizzle = Sizzle;