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,
169 isXMLFilter = set && set[0] && isXML(set[0]);
171 while ( expr && set.length ) {
172 for ( var type in Expr.filter ) {
173 if ( (match = Expr.match[ type ].exec( expr )) != null ) {
174 var filter = Expr.filter[ type ], found, item;
177 if ( curLoop == result ) {
181 if ( Expr.preFilter[ type ] ) {
182 match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not, isXMLFilter );
185 anyFound = found = true;
186 } else if ( match === true ) {
192 for ( var i = 0; (item = curLoop[i]) != null; i++ ) {
194 found = filter( item, match, i, curLoop );
195 var pass = not ^ !!found;
197 if ( inplace && found != null ) {
211 if ( found !== undefined ) {
216 expr = expr.replace( Expr.match[ type ], "" );
227 // Improper expression
229 if ( anyFound == null ) {
230 throw "Syntax error, unrecognized expression: " + expr;
242 var Expr = Sizzle.selectors = {
243 order: [ "ID", "NAME", "TAG" ],
245 ID: /#((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
246 CLASS: /\.((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
247 NAME: /\[name=['"]*((?:[\w\u00c0-\uFFFF_-]|\\.)+)['"]*\]/,
248 ATTR: /\[\s*((?:[\w\u00c0-\uFFFF_-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\s*\]/,
249 TAG: /^((?:[\w\u00c0-\uFFFF\*_-]|\\.)+)/,
250 CHILD: /:(only|nth|last|first)-child(?:\((even|odd|[\dn+-]*)\))?/,
251 POS: /:(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^-]|$)/,
252 PSEUDO: /:((?:[\w\u00c0-\uFFFF_-]|\\.)+)(?:\((['"]*)((?:\([^\)]+\)|[^\2\(\)]*)+)\2\))?/
255 "class": "className",
259 href: function(elem){
260 return elem.getAttribute("href");
264 "+": function(checkSet, part, isXML){
265 var isPartStr = typeof part === "string",
266 isTag = isPartStr && !/\W/.test(part),
267 isPartStrNotTag = isPartStr && !isTag;
269 if ( isTag && !isXML ) {
270 part = part.toUpperCase();
273 for ( var i = 0, l = checkSet.length, elem; i < l; i++ ) {
274 if ( (elem = checkSet[i]) ) {
275 while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {}
277 checkSet[i] = isPartStrNotTag || elem && elem.nodeName === part ?
283 if ( isPartStrNotTag ) {
284 Sizzle.filter( part, checkSet, true );
287 ">": function(checkSet, part, isXML){
288 var isPartStr = typeof part === "string";
290 if ( isPartStr && !/\W/.test(part) ) {
291 part = isXML ? part : part.toUpperCase();
293 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
294 var elem = checkSet[i];
296 var parent = elem.parentNode;
297 checkSet[i] = parent.nodeName === part ? parent : false;
301 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
302 var elem = checkSet[i];
304 checkSet[i] = isPartStr ?
306 elem.parentNode === part;
311 Sizzle.filter( part, checkSet, true );
315 "": function(checkSet, part, isXML){
316 var doneName = done++, checkFn = dirCheck;
318 if ( !part.match(/\W/) ) {
319 var nodeCheck = part = isXML ? part : part.toUpperCase();
320 checkFn = dirNodeCheck;
323 checkFn("parentNode", part, doneName, checkSet, nodeCheck, isXML);
325 "~": function(checkSet, part, isXML){
326 var doneName = done++, checkFn = dirCheck;
328 if ( typeof part === "string" && !part.match(/\W/) ) {
329 var nodeCheck = part = isXML ? part : part.toUpperCase();
330 checkFn = dirNodeCheck;
333 checkFn("previousSibling", part, doneName, checkSet, nodeCheck, isXML);
337 ID: function(match, context, isXML){
338 if ( typeof context.getElementById !== "undefined" && !isXML ) {
339 var m = context.getElementById(match[1]);
343 NAME: function(match, context, isXML){
344 if ( typeof context.getElementsByName !== "undefined" ) {
345 var ret = [], results = context.getElementsByName(match[1]);
347 for ( var i = 0, l = results.length; i < l; i++ ) {
348 if ( results[i].getAttribute("name") === match[1] ) {
349 ret.push( results[i] );
353 return ret.length === 0 ? null : ret;
356 TAG: function(match, context){
357 return context.getElementsByTagName(match[1]);
361 CLASS: function(match, curLoop, inplace, result, not, isXML){
362 match = " " + match[1].replace(/\\/g, "") + " ";
368 for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) {
370 if ( not ^ (elem.className && (" " + elem.className + " ").indexOf(match) >= 0) ) {
373 } else if ( inplace ) {
382 return match[1].replace(/\\/g, "");
384 TAG: function(match, curLoop){
385 for ( var i = 0; curLoop[i] === false; i++ ){}
386 return curLoop[i] && isXML(curLoop[i]) ? match[1] : match[1].toUpperCase();
388 CHILD: function(match){
389 if ( match[1] == "nth" ) {
390 // parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6'
391 var test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec(
392 match[2] == "even" && "2n" || match[2] == "odd" && "2n+1" ||
393 !/\D/.test( match[2] ) && "0n+" + match[2] || match[2]);
395 // calculate the numbers (first)n+(last) including if they are negative
396 match[2] = (test[1] + (test[2] || 1)) - 0;
397 match[3] = test[3] - 0;
400 // TODO: Move to normal caching system
405 ATTR: function(match, curLoop, inplace, result, not, isXML){
406 var name = match[1].replace(/\\/g, "");
408 if ( !isXML && Expr.attrMap[name] ) {
409 match[1] = Expr.attrMap[name];
412 if ( match[2] === "~=" ) {
413 match[4] = " " + match[4] + " ";
418 PSEUDO: function(match, curLoop, inplace, result, not){
419 if ( match[1] === "not" ) {
420 // If we're dealing with a complex expression, or a simple one
421 if ( match[3].match(chunker).length > 1 || /^\w/.test(match[3]) ) {
422 match[3] = Sizzle(match[3], null, null, curLoop);
424 var ret = Sizzle.filter(match[3], curLoop, inplace, true ^ not);
426 result.push.apply( result, ret );
430 } else if ( Expr.match.POS.test( match[0] ) || Expr.match.CHILD.test( match[0] ) ) {
436 POS: function(match){
437 match.unshift( true );
442 enabled: function(elem){
443 return elem.disabled === false && elem.type !== "hidden";
445 disabled: function(elem){
446 return elem.disabled === true;
448 checked: function(elem){
449 return elem.checked === true;
451 selected: function(elem){
452 // Accessing this property makes selected-by-default
453 // options in Safari work properly
454 elem.parentNode.selectedIndex;
455 return elem.selected === true;
457 parent: function(elem){
458 return !!elem.firstChild;
460 empty: function(elem){
461 return !elem.firstChild;
463 has: function(elem, i, match){
464 return !!Sizzle( match[3], elem ).length;
466 header: function(elem){
467 return /h\d/i.test( elem.nodeName );
469 text: function(elem){
470 return "text" === elem.type;
472 radio: function(elem){
473 return "radio" === elem.type;
475 checkbox: function(elem){
476 return "checkbox" === elem.type;
478 file: function(elem){
479 return "file" === elem.type;
481 password: function(elem){
482 return "password" === elem.type;
484 submit: function(elem){
485 return "submit" === elem.type;
487 image: function(elem){
488 return "image" === elem.type;
490 reset: function(elem){
491 return "reset" === elem.type;
493 button: function(elem){
494 return "button" === elem.type || elem.nodeName.toUpperCase() === "BUTTON";
496 input: function(elem){
497 return /input|select|textarea|button/i.test(elem.nodeName);
501 first: function(elem, i){
504 last: function(elem, i, match, array){
505 return i === array.length - 1;
507 even: function(elem, i){
510 odd: function(elem, i){
513 lt: function(elem, i, match){
514 return i < match[3] - 0;
516 gt: function(elem, i, match){
517 return i > match[3] - 0;
519 nth: function(elem, i, match){
520 return match[3] - 0 == i;
522 eq: function(elem, i, match){
523 return match[3] - 0 == i;
527 PSEUDO: function(elem, match, i, array){
528 var name = match[1], filter = Expr.filters[ name ];
531 return filter( elem, i, match, array );
532 } else if ( name === "contains" ) {
533 return (elem.textContent || elem.innerText || "").indexOf(match[3]) >= 0;
534 } else if ( name === "not" ) {
537 for ( var i = 0, l = not.length; i < l; i++ ) {
538 if ( not[i] === elem ) {
546 CHILD: function(elem, match){
547 var type = match[1], node = elem;
551 while (node = node.previousSibling) {
552 if ( node.nodeType === 1 ) return false;
554 if ( type == 'first') return true;
557 while (node = node.nextSibling) {
558 if ( node.nodeType === 1 ) return false;
562 var first = match[2], last = match[3];
564 if ( first == 1 && last == 0 ) {
568 var doneName = match[0],
569 parent = elem.parentNode;
571 if ( parent && (parent.sizcache !== doneName || !elem.nodeIndex) ) {
573 for ( node = parent.firstChild; node; node = node.nextSibling ) {
574 if ( node.nodeType === 1 ) {
575 node.nodeIndex = ++count;
578 parent.sizcache = doneName;
581 var diff = elem.nodeIndex - last;
585 return ( diff % first == 0 && diff / first >= 0 );
589 ID: function(elem, match){
590 return elem.nodeType === 1 && elem.getAttribute("id") === match;
592 TAG: function(elem, match){
593 return (match === "*" && elem.nodeType === 1) || elem.nodeName === match;
595 CLASS: function(elem, match){
596 return (" " + (elem.className || elem.getAttribute("class")) + " ")
597 .indexOf( match ) > -1;
599 ATTR: function(elem, match){
601 result = Expr.attrHandle[ name ] ?
602 Expr.attrHandle[ name ]( elem ) :
603 elem[ name ] != null ?
605 elem.getAttribute( name ),
610 return result == null ?
615 value.indexOf(check) >= 0 :
617 (" " + value + " ").indexOf(check) >= 0 :
619 value && result !== false :
623 value.indexOf(check) === 0 :
625 value.substr(value.length - check.length) === check :
627 value === check || value.substr(0, check.length + 1) === check + "-" :
630 POS: function(elem, match, i, array){
631 var name = match[2], filter = Expr.setFilters[ name ];
634 return filter( elem, i, match, array );
640 var origPOS = Expr.match.POS;
642 for ( var type in Expr.match ) {
643 Expr.match[ type ] = RegExp( Expr.match[ type ].source + /(?![^\[]*\])(?![^\(]*\))/.source );
646 var makeArray = function(array, results) {
647 array = Array.prototype.slice.call( array );
650 results.push.apply( results, array );
657 // Perform a simple check to determine if the browser is capable of
658 // converting a NodeList to an array using builtin methods.
660 Array.prototype.slice.call( document.documentElement.childNodes );
662 // Provide a fallback method if it does not work
664 makeArray = function(array, results) {
665 var ret = results || [];
667 if ( toString.call(array) === "[object Array]" ) {
668 Array.prototype.push.apply( ret, array );
670 if ( typeof array.length === "number" ) {
671 for ( var i = 0, l = array.length; i < l; i++ ) {
672 ret.push( array[i] );
675 for ( var i = 0; array[i]; i++ ) {
676 ret.push( array[i] );
687 if ( Array.prototype.indexOf ) {
688 var indexOf = Array.prototype.indexOf,
689 allSort = document.getElementsByTagName("*");
691 sortOrder = function( a, b ) {
692 var ret = indexOf.call( allSort, a ) - indexOf.call( allSort, b );
698 } else if ( document.documentElement.sourceIndex === 1 ) {
699 sortOrder = function( a, b ) {
700 var ret = a.sourceIndex - b.sourceIndex;
708 // Check to see if the browser returns elements by name when
709 // querying by getElementById (and provide a workaround)
711 // We're going to inject a fake input element with a specified name
712 var form = document.createElement("form"),
713 id = "script" + (new Date).getTime();
714 form.innerHTML = "<input name='" + id + "'/>";
716 // Inject it into the root element, check its status, and remove it quickly
717 var root = document.documentElement;
718 root.insertBefore( form, root.firstChild );
720 // The workaround has to do additional checks after a getElementById
721 // Which slows things down for other browsers (hence the branching)
722 if ( !!document.getElementById( id ) ) {
723 Expr.find.ID = function(match, context, isXML){
724 if ( typeof context.getElementById !== "undefined" && !isXML ) {
725 var m = context.getElementById(match[1]);
726 return m ? m.id === match[1] || typeof m.getAttributeNode !== "undefined" && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
730 Expr.filter.ID = function(elem, match){
731 var node = typeof elem.getAttributeNode !== "undefined" && elem.getAttributeNode("id");
732 return elem.nodeType === 1 && node && node.nodeValue === match;
736 root.removeChild( form );
740 // Check to see if the browser returns only elements
741 // when doing getElementsByTagName("*")
743 // Create a fake element
744 var div = document.createElement("div");
745 div.appendChild( document.createComment("") );
747 // Make sure no comments are found
748 if ( div.getElementsByTagName("*").length > 0 ) {
749 Expr.find.TAG = function(match, context){
750 var results = context.getElementsByTagName(match[1]);
752 // Filter out possible comments
753 if ( match[1] === "*" ) {
756 for ( var i = 0; results[i]; i++ ) {
757 if ( results[i].nodeType === 1 ) {
758 tmp.push( results[i] );
769 // Check to see if an attribute returns normalized href attributes
770 div.innerHTML = "<a href='#'></a>";
771 if ( div.firstChild && typeof div.firstChild.getAttribute !== "undefined" &&
772 div.firstChild.getAttribute("href") !== "#" ) {
773 Expr.attrHandle.href = function(elem){
774 return elem.getAttribute("href", 2);
779 if ( document.querySelectorAll ) (function(){
780 var oldSizzle = Sizzle, div = document.createElement("div");
781 div.innerHTML = "<p class='TEST'></p>";
783 // Safari can't handle uppercase or unicode characters when
785 if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) {
789 Sizzle = function(query, context, extra, seed){
790 context = context || document;
792 // Only use querySelectorAll on non-XML documents
793 // (ID selectors don't work in non-HTML documents)
794 if ( !seed && context.nodeType === 9 && !isXML(context) ) {
796 return makeArray( context.querySelectorAll(query), extra );
800 return oldSizzle(query, context, extra, seed);
803 Sizzle.find = oldSizzle.find;
804 Sizzle.filter = oldSizzle.filter;
805 Sizzle.selectors = oldSizzle.selectors;
806 Sizzle.matches = oldSizzle.matches;
809 if ( document.getElementsByClassName && document.documentElement.getElementsByClassName ) (function(){
810 var div = document.createElement("div");
811 div.innerHTML = "<div class='test e'></div><div class='test'></div>";
813 // Opera can't find a second classname (in 9.6)
814 if ( div.getElementsByClassName("e").length === 0 )
817 // Safari caches class attributes, doesn't catch changes (in 3.2)
818 div.lastChild.className = "e";
820 if ( div.getElementsByClassName("e").length === 1 )
823 Expr.order.splice(1, 0, "CLASS");
824 Expr.find.CLASS = function(match, context, isXML) {
825 if ( typeof context.getElementsByClassName !== "undefined" && !isXML ) {
826 return context.getElementsByClassName(match[1]);
831 function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
832 var sibDir = dir == "previousSibling" && !isXML;
833 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
834 var elem = checkSet[i];
836 if ( sibDir && elem.nodeType === 1 ){
837 elem.sizcache = doneName;
844 if ( elem.sizcache === doneName ) {
845 match = checkSet[elem.sizset];
849 if ( elem.nodeType === 1 && !isXML ){
850 elem.sizcache = doneName;
854 if ( elem.nodeName === cur ) {
867 function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
868 var sibDir = dir == "previousSibling" && !isXML;
869 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
870 var elem = checkSet[i];
872 if ( sibDir && elem.nodeType === 1 ) {
873 elem.sizcache = doneName;
880 if ( elem.sizcache === doneName ) {
881 match = checkSet[elem.sizset];
885 if ( elem.nodeType === 1 ) {
887 elem.sizcache = doneName;
890 if ( typeof cur !== "string" ) {
891 if ( elem === cur ) {
896 } else if ( Sizzle.filter( cur, [elem] ).length > 0 ) {
910 var contains = document.compareDocumentPosition ? function(a, b){
911 return a.compareDocumentPosition(b) & 16;
913 return a !== b && (a.contains ? a.contains(b) : true);
916 var isXML = function(elem){
917 return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
918 !!elem.ownerDocument && isXML( elem.ownerDocument );
921 var posProcess = function(selector, context){
922 var tmpSet = [], later = "", match,
923 root = context.nodeType ? [context] : context;
925 // Position selectors must be done after the filter
926 // And so must :not(positional) so we move all PSEUDOs to the end
927 while ( (match = Expr.match.PSEUDO.exec( selector )) ) {
929 selector = selector.replace( Expr.match.PSEUDO, "" );
932 selector = Expr.relative[selector] ? selector + "*" : selector;
934 for ( var i = 0, l = root.length; i < l; i++ ) {
935 Sizzle( selector, root[i], tmpSet );
938 return Sizzle.filter( later, tmpSet );
942 jQuery.find = Sizzle;
943 jQuery.filter = Sizzle.filter;
944 jQuery.expr = Sizzle.selectors;
945 jQuery.expr[":"] = jQuery.expr.filters;
947 Sizzle.selectors.filters.hidden = function(elem){
948 return elem.offsetWidth === 0 || elem.offsetHeight === 0;
951 Sizzle.selectors.filters.visible = function(elem){
952 return elem.offsetWidth > 0 || elem.offsetHeight > 0;
955 Sizzle.selectors.filters.animated = function(elem){
956 return jQuery.grep(jQuery.timers, function(fn){
957 return elem === fn.elem;
961 jQuery.multiFilter = function( expr, elems, not ) {
963 expr = ":not(" + expr + ")";
966 return Sizzle.matches(expr, elems);
969 jQuery.dir = function( elem, dir ){
970 var matched = [], cur = elem[dir];
971 while ( cur && cur != document ) {
972 if ( cur.nodeType == 1 )
979 jQuery.nth = function(cur, result, dir, elem){
980 result = result || 1;
983 for ( ; cur; cur = cur[dir] )
984 if ( cur.nodeType == 1 && ++num == result )
990 jQuery.sibling = function(n, elem){
993 for ( ; n; n = n.nextSibling ) {
994 if ( n.nodeType == 1 && n != elem )
1003 window.Sizzle = Sizzle;