Moved a bunch of methods out of the jQuery-specific Sizzle code into more-appropriate...
[jquery.git] / src / selector.js
index 9cc75fc..6291d86 100644 (file)
@@ -1,27 +1,29 @@
-/*
- * Sizzle CSS Selector Engine - v0.9
- *  Copyright 2009, John Resig (http://ejohn.org/)
- *  released under the MIT License
+/*!
+ * Sizzle CSS Selector Engine - v1.0
+ *  Copyright 2009, The Dojo Foundation
+ *  Released under the MIT, BSD, and GPL Licenses.
+ *  More information: http://sizzlejs.com/
  */
 (function(){
 
-var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|[^[\]]+)+\]|\\.|[^ >+~,(\[]+)+|[>+~])(\s*,\s*)?/g;
-
-var done = 0;
+var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|['"][^'"]*['"]|[^[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?/g,
+       done = 0,
+       toString = Object.prototype.toString,
+       hasDuplicate = false;
 
 var Sizzle = function(selector, context, results, seed) {
-       var doCache = !results;
        results = results || [];
-       context = context || document;
+       var origContext = context = context || document;
 
-       if ( context.nodeType !== 1 && context.nodeType !== 9 )
+       if ( context.nodeType !== 1 && context.nodeType !== 9 ) {
                return [];
+       }
        
        if ( !selector || typeof selector !== "string" ) {
                return results;
        }
 
-       var parts = [], m, set, checkSet, check, mode, extra;
+       var parts = [], m, set, checkSet, check, mode, extra, prune = true, contextXML = isXML(context);
        
        // Reset the position of the chunker regexp (start from head)
        chunker.lastIndex = 0;
@@ -35,60 +37,61 @@ var Sizzle = function(selector, context, results, seed) {
                }
        }
 
-       if ( parts.length > 1 && Expr.match.POS.exec( selector ) ) {
+       if ( parts.length > 1 && origPOS.exec( selector ) ) {
                if ( parts.length === 2 && Expr.relative[ parts[0] ] ) {
-                       var later = "", match;
-
-                       // Position selectors must be done after the filter
-                       while ( (match = Expr.match.POS.exec( selector )) ) {
-                               later += match[0];
-                               selector = selector.replace( Expr.match.POS, "" );
-                       }
-
-                       set = Sizzle.filter( later, Sizzle( selector, context ) );
+                       set = posProcess( parts[0] + parts[1], context );
                } else {
                        set = Expr.relative[ parts[0] ] ?
                                [ context ] :
                                Sizzle( parts.shift(), context );
 
                        while ( parts.length ) {
-                               var tmpSet = [];
-
                                selector = parts.shift();
+
                                if ( Expr.relative[ selector ] )
                                        selector += parts.shift();
 
-                               for ( var i = 0, l = set.length; i < l; i++ ) {
-                                       Sizzle( selector, set[i], tmpSet );
-                               }
-
-                               set = tmpSet;
+                               set = posProcess( selector, set );
                        }
                }
        } else {
-               var ret = seed ?
-                       { expr: parts.pop(), set: makeArray(seed) } :
-                       Sizzle.find( parts.pop(), parts.length === 1 && context.parentNode ? context.parentNode : context );
-               set = Sizzle.filter( ret.expr, ret.set );
-
-               if ( parts.length > 0 ) {
-                       checkSet = makeArray(set);
+               // Take a shortcut and set the context if the root selector is an ID
+               // (but not if it'll be faster if the inner selector is an ID)
+               if ( !seed && parts.length > 1 && context.nodeType === 9 && !contextXML &&
+                               Expr.match.ID.test(parts[0]) && !Expr.match.ID.test(parts[parts.length - 1]) ) {
+                       var ret = Sizzle.find( parts.shift(), context, contextXML );
+                       context = ret.expr ? Sizzle.filter( ret.expr, ret.set )[0] : ret.set[0];
                }
 
-               while ( parts.length ) {
-                       var cur = parts.pop(), pop = cur;
+               if ( context ) {
+                       var ret = seed ?
+                               { expr: parts.pop(), set: makeArray(seed) } :
+                               Sizzle.find( parts.pop(), parts.length === 1 && (parts[0] === "~" || parts[0] === "+") && context.parentNode ? context.parentNode : context, contextXML );
+                       set = ret.expr ? Sizzle.filter( ret.expr, ret.set ) : ret.set;
 
-                       if ( !Expr.relative[ cur ] ) {
-                               cur = "";
+                       if ( parts.length > 0 ) {
+                               checkSet = makeArray(set);
                        } else {
-                               pop = parts.pop();
+                               prune = false;
                        }
 
-                       if ( pop == null ) {
-                               pop = context;
-                       }
+                       while ( parts.length ) {
+                               var cur = parts.pop(), pop = cur;
+
+                               if ( !Expr.relative[ cur ] ) {
+                                       cur = "";
+                               } else {
+                                       pop = parts.pop();
+                               }
+
+                               if ( pop == null ) {
+                                       pop = context;
+                               }
 
-                       Expr.relative[ cur ]( checkSet, pop );
+                               Expr.relative[ cur ]( checkSet, pop, contextXML );
+                       }
+               } else {
+                       checkSet = parts = [];
                }
        }
 
@@ -100,8 +103,10 @@ var Sizzle = function(selector, context, results, seed) {
                throw "Syntax error, unrecognized expression: " + (cur || selector);
        }
 
-       if ( checkSet instanceof Array ) {
-               if ( context.nodeType === 1 ) {
+       if ( toString.call(checkSet) === "[object Array]" ) {
+               if ( !prune ) {
+                       results.push.apply( results, checkSet );
+               } else if ( context && context.nodeType === 1 ) {
                        for ( var i = 0; checkSet[i] != null; i++ ) {
                                if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && contains(context, checkSet[i])) ) {
                                        results.push( set[i] );
@@ -119,7 +124,25 @@ var Sizzle = function(selector, context, results, seed) {
        }
 
        if ( extra ) {
-               Sizzle( extra, context, results, seed );
+               Sizzle( extra, origContext, results, seed );
+               Sizzle.uniqueSort( results );
+       }
+
+       return results;
+};
+
+Sizzle.uniqueSort = function(results){
+       if ( sortOrder ) {
+               hasDuplicate = false;
+               results.sort(sortOrder);
+
+               if ( hasDuplicate ) {
+                       for ( var i = 1; i < results.length; i++ ) {
+                               if ( results[i] === results[i-1] ) {
+                                       results.splice(i--, 1);
+                               }
+                       }
+               }
        }
 
        return results;
@@ -129,7 +152,7 @@ Sizzle.matches = function(expr, set){
        return Sizzle(expr, null, null, set);
 };
 
-Sizzle.find = function(expr, context){
+Sizzle.find = function(expr, context, isXML){
        var set, match;
 
        if ( !expr ) {
@@ -144,7 +167,7 @@ Sizzle.find = function(expr, context){
 
                        if ( left.substr( left.length - 1 ) !== "\\" ) {
                                match[1] = (match[1] || "").replace(/\\/g, "");
-                               set = Expr.find[ type ]( match, context );
+                               set = Expr.find[ type ]( match, context, isXML );
                                if ( set != null ) {
                                        expr = expr.replace( Expr.match[ type ], "" );
                                        break;
@@ -160,13 +183,14 @@ Sizzle.find = function(expr, context){
        return {set: set, expr: expr};
 };
 
-Sizzle.filter = function(expr, set, inplace){
-       var old = expr, result = [], curLoop = set, match, anyFound;
+Sizzle.filter = function(expr, set, inplace, not){
+       var old = expr, result = [], curLoop = set, match, anyFound,
+               isXMLFilter = set && set[0] && isXML(set[0]);
 
        while ( expr && set.length ) {
                for ( var type in Expr.filter ) {
                        if ( (match = Expr.match[ type ].exec( expr )) != null ) {
-                               var filter = Expr.filter[ type ], goodArray = null, goodPos = 0, found, item;
+                               var filter = Expr.filter[ type ], found, item;
                                anyFound = false;
 
                                if ( curLoop == result ) {
@@ -174,35 +198,31 @@ Sizzle.filter = function(expr, set, inplace){
                                }
 
                                if ( Expr.preFilter[ type ] ) {
-                                       match = Expr.preFilter[ type ]( match, curLoop );
-
-                                       if ( match[0] === true ) {
-                                               goodArray = [];
-                                               var last = null, elem;
-                                               for ( var i = 0; (elem = curLoop[i]) !== undefined; i++ ) {
-                                                       if ( elem && last !== elem ) {
-                                                               goodArray.push( elem );
-                                                               last = elem;
-                                                       }
-                                               }
+                                       match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not, isXMLFilter );
+
+                                       if ( !match ) {
+                                               anyFound = found = true;
+                                       } else if ( match === true ) {
+                                               continue;
                                        }
                                }
 
-                               for ( var i = 0; (item = curLoop[i]) !== undefined; i++ ) {
-                                       if ( item ) {
-                                               if ( goodArray && item != goodArray[goodPos] ) {
-                                                       goodPos++;
-                                               }
-
-                                               found = filter( item, match, goodPos, goodArray );
-                                               if ( inplace && found != null ) {
-                                                       curLoop[i] = found ? curLoop[i] : false;
-                                                       if ( found ) {
+                               if ( match ) {
+                                       for ( var i = 0; (item = curLoop[i]) != null; i++ ) {
+                                               if ( item ) {
+                                                       found = filter( item, match, i, curLoop );
+                                                       var pass = not ^ !!found;
+
+                                                       if ( inplace && found != null ) {
+                                                               if ( pass ) {
+                                                                       anyFound = true;
+                                                               } else {
+                                                                       curLoop[i] = false;
+                                                               }
+                                                       } else if ( pass ) {
+                                                               result.push( item );
                                                                anyFound = true;
                                                        }
-                                               } else if ( found ) {
-                                                       result.push( item );
-                                                       anyFound = true;
                                                }
                                        }
                                }
@@ -223,8 +243,6 @@ Sizzle.filter = function(expr, set, inplace){
                        }
                }
 
-               expr = expr.replace(/\s*,\s*/, "");
-
                // Improper expression
                if ( expr == old ) {
                        if ( anyFound == null ) {
@@ -243,40 +261,53 @@ Sizzle.filter = function(expr, set, inplace){
 var Expr = Sizzle.selectors = {
        order: [ "ID", "NAME", "TAG" ],
        match: {
-               ID: /#((?:[\w\u0128-\uFFFF_-]|\\.)+)/,
-               CLASS: /\.((?:[\w\u0128-\uFFFF_-]|\\.)+)/,
-               NAME: /\[name=['"]*((?:[\w\u0128-\uFFFF_-]|\\.)+)['"]*\]/,
-               ATTR: /\[((?:[\w\u0128-\uFFFF_-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\]/,
-               TAG: /^((?:[\w\u0128-\uFFFF\*_-]|\\.)+)/,
-               CHILD: /:(only|nth|last|first)-child\(?(even|odd|[\dn+-]*)\)?/,
-               POS: /:(nth|eq|gt|lt|first|last|even|odd)\(?(\d*)\)?(?:[^-]|$)/,
-               PSEUDO: /:((?:[\w\u0128-\uFFFF_-]|\\.)+)(?:\((['"]*)((?:\([^\)]+\)|[^\2\(\)]*)+)\2\))?/
+               ID: /#((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
+               CLASS: /\.((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
+               NAME: /\[name=['"]*((?:[\w\u00c0-\uFFFF_-]|\\.)+)['"]*\]/,
+               ATTR: /\[\s*((?:[\w\u00c0-\uFFFF_-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\s*\]/,
+               TAG: /^((?:[\w\u00c0-\uFFFF\*_-]|\\.)+)/,
+               CHILD: /:(only|nth|last|first)-child(?:\((even|odd|[\dn+-]*)\))?/,
+               POS: /:(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^-]|$)/,
+               PSEUDO: /:((?:[\w\u00c0-\uFFFF_-]|\\.)+)(?:\((['"]*)((?:\([^\)]+\)|[^\2\(\)]*)+)\2\))?/
        },
        attrMap: {
-               "class": "className"
+               "class": "className",
+               "for": "htmlFor"
+       },
+       attrHandle: {
+               href: function(elem){
+                       return elem.getAttribute("href");
+               }
        },
        relative: {
-               "+": function(checkSet, part){
-                       for ( var i = 0, l = checkSet.length; i < l; i++ ) {
-                               var elem = checkSet[i];
-                               if ( elem ) {
-                                       var cur = elem.previousSibling;
-                                       while ( cur && cur.nodeType !== 1 ) {
-                                               cur = cur.previousSibling;
-                                       }
-                                       checkSet[i] = typeof part === "string" ?
-                                               cur || false :
-                                               cur === part;
+               "+": function(checkSet, part, isXML){
+                       var isPartStr = typeof part === "string",
+                               isTag = isPartStr && !/\W/.test(part),
+                               isPartStrNotTag = isPartStr && !isTag;
+
+                       if ( isTag && !isXML ) {
+                               part = part.toUpperCase();
+                       }
+
+                       for ( var i = 0, l = checkSet.length, elem; i < l; i++ ) {
+                               if ( (elem = checkSet[i]) ) {
+                                       while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {}
+
+                                       checkSet[i] = isPartStrNotTag || elem && elem.nodeName === part ?
+                                               elem || false :
+                                               elem === part;
                                }
                        }
 
-                       if ( typeof part === "string" ) {
+                       if ( isPartStrNotTag ) {
                                Sizzle.filter( part, checkSet, true );
                        }
                },
-               ">": function(checkSet, part){
-                       if ( typeof part === "string" && !/\W/.test(part) ) {
-                               part = part.toUpperCase();
+               ">": function(checkSet, part, isXML){
+                       var isPartStr = typeof part === "string";
+
+                       if ( isPartStr && !/\W/.test(part) ) {
+                               part = isXML ? part : part.toUpperCase();
 
                                for ( var i = 0, l = checkSet.length; i < l; i++ ) {
                                        var elem = checkSet[i];
@@ -289,61 +320,89 @@ var Expr = Sizzle.selectors = {
                                for ( var i = 0, l = checkSet.length; i < l; i++ ) {
                                        var elem = checkSet[i];
                                        if ( elem ) {
-                                               checkSet[i] = typeof part === "string" ?
+                                               checkSet[i] = isPartStr ?
                                                        elem.parentNode :
                                                        elem.parentNode === part;
                                        }
                                }
 
-                               if ( typeof part === "string" ) {
+                               if ( isPartStr ) {
                                        Sizzle.filter( part, checkSet, true );
                                }
                        }
                },
-               "": function(checkSet, part){
-                       var doneName = "done" + (done++), checkFn = dirCheck;
+               "": function(checkSet, part, isXML){
+                       var doneName = done++, checkFn = dirCheck;
 
-                       if ( !part.match(/\W/) ) {
-                               var nodeCheck = part = part.toUpperCase();
+                       if ( !/\W/.test(part) ) {
+                               var nodeCheck = part = isXML ? part : part.toUpperCase();
                                checkFn = dirNodeCheck;
                        }
 
-                       checkFn("parentNode", part, doneName, checkSet, nodeCheck);
+                       checkFn("parentNode", part, doneName, checkSet, nodeCheck, isXML);
                },
-               "~": function(checkSet, part){
-                       var doneName = "done" + (done++), checkFn = dirCheck;
+               "~": function(checkSet, part, isXML){
+                       var doneName = done++, checkFn = dirCheck;
 
-                       if ( typeof part === "string" && !part.match(/\W/) ) {
-                               var nodeCheck = part = part.toUpperCase();
+                       if ( typeof part === "string" && !/\W/.test(part) ) {
+                               var nodeCheck = part = isXML ? part : part.toUpperCase();
                                checkFn = dirNodeCheck;
                        }
 
-                       checkFn("previousSibling", part, doneName, checkSet, nodeCheck);
+                       checkFn("previousSibling", part, doneName, checkSet, nodeCheck, isXML);
                }
        },
        find: {
-               ID: function(match, context){
-                       if ( context.getElementById ) {
+               ID: function(match, context, isXML){
+                       if ( typeof context.getElementById !== "undefined" && !isXML ) {
                                var m = context.getElementById(match[1]);
                                return m ? [m] : [];
                        }
                },
-               NAME: function(match, context){
-                       return context.getElementsByName ? context.getElementsByName(match[1]) : null;
+               NAME: function(match, context, isXML){
+                       if ( typeof context.getElementsByName !== "undefined" ) {
+                               var ret = [], results = context.getElementsByName(match[1]);
+
+                               for ( var i = 0, l = results.length; i < l; i++ ) {
+                                       if ( results[i].getAttribute("name") === match[1] ) {
+                                               ret.push( results[i] );
+                                       }
+                               }
+
+                               return ret.length === 0 ? null : ret;
+                       }
                },
                TAG: function(match, context){
                        return context.getElementsByTagName(match[1]);
                }
        },
        preFilter: {
-               CLASS: function(match){
-                       return new RegExp( "(?:^|\\s)" + match[1] + "(?:\\s|$)" );
+               CLASS: function(match, curLoop, inplace, result, not, isXML){
+                       match = " " + match[1].replace(/\\/g, "") + " ";
+
+                       if ( isXML ) {
+                               return match;
+                       }
+
+                       for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) {
+                               if ( elem ) {
+                                       if ( not ^ (elem.className && (" " + elem.className + " ").indexOf(match) >= 0) ) {
+                                               if ( !inplace )
+                                                       result.push( elem );
+                                       } else if ( inplace ) {
+                                               curLoop[i] = false;
+                                       }
+                               }
+                       }
+
+                       return false;
                },
                ID: function(match){
-                       return match[1];
+                       return match[1].replace(/\\/g, "");
                },
-               TAG: function(match){
-                       return match[1].toUpperCase();
+               TAG: function(match, curLoop){
+                       for ( var i = 0; curLoop[i] === false; i++ ){}
+                       return curLoop[i] && isXML(curLoop[i]) ? match[1] : match[1].toUpperCase();
                },
                CHILD: function(match){
                        if ( match[1] == "nth" ) {
@@ -358,14 +417,14 @@ var Expr = Sizzle.selectors = {
                        }
 
                        // TODO: Move to normal caching system
-                       match[0] = "done" + (done++);
+                       match[0] = done++;
 
                        return match;
                },
-               ATTR: function(match){
-                       var name = match[1];
+               ATTR: function(match, curLoop, inplace, result, not, isXML){
+                       var name = match[1].replace(/\\/g, "");
                        
-                       if ( Expr.attrMap[name] ) {
+                       if ( !isXML && Expr.attrMap[name] ) {
                                match[1] = Expr.attrMap[name];
                        }
 
@@ -375,12 +434,20 @@ var Expr = Sizzle.selectors = {
 
                        return match;
                },
-               PSEUDO: function(match, curLoop){
+               PSEUDO: function(match, curLoop, inplace, result, not){
                        if ( match[1] === "not" ) {
                                // If we're dealing with a complex expression, or a simple one
-                               match[3] = match[3].match(chunker).length > 1 ?
-                                       Sizzle(match[3], null, null, curLoop) :
-                                       Sizzle.filter(match[3], curLoop);
+                               if ( chunker.exec(match[3]).length > 1 || /^\w/.test(match[3]) ) {
+                                       match[3] = Sizzle(match[3], null, null, curLoop);
+                               } else {
+                                       var ret = Sizzle.filter(match[3], curLoop, inplace, true ^ not);
+                                       if ( !inplace ) {
+                                               result.push.apply( result, ret );
+                                       }
+                                       return false;
+                               }
+                       } else if ( Expr.match.POS.test( match[0] ) || Expr.match.CHILD.test( match[0] ) ) {
+                               return true;
                        }
                        
                        return match;
@@ -476,47 +543,6 @@ var Expr = Sizzle.selectors = {
                }
        },
        filter: {
-               CHILD: function(elem, match){
-                       var type = match[1], parent = elem.parentNode;
-
-                       var doneName = match[0];
-                       
-                       if ( parent && !parent[ doneName ] ) {
-                               var count = 1;
-
-                               for ( var node = parent.firstChild; node; node = node.nextSibling ) {
-                                       if ( node.nodeType == 1 ) {
-                                               node.nodeIndex = count++;
-                                       }
-                               }
-
-                               parent[ doneName ] = count - 1;
-                       }
-
-                       if ( type == "first" ) {
-                               return elem.nodeIndex == 1;
-                       } else if ( type == "last" ) {
-                               return elem.nodeIndex == parent[ doneName ];
-                       } else if ( type == "only" ) {
-                               return parent[ doneName ] == 1;
-                       } else if ( type == "nth" ) {
-                               var add = false, first = match[2], last = match[3];
-
-                               if ( first == 1 && last == 0 ) {
-                                       return true;
-                               }
-
-                               if ( first == 0 ) {
-                                       if ( elem.nodeIndex == last ) {
-                                               add = true;
-                                       }
-                               } else if ( (elem.nodeIndex - last) % first == 0 && (elem.nodeIndex - last) / first >= 0 ) {
-                                       add = true;
-                               }
-
-                               return add;
-                       }
-               },
                PSEUDO: function(elem, match, i, array){
                        var name = match[1], filter = Expr.filters[ name ];
 
@@ -536,6 +562,49 @@ var Expr = Sizzle.selectors = {
                                return true;
                        }
                },
+               CHILD: function(elem, match){
+                       var type = match[1], node = elem;
+                       switch (type) {
+                               case 'only':
+                               case 'first':
+                                       while ( (node = node.previousSibling) )  {
+                                               if ( node.nodeType === 1 ) return false;
+                                       }
+                                       if ( type == 'first') return true;
+                                       node = elem;
+                               case 'last':
+                                       while ( (node = node.nextSibling) )  {
+                                               if ( node.nodeType === 1 ) return false;
+                                       }
+                                       return true;
+                               case 'nth':
+                                       var first = match[2], last = match[3];
+
+                                       if ( first == 1 && last == 0 ) {
+                                               return true;
+                                       }
+                                       
+                                       var doneName = match[0],
+                                               parent = elem.parentNode;
+       
+                                       if ( parent && (parent.sizcache !== doneName || !elem.nodeIndex) ) {
+                                               var count = 0;
+                                               for ( node = parent.firstChild; node; node = node.nextSibling ) {
+                                                       if ( node.nodeType === 1 ) {
+                                                               node.nodeIndex = ++count;
+                                                       }
+                                               } 
+                                               parent.sizcache = doneName;
+                                       }
+                                       
+                                       var diff = elem.nodeIndex - last;
+                                       if ( first == 0 ) {
+                                               return diff == 0;
+                                       } else {
+                                               return ( diff % first == 0 && diff / first >= 0 );
+                                       }
+                       }
+               },
                ID: function(elem, match){
                        return elem.nodeType === 1 && elem.getAttribute("id") === match;
                },
@@ -543,20 +612,30 @@ var Expr = Sizzle.selectors = {
                        return (match === "*" && elem.nodeType === 1) || elem.nodeName === match;
                },
                CLASS: function(elem, match){
-                       return match.test( elem.className );
+                       return (" " + (elem.className || elem.getAttribute("class")) + " ")
+                               .indexOf( match ) > -1;
                },
                ATTR: function(elem, match){
-                       var result = elem[ match[1] ] || elem.getAttribute( match[1] ), value = result + "", type = match[2], check = match[4];
+                       var name = match[1],
+                               result = Expr.attrHandle[ name ] ?
+                                       Expr.attrHandle[ name ]( elem ) :
+                                       elem[ name ] != null ?
+                                               elem[ name ] :
+                                               elem.getAttribute( name ),
+                               value = result + "",
+                               type = match[2],
+                               check = match[4];
+
                        return result == null ?
-                               false :
+                               type === "!=" :
                                type === "=" ?
                                value === check :
                                type === "*=" ?
                                value.indexOf(check) >= 0 :
                                type === "~=" ?
                                (" " + value + " ").indexOf(check) >= 0 :
-                               !match[4] ?
-                               result :
+                               !check ?
+                               value && result !== false :
                                type === "!=" ?
                                value != check :
                                type === "^=" ?
@@ -577,12 +656,14 @@ var Expr = Sizzle.selectors = {
        }
 };
 
+var origPOS = Expr.match.POS;
+
 for ( var type in Expr.match ) {
-       Expr.match[ type ] = RegExp( Expr.match[ type ].source + /(?![^\[]*\])(?![^\(]*\))/.source );
+       Expr.match[ type ] = new RegExp( Expr.match[ type ].source + /(?![^\[]*\])(?![^\(]*\))/.source );
 }
 
 var makeArray = function(array, results) {
-       array = Array.prototype.slice.call( array );
+       array = Array.prototype.slice.call( array, 0 );
 
        if ( results ) {
                results.push.apply( results, array );
@@ -595,14 +676,14 @@ var makeArray = function(array, results) {
 // Perform a simple check to determine if the browser is capable of
 // converting a NodeList to an array using builtin methods.
 try {
-       Array.prototype.slice.call( document.documentElement.childNodes );
+       Array.prototype.slice.call( document.documentElement.childNodes, 0 );
 
 // Provide a fallback method if it does not work
 } catch(e){
        makeArray = function(array, results) {
                var ret = results || [];
 
-               if ( array instanceof Array ) {
+               if ( toString.call(array) === "[object Array]" ) {
                        Array.prototype.push.apply( ret, array );
                } else {
                        if ( typeof array.length === "number" ) {
@@ -620,13 +701,67 @@ try {
        };
 }
 
+var sortOrder;
+
+if ( document.documentElement.compareDocumentPosition ) {
+       sortOrder = function( a, b ) {
+               if ( !a.compareDocumentPosition || !b.compareDocumentPosition ) {
+                       if ( a == b ) {
+                               hasDuplicate = true;
+                       }
+                       return 0;
+               }
+
+               var ret = a.compareDocumentPosition(b) & 4 ? -1 : a === b ? 0 : 1;
+               if ( ret === 0 ) {
+                       hasDuplicate = true;
+               }
+               return ret;
+       };
+} else if ( "sourceIndex" in document.documentElement ) {
+       sortOrder = function( a, b ) {
+               if ( !a.sourceIndex || !b.sourceIndex ) {
+                       if ( a == b ) {
+                               hasDuplicate = true;
+                       }
+                       return 0;
+               }
+
+               var ret = a.sourceIndex - b.sourceIndex;
+               if ( ret === 0 ) {
+                       hasDuplicate = true;
+               }
+               return ret;
+       };
+} else if ( document.createRange ) {
+       sortOrder = function( a, b ) {
+               if ( !a.ownerDocument || !b.ownerDocument ) {
+                       if ( a == b ) {
+                               hasDuplicate = true;
+                       }
+                       return 0;
+               }
+
+               var aRange = a.ownerDocument.createRange(), bRange = b.ownerDocument.createRange();
+               aRange.selectNode(a);
+               aRange.collapse(true);
+               bRange.selectNode(b);
+               bRange.collapse(true);
+               var ret = aRange.compareBoundaryPoints(Range.START_TO_END, bRange);
+               if ( ret === 0 ) {
+                       hasDuplicate = true;
+               }
+               return ret;
+       };
+}
+
 // Check to see if the browser returns elements by name when
 // querying by getElementById (and provide a workaround)
 (function(){
        // We're going to inject a fake input element with a specified name
-       var form = document.createElement("form"),
+       var form = document.createElement("div"),
                id = "script" + (new Date).getTime();
-       form.innerHTML = "<input name='" + id + "'/>";
+       form.innerHTML = "<a name='" + id + "'/>";
 
        // Inject it into the root element, check its status, and remove it quickly
        var root = document.documentElement;
@@ -635,25 +770,27 @@ try {
        // The workaround has to do additional checks after a getElementById
        // Which slows things down for other browsers (hence the branching)
        if ( !!document.getElementById( id ) ) {
-               Expr.find.ID = function(match, context){
-                       if ( context.getElementById ) {
+               Expr.find.ID = function(match, context, isXML){
+                       if ( typeof context.getElementById !== "undefined" && !isXML ) {
                                var m = context.getElementById(match[1]);
-                               return m ? m.id === match[1] || m.getAttributeNode && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
+                               return m ? m.id === match[1] || typeof m.getAttributeNode !== "undefined" && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
                        }
                };
 
                Expr.filter.ID = function(elem, match){
-                       var node = elem.getAttributeNode && elem.getAttributeNode("id");
+                       var node = typeof elem.getAttributeNode !== "undefined" && elem.getAttributeNode("id");
                        return elem.nodeType === 1 && node && node.nodeValue === match;
                };
        }
 
        root.removeChild( form );
+       root = form = null; // release memory in IE
 })();
 
-// Check to see if the browser returns only elements
-// when doing getElementsByTagName("*")
 (function(){
+       // Check to see if the browser returns only elements
+       // when doing getElementsByTagName("*")
+
        // Create a fake element
        var div = document.createElement("div");
        div.appendChild( document.createComment("") );
@@ -679,52 +816,96 @@ try {
                        return results;
                };
        }
+
+       // Check to see if an attribute returns normalized href attributes
+       div.innerHTML = "<a href='#'></a>";
+       if ( div.firstChild && typeof div.firstChild.getAttribute !== "undefined" &&
+                       div.firstChild.getAttribute("href") !== "#" ) {
+               Expr.attrHandle.href = function(elem){
+                       return elem.getAttribute("href", 2);
+               };
+       }
+
+       div = null; // release memory in IE
 })();
 
 if ( document.querySelectorAll ) (function(){
-       var oldSizzle = Sizzle;
+       var oldSizzle = Sizzle, div = document.createElement("div");
+       div.innerHTML = "<p class='TEST'></p>";
+
+       // Safari can't handle uppercase or unicode characters when
+       // in quirks mode.
+       if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) {
+               return;
+       }
        
        Sizzle = function(query, context, extra, seed){
                context = context || document;
 
-               if ( !seed && context.nodeType === 9 ) {
+               // Only use querySelectorAll on non-XML documents
+               // (ID selectors don't work in non-HTML documents)
+               if ( !seed && context.nodeType === 9 && !isXML(context) ) {
                        try {
-                               return makeArray( context.querySelectorAll(query) );
+                               return makeArray( context.querySelectorAll(query), extra );
                        } catch(e){}
                }
                
                return oldSizzle(query, context, extra, seed);
        };
 
-       Sizzle.find = oldSizzle.find;
-       Sizzle.filter = oldSizzle.filter;
-       Sizzle.selectors = oldSizzle.selectors;
-       Sizzle.matches = oldSizzle.matches;
+       for ( var prop in oldSizzle ) {
+               Sizzle[ prop ] = oldSizzle[ prop ];
+       }
+
+       div = null; // release memory in IE
 })();
 
-if ( document.documentElement.getElementsByClassName ) {
+if ( document.getElementsByClassName && document.documentElement.getElementsByClassName ) (function(){
+       var div = document.createElement("div");
+       div.innerHTML = "<div class='test e'></div><div class='test'></div>";
+
+       // Opera can't find a second classname (in 9.6)
+       if ( div.getElementsByClassName("e").length === 0 )
+               return;
+
+       // Safari caches class attributes, doesn't catch changes (in 3.2)
+       div.lastChild.className = "e";
+
+       if ( div.getElementsByClassName("e").length === 1 )
+               return;
+
        Expr.order.splice(1, 0, "CLASS");
-       Expr.find.CLASS = function(match, context) {
-               return context.getElementsByClassName(match[1]);
+       Expr.find.CLASS = function(match, context, isXML) {
+               if ( typeof context.getElementsByClassName !== "undefined" && !isXML ) {
+                       return context.getElementsByClassName(match[1]);
+               }
        };
-}
 
-function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck ) {
+       div = null; // release memory in IE
+})();
+
+function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
+       var sibDir = dir == "previousSibling" && !isXML;
        for ( var i = 0, l = checkSet.length; i < l; i++ ) {
                var elem = checkSet[i];
                if ( elem ) {
+                       if ( sibDir && elem.nodeType === 1 ){
+                               elem.sizcache = doneName;
+                               elem.sizset = i;
+                       }
                        elem = elem[dir];
                        var match = false;
 
-                       while ( elem && elem.nodeType ) {
-                               var done = elem[doneName];
-                               if ( done ) {
-                                       match = checkSet[ done ];
+                       while ( elem ) {
+                               if ( elem.sizcache === doneName ) {
+                                       match = checkSet[elem.sizset];
                                        break;
                                }
 
-                               if ( elem.nodeType === 1 )
-                                       elem[doneName] = i;
+                               if ( elem.nodeType === 1 && !isXML ){
+                                       elem.sizcache = doneName;
+                                       elem.sizset = i;
+                               }
 
                                if ( elem.nodeName === cur ) {
                                        match = elem;
@@ -739,22 +920,29 @@ function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck ) {
        }
 }
 
-function dirCheck( dir, cur, doneName, checkSet, nodeCheck ) {
+function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
+       var sibDir = dir == "previousSibling" && !isXML;
        for ( var i = 0, l = checkSet.length; i < l; i++ ) {
                var elem = checkSet[i];
                if ( elem ) {
+                       if ( sibDir && elem.nodeType === 1 ) {
+                               elem.sizcache = doneName;
+                               elem.sizset = i;
+                       }
                        elem = elem[dir];
                        var match = false;
 
-                       while ( elem && elem.nodeType ) {
-                               if ( elem[doneName] ) {
-                                       match = checkSet[ elem[doneName] ];
+                       while ( elem ) {
+                               if ( elem.sizcache === doneName ) {
+                                       match = checkSet[elem.sizset];
                                        break;
                                }
 
                                if ( elem.nodeType === 1 ) {
-                                       elem[doneName] = i;
-
+                                       if ( !isXML ) {
+                                               elem.sizcache = doneName;
+                                               elem.sizset = i;
+                                       }
                                        if ( typeof cur !== "string" ) {
                                                if ( elem === cur ) {
                                                        match = true;
@@ -781,69 +969,36 @@ var contains = document.compareDocumentPosition ?  function(a, b){
        return a !== b && (a.contains ? a.contains(b) : true);
 };
 
-// EXPOSE
-jQuery.find = Sizzle;
-jQuery.filter = Sizzle.filter;
-jQuery.expr = Sizzle.selectors;
-jQuery.expr[":"] = jQuery.expr.filters;
-
-Sizzle.selectors.filters.hidden = function(elem){
-       return "hidden" === elem.type ||
-               jQuery.css(elem, "display") === "none" ||
-               jQuery.css(elem, "visibility") === "hidden";
-};
-
-Sizzle.selectors.filters.visible = function(elem){
-       return "hidden" !== elem.type &&
-               jQuery.css(elem, "display") !== "none" &&
-               jQuery.css(elem, "visibility") !== "hidden";
+var isXML = function(elem){
+       return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
+               !!elem.ownerDocument && elem.ownerDocument.documentElement.nodeName !== "HTML";
 };
 
-Sizzle.selectors.filters.animated = function(elem){
-       return jQuery.grep(jQuery.timers, function(fn){
-               return elem === fn.elem;
-       }).length;
-};
+var posProcess = function(selector, context){
+       var tmpSet = [], later = "", match,
+               root = context.nodeType ? [context] : context;
 
-jQuery.multiFilter = function( expr, elems, not ) {
-       if ( not ) {
-               expr = ":not(" + expr + ")";
+       // Position selectors must be done after the filter
+       // And so must :not(positional) so we move all PSEUDOs to the end
+       while ( (match = Expr.match.PSEUDO.exec( selector )) ) {
+               later += match[0];
+               selector = selector.replace( Expr.match.PSEUDO, "" );
        }
 
-       return Sizzle.matches(expr, elems);
-};
+       selector = Expr.relative[selector] ? selector + "*" : selector;
 
-jQuery.dir = function( elem, dir ){
-       var matched = [], cur = elem[dir];
-       while ( cur && cur != document ) {
-               if ( cur.nodeType == 1 )
-                       matched.push( cur );
-               cur = cur[dir];
+       for ( var i = 0, l = root.length; i < l; i++ ) {
+               Sizzle( selector, root[i], tmpSet );
        }
-       return matched;
-};
-
-jQuery.nth = function(cur, result, dir, elem){
-       result = result || 1;
-       var num = 0;
 
-       for ( ; cur; cur = cur[dir] )
-               if ( cur.nodeType == 1 && ++num == result )
-                       break;
-
-       return cur;
+       return Sizzle.filter( later, tmpSet );
 };
 
-jQuery.sibling = function(n, elem){
-       var r = [];
-
-       for ( ; n; n = n.nextSibling ) {
-               if ( n.nodeType == 1 && n != elem )
-                       r.push( n );
-       }
-
-       return r;
-};
+// EXPOSE
+jQuery.find = Sizzle;
+jQuery.expr = Sizzle.selectors;
+jQuery.expr[":"] = jQuery.expr.filters;
+jQuery.unique = Sizzle.uniqueSort;
 
 return;