🌐 AI搜索 & 代理 主页
Skip to content

Commit a0abd15

Browse files
authored
CSS: Avoid forcing a reflow in width/height getters unless necessary
Fixes gh-4322 Closes gh-4325 Ref gh-3991 Ref gh-4010 Ref gh-4185 Ref gh-4187
1 parent 0ec25ab commit a0abd15

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/css.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,15 @@ function getWidthOrHeight( elem, dimension, extra ) {
118118

119119
// Start with computed style
120120
var styles = getStyles( elem ),
121-
val = curCSS( elem, dimension, styles ),
122-
isBorderBox = jQuery.css( elem, "boxSizing", false, styles ) === "border-box",
121+
122+
// To avoid forcing a reflow, only fetch boxSizing if we need it (gh-4322).
123+
// Fake content-box until we know it's needed to know the true value.
124+
boxSizingNeeded = !support.boxSizingReliable() || extra,
125+
isBorderBox = boxSizingNeeded &&
126+
jQuery.css( elem, "boxSizing", false, styles ) === "border-box",
123127
valueIsBorderBox = isBorderBox,
128+
129+
val = curCSS( elem, dimension, styles ),
124130
offsetProp = "offset" + dimension[ 0 ].toUpperCase() + dimension.slice( 1 );
125131

126132
// Support: Firefox <=54
@@ -141,10 +147,13 @@ function getWidthOrHeight( elem, dimension, extra ) {
141147
// Also use offsetWidth/offsetHeight for when box sizing is unreliable
142148
// We use getClientRects() to check for hidden/disconnected.
143149
// In those cases, the computed value can be trusted to be border-box
144-
if ( ( isBorderBox && !support.boxSizingReliable() || val === "auto" ||
150+
if ( ( !support.boxSizingReliable() && isBorderBox ||
151+
val === "auto" ||
145152
!parseFloat( val ) && jQuery.css( elem, "display", false, styles ) === "inline" ) &&
146153
elem.getClientRects().length ) {
147154

155+
isBorderBox = jQuery.css( elem, "boxSizing", false, styles ) === "border-box";
156+
148157
// Where available, offsetWidth/offsetHeight approximate border box dimensions.
149158
// Where not available (e.g., SVG), assume unreliable box-sizing and interpret the
150159
// retrieved value as a content box dimension.

test/unit/dimensions.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -397,17 +397,17 @@ QUnit.test( "SVG dimensions (border-box)", function( assert ) {
397397

398398
var svg = jQuery( "<svg style='width: 100px; height: 100px; box-sizing: border-box; border: 1px solid white; padding: 2px; margin: 3px'></svg>" ).appendTo( "#qunit-fixture" );
399399

400-
assert.equal( svg.width(), 94 );
401-
assert.equal( svg.height(), 94 );
400+
assert.equal( svg.width(), 94, "width" );
401+
assert.equal( svg.height(), 94, "height" );
402402

403-
assert.equal( svg.innerWidth(), 98 );
404-
assert.equal( svg.innerHeight(), 98 );
403+
assert.equal( svg.innerWidth(), 98, "innerWidth" );
404+
assert.equal( svg.innerHeight(), 98, "innerHeight" );
405405

406-
assert.equal( svg.outerWidth(), 100 );
407-
assert.equal( svg.outerHeight(), 100 );
406+
assert.equal( svg.outerWidth(), 100, "outerWidth" );
407+
assert.equal( svg.outerHeight(), 100, "outerHeight" );
408408

409-
assert.equal( svg.outerWidth( true ), 106 );
410-
assert.equal( svg.outerHeight( true ), 106 );
409+
assert.equal( svg.outerWidth( true ), 106, "outerWidth( true )" );
410+
assert.equal( svg.outerHeight( true ), 106, "outerHeight( true )" );
411411

412412
svg.remove();
413413
} );

0 commit comments

Comments
 (0)