Skip to content

Commit 37bcb36

Browse files
committed
Resizable: Fix content shrink on resize
Resizable element shrinks on resize when it has scrollbars and "box-sizing: content-box". Fixes: gh-2277
1 parent 54f96ee commit 37bcb36

File tree

3 files changed

+132
-9
lines changed

3 files changed

+132
-9
lines changed

tests/unit/resizable/core.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,57 @@ QUnit.test( "nested resizable", function( assert ) {
244244
outer.remove();
245245
} );
246246

247+
QUnit.test( "Resizable with scrollbars and box-sizing: border-box", function( assert ) {
248+
assert.expect( 2 );
249+
250+
var elementContent = $( "<div>" )
251+
.css( {
252+
width: 50,
253+
height: 200,
254+
padding: 10,
255+
border: 5,
256+
borderStyle: "solid"
257+
} )
258+
.appendTo( "#resizable1" ),
259+
element = $( "#resizable1" ).css( { overflow: "auto" } ).resizable(),
260+
handle = ".ui-resizable-se";
261+
262+
$( "*" ).css( "box-sizing", "border-box" );
263+
264+
testHelper.drag( handle, 10, 10 );
265+
assert.equal( element.width(), 110, "element width" );
266+
assert.equal( element.height(), 110, "element height" );
267+
268+
elementContent.remove();
269+
} );
270+
271+
QUnit.test( "Resizable with scrollbars and box-sizing: content-box", function( assert ) {
272+
assert.expect( 2 );
273+
274+
var elementContent = $( "<div>" )
275+
.css( {
276+
width: 50,
277+
height: 200,
278+
padding: 10,
279+
border: 5,
280+
borderStyle: "solid"
281+
} )
282+
.appendTo( "#resizable1" ),
283+
element = $( "#resizable1" ).css( { overflow: "auto" } ).resizable(),
284+
handle = ".ui-resizable-se";
285+
286+
$( "*" ).css( "box-sizing", "content-box" );
287+
288+
// In some browsers scrollbar may change element size (when "box-sizing: content-box")
289+
var widthBefore = element.innerWidth();
290+
var heightBefore = element.innerHeight();
291+
292+
testHelper.drag( handle, 10, 10 );
293+
294+
assert.equal( parseFloat( element.innerWidth() ), widthBefore + 10, "element width" );
295+
assert.equal( parseFloat( element.innerHeight() ), heightBefore + 10, "element height" );
296+
297+
elementContent.remove();
298+
} );
299+
247300
} );

tests/unit/resizable/options.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,4 +565,62 @@ QUnit.test( "alsoResize with box-sizing: border-box", function( assert ) {
565565
assert.equal( parseFloat( other.css( "height" ) ), 130, "alsoResize height" );
566566
} );
567567

568+
QUnit.test( "alsoResize with scrollbars and box-sizing: border-box", function( assert ) {
569+
assert.expect( 4 );
570+
571+
var other = $( "<div>" )
572+
.css( {
573+
width: 150,
574+
height: 150,
575+
padding: 10,
576+
border: 5,
577+
borderStyle: "solid",
578+
overflow: "scroll"
579+
} )
580+
.appendTo( "body" ),
581+
element = $( "#resizable1" ).resizable( {
582+
alsoResize: other
583+
} ),
584+
handle = ".ui-resizable-se";
585+
586+
587+
$( "*" ).css( "box-sizing", "border-box" );
588+
testHelper.drag( handle, 80, 80 );
589+
590+
assert.equal( element.width(), 180, "resizable width" );
591+
assert.equal( parseFloat( other.css( "width" ) ), 230, "alsoResize width" );
592+
assert.equal( element.height(), 180, "resizable height" );
593+
assert.equal( parseFloat( other.css( "height" ) ), 230, "alsoResize height" );
594+
} );
595+
596+
QUnit.test( "alsoResize with scrollbars and box-sizing: content-box", function( assert ) {
597+
assert.expect( 4 );
598+
599+
var other = $( "<div>" )
600+
.css( {
601+
width: 150,
602+
height: 150,
603+
overflow: "scroll"
604+
} )
605+
.appendTo( "body" ),
606+
element = $( "#resizable1" ).resizable( {
607+
alsoResize: other
608+
} ),
609+
handle = ".ui-resizable-se";
610+
611+
$( "*" ).css( "box-sizing", "content-box" );
612+
613+
// In some browsers scrollbar may change element size.
614+
var widthBefore = other.innerWidth();
615+
var heightBefore = other.innerHeight();
616+
617+
testHelper.drag( handle, 80, 80 );
618+
619+
assert.equal( element.width(), 180, "resizable width" );
620+
assert.equal( parseFloat( other.innerWidth() ), widthBefore + 80, "alsoResize width" );
621+
assert.equal( element.height(), 180, "resizable height" );
622+
assert.equal( parseFloat( other.innerHeight() ), heightBefore + 80, "alsoResize height" );
623+
} );
624+
625+
568626
} );

ui/widgets/resizable.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -384,18 +384,12 @@ $.widget( "ui.resizable", $.ui.mouse, {
384384
this.size = this._helper ? {
385385
width: this.helper.width(),
386386
height: this.helper.height()
387-
} : {
388-
width: el.width(),
389-
height: el.height()
390-
};
387+
} : this._calculateAdjustedElementDimensions( el );
391388

392389
this.originalSize = this._helper ? {
393390
width: el.outerWidth(),
394391
height: el.outerHeight()
395-
} : {
396-
width: el.width(),
397-
height: el.height()
398-
};
392+
} : this._calculateAdjustedElementDimensions( el );
399393

400394
this.sizeDiff = {
401395
width: el.outerWidth() - el.width(),
@@ -690,6 +684,21 @@ $.widget( "ui.resizable", $.ui.mouse, {
690684
};
691685
},
692686

687+
_calculateAdjustedElementDimensions: function( element ) {
688+
if ( !( /content-box/ ).test( element.css( "box-sizing" ) ) ) {
689+
return {
690+
height: parseFloat( element.css( "height" ) ),
691+
width: parseFloat( element.css( "width" ) )
692+
};
693+
}
694+
695+
var outerDimensions = this._getPaddingPlusBorderDimensions( element );
696+
return {
697+
height: element[ 0 ].getBoundingClientRect().height - outerDimensions.height,
698+
width: element[ 0 ].getBoundingClientRect().width - outerDimensions.width
699+
};
700+
},
701+
693702
_proportionallyResize: function() {
694703

695704
if ( !this._proportionallyResizeElements.length ) {
@@ -1045,8 +1054,11 @@ $.ui.plugin.add( "resizable", "alsoResize", {
10451054

10461055
$( o.alsoResize ).each( function() {
10471056
var el = $( this );
1057+
1058+
var elSize = that._calculateAdjustedElementDimensions( el );
1059+
10481060
el.data( "ui-resizable-alsoresize", {
1049-
width: parseFloat( el.css( "width" ) ), height: parseFloat( el.css( "height" ) ),
1061+
width: elSize.width, height: elSize.height,
10501062
left: parseFloat( el.css( "left" ) ), top: parseFloat( el.css( "top" ) )
10511063
} );
10521064
} );

0 commit comments

Comments
 (0)