Skip to content

Commit 53396b8

Browse files
committed
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.
1 parent 7e02cee commit 53396b8

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

src/css.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,11 @@ jQuery.each(["height", "width"], function( i, name ) {
169169
});
170170
}
171171

172-
if ( val < 0 ) {
173-
return elem.style[ name ] || "0px";
174-
}
175-
176-
if ( val === 0 ) {
172+
if ( val <= 0 ) {
177173
val = curCSS( elem, name, name );
178174

179175
if ( val != null ) {
180-
return val;
176+
return val === "auto" ? "" : val;
181177
}
182178
}
183179

src/dimensions.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,29 @@ jQuery.each([ "Height", "Width" ], function( i, name ) {
3333
});
3434
}
3535

36-
return jQuery.isWindow( elem ) ?
36+
if ( jQuery.isWindow( elem ) ) {
3737
// Everyone else use document.documentElement or document.body depending on Quirks vs Standards mode
38-
elem.document.compatMode === "CSS1Compat" && elem.document.documentElement[ "client" + name ] ||
39-
elem.document.body[ "client" + name ] :
40-
41-
// Get document width or height
42-
(elem.nodeType === 9) ? // is it a document
43-
// Either scroll[Width/Height] or offset[Width/Height], whichever is greater
44-
Math.max(
45-
elem.documentElement["client" + name],
46-
elem.body["scroll" + name], elem.documentElement["scroll" + name],
47-
elem.body["offset" + name], elem.documentElement["offset" + name]
48-
) :
49-
50-
// Get or set width or height on the element
51-
size === undefined ?
52-
// Get width or height on the element
53-
parseFloat( jQuery.css( elem, type ) ) :
54-
55-
// Set the width or height on the element (default to pixels if value is unitless)
56-
this.css( type, typeof size === "string" ? size : size + "px" );
38+
return elem.document.compatMode === "CSS1Compat" && elem.document.documentElement[ "client" + name ] ||
39+
elem.document.body[ "client" + name ];
40+
41+
// Get document width or height
42+
} else if ( elem.nodeType === 9 ) {
43+
// Either scroll[Width/Height] or offset[Width/Height], whichever is greater
44+
return Math.max(
45+
elem.documentElement["client" + name],
46+
elem.body["scroll" + name], elem.documentElement["scroll" + name],
47+
elem.body["offset" + name], elem.documentElement["offset" + name]
48+
);
49+
50+
// Get or set width or height on the element
51+
} else if ( size === undefined ) {
52+
var orig = jQuery.css( elem, type ), ret = parseFloat( orig );
53+
return jQuery.isNaN( ret ) ? orig : ret;
54+
55+
// Set the width or height on the element (default to pixels if value is unitless)
56+
} else {
57+
return this.css( type, typeof size === "string" ? size : size + "px" );
58+
}
5759
};
5860

5961
});

test/unit/css.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ test("css(String|Hash)", function() {
1313

1414
var div = jQuery( "<div>" );
1515

16-
equals( div.css("width") || "auto", "auto", "Width on disconnected node." );
17-
equals( div.css("height") || "auto", "auto", "Height on disconnected node." );
16+
equals( div.css("width"), "", "Width on disconnected node." );
17+
equals( div.css("height"), "", "Height on disconnected node." );
1818

1919
div.css({ width: 4, height: 4 });
2020

0 commit comments

Comments
 (0)