Make sure that [name=FOO] searches actually have the specified name (IE includes...
[jquery.git] / src / selector.js
index 331ad3c..4177672 100644 (file)
@@ -111,6 +111,19 @@ var Sizzle = function(selector, context, results, seed) {
 
        if ( extra ) {
                Sizzle( extra, context, results, seed );
+
+               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;
@@ -210,8 +223,6 @@ Sizzle.filter = function(expr, set, inplace, not){
                        }
                }
 
-               expr = expr.replace(/\s*,\s*/, "");
-
                // Improper expression
                if ( expr == old ) {
                        if ( anyFound == null ) {
@@ -320,8 +331,16 @@ var Expr = Sizzle.selectors = {
                        }
                },
                NAME: function(match, context, isXML){
-                       if ( typeof context.getElementsByName !== "undefined" && !isXML ) {
-                               return context.getElementsByName(match[1]);
+                       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){
@@ -385,7 +404,7 @@ var Expr = Sizzle.selectors = {
                PSEUDO: function(match, curLoop, inplace, result, not){
                        if ( match[1] === "not" ) {
                                // If we're dealing with a complex expression, or a simple one
-                               if ( match[3].match(chunker).length > 1 ) {
+                               if ( match[3].match(chunker).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);
@@ -648,6 +667,29 @@ try {
        };
 }
 
+var sortOrder;
+
+if ( Array.prototype.indexOf ) {
+       var indexOf = Array.prototype.indexOf,
+               allSort = document.getElementsByTagName("*");
+
+       sortOrder = function( a, b ) {
+               var ret = indexOf.call( allSort, a ) - indexOf.call( allSort, b );
+               if ( ret === 0 ) {
+                       hasDuplicate = true;
+               }
+               return ret;
+       };
+} else if ( document.documentElement.sourceIndex === 1 ) {
+       sortOrder = function( a, b ) {
+               var ret = a.sourceIndex - b.sourceIndex;
+               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(){