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 // Improper expression
228 if ( anyFound == null ) {
229 throw "Syntax error, unrecognized expression: " + expr;
241 var Expr = Sizzle.selectors = {
242 order: [ "ID", "NAME", "TAG" ],
244 ID: /#((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
245 CLASS: /\.((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
246 NAME: /\[name=['"]*((?:[\w\u00c0-\uFFFF_-]|\\.)+)['"]*\]/,
247 ATTR: /\[\s*((?:[\w\u00c0-\uFFFF_-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\s*\]/,
248 TAG: /^((?:[\w\u00c0-\uFFFF\*_-]|\\.)+)/,
249 CHILD: /:(only|nth|last|first)-child(?:\((even|odd|[\dn+-]*)\))?/,
250 POS: /:(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^-]|$)/,
251 PSEUDO: /:((?:[\w\u00c0-\uFFFF_-]|\\.)+)(?:\((['"]*)((?:\([^\)]+\)|[^\2\(\)]*)+)\2\))?/
254 "class": "className",
258 href: function(elem){
259 return elem.getAttribute("href");
263 "+": function(checkSet, part, isXML){
264 var isPartStr = typeof part === "string",
265 isTag = isPartStr && !/\W/.test(part),
266 isPartStrNotTag = isPartStr && !isTag;
268 if ( isTag && !isXML ) {
269 part = part.toUpperCase();
272 for ( var i = 0, l = checkSet.length, elem; i < l; i++ ) {
273 if ( (elem = checkSet[i]) ) {
274 while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {}
276 checkSet[i] = isPartStrNotTag || elem && elem.nodeName === part ?
282 if ( isPartStrNotTag ) {
283 Sizzle.filter( part, checkSet, true );
286 ">": function(checkSet, part, isXML){
287 var isPartStr = typeof part === "string";
289 if ( isPartStr && !/\W/.test(part) ) {
290 part = isXML ? part : part.toUpperCase();
292 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
293 var elem = checkSet[i];
295 var parent = elem.parentNode;
296 checkSet[i] = parent.nodeName === part ? parent : false;
300 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
301 var elem = checkSet[i];
303 checkSet[i] = isPartStr ?
305 elem.parentNode === part;
310 Sizzle.filter( part, checkSet, true );
314 "": function(checkSet, part, isXML){
315 var doneName = done++, checkFn = dirCheck;
317 if ( !part.match(/\W/) ) {
318 var nodeCheck = part = isXML ? part : part.toUpperCase();
319 checkFn = dirNodeCheck;
322 checkFn("parentNode", part, doneName, checkSet, nodeCheck, isXML);
324 "~": function(checkSet, part, isXML){
325 var doneName = done++, checkFn = dirCheck;
327 if ( typeof part === "string" && !part.match(/\W/) ) {
328 var nodeCheck = part = isXML ? part : part.toUpperCase();
329 checkFn = dirNodeCheck;
332 checkFn("previousSibling", part, doneName, checkSet, nodeCheck, isXML);
336 ID: function(match, context, isXML){
337 if ( typeof context.getElementById !== "undefined" && !isXML ) {
338 var m = context.getElementById(match[1]);
342 NAME: function(match, context, isXML){
343 if ( typeof context.getElementsByName !== "undefined" ) {
344 var ret = [], results = context.getElementsByName(match[1]);
346 for ( var i = 0, l = results.length; i < l; i++ ) {
347 if ( results[i].getAttribute("name") === match[1] ) {
348 ret.push( results[i] );
352 return ret.length === 0 ? null : ret;
355 TAG: function(match, context){
356 return context.getElementsByTagName(match[1]);
360 CLASS: function(match, curLoop, inplace, result, not){
361 match = " " + match[1].replace(/\\/g, "") + " ";
363 for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) {
365 if ( not ^ (elem.className && (" " + elem.className + " ").indexOf(match) >= 0) ) {
368 } else if ( inplace ) {
377 return match[1].replace(/\\/g, "");
379 TAG: function(match, curLoop){
380 for ( var i = 0; curLoop[i] === false; i++ ){}
381 return curLoop[i] && isXML(curLoop[i]) ? match[1] : match[1].toUpperCase();
383 CHILD: function(match){
384 if ( match[1] == "nth" ) {
385 // parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6'
386 var test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec(
387 match[2] == "even" && "2n" || match[2] == "odd" && "2n+1" ||
388 !/\D/.test( match[2] ) && "0n+" + match[2] || match[2]);
390 // calculate the numbers (first)n+(last) including if they are negative
391 match[2] = (test[1] + (test[2] || 1)) - 0;
392 match[3] = test[3] - 0;
395 // TODO: Move to normal caching system
400 ATTR: function(match){
401 var name = match[1].replace(/\\/g, "");
403 if ( Expr.attrMap[name] ) {
404 match[1] = Expr.attrMap[name];
407 if ( match[2] === "~=" ) {
408 match[4] = " " + match[4] + " ";
413 PSEUDO: function(match, curLoop, inplace, result, not){
414 if ( match[1] === "not" ) {
415 // If we're dealing with a complex expression, or a simple one
416 if ( match[3].match(chunker).length > 1 || /^\w/.test(match[3]) ) {
417 match[3] = Sizzle(match[3], null, null, curLoop);
419 var ret = Sizzle.filter(match[3], curLoop, inplace, true ^ not);
421 result.push.apply( result, ret );
425 } else if ( Expr.match.POS.test( match[0] ) || Expr.match.CHILD.test( match[0] ) ) {
431 POS: function(match){
432 match.unshift( true );
437 enabled: function(elem){
438 return elem.disabled === false && elem.type !== "hidden";
440 disabled: function(elem){
441 return elem.disabled === true;
443 checked: function(elem){
444 return elem.checked === true;
446 selected: function(elem){
447 // Accessing this property makes selected-by-default
448 // options in Safari work properly
449 elem.parentNode.selectedIndex;
450 return elem.selected === true;
452 parent: function(elem){
453 return !!elem.firstChild;
455 empty: function(elem){
456 return !elem.firstChild;
458 has: function(elem, i, match){
459 return !!Sizzle( match[3], elem ).length;
461 header: function(elem){
462 return /h\d/i.test( elem.nodeName );
464 text: function(elem){
465 return "text" === elem.type;
467 radio: function(elem){
468 return "radio" === elem.type;
470 checkbox: function(elem){
471 return "checkbox" === elem.type;
473 file: function(elem){
474 return "file" === elem.type;
476 password: function(elem){
477 return "password" === elem.type;
479 submit: function(elem){
480 return "submit" === elem.type;
482 image: function(elem){
483 return "image" === elem.type;
485 reset: function(elem){
486 return "reset" === elem.type;
488 button: function(elem){
489 return "button" === elem.type || elem.nodeName.toUpperCase() === "BUTTON";
491 input: function(elem){
492 return /input|select|textarea|button/i.test(elem.nodeName);
496 first: function(elem, i){
499 last: function(elem, i, match, array){
500 return i === array.length - 1;
502 even: function(elem, i){
505 odd: function(elem, i){
508 lt: function(elem, i, match){
509 return i < match[3] - 0;
511 gt: function(elem, i, match){
512 return i > match[3] - 0;
514 nth: function(elem, i, match){
515 return match[3] - 0 == i;
517 eq: function(elem, i, match){
518 return match[3] - 0 == i;
522 PSEUDO: function(elem, match, i, array){
523 var name = match[1], filter = Expr.filters[ name ];
526 return filter( elem, i, match, array );
527 } else if ( name === "contains" ) {
528 return (elem.textContent || elem.innerText || "").indexOf(match[3]) >= 0;
529 } else if ( name === "not" ) {
532 for ( var i = 0, l = not.length; i < l; i++ ) {
533 if ( not[i] === elem ) {
541 CHILD: function(elem, match){
542 var type = match[1], node = elem;
546 while (node = node.previousSibling) {
547 if ( node.nodeType === 1 ) return false;
549 if ( type == 'first') return true;
552 while (node = node.nextSibling) {
553 if ( node.nodeType === 1 ) return false;
557 var first = match[2], last = match[3];
559 if ( first == 1 && last == 0 ) {
563 var doneName = match[0],
564 parent = elem.parentNode;
566 if ( parent && (parent.sizcache !== doneName || !elem.nodeIndex) ) {
568 for ( node = parent.firstChild; node; node = node.nextSibling ) {
569 if ( node.nodeType === 1 ) {
570 node.nodeIndex = ++count;
573 parent.sizcache = doneName;
576 var diff = elem.nodeIndex - last;
580 return ( diff % first == 0 && diff / first >= 0 );
584 ID: function(elem, match){
585 return elem.nodeType === 1 && elem.getAttribute("id") === match;
587 TAG: function(elem, match){
588 return (match === "*" && elem.nodeType === 1) || elem.nodeName === match;
590 CLASS: function(elem, match){
591 return match.test( elem.className );
593 ATTR: function(elem, match){
595 result = Expr.attrHandle[ name ] ?
596 Expr.attrHandle[ name ]( elem ) :
597 elem[ name ] != null ?
599 elem.getAttribute( name ),
604 return result == null ?
609 value.indexOf(check) >= 0 :
611 (" " + value + " ").indexOf(check) >= 0 :
613 value && result !== false :
617 value.indexOf(check) === 0 :
619 value.substr(value.length - check.length) === check :
621 value === check || value.substr(0, check.length + 1) === check + "-" :
624 POS: function(elem, match, i, array){
625 var name = match[2], filter = Expr.setFilters[ name ];
628 return filter( elem, i, match, array );
634 var origPOS = Expr.match.POS;
636 for ( var type in Expr.match ) {
637 Expr.match[ type ] = RegExp( Expr.match[ type ].source + /(?![^\[]*\])(?![^\(]*\))/.source );
640 var makeArray = function(array, results) {
641 array = Array.prototype.slice.call( array );
644 results.push.apply( results, array );
651 // Perform a simple check to determine if the browser is capable of
652 // converting a NodeList to an array using builtin methods.
654 Array.prototype.slice.call( document.documentElement.childNodes );
656 // Provide a fallback method if it does not work
658 makeArray = function(array, results) {
659 var ret = results || [];
661 if ( toString.call(array) === "[object Array]" ) {
662 Array.prototype.push.apply( ret, array );
664 if ( typeof array.length === "number" ) {
665 for ( var i = 0, l = array.length; i < l; i++ ) {
666 ret.push( array[i] );
669 for ( var i = 0; array[i]; i++ ) {
670 ret.push( array[i] );
681 if ( Array.prototype.indexOf ) {
682 var indexOf = Array.prototype.indexOf,
683 allSort = document.getElementsByTagName("*");
685 sortOrder = function( a, b ) {
686 var ret = indexOf.call( allSort, a ) - indexOf.call( allSort, b );
692 } else if ( document.documentElement.sourceIndex === 1 ) {
693 sortOrder = function( a, b ) {
694 var ret = a.sourceIndex - b.sourceIndex;
702 // Check to see if the browser returns elements by name when
703 // querying by getElementById (and provide a workaround)
705 // We're going to inject a fake input element with a specified name
706 var form = document.createElement("form"),
707 id = "script" + (new Date).getTime();
708 form.innerHTML = "<input name='" + id + "'/>";
710 // Inject it into the root element, check its status, and remove it quickly
711 var root = document.documentElement;
712 root.insertBefore( form, root.firstChild );
714 // The workaround has to do additional checks after a getElementById
715 // Which slows things down for other browsers (hence the branching)
716 if ( !!document.getElementById( id ) ) {
717 Expr.find.ID = function(match, context, isXML){
718 if ( typeof context.getElementById !== "undefined" && !isXML ) {
719 var m = context.getElementById(match[1]);
720 return m ? m.id === match[1] || typeof m.getAttributeNode !== "undefined" && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
724 Expr.filter.ID = function(elem, match){
725 var node = typeof elem.getAttributeNode !== "undefined" && elem.getAttributeNode("id");
726 return elem.nodeType === 1 && node && node.nodeValue === match;
730 root.removeChild( form );
734 // Check to see if the browser returns only elements
735 // when doing getElementsByTagName("*")
737 // Create a fake element
738 var div = document.createElement("div");
739 div.appendChild( document.createComment("") );
741 // Make sure no comments are found
742 if ( div.getElementsByTagName("*").length > 0 ) {
743 Expr.find.TAG = function(match, context){
744 var results = context.getElementsByTagName(match[1]);
746 // Filter out possible comments
747 if ( match[1] === "*" ) {
750 for ( var i = 0; results[i]; i++ ) {
751 if ( results[i].nodeType === 1 ) {
752 tmp.push( results[i] );
763 // Check to see if an attribute returns normalized href attributes
764 div.innerHTML = "<a href='#'></a>";
765 if ( div.firstChild && typeof div.firstChild.getAttribute !== "undefined" &&
766 div.firstChild.getAttribute("href") !== "#" ) {
767 Expr.attrHandle.href = function(elem){
768 return elem.getAttribute("href", 2);
773 if ( document.querySelectorAll ) (function(){
774 var oldSizzle = Sizzle, div = document.createElement("div");
775 div.innerHTML = "<p class='TEST'></p>";
777 // Safari can't handle uppercase or unicode characters when
779 if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) {
783 Sizzle = function(query, context, extra, seed){
784 context = context || document;
786 // Only use querySelectorAll on non-XML documents
787 // (ID selectors don't work in non-HTML documents)
788 if ( !seed && context.nodeType === 9 && !isXML(context) ) {
790 return makeArray( context.querySelectorAll(query), extra );
794 return oldSizzle(query, context, extra, seed);
797 Sizzle.find = oldSizzle.find;
798 Sizzle.filter = oldSizzle.filter;
799 Sizzle.selectors = oldSizzle.selectors;
800 Sizzle.matches = oldSizzle.matches;
803 if ( document.getElementsByClassName && document.documentElement.getElementsByClassName ) (function(){
804 var div = document.createElement("div");
805 div.innerHTML = "<div class='test e'></div><div class='test'></div>";
807 // Opera can't find a second classname (in 9.6)
808 if ( div.getElementsByClassName("e").length === 0 )
811 // Safari caches class attributes, doesn't catch changes (in 3.2)
812 div.lastChild.className = "e";
814 if ( div.getElementsByClassName("e").length === 1 )
817 Expr.order.splice(1, 0, "CLASS");
818 Expr.find.CLASS = function(match, context) {
819 return context.getElementsByClassName(match[1]);
823 function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
824 var sibDir = dir == "previousSibling" && !isXML;
825 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
826 var elem = checkSet[i];
828 if ( sibDir && elem.nodeType === 1 ){
829 elem.sizcache = doneName;
836 if ( elem.sizcache === doneName ) {
837 match = checkSet[elem.sizset];
841 if ( elem.nodeType === 1 && !isXML ){
842 elem.sizcache = doneName;
846 if ( elem.nodeName === cur ) {
859 function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
860 var sibDir = dir == "previousSibling" && !isXML;
861 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
862 var elem = checkSet[i];
864 if ( sibDir && elem.nodeType === 1 ) {
865 elem.sizcache = doneName;
872 if ( elem.sizcache === doneName ) {
873 match = checkSet[elem.sizset];
877 if ( elem.nodeType === 1 ) {
879 elem.sizcache = doneName;
882 if ( typeof cur !== "string" ) {
883 if ( elem === cur ) {
888 } else if ( Sizzle.filter( cur, [elem] ).length > 0 ) {
902 var contains = document.compareDocumentPosition ? function(a, b){
903 return a.compareDocumentPosition(b) & 16;
905 return a !== b && (a.contains ? a.contains(b) : true);
908 var isXML = function(elem){
909 return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
910 !!elem.ownerDocument && isXML( elem.ownerDocument );
913 var posProcess = function(selector, context){
914 var tmpSet = [], later = "", match,
915 root = context.nodeType ? [context] : context;
917 // Position selectors must be done after the filter
918 // And so must :not(positional) so we move all PSEUDOs to the end
919 while ( (match = Expr.match.PSEUDO.exec( selector )) ) {
921 selector = selector.replace( Expr.match.PSEUDO, "" );
924 selector = Expr.relative[selector] ? selector + "*" : selector;
926 for ( var i = 0, l = root.length; i < l; i++ ) {
927 Sizzle( selector, root[i], tmpSet );
930 return Sizzle.filter( later, tmpSet );
934 jQuery.find = Sizzle;
935 jQuery.filter = Sizzle.filter;
936 jQuery.expr = Sizzle.selectors;
937 jQuery.expr[":"] = jQuery.expr.filters;
939 Sizzle.selectors.filters.hidden = function(elem){
940 return elem.offsetWidth === 0 || elem.offsetHeight === 0;
943 Sizzle.selectors.filters.visible = function(elem){
944 return elem.offsetWidth > 0 || elem.offsetHeight > 0;
947 Sizzle.selectors.filters.animated = function(elem){
948 return jQuery.grep(jQuery.timers, function(fn){
949 return elem === fn.elem;
953 jQuery.multiFilter = function( expr, elems, not ) {
955 expr = ":not(" + expr + ")";
958 return Sizzle.matches(expr, elems);
961 jQuery.dir = function( elem, dir ){
962 var matched = [], cur = elem[dir];
963 while ( cur && cur != document ) {
964 if ( cur.nodeType == 1 )
971 jQuery.nth = function(cur, result, dir, elem){
972 result = result || 1;
975 for ( ; cur; cur = cur[dir] )
976 if ( cur.nodeType == 1 && ++num == result )
982 jQuery.sibling = function(n, elem){
985 for ( ; n; n = n.nextSibling ) {
986 if ( n.nodeType == 1 && n != elem )
995 window.Sizzle = Sizzle;