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;
13 var Sizzle = function(selector, context, results, seed) {
14 results = results || [];
15 var origContext = context = context || document;
17 if ( context.nodeType !== 1 && context.nodeType !== 9 ) {
21 if ( !selector || typeof selector !== "string" ) {
25 var parts = [], m, set, checkSet, check, mode, extra, prune = true, contextXML = isXML(context);
27 // Reset the position of the chunker regexp (start from head)
28 chunker.lastIndex = 0;
30 while ( (m = chunker.exec(selector)) !== null ) {
34 extra = RegExp.rightContext;
39 if ( parts.length > 1 && origPOS.exec( selector ) ) {
40 if ( parts.length === 2 && Expr.relative[ parts[0] ] ) {
41 set = posProcess( parts[0] + parts[1], context );
43 set = Expr.relative[ parts[0] ] ?
45 Sizzle( parts.shift(), context );
47 while ( parts.length ) {
48 selector = parts.shift();
50 if ( Expr.relative[ selector ] )
51 selector += parts.shift();
53 set = posProcess( selector, set );
57 // Take a shortcut and set the context if the root selector is an ID
58 // (but not if it'll be faster if the inner selector is an ID)
59 if ( !seed && parts.length > 1 && context.nodeType === 9 && !contextXML &&
60 Expr.match.ID.test(parts[0]) && !Expr.match.ID.test(parts[parts.length - 1]) ) {
61 var ret = Sizzle.find( parts.shift(), context, contextXML );
62 context = ret.expr ? Sizzle.filter( ret.expr, ret.set )[0] : ret.set[0];
67 { expr: parts.pop(), set: makeArray(seed) } :
68 Sizzle.find( parts.pop(), parts.length === 1 && context.parentNode ? context.parentNode : context, contextXML );
69 set = ret.expr ? Sizzle.filter( ret.expr, ret.set ) : ret.set;
71 if ( parts.length > 0 ) {
72 checkSet = makeArray(set);
77 while ( parts.length ) {
78 var cur = parts.pop(), pop = cur;
80 if ( !Expr.relative[ cur ] ) {
90 Expr.relative[ cur ]( checkSet, pop, contextXML );
93 checkSet = parts = [];
102 throw "Syntax error, unrecognized expression: " + (cur || selector);
105 if ( toString.call(checkSet) === "[object Array]" ) {
107 results.push.apply( results, checkSet );
108 } else if ( context && context.nodeType === 1 ) {
109 for ( var i = 0; checkSet[i] != null; i++ ) {
110 if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && contains(context, checkSet[i])) ) {
111 results.push( set[i] );
115 for ( var i = 0; checkSet[i] != null; i++ ) {
116 if ( checkSet[i] && checkSet[i].nodeType === 1 ) {
117 results.push( set[i] );
122 makeArray( checkSet, results );
126 Sizzle( extra, origContext, results, seed );
129 hasDuplicate = false;
130 results.sort(sortOrder);
132 if ( hasDuplicate ) {
133 for ( var i = 1; i < results.length; i++ ) {
134 if ( results[i] === results[i-1] ) {
135 results.splice(i--, 1);
145 Sizzle.matches = function(expr, set){
146 return Sizzle(expr, null, null, set);
149 Sizzle.find = function(expr, context, isXML){
156 for ( var i = 0, l = Expr.order.length; i < l; i++ ) {
157 var type = Expr.order[i], match;
159 if ( (match = Expr.match[ type ].exec( expr )) ) {
160 var left = RegExp.leftContext;
162 if ( left.substr( left.length - 1 ) !== "\\" ) {
163 match[1] = (match[1] || "").replace(/\\/g, "");
164 set = Expr.find[ type ]( match, context, isXML );
166 expr = expr.replace( Expr.match[ type ], "" );
174 set = context.getElementsByTagName("*");
177 return {set: set, expr: expr};
180 Sizzle.filter = function(expr, set, inplace, not){
181 var old = expr, result = [], curLoop = set, match, anyFound,
182 isXMLFilter = set && set[0] && isXML(set[0]);
184 while ( expr && set.length ) {
185 for ( var type in Expr.filter ) {
186 if ( (match = Expr.match[ type ].exec( expr )) != null ) {
187 var filter = Expr.filter[ type ], found, item;
190 if ( curLoop == result ) {
194 if ( Expr.preFilter[ type ] ) {
195 match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not, isXMLFilter );
198 anyFound = found = true;
199 } else if ( match === true ) {
205 for ( var i = 0; (item = curLoop[i]) != null; i++ ) {
207 found = filter( item, match, i, curLoop );
208 var pass = not ^ !!found;
210 if ( inplace && found != null ) {
224 if ( found !== undefined ) {
229 expr = expr.replace( Expr.match[ type ], "" );
240 // Improper expression
242 if ( anyFound == null ) {
243 throw "Syntax error, unrecognized expression: " + expr;
255 var Expr = Sizzle.selectors = {
256 order: [ "ID", "NAME", "TAG" ],
258 ID: /#((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
259 CLASS: /\.((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
260 NAME: /\[name=['"]*((?:[\w\u00c0-\uFFFF_-]|\\.)+)['"]*\]/,
261 ATTR: /\[\s*((?:[\w\u00c0-\uFFFF_-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\s*\]/,
262 TAG: /^((?:[\w\u00c0-\uFFFF\*_-]|\\.)+)/,
263 CHILD: /:(only|nth|last|first)-child(?:\((even|odd|[\dn+-]*)\))?/,
264 POS: /:(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^-]|$)/,
265 PSEUDO: /:((?:[\w\u00c0-\uFFFF_-]|\\.)+)(?:\((['"]*)((?:\([^\)]+\)|[^\2\(\)]*)+)\2\))?/
268 "class": "className",
272 href: function(elem){
273 return elem.getAttribute("href");
277 "+": function(checkSet, part, isXML){
278 var isPartStr = typeof part === "string",
279 isTag = isPartStr && !/\W/.test(part),
280 isPartStrNotTag = isPartStr && !isTag;
282 if ( isTag && !isXML ) {
283 part = part.toUpperCase();
286 for ( var i = 0, l = checkSet.length, elem; i < l; i++ ) {
287 if ( (elem = checkSet[i]) ) {
288 while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {}
290 checkSet[i] = isPartStrNotTag || elem && elem.nodeName === part ?
296 if ( isPartStrNotTag ) {
297 Sizzle.filter( part, checkSet, true );
300 ">": function(checkSet, part, isXML){
301 var isPartStr = typeof part === "string";
303 if ( isPartStr && !/\W/.test(part) ) {
304 part = isXML ? part : part.toUpperCase();
306 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
307 var elem = checkSet[i];
309 var parent = elem.parentNode;
310 checkSet[i] = parent.nodeName === part ? parent : false;
314 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
315 var elem = checkSet[i];
317 checkSet[i] = isPartStr ?
319 elem.parentNode === part;
324 Sizzle.filter( part, checkSet, true );
328 "": function(checkSet, part, isXML){
329 var doneName = done++, checkFn = dirCheck;
331 if ( !part.match(/\W/) ) {
332 var nodeCheck = part = isXML ? part : part.toUpperCase();
333 checkFn = dirNodeCheck;
336 checkFn("parentNode", part, doneName, checkSet, nodeCheck, isXML);
338 "~": function(checkSet, part, isXML){
339 var doneName = done++, checkFn = dirCheck;
341 if ( typeof part === "string" && !part.match(/\W/) ) {
342 var nodeCheck = part = isXML ? part : part.toUpperCase();
343 checkFn = dirNodeCheck;
346 checkFn("previousSibling", part, doneName, checkSet, nodeCheck, isXML);
350 ID: function(match, context, isXML){
351 if ( typeof context.getElementById !== "undefined" && !isXML ) {
352 var m = context.getElementById(match[1]);
356 NAME: function(match, context, isXML){
357 if ( typeof context.getElementsByName !== "undefined" ) {
358 var ret = [], results = context.getElementsByName(match[1]);
360 for ( var i = 0, l = results.length; i < l; i++ ) {
361 if ( results[i].getAttribute("name") === match[1] ) {
362 ret.push( results[i] );
366 return ret.length === 0 ? null : ret;
369 TAG: function(match, context){
370 return context.getElementsByTagName(match[1]);
374 CLASS: function(match, curLoop, inplace, result, not, isXML){
375 match = " " + match[1].replace(/\\/g, "") + " ";
381 for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) {
383 if ( not ^ (elem.className && (" " + elem.className + " ").indexOf(match) >= 0) ) {
386 } else if ( inplace ) {
395 return match[1].replace(/\\/g, "");
397 TAG: function(match, curLoop){
398 for ( var i = 0; curLoop[i] === false; i++ ){}
399 return curLoop[i] && isXML(curLoop[i]) ? match[1] : match[1].toUpperCase();
401 CHILD: function(match){
402 if ( match[1] == "nth" ) {
403 // parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6'
404 var test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec(
405 match[2] == "even" && "2n" || match[2] == "odd" && "2n+1" ||
406 !/\D/.test( match[2] ) && "0n+" + match[2] || match[2]);
408 // calculate the numbers (first)n+(last) including if they are negative
409 match[2] = (test[1] + (test[2] || 1)) - 0;
410 match[3] = test[3] - 0;
413 // TODO: Move to normal caching system
418 ATTR: function(match, curLoop, inplace, result, not, isXML){
419 var name = match[1].replace(/\\/g, "");
421 if ( !isXML && Expr.attrMap[name] ) {
422 match[1] = Expr.attrMap[name];
425 if ( match[2] === "~=" ) {
426 match[4] = " " + match[4] + " ";
431 PSEUDO: function(match, curLoop, inplace, result, not){
432 if ( match[1] === "not" ) {
433 // If we're dealing with a complex expression, or a simple one
434 if ( match[3].match(chunker).length > 1 || /^\w/.test(match[3]) ) {
435 match[3] = Sizzle(match[3], null, null, curLoop);
437 var ret = Sizzle.filter(match[3], curLoop, inplace, true ^ not);
439 result.push.apply( result, ret );
443 } else if ( Expr.match.POS.test( match[0] ) || Expr.match.CHILD.test( match[0] ) ) {
449 POS: function(match){
450 match.unshift( true );
455 enabled: function(elem){
456 return elem.disabled === false && elem.type !== "hidden";
458 disabled: function(elem){
459 return elem.disabled === true;
461 checked: function(elem){
462 return elem.checked === true;
464 selected: function(elem){
465 // Accessing this property makes selected-by-default
466 // options in Safari work properly
467 elem.parentNode.selectedIndex;
468 return elem.selected === true;
470 parent: function(elem){
471 return !!elem.firstChild;
473 empty: function(elem){
474 return !elem.firstChild;
476 has: function(elem, i, match){
477 return !!Sizzle( match[3], elem ).length;
479 header: function(elem){
480 return /h\d/i.test( elem.nodeName );
482 text: function(elem){
483 return "text" === elem.type;
485 radio: function(elem){
486 return "radio" === elem.type;
488 checkbox: function(elem){
489 return "checkbox" === elem.type;
491 file: function(elem){
492 return "file" === elem.type;
494 password: function(elem){
495 return "password" === elem.type;
497 submit: function(elem){
498 return "submit" === elem.type;
500 image: function(elem){
501 return "image" === elem.type;
503 reset: function(elem){
504 return "reset" === elem.type;
506 button: function(elem){
507 return "button" === elem.type || elem.nodeName.toUpperCase() === "BUTTON";
509 input: function(elem){
510 return /input|select|textarea|button/i.test(elem.nodeName);
514 first: function(elem, i){
517 last: function(elem, i, match, array){
518 return i === array.length - 1;
520 even: function(elem, i){
523 odd: function(elem, i){
526 lt: function(elem, i, match){
527 return i < match[3] - 0;
529 gt: function(elem, i, match){
530 return i > match[3] - 0;
532 nth: function(elem, i, match){
533 return match[3] - 0 == i;
535 eq: function(elem, i, match){
536 return match[3] - 0 == i;
540 PSEUDO: function(elem, match, i, array){
541 var name = match[1], filter = Expr.filters[ name ];
544 return filter( elem, i, match, array );
545 } else if ( name === "contains" ) {
546 return (elem.textContent || elem.innerText || "").indexOf(match[3]) >= 0;
547 } else if ( name === "not" ) {
550 for ( var i = 0, l = not.length; i < l; i++ ) {
551 if ( not[i] === elem ) {
559 CHILD: function(elem, match){
560 var type = match[1], node = elem;
564 while (node = node.previousSibling) {
565 if ( node.nodeType === 1 ) return false;
567 if ( type == 'first') return true;
570 while (node = node.nextSibling) {
571 if ( node.nodeType === 1 ) return false;
575 var first = match[2], last = match[3];
577 if ( first == 1 && last == 0 ) {
581 var doneName = match[0],
582 parent = elem.parentNode;
584 if ( parent && (parent.sizcache !== doneName || !elem.nodeIndex) ) {
586 for ( node = parent.firstChild; node; node = node.nextSibling ) {
587 if ( node.nodeType === 1 ) {
588 node.nodeIndex = ++count;
591 parent.sizcache = doneName;
594 var diff = elem.nodeIndex - last;
598 return ( diff % first == 0 && diff / first >= 0 );
602 ID: function(elem, match){
603 return elem.nodeType === 1 && elem.getAttribute("id") === match;
605 TAG: function(elem, match){
606 return (match === "*" && elem.nodeType === 1) || elem.nodeName === match;
608 CLASS: function(elem, match){
609 return (" " + (elem.className || elem.getAttribute("class")) + " ")
610 .indexOf( match ) > -1;
612 ATTR: function(elem, match){
614 result = Expr.attrHandle[ name ] ?
615 Expr.attrHandle[ name ]( elem ) :
616 elem[ name ] != null ?
618 elem.getAttribute( name ),
623 return result == null ?
628 value.indexOf(check) >= 0 :
630 (" " + value + " ").indexOf(check) >= 0 :
632 value && result !== false :
636 value.indexOf(check) === 0 :
638 value.substr(value.length - check.length) === check :
640 value === check || value.substr(0, check.length + 1) === check + "-" :
643 POS: function(elem, match, i, array){
644 var name = match[2], filter = Expr.setFilters[ name ];
647 return filter( elem, i, match, array );
653 var origPOS = Expr.match.POS;
655 for ( var type in Expr.match ) {
656 Expr.match[ type ] = RegExp( Expr.match[ type ].source + /(?![^\[]*\])(?![^\(]*\))/.source );
659 var makeArray = function(array, results) {
660 array = Array.prototype.slice.call( array );
663 results.push.apply( results, array );
670 // Perform a simple check to determine if the browser is capable of
671 // converting a NodeList to an array using builtin methods.
673 Array.prototype.slice.call( document.documentElement.childNodes );
675 // Provide a fallback method if it does not work
677 makeArray = function(array, results) {
678 var ret = results || [];
680 if ( toString.call(array) === "[object Array]" ) {
681 Array.prototype.push.apply( ret, array );
683 if ( typeof array.length === "number" ) {
684 for ( var i = 0, l = array.length; i < l; i++ ) {
685 ret.push( array[i] );
688 for ( var i = 0; array[i]; i++ ) {
689 ret.push( array[i] );
700 if ( document.documentElement.compareDocumentPosition ) {
701 sortOrder = function( a, b ) {
702 var ret = a.compareDocumentPosition(b) & 4 ? -1 : a === b ? 0 : 1;
708 } else if ( "sourceIndex" in document.documentElement ) {
709 sortOrder = function( a, b ) {
710 var ret = a.sourceIndex - b.sourceIndex;
716 } else if ( document.createRange ) {
717 sortOrder = function( a, b ) {
718 var aRange = a.ownerDocument.createRange(), bRange = b.ownerDocument.createRange();
719 aRange.selectNode(a);
720 aRange.collapse(true);
721 bRange.selectNode(b);
722 bRange.collapse(true);
723 var ret = aRange.compareBoundaryPoints(Range.START_TO_END, bRange);
731 // Check to see if the browser returns elements by name when
732 // querying by getElementById (and provide a workaround)
734 // We're going to inject a fake input element with a specified name
735 var form = document.createElement("form"),
736 id = "script" + (new Date).getTime();
737 form.innerHTML = "<input name='" + id + "'/>";
739 // Inject it into the root element, check its status, and remove it quickly
740 var root = document.documentElement;
741 root.insertBefore( form, root.firstChild );
743 // The workaround has to do additional checks after a getElementById
744 // Which slows things down for other browsers (hence the branching)
745 if ( !!document.getElementById( id ) ) {
746 Expr.find.ID = function(match, context, isXML){
747 if ( typeof context.getElementById !== "undefined" && !isXML ) {
748 var m = context.getElementById(match[1]);
749 return m ? m.id === match[1] || typeof m.getAttributeNode !== "undefined" && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
753 Expr.filter.ID = function(elem, match){
754 var node = typeof elem.getAttributeNode !== "undefined" && elem.getAttributeNode("id");
755 return elem.nodeType === 1 && node && node.nodeValue === match;
759 root.removeChild( form );
763 // Check to see if the browser returns only elements
764 // when doing getElementsByTagName("*")
766 // Create a fake element
767 var div = document.createElement("div");
768 div.appendChild( document.createComment("") );
770 // Make sure no comments are found
771 if ( div.getElementsByTagName("*").length > 0 ) {
772 Expr.find.TAG = function(match, context){
773 var results = context.getElementsByTagName(match[1]);
775 // Filter out possible comments
776 if ( match[1] === "*" ) {
779 for ( var i = 0; results[i]; i++ ) {
780 if ( results[i].nodeType === 1 ) {
781 tmp.push( results[i] );
792 // Check to see if an attribute returns normalized href attributes
793 div.innerHTML = "<a href='#'></a>";
794 if ( div.firstChild && typeof div.firstChild.getAttribute !== "undefined" &&
795 div.firstChild.getAttribute("href") !== "#" ) {
796 Expr.attrHandle.href = function(elem){
797 return elem.getAttribute("href", 2);
802 if ( document.querySelectorAll ) (function(){
803 var oldSizzle = Sizzle, div = document.createElement("div");
804 div.innerHTML = "<p class='TEST'></p>";
806 // Safari can't handle uppercase or unicode characters when
808 if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) {
812 Sizzle = function(query, context, extra, seed){
813 context = context || document;
815 // Only use querySelectorAll on non-XML documents
816 // (ID selectors don't work in non-HTML documents)
817 if ( !seed && context.nodeType === 9 && !isXML(context) ) {
819 return makeArray( context.querySelectorAll(query), extra );
823 return oldSizzle(query, context, extra, seed);
826 Sizzle.find = oldSizzle.find;
827 Sizzle.filter = oldSizzle.filter;
828 Sizzle.selectors = oldSizzle.selectors;
829 Sizzle.matches = oldSizzle.matches;
832 if ( document.getElementsByClassName && document.documentElement.getElementsByClassName ) (function(){
833 var div = document.createElement("div");
834 div.innerHTML = "<div class='test e'></div><div class='test'></div>";
836 // Opera can't find a second classname (in 9.6)
837 if ( div.getElementsByClassName("e").length === 0 )
840 // Safari caches class attributes, doesn't catch changes (in 3.2)
841 div.lastChild.className = "e";
843 if ( div.getElementsByClassName("e").length === 1 )
846 Expr.order.splice(1, 0, "CLASS");
847 Expr.find.CLASS = function(match, context, isXML) {
848 if ( typeof context.getElementsByClassName !== "undefined" && !isXML ) {
849 return context.getElementsByClassName(match[1]);
854 function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
855 var sibDir = dir == "previousSibling" && !isXML;
856 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
857 var elem = checkSet[i];
859 if ( sibDir && elem.nodeType === 1 ){
860 elem.sizcache = doneName;
867 if ( elem.sizcache === doneName ) {
868 match = checkSet[elem.sizset];
872 if ( elem.nodeType === 1 && !isXML ){
873 elem.sizcache = doneName;
877 if ( elem.nodeName === cur ) {
890 function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
891 var sibDir = dir == "previousSibling" && !isXML;
892 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
893 var elem = checkSet[i];
895 if ( sibDir && elem.nodeType === 1 ) {
896 elem.sizcache = doneName;
903 if ( elem.sizcache === doneName ) {
904 match = checkSet[elem.sizset];
908 if ( elem.nodeType === 1 ) {
910 elem.sizcache = doneName;
913 if ( typeof cur !== "string" ) {
914 if ( elem === cur ) {
919 } else if ( Sizzle.filter( cur, [elem] ).length > 0 ) {
933 var contains = document.compareDocumentPosition ? function(a, b){
934 return a.compareDocumentPosition(b) & 16;
936 return a !== b && (a.contains ? a.contains(b) : true);
939 var isXML = function(elem){
940 return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
941 !!elem.ownerDocument && isXML( elem.ownerDocument );
944 var posProcess = function(selector, context){
945 var tmpSet = [], later = "", match,
946 root = context.nodeType ? [context] : context;
948 // Position selectors must be done after the filter
949 // And so must :not(positional) so we move all PSEUDOs to the end
950 while ( (match = Expr.match.PSEUDO.exec( selector )) ) {
952 selector = selector.replace( Expr.match.PSEUDO, "" );
955 selector = Expr.relative[selector] ? selector + "*" : selector;
957 for ( var i = 0, l = root.length; i < l; i++ ) {
958 Sizzle( selector, root[i], tmpSet );
961 return Sizzle.filter( later, tmpSet );
965 jQuery.find = Sizzle;
966 jQuery.expr = Sizzle.selectors;
967 jQuery.expr[":"] = jQuery.expr.filters;
969 Sizzle.selectors.filters.hidden = function(elem){
970 return elem.offsetWidth === 0 || elem.offsetHeight === 0;
973 Sizzle.selectors.filters.visible = function(elem){
974 return elem.offsetWidth > 0 || elem.offsetHeight > 0;
977 Sizzle.selectors.filters.animated = function(elem){
978 return jQuery.grep(jQuery.timers, function(fn){
979 return elem === fn.elem;
983 jQuery.filter = jQuery.multiFilter = function( expr, elems, not ) {
985 expr = ":not(" + expr + ")";
988 return Sizzle.matches(expr, elems);
991 jQuery.dir = function( elem, dir ){
992 var matched = [], cur = elem[dir];
993 while ( cur && cur != document ) {
994 if ( cur.nodeType == 1 )
1001 jQuery.nth = function(cur, result, dir, elem){
1002 result = result || 1;
1005 for ( ; cur; cur = cur[dir] )
1006 if ( cur.nodeType == 1 && ++num == result )
1012 jQuery.sibling = function(n, elem){
1015 for ( ; n; n = n.nextSibling ) {
1016 if ( n.nodeType == 1 && n != elem )
1025 window.Sizzle = Sizzle;