Standardize on using .nodeName in place of .tagName. Fixes jQuery bug #4923.
[jquery.git] / src / selector.js
index 48aeea5..c6fc025 100644 (file)
@@ -8,7 +8,8 @@
 
 var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|['"][^'"]*['"]|[^[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?/g,
        done = 0,
-       toString = Object.prototype.toString;
+       toString = Object.prototype.toString,
+       hasDuplicate = false;
 
 var Sizzle = function(selector, context, results, seed) {
        results = results || [];
@@ -65,7 +66,7 @@ var Sizzle = function(selector, context, results, seed) {
                if ( context ) {
                        var ret = seed ?
                                { expr: parts.pop(), set: makeArray(seed) } :
-                               Sizzle.find( parts.pop(), parts.length === 1 && context.parentNode ? context.parentNode : context, contextXML );
+                               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 ( parts.length > 0 ) {
@@ -331,7 +332,7 @@ var Expr = Sizzle.selectors = {
                "": function(checkSet, part, isXML){
                        var doneName = done++, checkFn = dirCheck;
 
-                       if ( !part.match(/\W/) ) {
+                       if ( !/\W/.test(part) ) {
                                var nodeCheck = part = isXML ? part : part.toUpperCase();
                                checkFn = dirNodeCheck;
                        }
@@ -341,7 +342,7 @@ var Expr = Sizzle.selectors = {
                "~": function(checkSet, part, isXML){
                        var doneName = done++, checkFn = dirCheck;
 
-                       if ( typeof part === "string" && !part.match(/\W/) ) {
+                       if ( typeof part === "string" && !/\W/.test(part) ) {
                                var nodeCheck = part = isXML ? part : part.toUpperCase();
                                checkFn = dirNodeCheck;
                        }
@@ -434,7 +435,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 || /^\w/.test(match[3]) ) {
+                               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);
@@ -550,7 +551,7 @@ var Expr = Sizzle.selectors = {
                        } else if ( name === "not" ) {
                                var not = match[3];
 
-                               for ( var i = 0, l = not.length; i < l; i++ ) {
+                               for ( i = 0, l = not.length; i < l; i++ ) {
                                        if ( not[i] === elem ) {
                                                return false;
                                        }
@@ -564,13 +565,13 @@ var Expr = Sizzle.selectors = {
                        switch (type) {
                                case 'only':
                                case 'first':
-                                       while (node = node.previousSibling)  {
+                                       while ( (node = node.previousSibling) )  {
                                                if ( node.nodeType === 1 ) return false;
                                        }
                                        if ( type == 'first') return true;
                                        node = elem;
                                case 'last':
-                                       while (node = node.nextSibling)  {
+                                       while ( (node = node.nextSibling) )  {
                                                if ( node.nodeType === 1 ) return false;
                                        }
                                        return true;
@@ -656,7 +657,7 @@ 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) {
@@ -735,9 +736,9 @@ if ( document.documentElement.compareDocumentPosition ) {
 // 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;
@@ -760,6 +761,7 @@ if ( document.documentElement.compareDocumentPosition ) {
        }
 
        root.removeChild( form );
+       root = form = null; // release memory in IE
 })();
 
 (function(){
@@ -800,6 +802,8 @@ if ( document.documentElement.compareDocumentPosition ) {
                        return elem.getAttribute("href", 2);
                };
        }
+
+       div = null; // release memory in IE
 })();
 
 if ( document.querySelectorAll ) (function(){
@@ -826,10 +830,11 @@ if ( document.querySelectorAll ) (function(){
                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.getElementsByClassName && document.documentElement.getElementsByClassName ) (function(){
@@ -852,6 +857,8 @@ if ( document.getElementsByClassName && document.documentElement.getElementsByCl
                        return context.getElementsByClassName(match[1]);
                }
        };
+
+       div = null; // release memory in IE
 })();
 
 function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
@@ -941,7 +948,7 @@ var contains = document.compareDocumentPosition ?  function(a, b){
 
 var isXML = function(elem){
        return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
-               !!elem.ownerDocument && isXML( elem.ownerDocument );
+               !!elem.ownerDocument && elem.ownerDocument.documentElement.nodeName !== "HTML";
 };
 
 var posProcess = function(selector, context){
@@ -970,11 +977,17 @@ jQuery.expr = Sizzle.selectors;
 jQuery.expr[":"] = jQuery.expr.filters;
 
 Sizzle.selectors.filters.hidden = function(elem){
-       return elem.offsetWidth === 0 || elem.offsetHeight === 0;
+       var width = elem.offsetWidth, height = elem.offsetHeight,
+                force = /^tr$/i.test( elem.nodeName ); // ticket #4512
+       return ( width === 0 && height === 0 && !force ) ?
+               true :
+                       ( width !== 0 && height !== 0 && !force ) ?
+                               false :
+                                       !!( jQuery.curCSS(elem, "display") === "none" );
 };
 
 Sizzle.selectors.filters.visible = function(elem){
-       return elem.offsetWidth > 0 || elem.offsetHeight > 0;
+       return !Sizzle.selectors.filters.hidden(elem);
 };
 
 Sizzle.selectors.filters.animated = function(elem){