Added support for class selectors and class attribute selectors on XML documents...
[jquery.git] / src / selector.js
index a250746..c97ba61 100644 (file)
@@ -1,12 +1,12 @@
 /*!
- * Sizzle CSS Selector Engine - v0.9.1
+ * Sizzle CSS Selector Engine - v0.9.3
  *  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;
 
@@ -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;
@@ -152,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 ) {
@@ -165,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;
@@ -210,8 +224,6 @@ Sizzle.filter = function(expr, set, inplace, not){
                        }
                }
 
-               expr = expr.replace(/\s*,\s*/, "");
-
                // Improper expression
                if ( expr == old ) {
                        if ( anyFound == null ) {
@@ -249,26 +261,33 @@ var Expr = Sizzle.selectors = {
                }
        },
        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, isXML){
-                       if ( typeof part === "string" && !/\W/.test(part) ) {
+                       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++ ) {
@@ -282,19 +301,19 @@ 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, isXML){
-                       var doneName = "done" + (done++), checkFn = dirCheck;
+                       var doneName = done++, checkFn = dirCheck;
 
                        if ( !part.match(/\W/) ) {
                                var nodeCheck = part = isXML ? part : part.toUpperCase();
@@ -304,7 +323,7 @@ var Expr = Sizzle.selectors = {
                        checkFn("parentNode", part, doneName, checkSet, nodeCheck, isXML);
                },
                "~": function(checkSet, part, isXML){
-                       var doneName = "done" + (done++), checkFn = dirCheck;
+                       var doneName = done++, checkFn = dirCheck;
 
                        if ( typeof part === "string" && !part.match(/\W/) ) {
                                var nodeCheck = part = isXML ? part : part.toUpperCase();
@@ -322,8 +341,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){
@@ -331,13 +358,16 @@ 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, "") + " ";
 
-                       var elem;
-                       for ( var i = 0; (elem = curLoop[i]) != null; i++ ) {
+                       if ( isXML ) {
+                               return match;
+                       }
+
+                       for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) {
                                if ( elem ) {
-                                       if ( not ^ (" " + elem.className + " ").indexOf(match) >= 0 ) {
+                                       if ( not ^ (elem.className && (" " + elem.className + " ").indexOf(match) >= 0) ) {
                                                if ( !inplace )
                                                        result.push( elem );
                                        } else if ( inplace ) {
@@ -368,14 +398,14 @@ var Expr = Sizzle.selectors = {
                        }
 
                        // TODO: Move to normal caching system
-                       match[0] = "done" + (done++);
+                       match[0] = done++;
 
                        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];
                        }
 
@@ -388,7 +418,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);
@@ -397,7 +427,7 @@ var Expr = Sizzle.selectors = {
                                        }
                                        return false;
                                }
-                       } else if ( Expr.match.POS.test( match[0] ) ) {
+                       } else if ( Expr.match.POS.test( match[0] ) || Expr.match.CHILD.test( match[0] ) ) {
                                return true;
                        }
                        
@@ -494,47 +524,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 ] || !elem.nodeIndex) ) {
-                               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 ];
 
@@ -554,6 +543,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;
                },
@@ -561,10 +593,20 @@ 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 = Expr.attrHandle[ match[1] ] ? Expr.attrHandle[ match[1] ]( elem ) : 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 ?
                                type === "!=" :
                                type === "=" ?
@@ -573,8 +615,8 @@ var Expr = Sizzle.selectors = {
                                value.indexOf(check) >= 0 :
                                type === "~=" ?
                                (" " + value + " ").indexOf(check) >= 0 :
-                               !match[4] ?
-                               result :
+                               !check ?
+                               value && result !== false :
                                type === "!=" ?
                                value != check :
                                type === "^=" ?
@@ -640,6 +682,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(){
@@ -655,15 +720,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;
                };
        }
@@ -703,7 +768,8 @@ try {
 
        // Check to see if an attribute returns normalized href attributes
        div.innerHTML = "<a href='#'></a>";
-       if ( div.firstChild && div.firstChild.getAttribute("href") !== "#" ) {
+       if ( div.firstChild && typeof div.firstChild.getAttribute !== "undefined" &&
+                       div.firstChild.getAttribute("href") !== "#" ) {
                Expr.attrHandle.href = function(elem){
                        return elem.getAttribute("href", 2);
                };
@@ -723,7 +789,9 @@ if ( document.querySelectorAll ) (function(){
        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){}
@@ -738,29 +806,50 @@ if ( document.querySelectorAll ) (function(){
        Sizzle.matches = oldSizzle.matches;
 })();
 
-if ( document.getElementsByClassName && 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, 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 && !isXML )
-                                       elem[doneName] = i;
+                               if ( elem.nodeType === 1 && !isXML ){
+                                       elem.sizcache = doneName;
+                                       elem.sizset = i;
+                               }
 
                                if ( elem.nodeName === cur ) {
                                        match = elem;
@@ -776,22 +865,28 @@ function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
 }
 
 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 ) {
-                                       if ( !isXML )
-                                               elem[doneName] = i;
-
+                                       if ( !isXML ) {
+                                               elem.sizcache = doneName;
+                                               elem.sizset = i;
+                                       }
                                        if ( typeof cur !== "string" ) {
                                                if ( elem === cur ) {
                                                        match = true;
@@ -850,15 +945,11 @@ 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";
+       return elem.offsetWidth === 0 || elem.offsetHeight === 0;
 };
 
 Sizzle.selectors.filters.visible = function(elem){
-       return "hidden" !== elem.type &&
-               jQuery.css(elem, "display") !== "none" &&
-               jQuery.css(elem, "visibility") !== "hidden";
+       return elem.offsetWidth > 0 || elem.offsetHeight > 0;
 };
 
 Sizzle.selectors.filters.animated = function(elem){