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 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 ( document.documentElement.compareDocumentPosition ) {
688 sortOrder = function( a, b ) {
689 var ret = a.compareDocumentPosition(b) & 4 ? -1 : a === b ? 0 : 1;
695 } else if ( "sourceIndex" in document.documentElement ) {
696 sortOrder = function( a, b ) {
697 var ret = a.sourceIndex - b.sourceIndex;
703 } else if ( document.createRange ) {
704 sortOrder = function( a, b ) {
705 var aRange = a.ownerDocument.createRange(), bRange = b.ownerDocument.createRange();
706 aRange.selectNode(a);
707 aRange.collapse(true);
708 bRange.selectNode(b);
709 bRange.collapse(true);
710 var ret = aRange.compareBoundaryPoints(Range.START_TO_END, bRange);
718 // Check to see if the browser returns elements by name when
719 // querying by getElementById (and provide a workaround)
721 // We're going to inject a fake input element with a specified name
722 var form = document.createElement("form"),
723 id = "script" + (new Date).getTime();
724 form.innerHTML = "<input name='" + id + "'/>";
726 // Inject it into the root element, check its status, and remove it quickly
727 var root = document.documentElement;
728 root.insertBefore( form, root.firstChild );
730 // The workaround has to do additional checks after a getElementById
731 // Which slows things down for other browsers (hence the branching)
732 if ( !!document.getElementById( id ) ) {
733 Expr.find.ID = function(match, context, isXML){
734 if ( typeof context.getElementById !== "undefined" && !isXML ) {
735 var m = context.getElementById(match[1]);
736 return m ? m.id === match[1] || typeof m.getAttributeNode !== "undefined" && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
740 Expr.filter.ID = function(elem, match){
741 var node = typeof elem.getAttributeNode !== "undefined" && elem.getAttributeNode("id");
742 return elem.nodeType === 1 && node && node.nodeValue === match;
746 root.removeChild( form );
750 // Check to see if the browser returns only elements
751 // when doing getElementsByTagName("*")
753 // Create a fake element
754 var div = document.createElement("div");
755 div.appendChild( document.createComment("") );
757 // Make sure no comments are found
758 if ( div.getElementsByTagName("*").length > 0 ) {
759 Expr.find.TAG = function(match, context){
760 var results = context.getElementsByTagName(match[1]);
762 // Filter out possible comments
763 if ( match[1] === "*" ) {
766 for ( var i = 0; results[i]; i++ ) {
767 if ( results[i].nodeType === 1 ) {
768 tmp.push( results[i] );
779 // Check to see if an attribute returns normalized href attributes
780 div.innerHTML = "<a href='#'></a>";
781 if ( div.firstChild && typeof div.firstChild.getAttribute !== "undefined" &&
782 div.firstChild.getAttribute("href") !== "#" ) {
783 Expr.attrHandle.href = function(elem){
784 return elem.getAttribute("href", 2);
789 if ( document.querySelectorAll ) (function(){
790 var oldSizzle = Sizzle, div = document.createElement("div");
791 div.innerHTML = "<p class='TEST'></p>";
793 // Safari can't handle uppercase or unicode characters when
795 if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) {
799 Sizzle = function(query, context, extra, seed){
800 context = context || document;
802 // Only use querySelectorAll on non-XML documents
803 // (ID selectors don't work in non-HTML documents)
804 if ( !seed && context.nodeType === 9 && !isXML(context) ) {
806 return makeArray( context.querySelectorAll(query), extra );
810 return oldSizzle(query, context, extra, seed);
813 Sizzle.find = oldSizzle.find;
814 Sizzle.filter = oldSizzle.filter;
815 Sizzle.selectors = oldSizzle.selectors;
816 Sizzle.matches = oldSizzle.matches;
819 if ( document.getElementsByClassName && document.documentElement.getElementsByClassName ) (function(){
820 var div = document.createElement("div");
821 div.innerHTML = "<div class='test e'></div><div class='test'></div>";
823 // Opera can't find a second classname (in 9.6)
824 if ( div.getElementsByClassName("e").length === 0 )
827 // Safari caches class attributes, doesn't catch changes (in 3.2)
828 div.lastChild.className = "e";
830 if ( div.getElementsByClassName("e").length === 1 )
833 Expr.order.splice(1, 0, "CLASS");
834 Expr.find.CLASS = function(match, context, isXML) {
835 if ( typeof context.getElementsByClassName !== "undefined" && !isXML ) {
836 return context.getElementsByClassName(match[1]);
841 function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
842 var sibDir = dir == "previousSibling" && !isXML;
843 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
844 var elem = checkSet[i];
846 if ( sibDir && elem.nodeType === 1 ){
847 elem.sizcache = doneName;
854 if ( elem.sizcache === doneName ) {
855 match = checkSet[elem.sizset];
859 if ( elem.nodeType === 1 && !isXML ){
860 elem.sizcache = doneName;
864 if ( elem.nodeName === cur ) {
877 function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
878 var sibDir = dir == "previousSibling" && !isXML;
879 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
880 var elem = checkSet[i];
882 if ( sibDir && elem.nodeType === 1 ) {
883 elem.sizcache = doneName;
890 if ( elem.sizcache === doneName ) {
891 match = checkSet[elem.sizset];
895 if ( elem.nodeType === 1 ) {
897 elem.sizcache = doneName;
900 if ( typeof cur !== "string" ) {
901 if ( elem === cur ) {
906 } else if ( Sizzle.filter( cur, [elem] ).length > 0 ) {
920 var contains = document.compareDocumentPosition ? function(a, b){
921 return a.compareDocumentPosition(b) & 16;
923 return a !== b && (a.contains ? a.contains(b) : true);
926 var isXML = function(elem){
927 return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
928 !!elem.ownerDocument && isXML( elem.ownerDocument );
931 var posProcess = function(selector, context){
932 var tmpSet = [], later = "", match,
933 root = context.nodeType ? [context] : context;
935 // Position selectors must be done after the filter
936 // And so must :not(positional) so we move all PSEUDOs to the end
937 while ( (match = Expr.match.PSEUDO.exec( selector )) ) {
939 selector = selector.replace( Expr.match.PSEUDO, "" );
942 selector = Expr.relative[selector] ? selector + "*" : selector;
944 for ( var i = 0, l = root.length; i < l; i++ ) {
945 Sizzle( selector, root[i], tmpSet );
948 return Sizzle.filter( later, tmpSet );
952 jQuery.find = Sizzle;
953 jQuery.expr = Sizzle.selectors;
954 jQuery.expr[":"] = jQuery.expr.filters;
956 Sizzle.selectors.filters.hidden = function(elem){
957 return elem.offsetWidth === 0 || elem.offsetHeight === 0;
960 Sizzle.selectors.filters.visible = function(elem){
961 return elem.offsetWidth > 0 || elem.offsetHeight > 0;
964 Sizzle.selectors.filters.animated = function(elem){
965 return jQuery.grep(jQuery.timers, function(fn){
966 return elem === fn.elem;
970 jQuery.filter = jQuery.multiFilter = function( expr, elems, not ) {
972 expr = ":not(" + expr + ")";
975 return Sizzle.matches(expr, elems);
978 jQuery.dir = function( elem, dir ){
979 var matched = [], cur = elem[dir];
980 while ( cur && cur != document ) {
981 if ( cur.nodeType == 1 )
988 jQuery.nth = function(cur, result, dir, elem){
989 result = result || 1;
992 for ( ; cur; cur = cur[dir] )
993 if ( cur.nodeType == 1 && ++num == result )
999 jQuery.sibling = function(n, elem){
1002 for ( ; n; n = n.nextSibling ) {
1003 if ( n.nodeType == 1 && n != elem )
1012 window.Sizzle = Sizzle;