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

Commit 473d2ea

Browse files
committed
Dimensions: fall back to offsetWidth/Height for inline elems
Close gh-3577 Fixes gh-3571
1 parent fcc9a9e commit 473d2ea

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/css.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ function getWidthOrHeight( elem, name, extra ) {
144144
valueIsBorderBox = isBorderBox &&
145145
( support.boxSizingReliable() || val === elem.style[ name ] );
146146

147+
// Fall back to offsetWidth/Height when value is "auto"
148+
// This happens for inline elements with no explicit setting (gh-3571)
149+
if ( val === "auto" ) {
150+
val = elem[ "offset" + name[ 0 ].toUpperCase() + name.slice( 1 ) ];
151+
}
152+
147153
// Normalize "", auto, and prepare for extra
148154
val = parseFloat( val ) || 0;
149155

src/css/curCSS.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ define( [
1010
"use strict";
1111

1212
function curCSS( elem, name, computed ) {
13-
var width, minWidth, maxWidth, ret,
14-
style = elem.style;
13+
var width, minWidth, maxWidth, ret, style;
1514

1615
computed = computed || getStyles( elem );
1716

@@ -31,6 +30,7 @@ function curCSS( elem, name, computed ) {
3130
// This is against the CSSOM draft spec:
3231
// https://drafts.csswg.org/cssom/#resolved-values
3332
if ( !support.pixelMarginRight() && rnumnonpx.test( ret ) && rmargin.test( name ) ) {
33+
style = elem.style;
3434

3535
// Remember the original values
3636
width = style.width;

test/unit/dimensions.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,4 +538,18 @@ QUnit.test( "width/height on element with transform (gh-3193)", function( assert
538538
assert.equal( $elem.height(), 200, "Height ignores transforms" );
539539
} );
540540

541+
QUnit.test( "width/height on an inline element with no explicitly-set dimensions (gh-3571)", function( assert ) {
542+
assert.expect( 8 );
543+
544+
var $elem = jQuery( "<span style='border: 2px solid black;padding: 1px;margin: 3px;'>Hello, I'm some text.</span>" ).appendTo( "#qunit-fixture" );
545+
546+
jQuery.each( [ "Width", "Height" ], function( i, method ) {
547+
var val = $elem[ method.toLowerCase() ]();
548+
assert.notEqual( val, 0, method + " should not be zero on inline element." );
549+
assert.equal( $elem[ "inner" + method ](), val + 2, "inner" + method + " should include padding" );
550+
assert.equal( $elem[ "outer" + method ](), val + 6, "outer" + method + " should include padding and border" );
551+
assert.equal( $elem[ "outer" + method ]( true ), val + 12, "outer" + method + "(true) should include padding, border, and margin" );
552+
} );
553+
} );
554+
541555
} )();

0 commit comments

Comments
 (0)