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,
14 var Sizzle = function(selector, context, results, seed) {
15 results = results || [];
16 var origContext = context = context || document;
18 if ( context.nodeType !== 1 && context.nodeType !== 9 ) {
22 if ( !selector || typeof selector !== "string" ) {
26 var parts = [], m, set, checkSet, check, mode, extra, prune = true, contextXML = isXML(context);
28 // Reset the position of the chunker regexp (start from head)
29 chunker.lastIndex = 0;
31 while ( (m = chunker.exec(selector)) !== null ) {
35 extra = RegExp.rightContext;
40 if ( parts.length > 1 && origPOS.exec( selector ) ) {
41 if ( parts.length === 2 && Expr.relative[ parts[0] ] ) {
42 set = posProcess( parts[0] + parts[1], context );
44 set = Expr.relative[ parts[0] ] ?
46 Sizzle( parts.shift(), context );
48 while ( parts.length ) {
49 selector = parts.shift();
51 if ( Expr.relative[ selector ] )
52 selector += parts.shift();
54 set = posProcess( selector, set );
58 // Take a shortcut and set the context if the root selector is an ID
59 // (but not if it'll be faster if the inner selector is an ID)
60 if ( !seed && parts.length > 1 && context.nodeType === 9 && !contextXML &&
61 Expr.match.ID.test(parts[0]) && !Expr.match.ID.test(parts[parts.length - 1]) ) {
62 var ret = Sizzle.find( parts.shift(), context, contextXML );
63 context = ret.expr ? Sizzle.filter( ret.expr, ret.set )[0] : ret.set[0];
68 { expr: parts.pop(), set: makeArray(seed) } :
69 Sizzle.find( parts.pop(), parts.length === 1 && (parts[0] === "~" || parts[0] === "+") && context.parentNode ? context.parentNode : context, contextXML );
70 set = ret.expr ? Sizzle.filter( ret.expr, ret.set ) : ret.set;
72 if ( parts.length > 0 ) {
73 checkSet = makeArray(set);
78 while ( parts.length ) {
79 var cur = parts.pop(), pop = cur;
81 if ( !Expr.relative[ cur ] ) {
91 Expr.relative[ cur ]( checkSet, pop, contextXML );
94 checkSet = parts = [];
103 throw "Syntax error, unrecognized expression: " + (cur || selector);
106 if ( toString.call(checkSet) === "[object Array]" ) {
108 results.push.apply( results, checkSet );
109 } else if ( context && context.nodeType === 1 ) {
110 for ( var i = 0; checkSet[i] != null; i++ ) {
111 if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && contains(context, checkSet[i])) ) {
112 results.push( set[i] );
116 for ( var i = 0; checkSet[i] != null; i++ ) {
117 if ( checkSet[i] && checkSet[i].nodeType === 1 ) {
118 results.push( set[i] );
123 makeArray( checkSet, results );
127 Sizzle( extra, origContext, results, seed );
128 Sizzle.uniqueSort( results );
134 Sizzle.uniqueSort = function(results){
136 hasDuplicate = false;
137 results.sort(sortOrder);
139 if ( hasDuplicate ) {
140 for ( var i = 1; i < results.length; i++ ) {
141 if ( results[i] === results[i-1] ) {
142 results.splice(i--, 1);
149 Sizzle.matches = function(expr, set){
150 return Sizzle(expr, null, null, set);
153 Sizzle.find = function(expr, context, isXML){
160 for ( var i = 0, l = Expr.order.length; i < l; i++ ) {
161 var type = Expr.order[i], match;
163 if ( (match = Expr.match[ type ].exec( expr )) ) {
164 var left = RegExp.leftContext;
166 if ( left.substr( left.length - 1 ) !== "\\" ) {
167 match[1] = (match[1] || "").replace(/\\/g, "");
168 set = Expr.find[ type ]( match, context, isXML );
170 expr = expr.replace( Expr.match[ type ], "" );
178 set = context.getElementsByTagName("*");
181 return {set: set, expr: expr};
184 Sizzle.filter = function(expr, set, inplace, not){
185 var old = expr, result = [], curLoop = set, match, anyFound,
186 isXMLFilter = set && set[0] && isXML(set[0]);
188 while ( expr && set.length ) {
189 for ( var type in Expr.filter ) {
190 if ( (match = Expr.match[ type ].exec( expr )) != null ) {
191 var filter = Expr.filter[ type ], found, item;
194 if ( curLoop == result ) {
198 if ( Expr.preFilter[ type ] ) {
199 match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not, isXMLFilter );
202 anyFound = found = true;
203 } else if ( match === true ) {
209 for ( var i = 0; (item = curLoop[i]) != null; i++ ) {
211 found = filter( item, match, i, curLoop );
212 var pass = not ^ !!found;
214 if ( inplace && found != null ) {
228 if ( found !== undefined ) {
233 expr = expr.replace( Expr.match[ type ], "" );
244 // Improper expression
246 if ( anyFound == null ) {
247 throw "Syntax error, unrecognized expression: " + expr;
259 var Expr = Sizzle.selectors = {
260 order: [ "ID", "NAME", "TAG" ],
262 ID: /#((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
263 CLASS: /\.((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
264 NAME: /\[name=['"]*((?:[\w\u00c0-\uFFFF_-]|\\.)+)['"]*\]/,
265 ATTR: /\[\s*((?:[\w\u00c0-\uFFFF_-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\s*\]/,
266 TAG: /^((?:[\w\u00c0-\uFFFF\*_-]|\\.)+)/,
267 CHILD: /:(only|nth|last|first)-child(?:\((even|odd|[\dn+-]*)\))?/,
268 POS: /:(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^-]|$)/,
269 PSEUDO: /:((?:[\w\u00c0-\uFFFF_-]|\\.)+)(?:\((['"]*)((?:\([^\)]+\)|[^\2\(\)]*)+)\2\))?/
272 "class": "className",
276 href: function(elem){
277 return elem.getAttribute("href");
281 "+": function(checkSet, part, isXML){
282 var isPartStr = typeof part === "string",
283 isTag = isPartStr && !/\W/.test(part),
284 isPartStrNotTag = isPartStr && !isTag;
286 if ( isTag && !isXML ) {
287 part = part.toUpperCase();
290 for ( var i = 0, l = checkSet.length, elem; i < l; i++ ) {
291 if ( (elem = checkSet[i]) ) {
292 while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {}
294 checkSet[i] = isPartStrNotTag || elem && elem.nodeName === part ?
300 if ( isPartStrNotTag ) {
301 Sizzle.filter( part, checkSet, true );
304 ">": function(checkSet, part, isXML){
305 var isPartStr = typeof part === "string";
307 if ( isPartStr && !/\W/.test(part) ) {
308 part = isXML ? part : part.toUpperCase();
310 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
311 var elem = checkSet[i];
313 var parent = elem.parentNode;
314 checkSet[i] = parent.nodeName === part ? parent : false;
318 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
319 var elem = checkSet[i];
321 checkSet[i] = isPartStr ?
323 elem.parentNode === part;
328 Sizzle.filter( part, checkSet, true );
332 "": function(checkSet, part, isXML){
333 var doneName = done++, checkFn = dirCheck;
335 if ( !part.match(/\W/) ) {
336 var nodeCheck = part = isXML ? part : part.toUpperCase();
337 checkFn = dirNodeCheck;
340 checkFn("parentNode", part, doneName, checkSet, nodeCheck, isXML);
342 "~": function(checkSet, part, isXML){
343 var doneName = done++, checkFn = dirCheck;
345 if ( typeof part === "string" && !part.match(/\W/) ) {
346 var nodeCheck = part = isXML ? part : part.toUpperCase();
347 checkFn = dirNodeCheck;
350 checkFn("previousSibling", part, doneName, checkSet, nodeCheck, isXML);
354 ID: function(match, context, isXML){
355 if ( typeof context.getElementById !== "undefined" && !isXML ) {
356 var m = context.getElementById(match[1]);
360 NAME: function(match, context, isXML){
361 if ( typeof context.getElementsByName !== "undefined" ) {
362 var ret = [], results = context.getElementsByName(match[1]);
364 for ( var i = 0, l = results.length; i < l; i++ ) {
365 if ( results[i].getAttribute("name") === match[1] ) {
366 ret.push( results[i] );
370 return ret.length === 0 ? null : ret;
373 TAG: function(match, context){
374 return context.getElementsByTagName(match[1]);
378 CLASS: function(match, curLoop, inplace, result, not, isXML){
379 match = " " + match[1].replace(/\\/g, "") + " ";
385 for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) {
387 if ( not ^ (elem.className && (" " + elem.className + " ").indexOf(match) >= 0) ) {
390 } else if ( inplace ) {
399 return match[1].replace(/\\/g, "");
401 TAG: function(match, curLoop){
402 for ( var i = 0; curLoop[i] === false; i++ ){}
403 return curLoop[i] && isXML(curLoop[i]) ? match[1] : match[1].toUpperCase();
405 CHILD: function(match){
406 if ( match[1] == "nth" ) {
407 // parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6'
408 var test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec(
409 match[2] == "even" && "2n" || match[2] == "odd" && "2n+1" ||
410 !/\D/.test( match[2] ) && "0n+" + match[2] || match[2]);
412 // calculate the numbers (first)n+(last) including if they are negative
413 match[2] = (test[1] + (test[2] || 1)) - 0;
414 match[3] = test[3] - 0;
417 // TODO: Move to normal caching system
422 ATTR: function(match, curLoop, inplace, result, not, isXML){
423 var name = match[1].replace(/\\/g, "");
425 if ( !isXML && Expr.attrMap[name] ) {
426 match[1] = Expr.attrMap[name];
429 if ( match[2] === "~=" ) {
430 match[4] = " " + match[4] + " ";
435 PSEUDO: function(match, curLoop, inplace, result, not){
436 if ( match[1] === "not" ) {
437 // If we're dealing with a complex expression, or a simple one
438 if ( match[3].match(chunker).length > 1 || /^\w/.test(match[3]) ) {
439 match[3] = Sizzle(match[3], null, null, curLoop);
441 var ret = Sizzle.filter(match[3], curLoop, inplace, true ^ not);
443 result.push.apply( result, ret );
447 } else if ( Expr.match.POS.test( match[0] ) || Expr.match.CHILD.test( match[0] ) ) {
453 POS: function(match){
454 match.unshift( true );
459 enabled: function(elem){
460 return elem.disabled === false && elem.type !== "hidden";
462 disabled: function(elem){
463 return elem.disabled === true;
465 checked: function(elem){
466 return elem.checked === true;
468 selected: function(elem){
469 // Accessing this property makes selected-by-default
470 // options in Safari work properly
471 elem.parentNode.selectedIndex;
472 return elem.selected === true;
474 parent: function(elem){
475 return !!elem.firstChild;
477 empty: function(elem){
478 return !elem.firstChild;
480 has: function(elem, i, match){
481 return !!Sizzle( match[3], elem ).length;
483 header: function(elem){
484 return /h\d/i.test( elem.nodeName );
486 text: function(elem){
487 return "text" === elem.type;
489 radio: function(elem){
490 return "radio" === elem.type;
492 checkbox: function(elem){
493 return "checkbox" === elem.type;
495 file: function(elem){
496 return "file" === elem.type;
498 password: function(elem){
499 return "password" === elem.type;
501 submit: function(elem){
502 return "submit" === elem.type;
504 image: function(elem){
505 return "image" === elem.type;
507 reset: function(elem){
508 return "reset" === elem.type;
510 button: function(elem){
511 return "button" === elem.type || elem.nodeName.toUpperCase() === "BUTTON";
513 input: function(elem){
514 return /input|select|textarea|button/i.test(elem.nodeName);
518 first: function(elem, i){
521 last: function(elem, i, match, array){
522 return i === array.length - 1;
524 even: function(elem, i){
527 odd: function(elem, i){
530 lt: function(elem, i, match){
531 return i < match[3] - 0;
533 gt: function(elem, i, match){
534 return i > match[3] - 0;
536 nth: function(elem, i, match){
537 return match[3] - 0 == i;
539 eq: function(elem, i, match){
540 return match[3] - 0 == i;
544 PSEUDO: function(elem, match, i, array){
545 var name = match[1], filter = Expr.filters[ name ];
548 return filter( elem, i, match, array );
549 } else if ( name === "contains" ) {
550 return (elem.textContent || elem.innerText || "").indexOf(match[3]) >= 0;
551 } else if ( name === "not" ) {
554 for ( var i = 0, l = not.length; i < l; i++ ) {
555 if ( not[i] === elem ) {
563 CHILD: function(elem, match){
564 var type = match[1], node = elem;
568 while (node = node.previousSibling) {
569 if ( node.nodeType === 1 ) return false;
571 if ( type == 'first') return true;
574 while (node = node.nextSibling) {
575 if ( node.nodeType === 1 ) return false;
579 var first = match[2], last = match[3];
581 if ( first == 1 && last == 0 ) {
585 var doneName = match[0],
586 parent = elem.parentNode;
588 if ( parent && (parent.sizcache !== doneName || !elem.nodeIndex) ) {
590 for ( node = parent.firstChild; node; node = node.nextSibling ) {
591 if ( node.nodeType === 1 ) {
592 node.nodeIndex = ++count;
595 parent.sizcache = doneName;
598 var diff = elem.nodeIndex - last;
602 return ( diff % first == 0 && diff / first >= 0 );
606 ID: function(elem, match){
607 return elem.nodeType === 1 && elem.getAttribute("id") === match;
609 TAG: function(elem, match){
610 return (match === "*" && elem.nodeType === 1) || elem.nodeName === match;
612 CLASS: function(elem, match){
613 return (" " + (elem.className || elem.getAttribute("class")) + " ")
614 .indexOf( match ) > -1;
616 ATTR: function(elem, match){
618 result = Expr.attrHandle[ name ] ?
619 Expr.attrHandle[ name ]( elem ) :
620 elem[ name ] != null ?
622 elem.getAttribute( name ),
627 return result == null ?
632 value.indexOf(check) >= 0 :
634 (" " + value + " ").indexOf(check) >= 0 :
636 value && result !== false :
640 value.indexOf(check) === 0 :
642 value.substr(value.length - check.length) === check :
644 value === check || value.substr(0, check.length + 1) === check + "-" :
647 POS: function(elem, match, i, array){
648 var name = match[2], filter = Expr.setFilters[ name ];
651 return filter( elem, i, match, array );
657 var origPOS = Expr.match.POS;
659 for ( var type in Expr.match ) {
660 Expr.match[ type ] = new RegExp( Expr.match[ type ].source + /(?![^\[]*\])(?![^\(]*\))/.source );
663 var makeArray = function(array, results) {
664 array = Array.prototype.slice.call( array );
667 results.push.apply( results, array );
674 // Perform a simple check to determine if the browser is capable of
675 // converting a NodeList to an array using builtin methods.
677 Array.prototype.slice.call( document.documentElement.childNodes );
679 // Provide a fallback method if it does not work
681 makeArray = function(array, results) {
682 var ret = results || [];
684 if ( toString.call(array) === "[object Array]" ) {
685 Array.prototype.push.apply( ret, array );
687 if ( typeof array.length === "number" ) {
688 for ( var i = 0, l = array.length; i < l; i++ ) {
689 ret.push( array[i] );
692 for ( var i = 0; array[i]; i++ ) {
693 ret.push( array[i] );
704 if ( document.documentElement.compareDocumentPosition ) {
705 sortOrder = function( a, b ) {
706 var ret = a.compareDocumentPosition(b) & 4 ? -1 : a === b ? 0 : 1;
712 } else if ( "sourceIndex" in document.documentElement ) {
713 sortOrder = function( a, b ) {
714 var ret = a.sourceIndex - b.sourceIndex;
720 } else if ( document.createRange ) {
721 sortOrder = function( a, b ) {
722 var aRange = a.ownerDocument.createRange(), bRange = b.ownerDocument.createRange();
723 aRange.selectNode(a);
724 aRange.collapse(true);
725 bRange.selectNode(b);
726 bRange.collapse(true);
727 var ret = aRange.compareBoundaryPoints(Range.START_TO_END, bRange);
735 // Check to see if the browser returns elements by name when
736 // querying by getElementById (and provide a workaround)
738 // We're going to inject a fake input element with a specified name
739 var form = document.createElement("div"),
740 id = "script" + (new Date).getTime();
741 form.innerHTML = "<a name='" + id + "'/>";
743 // Inject it into the root element, check its status, and remove it quickly
744 var root = document.documentElement;
745 root.insertBefore( form, root.firstChild );
747 // The workaround has to do additional checks after a getElementById
748 // Which slows things down for other browsers (hence the branching)
749 if ( !!document.getElementById( id ) ) {
750 Expr.find.ID = function(match, context, isXML){
751 if ( typeof context.getElementById !== "undefined" && !isXML ) {
752 var m = context.getElementById(match[1]);
753 return m ? m.id === match[1] || typeof m.getAttributeNode !== "undefined" && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
757 Expr.filter.ID = function(elem, match){
758 var node = typeof elem.getAttributeNode !== "undefined" && elem.getAttributeNode("id");
759 return elem.nodeType === 1 && node && node.nodeValue === match;
763 root.removeChild( form );
764 root = form = null; // release memory in IE
768 // Check to see if the browser returns only elements
769 // when doing getElementsByTagName("*")
771 // Create a fake element
772 var div = document.createElement("div");
773 div.appendChild( document.createComment("") );
775 // Make sure no comments are found
776 if ( div.getElementsByTagName("*").length > 0 ) {
777 Expr.find.TAG = function(match, context){
778 var results = context.getElementsByTagName(match[1]);
780 // Filter out possible comments
781 if ( match[1] === "*" ) {
784 for ( var i = 0; results[i]; i++ ) {
785 if ( results[i].nodeType === 1 ) {
786 tmp.push( results[i] );
797 // Check to see if an attribute returns normalized href attributes
798 div.innerHTML = "<a href='#'></a>";
799 if ( div.firstChild && typeof div.firstChild.getAttribute !== "undefined" &&
800 div.firstChild.getAttribute("href") !== "#" ) {
801 Expr.attrHandle.href = function(elem){
802 return elem.getAttribute("href", 2);
806 div = null; // release memory in IE
809 if ( document.querySelectorAll ) (function(){
810 var oldSizzle = Sizzle, div = document.createElement("div");
811 div.innerHTML = "<p class='TEST'></p>";
813 // Safari can't handle uppercase or unicode characters when
815 if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) {
819 Sizzle = function(query, context, extra, seed){
820 context = context || document;
822 // Only use querySelectorAll on non-XML documents
823 // (ID selectors don't work in non-HTML documents)
824 if ( !seed && context.nodeType === 9 && !isXML(context) ) {
826 return makeArray( context.querySelectorAll(query), extra );
830 return oldSizzle(query, context, extra, seed);
833 for ( var prop in oldSizzle ) {
834 Sizzle[ prop ] = oldSizzle[ prop ];
837 div = null; // release memory in IE
840 if ( document.getElementsByClassName && document.documentElement.getElementsByClassName ) (function(){
841 var div = document.createElement("div");
842 div.innerHTML = "<div class='test e'></div><div class='test'></div>";
844 // Opera can't find a second classname (in 9.6)
845 if ( div.getElementsByClassName("e").length === 0 )
848 // Safari caches class attributes, doesn't catch changes (in 3.2)
849 div.lastChild.className = "e";
851 if ( div.getElementsByClassName("e").length === 1 )
854 Expr.order.splice(1, 0, "CLASS");
855 Expr.find.CLASS = function(match, context, isXML) {
856 if ( typeof context.getElementsByClassName !== "undefined" && !isXML ) {
857 return context.getElementsByClassName(match[1]);
861 div = null; // release memory in IE
864 function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
865 var sibDir = dir == "previousSibling" && !isXML;
866 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
867 var elem = checkSet[i];
869 if ( sibDir && elem.nodeType === 1 ){
870 elem.sizcache = doneName;
877 if ( elem.sizcache === doneName ) {
878 match = checkSet[elem.sizset];
882 if ( elem.nodeType === 1 && !isXML ){
883 elem.sizcache = doneName;
887 if ( elem.nodeName === cur ) {
900 function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
901 var sibDir = dir == "previousSibling" && !isXML;
902 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
903 var elem = checkSet[i];
905 if ( sibDir && elem.nodeType === 1 ) {
906 elem.sizcache = doneName;
913 if ( elem.sizcache === doneName ) {
914 match = checkSet[elem.sizset];
918 if ( elem.nodeType === 1 ) {
920 elem.sizcache = doneName;
923 if ( typeof cur !== "string" ) {
924 if ( elem === cur ) {
929 } else if ( Sizzle.filter( cur, [elem] ).length > 0 ) {
943 var contains = document.compareDocumentPosition ? function(a, b){
944 return a.compareDocumentPosition(b) & 16;
946 return a !== b && (a.contains ? a.contains(b) : true);
949 var isXML = function(elem){
950 return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
951 !!elem.ownerDocument && elem.ownerDocument.documentElement.nodeName !== "HTML";
954 var posProcess = function(selector, context){
955 var tmpSet = [], later = "", match,
956 root = context.nodeType ? [context] : context;
958 // Position selectors must be done after the filter
959 // And so must :not(positional) so we move all PSEUDOs to the end
960 while ( (match = Expr.match.PSEUDO.exec( selector )) ) {
962 selector = selector.replace( Expr.match.PSEUDO, "" );
965 selector = Expr.relative[selector] ? selector + "*" : selector;
967 for ( var i = 0, l = root.length; i < l; i++ ) {
968 Sizzle( selector, root[i], tmpSet );
971 return Sizzle.filter( later, tmpSet );
975 jQuery.find = Sizzle;
976 jQuery.expr = Sizzle.selectors;
977 jQuery.expr[":"] = jQuery.expr.filters;
979 Sizzle.selectors.filters.hidden = function(elem){
980 return elem.offsetWidth === 0 && elem.offsetHeight === 0;
983 Sizzle.selectors.filters.visible = function(elem){
984 return elem.offsetWidth > 0 || elem.offsetHeight > 0;
987 Sizzle.selectors.filters.animated = function(elem){
988 return jQuery.grep(jQuery.timers, function(fn){
989 return elem === fn.elem;
993 jQuery.filter = jQuery.multiFilter = function( expr, elems, not ) {
995 expr = ":not(" + expr + ")";
998 return Sizzle.matches(expr, elems);
1001 jQuery.dir = function( elem, dir ){
1002 var matched = [], cur = elem[dir];
1003 while ( cur && cur != document ) {
1004 if ( cur.nodeType == 1 )
1005 matched.push( cur );
1011 jQuery.nth = function(cur, result, dir, elem){
1012 result = result || 1;
1015 for ( ; cur; cur = cur[dir] )
1016 if ( cur.nodeType == 1 && ++num == result )
1022 jQuery.sibling = function(n, elem){
1025 for ( ; n; n = n.nextSibling ) {
1026 if ( n.nodeType == 1 && n != elem )
1035 window.Sizzle = Sizzle;