Added some significant speed-ups to height/width checks, thanks to some code and...
[jquery.git] / src / core.js
index 24438e3..508060a 100644 (file)
@@ -47,20 +47,16 @@ jQuery.fn = jQuery.prototype = {
                                else {
                                        var elem = document.getElementById( match[3] );
 
-                                       // Make sure an element was located
-                                       if ( elem ){
-                                               // Handle the case where IE and Opera return items
-                                               // by name instead of ID
-                                               if ( elem.id != match[3] )
-                                                       return jQuery().find( selector );
-
-                                               // Otherwise, we inject the element directly into the jQuery object
-                                               var ret = jQuery( elem );
-                                               ret.context = document;
-                                               ret.selector = selector;
-                                               return ret;
-                                       }
-                                       selector = [];
+                                       // Handle the case where IE and Opera return items
+                                       // by name instead of ID
+                                       if ( elem && elem.id != match[3] )
+                                               return jQuery().find( selector );
+
+                                       // Otherwise, we inject the element directly into the jQuery object
+                                       var ret = jQuery( elem || [] );
+                                       ret.context = document;
+                                       ret.selector = selector;
+                                       return ret;
                                }
 
                        // HANDLE: $(expr, [context])
@@ -206,20 +202,22 @@ jQuery.fn = jQuery.prototype = {
        },
 
        wrapAll: function( html ) {
-               if ( this[0] )
+               if ( this[0] ) {
                        // The elements to wrap the target around
-                       jQuery( html, this[0].ownerDocument )
-                               .clone()
-                               .insertBefore( this[0] )
-                               .map(function(){
-                                       var elem = this;
+                       var wrap = jQuery( html, this[0].ownerDocument ).clone();
+
+                       if ( this[0].parentNode )
+                               wrap.insertBefore( this[0] );
 
-                                       while ( elem.firstChild )
-                                               elem = elem.firstChild;
+                       wrap.map(function(){
+                               var elem = this;
 
-                                       return elem;
-                               })
-                               .append(this);
+                               while ( elem.firstChild )
+                                       elem = elem.firstChild;
+
+                               return elem;
+                       }).append(this);
+               }
 
                return this;
        },
@@ -266,6 +264,8 @@ jQuery.fn = jQuery.prototype = {
                return this.prevObject || jQuery( [] );
        },
 
+       // For internal use only.
+       // Behaves like an Array's .push method, not like a jQuery method.
        push: [].push,
 
        find: function( selector ) {
@@ -297,33 +297,37 @@ jQuery.fn = jQuery.prototype = {
                                // attributes in IE that are actually only stored
                                // as properties will not be copied (such as the
                                // the name attribute on an input).
-                               var clone = this.cloneNode(true),
-                                       container = document.createElement("div");
-                               container.appendChild(clone);
-                               return jQuery.clean([container.innerHTML])[0];
+                               var html = this.outerHTML;
+                               if ( !html ) {
+                                       var div = this.ownerDocument.createElement("div");
+                                       div.appendChild( this.cloneNode(true) );
+                                       html = div.innerHTML;
+                               }
+
+                               return jQuery.clean([html.replace(/ jQuery\d+="(?:\d+|null)"/g, "").replace(/^\s*/, "")])[0];
                        } else
                                return this.cloneNode(true);
                });
 
-               // Need to set the expando to null on the cloned set if it exists
-               // removeData doesn't work here, IE removes it from the original as well
-               // this is primarily for IE but the data expando shouldn't be copied over in any browser
-               var clone = ret.find("*").andSelf().each(function(){
-                       if ( this[ expando ] !== undefined )
-                               this[ expando ] = null;
-               });
-
                // Copy the events from the original to the clone
-               if ( events === true )
-                       this.find("*").andSelf().each(function(i){
-                               if (this.nodeType == 3)
+               if ( events === true ) {
+                       var orig = this.find("*").andSelf(), i = 0;
+
+                       ret.find("*").andSelf().each(function(){
+                               if ( this.nodeName !== orig[i].nodeName )
                                        return;
-                               var events = jQuery.data( this, "events" );
 
-                               for ( var type in events )
-                                       for ( var handler in events[ type ] )
-                                               jQuery.event.add( clone[ i ], type, events[ type ][ handler ], events[ type ][ handler ].data );
+                               var events = jQuery.data( orig[i], "events" );
+
+                               for ( var type in events ) {
+                                       for ( var handler in events[ type ] ) {
+                                               jQuery.event.add( this, type, events[ type ][ handler ], events[ type ][ handler ].data );
+                                       }
+                               }
+
+                               i++;
                        });
+               }
 
                // Return the cloned set
                return ret;
@@ -342,14 +346,18 @@ jQuery.fn = jQuery.prototype = {
        },
 
        closest: function( selector ) {
-               var pos = jQuery.expr.match.POS.test( selector ) ? jQuery(selector) : null;
+               var pos = jQuery.expr.match.POS.test( selector ) ? jQuery(selector) : null,
+                       closer = 0;
 
                return this.map(function(){
                        var cur = this;
                        while ( cur && cur.ownerDocument ) {
-                               if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selector) )
+                               if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selector) ) {
+                                       jQuery.data(cur, "closest", closer);
                                        return cur;
+                               }
                                cur = cur.parentNode;
+                               closer++;
                        }
                });
        },
@@ -462,7 +470,7 @@ jQuery.fn = jQuery.prototype = {
        html: function( value ) {
                return value === undefined ?
                        (this[0] ?
-                               this[0].innerHTML :
+                               this[0].innerHTML.replace(/ jQuery\d+="(?:\d+|null)"/g, "") :
                                null) :
                        this.empty().append( value );
        },
@@ -617,15 +625,13 @@ jQuery.extend({
 
        // check if an element is in a (or is an) XML document
        isXMLDoc: function( elem ) {
-               return elem.documentElement && !elem.body ||
-                       elem.tagName && elem.ownerDocument && !elem.ownerDocument.body;
+               return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
+                       !!elem.ownerDocument && jQuery.isXMLDoc( elem.ownerDocument );
        },
 
        // Evalulates a script in a global context
        globalEval: function( data ) {
-               data = jQuery.trim( data );
-
-               if ( data ) {
+               if ( data && /\S/.test(data) ) {
                        // Inspired by code by Andrea Giammarchi
                        // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
                        var head = document.getElementsByTagName("head")[0] || document.documentElement,
@@ -708,7 +714,7 @@ jQuery.extend({
 
                // internal only, use hasClass("class")
                has: function( elem, className ) {
-                       return jQuery.inArray( className, (elem.className || elem).toString().split(/\s+/) ) > -1;
+                       return elem && jQuery.inArray( className, (elem.className || elem).toString().split(/\s+/) ) > -1;
                }
        },
 
@@ -728,26 +734,32 @@ jQuery.extend({
                        elem.style[ name ] = old[ name ];
        },
 
-       css: function( elem, name, force ) {
+       css: function( elem, name, force, extra ) {
                if ( name == "width" || name == "height" ) {
                        var val, props = { position: "absolute", visibility: "hidden", display:"block" }, which = name == "width" ? [ "Left", "Right" ] : [ "Top", "Bottom" ];
 
                        function getWH() {
                                val = name == "width" ? elem.offsetWidth : elem.offsetHeight;
-                               var padding = 0, border = 0;
+
+                               if ( extra === "border" )
+                                       return;
+
                                jQuery.each( which, function() {
-                                       padding += parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
-                                       border += parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
+                                       if ( !extra )
+                                               val -= parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
+                                       if ( extra === "margin" )
+                                               val += parseFloat(jQuery.curCSS( elem, "margin" + this, true)) || 0;
+                                       else
+                                               val -= parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
                                });
-                               val -= Math.round(padding + border);
                        }
 
-                       if ( jQuery(elem).is(":visible") )
+                       if ( elem.offsetWidth !== 0 )
                                getWH();
                        else
                                jQuery.swap( elem, props, getWH );
 
-                       return Math.max(0, val);
+                       return Math.max(0, Math.round(val));
                }
 
                return jQuery.curCSS( elem, name, force );
@@ -853,7 +865,7 @@ jQuery.extend({
                                });
 
                                // Trim whitespace, otherwise indexOf won't work as expected
-                               var tags = jQuery.trim( elem ).toLowerCase();
+                               var tags = elem.replace(/^\s+/, "").substring(0, 10).toLowerCase();
 
                                var wrap =
                                        // option or optgroup
@@ -893,11 +905,12 @@ jQuery.extend({
                                if ( !jQuery.support.tbody ) {
 
                                        // String was a <table>, *may* have spurious <tbody>
-                                       var tbody = !tags.indexOf("<table") && tags.indexOf("<tbody") < 0 ?
-                                               div.firstChild && div.firstChild.childNodes :
+                                       var hasBody = /<tbody/i.test(elem),
+                                               tbody = !tags.indexOf("<table") && !hasBody ?
+                                                       div.firstChild && div.firstChild.childNodes :
 
                                                // String was a bare <thead> or <tfoot>
-                                               wrap[1] == "<table>" && tags.indexOf("<tbody") < 0 ?
+                                               wrap[1] == "<table>" && !hasBody ?
                                                        div.childNodes :
                                                        [];
 
@@ -923,7 +936,7 @@ jQuery.extend({
 
                if ( fragment ) {
                        for ( var i = 0; ret[i]; i++ ) {
-                               if ( jQuery.nodeName( ret[i], "script" ) ) {
+                               if ( jQuery.nodeName( ret[i], "script" ) && (!ret[i].type || ret[i].type.toLowerCase() === "text/javascript") ) {
                                        scripts.push( ret[i].parentNode ? ret[i].parentNode.removeChild( ret[i] ) : ret[i] );
                                } else {
                                        if ( ret[i].nodeType === 1 )
@@ -959,7 +972,7 @@ jQuery.extend({
 
                        // Safari mis-reports the default selected property of a hidden option
                        // Accessing the parent's selectedIndex property fixes it
-                       if ( name == "selected" )
+                       if ( name == "selected" && elem.parentNode )
                                elem.parentNode.selectedIndex;
 
                        // If applicable, access the attribute via the DOM 0 way
@@ -982,9 +995,11 @@ jQuery.extend({
                                        var attributeNode = elem.getAttributeNode( "tabIndex" );
                                        return attributeNode && attributeNode.specified
                                                ? attributeNode.value
-                                               : elem.nodeName.match(/^(a|area|button|input|object|select|textarea)$/i)
+                                               : elem.nodeName.match(/(button|input|object|select|textarea)/i)
                                                        ? 0
-                                                       : undefined;
+                                                       : elem.nodeName.match(/^(a|area)$/i) && elem.href
+                                                               ? 0
+                                                               : undefined;
                                }
 
                                return elem[ name ];
@@ -1146,9 +1161,6 @@ jQuery.browser = {
        mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
 };
 
-// Check to see if the W3C box model is being used
-jQuery.boxModel = !jQuery.browser.msie || document.compatMode == "CSS1Compat";
-
 jQuery.each({
        parent: function(elem){return elem.parentNode;},
        parents: function(elem){return jQuery.dir(elem,"parentNode");},