Skip to content

Commit 669d601

Browse files
author
Gabriel Schulhof
committed
Controlgroup: Correctly handle non-empty child class key
Fixes #14984 Closes gh-1713
1 parent 8a79fc8 commit 669d601

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

tests/unit/controlgroup/controlgroup.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@
6868
<div class="controlgroup-single-button">
6969
<button class="single-button">button</button>
7070
</div>
71+
<div class="controlgroup-non-empty-class-key-init">
72+
<select id="non-empty-class-key-init-child">
73+
<option>Option 1</option>
74+
<option>Option 2</option>
75+
</select>
76+
</div>
77+
<div class="controlgroup-non-empty-class-key-value-no-dupe">
78+
<select id="non-empty-class-key-value-no-dupe-first">
79+
<option>Option 1</option>
80+
<option>Option 2</option>
81+
</select>
82+
<select id="non-empty-class-key-value-no-dupe-second">
83+
<option>Option 1</option>
84+
<option>Option 2</option>
85+
</select>
86+
</div>
7187
</div>
7288
</body>
7389
</html>

tests/unit/controlgroup/core.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,42 @@ QUnit.test( "Single controlgroup button - vertical", function( assert ) {
160160
" ui-corner-tr ui-corner-tl ui-corner-bl ui corner-br" );
161161
} );
162162

163+
QUnit.module( "Controlgroup: Non-empty class key", {
164+
setup: function() {
165+
this.classKey = $.ui.selectmenu.prototype.options.classes[ "ui-selectmenu-button-closed" ];
166+
$.ui.selectmenu.prototype.options.classes[ "ui-selectmenu-button-closed" ] =
167+
$.trim( $.ui.selectmenu.prototype.options.classes[ "ui-selectmenu-button-closed" ] +
168+
" something-custom" );
169+
},
170+
teardown: function() {
171+
$.ui.selectmenu.prototype.options.classes[ "ui-selectmenu-button-closed" ] = this.classKey;
172+
}
173+
} );
174+
175+
QUnit.test( "Controlgroup instantiates child widgets with correct options", function( assert ) {
176+
assert.expect( 1 );
177+
178+
$( ".controlgroup-non-empty-class-key-init" ).controlgroup();
179+
180+
assert.hasClasses( $( "#non-empty-class-key-init-child" ).next(), "something-custom" );
181+
} );
182+
183+
QUnit.test( "Controlgroup correctly assigns child widget classes options key", function( assert ) {
184+
assert.expect( 2 );
185+
186+
$( ".controlgroup-non-empty-class-key-value-no-dupe" ).controlgroup();
187+
188+
assert.strictEqual(
189+
( $( "#non-empty-class-key-value-no-dupe-first" )
190+
.selectmenu( "option", "classes" )[ "ui-selectmenu-button-closed" ]
191+
.match( /something-custom/g ) || [] ).length, 1,
192+
"Class 'something-custom' appears exactly once in the first widget's class key value" );
193+
194+
assert.strictEqual(
195+
( $( "#non-empty-class-key-value-no-dupe-second" )
196+
.selectmenu( "option", "classes" )[ "ui-selectmenu-button-closed" ]
197+
.match( /something-custom/g ) || [] ).length, 1,
198+
"Class 'something-custom' appears exactly once in the second widget's class key value" );
199+
} );
200+
163201
} );

ui/widgets/controlgroup.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,24 @@ return $.widget( "ui.controlgroup", {
118118
var element = $( this );
119119
var instance = element[ widget ]( "instance" );
120120

121+
// We need to clone the default options for this type of widget to avoid
122+
// polluting the variable options which has a wider scope than a single widget.
123+
var instanceOptions = $.widget.extend( {}, options );
124+
121125
// If the button is the child of a spinner ignore it
122126
if ( widget === "button" && element.parent( ".ui-spinner" ).length ) {
123127
return;
124128
}
129+
130+
// Create the widget if it doesn't exist
131+
if ( !instance ) {
132+
instance = element[ widget ]()[ widget ]( "instance" );
133+
}
125134
if ( instance ) {
126-
options.classes = that._resolveClassesValues( options.classes, instance );
135+
instanceOptions.classes =
136+
that._resolveClassesValues( instanceOptions.classes, instance );
127137
}
128-
element[ widget ]( options );
138+
element[ widget ]( instanceOptions );
129139

130140
// Store an instance of the controlgroup to be able to reference
131141
// from the outermost element for changing options and refresh
@@ -218,12 +228,13 @@ return $.widget( "ui.controlgroup", {
218228
},
219229

220230
_resolveClassesValues: function( classes, instance ) {
231+
var result = {};
221232
$.each( classes, function( key ) {
222233
var current = instance.options.classes[ key ] || "";
223234
current = current.replace( controlgroupCornerRegex, "" ).trim();
224-
classes[ key ] = ( current + " " + classes[ key ] ).replace( /\s+/g, " " );
235+
result[ key ] = ( current + " " + classes[ key ] ).replace( /\s+/g, " " );
225236
} );
226-
return classes;
237+
return result;
227238
},
228239

229240
_setOption: function( key, value ) {

0 commit comments

Comments
 (0)