From 3cf1d14ccc63b8054ee977e90c72c5da07c80e59 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Mon, 7 Aug 2017 12:26:03 -0400 Subject: [PATCH] Dimensions: Don't trust non-pixel computed width/height Fixes gh-3611 Closes gh-3741 --- src/css.js | 8 ++++++-- test/unit/dimensions.js | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/css.js b/src/css.js index 2395f42b4..107d85a93 100644 --- a/src/css.js +++ b/src/css.js @@ -153,9 +153,13 @@ function getWidthOrHeight( elem, dimension, extra ) { isBorderBox = jQuery.css( elem, "boxSizing", false, styles ) === "border-box", valueIsBorderBox = isBorderBox; - // Computed unit is not pixels. Stop here and return. + // Support: Firefox <=54 + // Return a confounding non-pixel value or feign ignorance, as appropriate. if ( rnumnonpx.test( val ) ) { - return val; + if ( !extra ) { + return val; + } + val = "auto"; } // Check for style in case a browser which returns unreliable values diff --git a/test/unit/dimensions.js b/test/unit/dimensions.js index 991a59113..d7e553369 100644 --- a/test/unit/dimensions.js +++ b/test/unit/dimensions.js @@ -544,6 +544,29 @@ QUnit.test( "width/height on an inline element with no explicitly-set dimensions } ); } ); +QUnit.test( "width/height on an inline element with percentage dimensions (gh-3611)", + function( assert ) { + assert.expect( 4 ); + + jQuery( "
" + + "text" + + "
" ).appendTo( "#qunit-fixture" ); + + var $elem = jQuery( "#gh3611 span" ), + actualWidth = $elem[ 0 ].getBoundingClientRect().width, + marginWidth = $elem.outerWidth( true ), + borderWidth = $elem.outerWidth(), + paddingWidth = $elem.innerWidth(), + contentWidth = $elem.width(); + + assert.equal( Math.round( borderWidth ), Math.round( actualWidth ), + ".outerWidth(): " + borderWidth + " approximates " + actualWidth ); + assert.equal( marginWidth, borderWidth, ".outerWidth(true) matches .outerWidth()" ); + assert.equal( paddingWidth, borderWidth, ".innerWidth() matches .outerWidth()" ); + assert.equal( contentWidth, borderWidth - 10, ".width() excludes padding" ); + } +); + QUnit.test( "width/height on a table row with phantom borders (gh-3698)", function( assert ) { assert.expect( 4 ); -- 2.39.5