Tagged the Sizzle 1.0 release.
[jquery.git] / src / selector.js
index 505198b..a0b4077 100644 (file)
@@ -1,5 +1,5 @@
 /*!
- * Sizzle CSS Selector Engine - v0.9.3
+ * Sizzle CSS Selector Engine - v1.0
  *  Copyright 2009, The Dojo Foundation
  *  Released under the MIT, BSD, and GPL Licenses.
  *  More information: http://sizzlejs.com/
@@ -165,7 +165,8 @@ Sizzle.find = function(expr, context, isXML){
 };
 
 Sizzle.filter = function(expr, set, inplace, not){
-       var old = expr, result = [], curLoop = set, match, anyFound;
+       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 ) {
@@ -178,7 +179,7 @@ Sizzle.filter = function(expr, set, inplace, not){
                                }
 
                                if ( Expr.preFilter[ type ] ) {
-                                       match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not );
+                                       match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not, isXMLFilter );
 
                                        if ( !match ) {
                                                anyFound = found = true;
@@ -357,9 +358,13 @@ var Expr = Sizzle.selectors = {
                }
        },
        preFilter: {
-               CLASS: function(match, curLoop, inplace, result, not){
+               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) ) {
@@ -397,10 +402,10 @@ var Expr = Sizzle.selectors = {
 
                        return match;
                },
-               ATTR: function(match){
+               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];
                        }
 
@@ -588,7 +593,8 @@ 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 name = match[1],
@@ -678,18 +684,15 @@ try {
 
 var sortOrder;
 
-if ( Array.prototype.indexOf ) {
-       var indexOf = Array.prototype.indexOf,
-               allSort = document.getElementsByTagName("*");
-
+if ( document.documentElement.compareDocumentPosition ) {
        sortOrder = function( a, b ) {
-               var ret = indexOf.call( allSort, a ) - indexOf.call( allSort, b );
+               var ret = a.compareDocumentPosition(b) & 4 ? -1 : a === b ? 0 : 1;
                if ( ret === 0 ) {
                        hasDuplicate = true;
                }
                return ret;
        };
-} else if ( document.documentElement.sourceIndex === 1 ) {
+} else if ( "sourceIndex" in document.documentElement ) {
        sortOrder = function( a, b ) {
                var ret = a.sourceIndex - b.sourceIndex;
                if ( ret === 0 ) {
@@ -697,6 +700,19 @@ if ( Array.prototype.indexOf ) {
                }
                return ret;
        };
+} else if ( document.createRange ) {
+       sortOrder = function( a, b ) {
+               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
@@ -815,8 +831,10 @@ if ( document.getElementsByClassName && document.documentElement.getElementsByCl
                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]);
+               }
        };
 })();
 
@@ -932,7 +950,6 @@ var posProcess = function(selector, context){
 
 // EXPOSE
 jQuery.find = Sizzle;
-jQuery.filter = Sizzle.filter;
 jQuery.expr = Sizzle.selectors;
 jQuery.expr[":"] = jQuery.expr.filters;
 
@@ -950,7 +967,7 @@ Sizzle.selectors.filters.animated = function(elem){
        }).length;
 };
 
-jQuery.multiFilter = function( expr, elems, not ) {
+jQuery.filter = jQuery.multiFilter = function( expr, elems, not ) {
        if ( not ) {
                expr = ":not(" + expr + ")";
        }