Skip to content

Commit 75203de

Browse files
author
timmywil
committed
Optimize width/height retrieval (moved logic to getWH, removed adjustWH). Supplements #9441, #9300.
1 parent 80ad14b commit 75203de

File tree

3 files changed

+50
-72
lines changed

3 files changed

+50
-72
lines changed

src/css.js

Lines changed: 39 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -172,59 +172,21 @@ jQuery.each(["height", "width"], function( i, name ) {
172172

173173
if ( computed ) {
174174
if ( elem.offsetWidth !== 0 ) {
175-
val = getWH( elem, name, extra );
175+
return getWH( elem, name, extra );
176176
} else {
177177
jQuery.swap( elem, cssShow, function() {
178178
val = getWH( elem, name, extra );
179179
});
180180
}
181181

182-
if ( val <= 0 ) {
183-
val = curCSS( elem, name, name );
184-
185-
if ( val === "0px" && currentStyle ) {
186-
val = currentStyle( elem, name, name );
187-
}
188-
189-
if ( val != null ) {
190-
fellback = true;
191-
}
192-
}
193-
194-
if ( !fellback && ( val < 0 || val == null ) ) {
195-
val = elem.style[ name ];
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";
205-
}
206-
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-
220182
return val;
221183
}
222184
},
223185

224186
set: function( elem, value ) {
225187
if ( rnumpx.test( value ) ) {
226188
// ignore negative width and height values #1599
227-
value = parseFloat(value);
189+
value = parseFloat( value );
228190

229191
if ( value >= 0 ) {
230192
return value + "px";
@@ -347,36 +309,51 @@ if ( document.documentElement.currentStyle ) {
347309
curCSS = getComputedStyle || currentStyle;
348310

349311
function getWH( elem, name, extra ) {
350-
var val = name === "width" ? elem.offsetWidth : elem.offsetHeight;
351312

352-
if ( extra === "border" ) {
353-
return val;
354-
}
313+
// Start with offset property
314+
var val = name === "width" ? elem.offsetWidth : elem.offsetHeight,
315+
which = name === "width" ? cssWidth : cssHeight;
355316

356-
if ( !extra ) {
357-
val -= adjustWH( elem, name, "padding");
317+
if ( extra !== "margin" && extra !== "border" ) {
318+
jQuery.each( which, function() {
319+
val -= parseFloat( jQuery.css( elem, "border" + this + "Width" ) ) || 0;
320+
if ( !extra ) {
321+
val -= parseFloat( jQuery.css( elem, "padding" + this ) ) || 0;
322+
}
323+
});
358324
}
359325

360-
if ( extra !== "margin" ) {
361-
val -= adjustWH( elem, name, "border", "Width");
326+
if ( val > 0 ) {
327+
if ( extra === "margin" ) {
328+
jQuery.each( which, function() {
329+
val += parseFloat( jQuery.css( elem, extra + this ) ) || 0;
330+
});
331+
}
332+
return val + "px";
362333
}
363334

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 = "";
335+
// Fall back to computed then uncomputed css if necessary
336+
val = curCSS( elem, name, name );
337+
if ( val < 0 || val == null ) {
338+
val = elem.style[ name ] || 0;
339+
}
340+
// Normalize "", auto, and prepare for extra
341+
val = parseFloat( val ) || 0;
342+
343+
// Add padding, border, margin
344+
if ( extra ) {
345+
jQuery.each( which, function() {
346+
val += parseFloat( jQuery.css( elem, "padding" + this ) ) || 0;
347+
if ( extra !== "padding" ) {
348+
val += parseFloat( jQuery.css( elem, "border" + this + "Width" ) ) || 0;
349+
}
350+
if ( extra === "margin" ) {
351+
val += parseFloat( jQuery.css( elem, extra + this ) ) || 0;
352+
}
353+
});
373354
}
374355

375-
jQuery.each( which, function() {
376-
val += parseFloat( jQuery.css( elem, prepend + this + append ) ) || 0;
377-
});
378-
379-
return val;
356+
return val + "px";
380357
}
381358

382359
if ( jQuery.expr && jQuery.expr.filters ) {

src/dimensions.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
(function( jQuery ) {
22

3-
// Create innerHeight, innerWidth, outerHeight and outerWidth methods
3+
// Create width, height, innerHeight, innerWidth, outerHeight and outerWidth methods
44
jQuery.each([ "Height", "Width" ], function( i, name ) {
55

66
var type = name.toLowerCase();
77

88
// innerHeight and innerWidth
9-
jQuery.fn["inner" + name] = function() {
9+
jQuery.fn[ "inner" + name ] = function() {
1010
var elem = this[0];
1111
return elem && elem.style ?
1212
parseFloat( jQuery.css( elem, type, "padding" ) ) :
1313
null;
1414
};
1515

1616
// outerHeight and outerWidth
17-
jQuery.fn["outer" + name] = function( margin ) {
17+
jQuery.fn[ "outer" + name ] = function( margin ) {
1818
var elem = this[0];
1919
return elem && elem.style ?
2020
parseFloat( jQuery.css( elem, type, margin ? "margin" : "border" ) ) :

test/unit/dimensions.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,23 +214,24 @@ test("outerWidth()", function() {
214214
test("child of a hidden elem has accurate inner/outer/Width()/Height() see #9441 #9300", function() {
215215
expect(8);
216216

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 );
217+
// setup html
218+
var $divNormal = jQuery("<div>").css({ width: "100px", height: "100px", border: "10px solid white", padding: "2px", margin: "3px" }),
219+
$divChild = $divNormal.clone(),
220+
$divHiddenParent = jQuery("<div>").css( "display", "none" ).append( $divChild ).appendTo("body");
221+
$divNormal.appendTo("body");
222222

223-
//tests that child div of a hidden div works the same as a normal div
223+
// tests that child div of a hidden div works the same as a normal div
224224
equals( $divChild.width(), $divNormal.width(), "child of a hidden element width() is wrong see #9441" );
225225
equals( $divChild.innerWidth(), $divNormal.innerWidth(), "child of a hidden element innerWidth() is wrong see #9441" );
226226
equals( $divChild.outerWidth(), $divNormal.outerWidth(), "child of a hidden element outerWidth() is wrong see #9441" );
227227
equals( $divChild.outerWidth(true), $divNormal.outerWidth( true ), "child of a hidden element outerWidth( true ) is wrong see #9300" );
228+
228229
equals( $divChild.height(), $divNormal.height(), "child of a hidden element height() is wrong see #9441" );
229230
equals( $divChild.innerHeight(), $divNormal.innerHeight(), "child of a hidden element innerHeight() is wrong see #9441" );
230231
equals( $divChild.outerHeight(), $divNormal.outerHeight(), "child of a hidden element outerHeight() is wrong see #9441" );
231232
equals( $divChild.outerHeight(true), $divNormal.outerHeight( true ), "child of a hidden element outerHeight( true ) is wrong see #9300" );
232233

233-
//teardown html
234+
// teardown html
234235
$divHiddenParent.remove();
235236
$divNormal.remove();
236237
});

0 commit comments

Comments
 (0)