From 53396b879bd29c090824da182e3cf69158829f82 Mon Sep 17 00:00:00 2001 From: jeresig Date: Fri, 22 Oct 2010 02:39:06 -0400 Subject: [PATCH] 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. --- src/css.js | 8 ++------ src/dimensions.js | 42 ++++++++++++++++++++++-------------------- test/unit/css.js | 4 ++-- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/css.js b/src/css.js index 88c4ffa05..30cecf39d 100644 --- a/src/css.js +++ b/src/css.js @@ -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; } } diff --git a/src/dimensions.js b/src/dimensions.js index 5aafbf41e..f5212e1b6 100644 --- a/src/dimensions.js +++ b/src/dimensions.js @@ -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 ] : - - // 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 or set width or height on the element - size === undefined ? - // Get width or height on the element - parseFloat( jQuery.css( elem, type ) ) : - - // Set the width or height on the element (default to pixels if value is unitless) - this.css( type, typeof size === "string" ? size : size + "px" ); + return elem.document.compatMode === "CSS1Compat" && elem.document.documentElement[ "client" + name ] || + elem.document.body[ "client" + 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 + } 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) + } else { + return this.css( type, typeof size === "string" ? size : size + "px" ); + } }; }); diff --git a/test/unit/css.js b/test/unit/css.js index 2c2e9ed21..9c262af97 100644 --- a/test/unit/css.js +++ b/test/unit/css.js @@ -13,8 +13,8 @@ test("css(String|Hash)", function() { var div = jQuery( "
" ); - 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 }); -- 2.39.5