Added some additional checks to make sure that the correct methods are being used...
[jquery.git] / src / selector.js
index 56c8a2a..6b9152d 100644 (file)
@@ -1,12 +1,12 @@
 /*!
- * Sizzle CSS Selector Engine - v0.9.1
+ * Sizzle CSS Selector Engine - v0.9.2
  *  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 chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|['"][^'"]+['"]|[^[\]'"]+)+\]|\\.|[^ >+~,(\[]+)+|[>+~])(\s*,\s*)?/g,
        done = 0,
        toString = Object.prototype.toString;
 
@@ -35,40 +35,27 @@ 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( /\s$/.test(selector) ? selector + "*" : 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 );
+                       Sizzle.find( parts.pop(), parts.length === 1 && context.parentNode ? context.parentNode : context, isXML(context) );
                set = Sizzle.filter( ret.expr, ret.set );
 
                if ( parts.length > 0 ) {
@@ -133,7 +120,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 ) {
@@ -148,7 +135,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;
@@ -170,7 +157,7 @@ Sizzle.filter = function(expr, set, inplace, not){
        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 ) {
@@ -184,26 +171,13 @@ Sizzle.filter = function(expr, set, inplace, not){
                                                anyFound = found = true;
                                        } else if ( match === true ) {
                                                continue;
-                                       } else 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;
-                                                       }
-                                               }
                                        }
                                }
 
                                if ( match ) {
-                                       for ( var i = 0; (item = curLoop[i]) !== undefined; i++ ) {
+                                       for ( var i = 0; (item = curLoop[i]) != null; i++ ) {
                                                if ( item ) {
-                                                       if ( goodArray && item != goodArray[goodPos] ) {
-                                                               goodPos++;
-                                                       }
-       
-                                                       found = filter( item, match, goodPos, goodArray );
+                                                       found = filter( item, match, i, curLoop );
                                                        var pass = not ^ !!found;
 
                                                        if ( inplace && found != null ) {
@@ -341,14 +315,16 @@ var Expr = Sizzle.selectors = {
                }
        },
        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" && !isXML ) {
+                               return context.getElementsByName(match[1]);
+                       }
                },
                TAG: function(match, context){
                        return context.getElementsByTagName(match[1]);
@@ -358,12 +334,15 @@ var Expr = Sizzle.selectors = {
                CLASS: function(match, curLoop, inplace, result, not){
                        match = " " + match[1].replace(/\\/g, "") + " ";
 
-                       for ( var i = 0; curLoop[i]; i++ ) {
-                               if ( not ^ (" " + curLoop[i].className + " ").indexOf(match) >= 0 ) {
-                                       if ( !inplace )
-                                               result.push( curLoop[i] );
-                               } else if ( inplace ) {
-                                       curLoop[i] = false;
+                       var elem;
+                       for ( var i = 0; (elem = curLoop[i]) != null; i++ ) {
+                               if ( elem ) {
+                                       if ( not ^ (" " + elem.className + " ").indexOf(match) >= 0 ) {
+                                               if ( !inplace )
+                                                       result.push( elem );
+                                       } else if ( inplace ) {
+                                               curLoop[i] = false;
+                                       }
                                }
                        }
 
@@ -373,8 +352,8 @@ var Expr = Sizzle.selectors = {
                        return match[1].replace(/\\/g, "");
                },
                TAG: function(match, curLoop){
-                       for ( var i = 0; !curLoop[i]; i++ ){}
-                       return isXML(curLoop[i]) ? match[1] : match[1].toUpperCase();
+                       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" ) {
@@ -394,7 +373,7 @@ var Expr = Sizzle.selectors = {
                        return match;
                },
                ATTR: function(match){
-                       var name = match[1];
+                       var name = match[1].replace(/\\/g, "");
                        
                        if ( Expr.attrMap[name] ) {
                                match[1] = Expr.attrMap[name];
@@ -518,7 +497,7 @@ var Expr = Sizzle.selectors = {
                CHILD: function(elem, match){
                        var type = match[1], parent = elem.parentNode;
 
-                       var doneName = "child" + parent.childNodes.length;
+                       var doneName = match[0];
                        
                        if ( parent && (!parent[ doneName ] || !elem.nodeIndex) ) {
                                var count = 1;
@@ -587,7 +566,7 @@ var Expr = Sizzle.selectors = {
                ATTR: function(elem, match){
                        var result = Expr.attrHandle[ match[1] ] ? Expr.attrHandle[ match[1] ]( elem ) : elem[ match[1] ] || elem.getAttribute( match[1] ), value = result + "", type = match[2], check = match[4];
                        return result == null ?
-                               false :
+                               type === "!=" :
                                type === "=" ?
                                value === check :
                                type === "*=" ?
@@ -616,6 +595,8 @@ var Expr = Sizzle.selectors = {
        }
 };
 
+var origPOS = Expr.match.POS;
+
 for ( var type in Expr.match ) {
        Expr.match[ type ] = RegExp( Expr.match[ type ].source + /(?![^\[]*\])(?![^\(]*\))/.source );
 }
@@ -674,15 +655,15 @@ 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;
                };
        }
@@ -722,7 +703,7 @@ try {
 
        // Check to see if an attribute returns normalized href attributes
        div.innerHTML = "<a href='#'></a>";
-       if ( div.firstChild.getAttribute("href") !== "#" ) {
+       if ( div.firstChild && div.firstChild.getAttribute("href") !== "#" ) {
                Expr.attrHandle.href = function(elem){
                        return elem.getAttribute("href", 2);
                };
@@ -730,12 +711,21 @@ try {
 })();
 
 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), extra );
                        } catch(e){}
@@ -750,7 +740,7 @@ if ( document.querySelectorAll ) (function(){
        Sizzle.matches = oldSizzle.matches;
 })();
 
-if ( document.documentElement.getElementsByClassName ) {
+if ( document.getElementsByClassName && document.documentElement.getElementsByClassName ) {
        Expr.order.splice(1, 0, "CLASS");
        Expr.find.CLASS = function(match, context) {
                return context.getElementsByClassName(match[1]);
@@ -831,8 +821,28 @@ var contains = document.compareDocumentPosition ?  function(a, b){
 };
 
 var isXML = function(elem){
-       return elem.documentElement && !elem.body ||
-               elem.tagName && elem.ownerDocument && !elem.ownerDocument.body;
+       return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
+               !!elem.ownerDocument && isXML( elem.ownerDocument );
+};
+
+var posProcess = function(selector, context){
+       var tmpSet = [], later = "", match,
+               root = context.nodeType ? [context] : context;
+
+       // 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, "" );
+       }
+
+       selector = Expr.relative[selector] ? selector + "*" : selector;
+
+       for ( var i = 0, l = root.length; i < l; i++ ) {
+               Sizzle( selector, root[i], tmpSet );
+       }
+
+       return Sizzle.filter( later, tmpSet );
 };
 
 // EXPOSE