Skip to content

Commit 80ad14b

Browse files
mikesherovtimmywil
authored andcommitted
Add margin after checking width. Add tests. Fixes #9441. Fixes #9300.
1 parent 0742056 commit 80ad14b

File tree

2 files changed

+70
-21
lines changed

2 files changed

+70
-21
lines changed

src/css.js

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,11 @@ jQuery.curCSS = jQuery.css;
168168
jQuery.each(["height", "width"], function( i, name ) {
169169
jQuery.cssHooks[ name ] = {
170170
get: function( elem, computed, extra ) {
171-
var val;
171+
var val, fellback = false;
172172

173173
if ( computed ) {
174174
if ( elem.offsetWidth !== 0 ) {
175175
val = getWH( elem, name, extra );
176-
177176
} else {
178177
jQuery.swap( elem, cssShow, function() {
179178
val = getWH( elem, name, extra );
@@ -188,20 +187,37 @@ jQuery.each(["height", "width"], function( i, name ) {
188187
}
189188

190189
if ( val != null ) {
191-
// Should return "auto" instead of 0, use 0 for
192-
// temporary backwards-compat
193-
return val === "" || val === "auto" ? "0px" : val;
190+
fellback = true;
194191
}
195192
}
196193

197-
if ( val < 0 || val == null ) {
194+
if ( !fellback && ( val < 0 || val == null ) ) {
198195
val = elem.style[ name ];
199-
// Should return "auto" instead of 0, use 0 for
200-
// temporary backwards-compat
201-
return val === "" || val === "auto" ? "0px" : val;
196+
fellback = true;
197+
}
198+
199+
// Should return "auto" instead of 0, use 0 for
200+
// temporary backwards-compat
201+
if ( fellback && ( val === "" || val === "auto" ) ) {
202+
val = "0px";
203+
} else if ( typeof val !== "string" ) {
204+
val += "px";
202205
}
203206

204-
return typeof val === "string" ? val : val + "px";
207+
if ( extra ) {
208+
val = parseFloat( val ) || 0;
209+
if ( fellback ) {
210+
val += adjustWH( elem, name, "padding" );
211+
if ( extra !== "padding" ) {
212+
val += adjustWH( elem, name, "border", "Width" );
213+
}
214+
}
215+
if ( extra === "margin" ) {
216+
val += adjustWH( elem, name, "margin" );
217+
}
218+
}
219+
220+
return val;
205221
}
206222
},
207223

@@ -331,24 +347,33 @@ if ( document.documentElement.currentStyle ) {
331347
curCSS = getComputedStyle || currentStyle;
332348

333349
function getWH( elem, name, extra ) {
334-
var which = name === "width" ? cssWidth : cssHeight,
335-
val = name === "width" ? elem.offsetWidth : elem.offsetHeight;
350+
var val = name === "width" ? elem.offsetWidth : elem.offsetHeight;
336351

337352
if ( extra === "border" ) {
338353
return val;
339354
}
340355

341-
jQuery.each( which, function() {
342-
if ( !extra ) {
343-
val -= parseFloat(jQuery.css( elem, "padding" + this )) || 0;
344-
}
356+
if ( !extra ) {
357+
val -= adjustWH( elem, name, "padding");
358+
}
345359

346-
if ( extra === "margin" ) {
347-
val += parseFloat(jQuery.css( elem, "margin" + this )) || 0;
360+
if ( extra !== "margin" ) {
361+
val -= adjustWH( elem, name, "border", "Width");
362+
}
348363

349-
} else {
350-
val -= parseFloat(jQuery.css( elem, "border" + this + "Width" )) || 0;
351-
}
364+
return val;
365+
}
366+
367+
function adjustWH( elem, name, prepend, append ) {
368+
var which = name === "width" ? cssWidth : cssHeight,
369+
val = 0;
370+
371+
if( !append ){
372+
append = "";
373+
}
374+
375+
jQuery.each( which, function() {
376+
val += parseFloat( jQuery.css( elem, prepend + this + append ) ) || 0;
352377
});
353378

354379
return val;

test/unit/dimensions.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,30 @@ test("outerWidth()", function() {
211211
jQuery.removeData($div[0], "olddisplay", true);
212212
});
213213

214+
test("child of a hidden elem has accurate inner/outer/Width()/Height() see #9441 #9300", function() {
215+
expect(8);
216+
217+
//setup html
218+
var $divNormal = jQuery( '<div>' ).css({ width: "100px", border: "10px solid white", padding: "2px", margin: "3px" });
219+
var $divChild = $divNormal.clone();
220+
var $divHiddenParent = jQuery( '<div>' ).css( "display", "none" ).append( $divChild );
221+
jQuery( 'body' ).append( $divHiddenParent ).append( $divNormal );
222+
223+
//tests that child div of a hidden div works the same as a normal div
224+
equals( $divChild.width(), $divNormal.width(), "child of a hidden element width() is wrong see #9441" );
225+
equals( $divChild.innerWidth(), $divNormal.innerWidth(), "child of a hidden element innerWidth() is wrong see #9441" );
226+
equals( $divChild.outerWidth(), $divNormal.outerWidth(), "child of a hidden element outerWidth() is wrong see #9441" );
227+
equals( $divChild.outerWidth(true), $divNormal.outerWidth( true ), "child of a hidden element outerWidth( true ) is wrong see #9300" );
228+
equals( $divChild.height(), $divNormal.height(), "child of a hidden element height() is wrong see #9441" );
229+
equals( $divChild.innerHeight(), $divNormal.innerHeight(), "child of a hidden element innerHeight() is wrong see #9441" );
230+
equals( $divChild.outerHeight(), $divNormal.outerHeight(), "child of a hidden element outerHeight() is wrong see #9441" );
231+
equals( $divChild.outerHeight(true), $divNormal.outerHeight( true ), "child of a hidden element outerHeight( true ) is wrong see #9300" );
232+
233+
//teardown html
234+
$divHiddenParent.remove();
235+
$divNormal.remove();
236+
});
237+
214238
test("outerHeight()", function() {
215239
expect(11);
216240

0 commit comments

Comments
 (0)