Added some significant speed-ups to height/width checks, thanks to some code and...
[jquery.git] / src / core.js
index 5c37858..508060a 100644 (file)
@@ -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 );
        },
@@ -623,9 +631,7 @@ jQuery.extend({
 
        // 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,
@@ -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 :
                                                        [];