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

Commit 3cf1d14

Browse files
authored
Dimensions: Don't trust non-pixel computed width/height
Fixes gh-3611 Closes gh-3741
1 parent f3c5776 commit 3cf1d14

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/css.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,13 @@ function getWidthOrHeight( elem, dimension, extra ) {
153153
isBorderBox = jQuery.css( elem, "boxSizing", false, styles ) === "border-box",
154154
valueIsBorderBox = isBorderBox;
155155

156-
// Computed unit is not pixels. Stop here and return.
156+
// Support: Firefox <=54
157+
// Return a confounding non-pixel value or feign ignorance, as appropriate.
157158
if ( rnumnonpx.test( val ) ) {
158-
return val;
159+
if ( !extra ) {
160+
return val;
161+
}
162+
val = "auto";
159163
}
160164

161165
// Check for style in case a browser which returns unreliable values

test/unit/dimensions.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,29 @@ QUnit.test( "width/height on an inline element with no explicitly-set dimensions
544544
} );
545545
} );
546546

547+
QUnit.test( "width/height on an inline element with percentage dimensions (gh-3611)",
548+
function( assert ) {
549+
assert.expect( 4 );
550+
551+
jQuery( "<div id='gh3611' style='width: 100px;'>" +
552+
"<span style='width: 100%; padding: 0 5px'>text</span>" +
553+
"</div>" ).appendTo( "#qunit-fixture" );
554+
555+
var $elem = jQuery( "#gh3611 span" ),
556+
actualWidth = $elem[ 0 ].getBoundingClientRect().width,
557+
marginWidth = $elem.outerWidth( true ),
558+
borderWidth = $elem.outerWidth(),
559+
paddingWidth = $elem.innerWidth(),
560+
contentWidth = $elem.width();
561+
562+
assert.equal( Math.round( borderWidth ), Math.round( actualWidth ),
563+
".outerWidth(): " + borderWidth + " approximates " + actualWidth );
564+
assert.equal( marginWidth, borderWidth, ".outerWidth(true) matches .outerWidth()" );
565+
assert.equal( paddingWidth, borderWidth, ".innerWidth() matches .outerWidth()" );
566+
assert.equal( contentWidth, borderWidth - 10, ".width() excludes padding" );
567+
}
568+
);
569+
547570
QUnit.test( "width/height on a table row with phantom borders (gh-3698)", function( assert ) {
548571
assert.expect( 4 );
549572

0 commit comments

Comments
 (0)