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;
267 if ( isTag && !isXML ) part = part.toUpperCase();
268 for ( var i = 0, l = checkSet.length, elem; i < l; i++ ) {
269 if ( elem = checkSet[i] ) {
270 while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {};
271 checkSet[i] = isPartStrNotTag || elem && elem.nodeName === part ?
272 elem : elem === part;
275 if (isPartStrNotTag) {
276 Sizzle.filter( part, checkSet, true );
279 ">": function(checkSet, part, isXML){
280 if ( typeof part === "string" && !/\W/.test(part) ) {
281 part = isXML ? part : part.toUpperCase();
283 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
284 var elem = checkSet[i];
286 var parent = elem.parentNode;
287 checkSet[i] = parent.nodeName === part ? parent : false;
291 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
292 var elem = checkSet[i];
294 checkSet[i] = typeof part === "string" ?
296 elem.parentNode === part;
300 if ( typeof part === "string" ) {
301 Sizzle.filter( part, checkSet, true );
305 "": function(checkSet, part, isXML){
306 var doneName = done++, checkFn = dirCheck;
308 if ( !part.match(/\W/) ) {
309 var nodeCheck = part = isXML ? part : part.toUpperCase();
310 checkFn = dirNodeCheck;
313 checkFn("parentNode", part, doneName, checkSet, nodeCheck, isXML);
315 "~": function(checkSet, part, isXML){
316 var doneName = done++, checkFn = dirCheck;
318 if ( typeof part === "string" && !part.match(/\W/) ) {
319 var nodeCheck = part = isXML ? part : part.toUpperCase();
320 checkFn = dirNodeCheck;
323 checkFn("previousSibling", part, doneName, checkSet, nodeCheck, isXML);
327 ID: function(match, context, isXML){
328 if ( typeof context.getElementById !== "undefined" && !isXML ) {
329 var m = context.getElementById(match[1]);
333 NAME: function(match, context, isXML){
334 if ( typeof context.getElementsByName !== "undefined" ) {
335 var ret = [], results = context.getElementsByName(match[1]);
337 for ( var i = 0, l = results.length; i < l; i++ ) {
338 if ( results[i].getAttribute("name") === match[1] ) {
339 ret.push( results[i] );
343 return ret.length === 0 ? null : ret;
346 TAG: function(match, context){
347 return context.getElementsByTagName(match[1]);
351 CLASS: function(match, curLoop, inplace, result, not){
352 match = " " + match[1].replace(/\\/g, "") + " ";
354 for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) {
356 if ( not ^ (elem.className && (" " + elem.className + " ").indexOf(match) >= 0) ) {
359 } else if ( inplace ) {
368 return match[1].replace(/\\/g, "");
370 TAG: function(match, curLoop){
371 for ( var i = 0; curLoop[i] === false; i++ ){}
372 return curLoop[i] && isXML(curLoop[i]) ? match[1] : match[1].toUpperCase();
374 CHILD: function(match){
375 if ( match[1] == "nth" ) {
376 // parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6'
377 var test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec(
378 match[2] == "even" && "2n" || match[2] == "odd" && "2n+1" ||
379 !/\D/.test( match[2] ) && "0n+" + match[2] || match[2]);
381 // calculate the numbers (first)n+(last) including if they are negative
382 match[2] = (test[1] + (test[2] || 1)) - 0;
383 match[3] = test[3] - 0;
386 // TODO: Move to normal caching system
391 ATTR: function(match){
392 var name = match[1].replace(/\\/g, "");
394 if ( Expr.attrMap[name] ) {
395 match[1] = Expr.attrMap[name];
398 if ( match[2] === "~=" ) {
399 match[4] = " " + match[4] + " ";
404 PSEUDO: function(match, curLoop, inplace, result, not){
405 if ( match[1] === "not" ) {
406 // If we're dealing with a complex expression, or a simple one
407 if ( match[3].match(chunker).length > 1 || /^\w/.test(match[3]) ) {
408 match[3] = Sizzle(match[3], null, null, curLoop);
410 var ret = Sizzle.filter(match[3], curLoop, inplace, true ^ not);
412 result.push.apply( result, ret );
416 } else if ( Expr.match.POS.test( match[0] ) ) {
422 POS: function(match){
423 match.unshift( true );
428 enabled: function(elem){
429 return elem.disabled === false && elem.type !== "hidden";
431 disabled: function(elem){
432 return elem.disabled === true;
434 checked: function(elem){
435 return elem.checked === true;
437 selected: function(elem){
438 // Accessing this property makes selected-by-default
439 // options in Safari work properly
440 elem.parentNode.selectedIndex;
441 return elem.selected === true;
443 parent: function(elem){
444 return !!elem.firstChild;
446 empty: function(elem){
447 return !elem.firstChild;
449 has: function(elem, i, match){
450 return !!Sizzle( match[3], elem ).length;
452 header: function(elem){
453 return /h\d/i.test( elem.nodeName );
455 text: function(elem){
456 return "text" === elem.type;
458 radio: function(elem){
459 return "radio" === elem.type;
461 checkbox: function(elem){
462 return "checkbox" === elem.type;
464 file: function(elem){
465 return "file" === elem.type;
467 password: function(elem){
468 return "password" === elem.type;
470 submit: function(elem){
471 return "submit" === elem.type;
473 image: function(elem){
474 return "image" === elem.type;
476 reset: function(elem){
477 return "reset" === elem.type;
479 button: function(elem){
480 return "button" === elem.type || elem.nodeName.toUpperCase() === "BUTTON";
482 input: function(elem){
483 return /input|select|textarea|button/i.test(elem.nodeName);
487 first: function(elem, i){
490 last: function(elem, i, match, array){
491 return i === array.length - 1;
493 even: function(elem, i){
496 odd: function(elem, i){
499 lt: function(elem, i, match){
500 return i < match[3] - 0;
502 gt: function(elem, i, match){
503 return i > match[3] - 0;
505 nth: function(elem, i, match){
506 return match[3] - 0 == i;
508 eq: function(elem, i, match){
509 return match[3] - 0 == i;
513 CHILD: function(elem, match){
514 var type = match[1], node = elem;
518 while (node = node.previousSibling) {
519 if ( node.nodeType === 1 ) return false;
521 if ( type == 'first') return true;
524 while (node = node.nextSibling) {
525 if ( node.nodeType === 1 ) return false;
529 var first = match[2], last = match[3];
531 if ( first == 1 && last == 0 ) {
535 var doneName = match[0],
536 parent = elem.parentNode;
538 if ( parent && (parent.sizcache !== doneName || !elem.nodeIndex) ) {
540 for ( node = parent.firstChild; node; node = node.nextSibling ) {
541 if ( node.nodeType === 1 ) {
542 node.nodeIndex = ++count;
545 parent.sizcache = doneName;
548 var diff = elem.nodeIndex - last;
552 return ( diff % first == 0 && diff / first >= 0 );
556 PSEUDO: function(elem, match, i, array){
557 var name = match[1], filter = Expr.filters[ name ];
560 return filter( elem, i, match, array );
561 } else if ( name === "contains" ) {
562 return (elem.textContent || elem.innerText || "").indexOf(match[3]) >= 0;
563 } else if ( name === "not" ) {
566 for ( var i = 0, l = not.length; i < l; i++ ) {
567 if ( not[i] === elem ) {
575 ID: function(elem, match){
576 return elem.nodeType === 1 && elem.getAttribute("id") === match;
578 TAG: function(elem, match){
579 return (match === "*" && elem.nodeType === 1) || elem.nodeName === match;
581 CLASS: function(elem, match){
582 return match.test( elem.className );
584 ATTR: function(elem, match){
586 result = Expr.attrHandle[ name ] ?
587 Expr.attrHandle[ name ]( elem ) :
588 elem[ name ] != null ?
590 elem.getAttribute( name ),
595 return result == null ?
600 value.indexOf(check) >= 0 :
602 (" " + value + " ").indexOf(check) >= 0 :
604 value && result !== false :
608 value.indexOf(check) === 0 :
610 value.substr(value.length - check.length) === check :
612 value === check || value.substr(0, check.length + 1) === check + "-" :
615 POS: function(elem, match, i, array){
616 var name = match[2], filter = Expr.setFilters[ name ];
619 return filter( elem, i, match, array );
625 var origPOS = Expr.match.POS;
627 for ( var type in Expr.match ) {
628 Expr.match[ type ] = RegExp( Expr.match[ type ].source + /(?![^\[]*\])(?![^\(]*\))/.source );
631 var makeArray = function(array, results) {
632 array = Array.prototype.slice.call( array );
635 results.push.apply( results, array );
642 // Perform a simple check to determine if the browser is capable of
643 // converting a NodeList to an array using builtin methods.
645 Array.prototype.slice.call( document.documentElement.childNodes );
647 // Provide a fallback method if it does not work
649 makeArray = function(array, results) {
650 var ret = results || [];
652 if ( toString.call(array) === "[object Array]" ) {
653 Array.prototype.push.apply( ret, array );
655 if ( typeof array.length === "number" ) {
656 for ( var i = 0, l = array.length; i < l; i++ ) {
657 ret.push( array[i] );
660 for ( var i = 0; array[i]; i++ ) {
661 ret.push( array[i] );
672 if ( Array.prototype.indexOf ) {
673 var indexOf = Array.prototype.indexOf,
674 allSort = document.getElementsByTagName("*");
676 sortOrder = function( a, b ) {
677 var ret = indexOf.call( allSort, a ) - indexOf.call( allSort, b );
683 } else if ( document.documentElement.sourceIndex === 1 ) {
684 sortOrder = function( a, b ) {
685 var ret = a.sourceIndex - b.sourceIndex;
693 // Check to see if the browser returns elements by name when
694 // querying by getElementById (and provide a workaround)
696 // We're going to inject a fake input element with a specified name
697 var form = document.createElement("form"),
698 id = "script" + (new Date).getTime();
699 form.innerHTML = "<input name='" + id + "'/>";
701 // Inject it into the root element, check its status, and remove it quickly
702 var root = document.documentElement;
703 root.insertBefore( form, root.firstChild );
705 // The workaround has to do additional checks after a getElementById
706 // Which slows things down for other browsers (hence the branching)
707 if ( !!document.getElementById( id ) ) {
708 Expr.find.ID = function(match, context, isXML){
709 if ( typeof context.getElementById !== "undefined" && !isXML ) {
710 var m = context.getElementById(match[1]);
711 return m ? m.id === match[1] || typeof m.getAttributeNode !== "undefined" && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
715 Expr.filter.ID = function(elem, match){
716 var node = typeof elem.getAttributeNode !== "undefined" && elem.getAttributeNode("id");
717 return elem.nodeType === 1 && node && node.nodeValue === match;
721 root.removeChild( form );
725 // Check to see if the browser returns only elements
726 // when doing getElementsByTagName("*")
728 // Create a fake element
729 var div = document.createElement("div");
730 div.appendChild( document.createComment("") );
732 // Make sure no comments are found
733 if ( div.getElementsByTagName("*").length > 0 ) {
734 Expr.find.TAG = function(match, context){
735 var results = context.getElementsByTagName(match[1]);
737 // Filter out possible comments
738 if ( match[1] === "*" ) {
741 for ( var i = 0; results[i]; i++ ) {
742 if ( results[i].nodeType === 1 ) {
743 tmp.push( results[i] );
754 // Check to see if an attribute returns normalized href attributes
755 div.innerHTML = "<a href='#'></a>";
756 if ( div.firstChild && typeof div.firstChild.getAttribute !== "undefined" &&
757 div.firstChild.getAttribute("href") !== "#" ) {
758 Expr.attrHandle.href = function(elem){
759 return elem.getAttribute("href", 2);
764 if ( document.querySelectorAll ) (function(){
765 var oldSizzle = Sizzle, div = document.createElement("div");
766 div.innerHTML = "<p class='TEST'></p>";
768 // Safari can't handle uppercase or unicode characters when
770 if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) {
774 Sizzle = function(query, context, extra, seed){
775 context = context || document;
777 // Only use querySelectorAll on non-XML documents
778 // (ID selectors don't work in non-HTML documents)
779 if ( !seed && context.nodeType === 9 && !isXML(context) ) {
781 return makeArray( context.querySelectorAll(query), extra );
785 return oldSizzle(query, context, extra, seed);
788 Sizzle.find = oldSizzle.find;
789 Sizzle.filter = oldSizzle.filter;
790 Sizzle.selectors = oldSizzle.selectors;
791 Sizzle.matches = oldSizzle.matches;
794 if ( document.getElementsByClassName && document.documentElement.getElementsByClassName ) (function(){
795 var div = document.createElement("div");
796 div.innerHTML = "<div class='test e'></div><div class='test'></div>";
798 // Opera can't find a second classname (in 9.6)
799 if ( div.getElementsByClassName("e").length === 0 )
802 // Safari caches class attributes, doesn't catch changes (in 3.2)
803 div.lastChild.className = "e";
805 if ( div.getElementsByClassName("e").length === 1 )
808 Expr.order.splice(1, 0, "CLASS");
809 Expr.find.CLASS = function(match, context) {
810 return context.getElementsByClassName(match[1]);
814 function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
815 var sibDir = dir == "previousSibling" && !isXML;
816 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
817 var elem = checkSet[i];
819 if ( sibDir && elem.nodeType === 1 ){
820 elem.sizcache = doneName;
827 if ( elem.sizcache === doneName ) {
828 match = checkSet[elem.sizset];
832 if ( elem.nodeType === 1 && !isXML ){
833 elem.sizcache = doneName;
837 if ( elem.nodeName === cur ) {
850 function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
851 var sibDir = dir == "previousSibling" && !isXML;
852 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
853 var elem = checkSet[i];
855 if ( sibDir && elem.nodeType === 1 ) {
856 elem.sizcache = doneName;
863 if ( elem.sizcache === doneName ) {
864 match = checkSet[elem.sizset];
868 if ( elem.nodeType === 1 ) {
870 elem.sizcache = doneName;
873 if ( typeof cur !== "string" ) {
874 if ( elem === cur ) {
879 } else if ( Sizzle.filter( cur, [elem] ).length > 0 ) {
893 var contains = document.compareDocumentPosition ? function(a, b){
894 return a.compareDocumentPosition(b) & 16;
896 return a !== b && (a.contains ? a.contains(b) : true);
899 var isXML = function(elem){
900 return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
901 !!elem.ownerDocument && isXML( elem.ownerDocument );
904 var posProcess = function(selector, context){
905 var tmpSet = [], later = "", match,
906 root = context.nodeType ? [context] : context;
908 // Position selectors must be done after the filter
909 // And so must :not(positional) so we move all PSEUDOs to the end
910 while ( (match = Expr.match.PSEUDO.exec( selector )) ) {
912 selector = selector.replace( Expr.match.PSEUDO, "" );
915 selector = Expr.relative[selector] ? selector + "*" : selector;
917 for ( var i = 0, l = root.length; i < l; i++ ) {
918 Sizzle( selector, root[i], tmpSet );
921 return Sizzle.filter( later, tmpSet );
925 jQuery.find = Sizzle;
926 jQuery.filter = Sizzle.filter;
927 jQuery.expr = Sizzle.selectors;
928 jQuery.expr[":"] = jQuery.expr.filters;
930 Sizzle.selectors.filters.hidden = function(elem){
931 return "hidden" === elem.type ||
932 jQuery.css(elem, "display") === "none" ||
933 jQuery.css(elem, "visibility") === "hidden";
936 Sizzle.selectors.filters.visible = function(elem){
937 return "hidden" !== elem.type &&
938 jQuery.css(elem, "display") !== "none" &&
939 jQuery.css(elem, "visibility") !== "hidden";
942 Sizzle.selectors.filters.animated = function(elem){
943 return jQuery.grep(jQuery.timers, function(fn){
944 return elem === fn.elem;
948 jQuery.multiFilter = function( expr, elems, not ) {
950 expr = ":not(" + expr + ")";
953 return Sizzle.matches(expr, elems);
956 jQuery.dir = function( elem, dir ){
957 var matched = [], cur = elem[dir];
958 while ( cur && cur != document ) {
959 if ( cur.nodeType == 1 )
966 jQuery.nth = function(cur, result, dir, elem){
967 result = result || 1;
970 for ( ; cur; cur = cur[dir] )
971 if ( cur.nodeType == 1 && ++num == result )
977 jQuery.sibling = function(n, elem){
980 for ( ; n; n = n.nextSibling ) {
981 if ( n.nodeType == 1 && n != elem )
990 window.Sizzle = Sizzle;