Skip to content

Commit 5404f7f

Browse files
author
Gabriel Schulhof
committed
Widget: Avoid adding extra spaces to the result of this._classes(...)
1 parent 75efe38 commit 5404f7f

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

tests/unit/widget/widget_core.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,17 +608,20 @@ test( ".option() - deep option setter", function() {
608608
});
609609

610610
test( "_classes", function(){
611-
expect( 3 );
611+
expect( 4 );
612612
$.widget( "ui.testWidget", {
613613
options: {
614614
classes: {
615615
"test": "class1 class2",
616+
"testEmpty": "",
616617
"test2": "class3"
617618
}
618619
},
619620
_create: function() {
620621
equal( this._classes( "test" ), "test class1 class2" );
621622
equal( this._classes( "test2" ), "test2 class3" );
623+
deepEqual( this._classes( "testEmpty" ), "testEmpty",
624+
"Computed value of empty-string-valued class key has no extra spaces" );
622625
equal( this._classes( "test test2" ), "test2 class3 test class1 class2" );
623626
}
624627
});

ui/widget.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,13 @@ $.Widget.prototype = {
410410

411411
while ( i-- ) {
412412
out.push( parts[ i ] );
413-
out.push( classes[ parts[ i ] ] );
413+
414+
// The empty string is a valid value for a class key, but pushing it into the array
415+
// which we then join into a string below will result in our return value having
416+
// superfluous spaces. Let's only add the value of the key if it's truthy.
417+
if ( classes[ parts[ i ] ] ) {
418+
out.push( classes[ parts[ i ] ] );
419+
}
414420
}
415421

416422
return out.join( " " );

0 commit comments

Comments
 (0)