Skip to content

Commit

Permalink
CSS: Make offsetHeight( true ), etc. include negative margins
Browse files Browse the repository at this point in the history
This regressed in gh-3656 as the added logic to include scroll gutters
in `.innerWidth()` / `.innerHeight()` didn't take negative margins into
account. This broke handling of negative margins in
`.offsetHeight( true )` and `.offsetWidth( true )`. To fix it, calculate
margin delta separately and only add it after the scroll gutter
adjustment logic.

Fixes gh-3982
Closes gh-5234
Ref gh-3656

(cherry picked from commit bce13b7)
  • Loading branch information
mgol committed Apr 4, 2023
1 parent 9ab26aa commit 7bb48a0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ function setPositiveNumber( _elem, value, subtract ) {
function boxModelAdjustment( elem, dimension, box, isBorderBox, styles, computedVal ) {
var i = dimension === "width" ? 1 : 0,
extra = 0,
delta = 0;
delta = 0,
marginDelta = 0;

// Adjustment may not be necessary
if ( box === ( isBorderBox ? "border" : "content" ) ) {
Expand All @@ -61,8 +62,10 @@ function boxModelAdjustment( elem, dimension, box, isBorderBox, styles, computed
for ( ; i < 4; i += 2 ) {

// Both box models exclude margin
// Count margin delta separately to only add it after scroll gutter adjustment.
// This is needed to make negative margins work with `outerHeight( true )` (gh-3982).
if ( box === "margin" ) {
delta += jQuery.css( elem, box + cssExpand[ i ], true, styles );
marginDelta += jQuery.css( elem, box + cssExpand[ i ], true, styles );
}

// If we get here with a content-box, we're seeking "padding" or "border" or "margin"
Expand Down Expand Up @@ -113,7 +116,7 @@ function boxModelAdjustment( elem, dimension, box, isBorderBox, styles, computed
) ) || 0;
}

return delta;
return delta + marginDelta;
}

function getWidthOrHeight( elem, dimension, extra ) {
Expand Down
7 changes: 6 additions & 1 deletion test/unit/dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ QUnit.test( "outerWidth()", function( assert ) {
} );

QUnit.test( "outerHeight()", function( assert ) {
assert.expect( 12 );
assert.expect( 14 );

var $div, div,
$win = jQuery( window ),
Expand Down Expand Up @@ -268,6 +268,11 @@ QUnit.test( "outerHeight()", function( assert ) {
$div.css( "display", "none" );
assert.equal( $div.outerHeight( true ), 94, "Test hidden div with padding, border and margin with margin option" );

$div.css( "display", "" );
$div.css( "margin", "-10px" );
assert.equal( $div.outerHeight(), 74, "Test with padding, border and negative margin without margin option" );
assert.equal( $div.outerHeight( true ), 54, "Test with padding, border and negative margin with margin option" );

// reset styles
$div.css( { "position": "", "display": "", "border": "", "padding": "", "width": "", "height": "" } );

Expand Down

0 comments on commit 7bb48a0

Please sign in to comment.