Skip to content

Commit d6180d5

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

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

tests/unit/controlgroup/controlgroup.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@
6868
<div class="controlgroup-single-button">
6969
<button class="single-button">button</button>
7070
</div>
71+
<div class="controlgroup-non-empty-class-key">
72+
<select id="non-empty-class-key-first">
73+
<option>Option 1</option>
74+
<option>Option 2</option>
75+
</select>
76+
<select id="non-empty-class-key-second">
77+
<option>Option 1</option>
78+
<option>Option 2</option>
79+
</select>
80+
</div>
7181
</div>
7282
</body>
7383
</html>

tests/unit/controlgroup/core.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,39 @@ 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 handles non-empty class key correctly", function( assert ) {
176+
assert.expect( 2 );
177+
178+
$( ".controlgroup-non-empty-class-key" )
179+
.controlgroup()
180+
.controlgroup( "instance" );
181+
182+
assert.deepEqual(
183+
( ( ( $( "#non-empty-class-key-first" )
184+
.selectmenu( "option", "classes" )[ "ui-selectmenu-button-closed" ] || "" )
185+
.match( /\S+/g ) ) || [] ).sort(),
186+
[ "something-custom", "ui-corner-left" ],
187+
"First controlgroup item has the expected value for the class key" );
188+
189+
assert.deepEqual(
190+
( ( ( $( "#non-empty-class-key-second" )
191+
.selectmenu( "option", "classes" )[ "ui-selectmenu-button-closed" ] || "" )
192+
.match( /\S+/g ) ) || [] ).sort(),
193+
[ "something-custom", "ui-corner-right" ],
194+
"Second controlgroup item has the expected value for the class key" );
195+
196+
} );
197+
163198
} );

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)