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

Commit 6a97bba

Browse files
committed
Dimensions: fix computing outerWidth on SVGs
Fixes gh-3964
1 parent 625e19c commit 6a97bba

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/css.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,12 @@ function boxModelAdjustment( elem, dimension, box, isBorderBox, styles, computed
130130

131131
// Account for positive content-box scroll gutter when requested by providing computedVal
132132
if ( !isBorderBox && computedVal >= 0 ) {
133+
var val = elem[ "offset" + dimension[ 0 ].toUpperCase() + dimension.slice( 1 ) ];
133134

134135
// offsetWidth/offsetHeight is a rounded sum of content, padding, scroll gutter, and border
135136
// Assuming integer scroll gutter, subtract the rest and round down
136137
delta += Math.max( 0, Math.ceil(
137-
elem[ "offset" + dimension[ 0 ].toUpperCase() + dimension.slice( 1 ) ] -
138+
( val === undefined ? computedVal : val ) -
138139
computedVal -
139140
delta -
140141
extra -

test/unit/dimensions.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,66 @@ QUnit.test( "table dimensions", function( assert ) {
352352
assert.equal( colElem.width(), 300, "col elements have width(), see #12243" );
353353
} );
354354

355+
QUnit.test( "SVG dimensions (basic content-box)", function( assert ) {
356+
assert.expect( 8 );
357+
358+
var svg = jQuery( "<svg width='100' height='100'></svg>" ).appendTo( "#qunit-fixture" );
359+
360+
assert.equal( svg.width(), 100 );
361+
assert.equal( svg.height(), 100 );
362+
363+
assert.equal( svg.innerWidth(), 100 );
364+
assert.equal( svg.innerHeight(), 100 );
365+
366+
assert.equal( svg.outerWidth(), 100 );
367+
assert.equal( svg.outerHeight(), 100 );
368+
369+
assert.equal( svg.outerWidth( true ), 100 );
370+
assert.equal( svg.outerHeight( true ), 100 );
371+
372+
svg.remove();
373+
} );
374+
375+
QUnit.test( "SVG dimensions (content-box)", function( assert ) {
376+
assert.expect( 8 );
377+
378+
var svg = jQuery( "<svg width='100' height='100' style='box-sizing: content-box; border: 1px solid white; padding: 2px; margin: 3px'></svg>" ).appendTo( "#qunit-fixture" );
379+
380+
assert.equal( svg.width(), 100 );
381+
assert.equal( svg.height(), 100 );
382+
383+
assert.equal( svg.innerWidth(), 104 );
384+
assert.equal( svg.innerHeight(), 104 );
385+
386+
assert.equal( svg.outerWidth(), 106 );
387+
assert.equal( svg.outerHeight(), 106 );
388+
389+
assert.equal( svg.outerWidth( true ), 112 );
390+
assert.equal( svg.outerHeight( true ), 112 );
391+
392+
svg.remove();
393+
} );
394+
395+
QUnit.test( "SVG dimensions (border-box)", function( assert ) {
396+
assert.expect( 8 );
397+
398+
var svg = jQuery( "<svg width='100' height='100' style='box-sizing: border-box; border: 1px solid white; padding: 2px; margin: 3px'></svg>" ).appendTo( "#qunit-fixture" );
399+
400+
assert.equal( svg.width(), 94 );
401+
assert.equal( svg.height(), 94 );
402+
403+
assert.equal( svg.innerWidth(), 98 );
404+
assert.equal( svg.innerHeight(), 98 );
405+
406+
assert.equal( svg.outerWidth(), 100 );
407+
assert.equal( svg.outerHeight(), 100 );
408+
409+
assert.equal( svg.outerWidth( true ), 106 );
410+
assert.equal( svg.outerHeight( true ), 106 );
411+
412+
svg.remove();
413+
} );
414+
355415
QUnit.test( "box-sizing:border-box child of a hidden elem (or unconnected node) has accurate inner/outer/Width()/Height() see #10413", function( assert ) {
356416
assert.expect( 16 );
357417

0 commit comments

Comments
 (0)