Make sure that .width()/.height() don't return NaN also standardize on returning instead of auto for default values (which is what we do elsewhere in .css() as well). Fixes #7225.

This commit is contained in:
jeresig 2010-10-22 02:39:06 -04:00
parent 7e02cee5ff
commit 53396b879b
3 changed files with 23 additions and 25 deletions

View file

@ -169,15 +169,11 @@ jQuery.each(["height", "width"], function( i, name ) {
});
}
if ( val < 0 ) {
return elem.style[ name ] || "0px";
}
if ( val === 0 ) {
if ( val <= 0 ) {
val = curCSS( elem, name, name );
if ( val != null ) {
return val;
return val === "auto" ? "" : val;
}
}

View file

@ -33,27 +33,29 @@ jQuery.each([ "Height", "Width" ], function( i, name ) {
});
}
return jQuery.isWindow( elem ) ?
if ( jQuery.isWindow( elem ) ) {
// Everyone else use document.documentElement or document.body depending on Quirks vs Standards mode
elem.document.compatMode === "CSS1Compat" && elem.document.documentElement[ "client" + name ] ||
elem.document.body[ "client" + name ] :
return elem.document.compatMode === "CSS1Compat" && elem.document.documentElement[ "client" + name ] ||
elem.document.body[ "client" + name ];
// Get document width or height
(elem.nodeType === 9) ? // is it a document
// Either scroll[Width/Height] or offset[Width/Height], whichever is greater
Math.max(
elem.documentElement["client" + name],
elem.body["scroll" + name], elem.documentElement["scroll" + name],
elem.body["offset" + name], elem.documentElement["offset" + name]
) :
// Get document width or height
} else if ( elem.nodeType === 9 ) {
// Either scroll[Width/Height] or offset[Width/Height], whichever is greater
return Math.max(
elem.documentElement["client" + name],
elem.body["scroll" + name], elem.documentElement["scroll" + name],
elem.body["offset" + name], elem.documentElement["offset" + name]
);
// Get or set width or height on the element
size === undefined ?
// Get width or height on the element
parseFloat( jQuery.css( elem, type ) ) :
// Get or set width or height on the element
} else if ( size === undefined ) {
var orig = jQuery.css( elem, type ), ret = parseFloat( orig );
return jQuery.isNaN( ret ) ? orig : ret;
// Set the width or height on the element (default to pixels if value is unitless)
this.css( type, typeof size === "string" ? size : size + "px" );
// Set the width or height on the element (default to pixels if value is unitless)
} else {
return this.css( type, typeof size === "string" ? size : size + "px" );
}
};
});

View file

@ -13,8 +13,8 @@ test("css(String|Hash)", function() {
var div = jQuery( "<div>" );
equals( div.css("width") || "auto", "auto", "Width on disconnected node." );
equals( div.css("height") || "auto", "auto", "Height on disconnected node." );
equals( div.css("width"), "", "Width on disconnected node." );
equals( div.css("height"), "", "Height on disconnected node." );
div.css({ width: 4, height: 4 });