Skip to content

Commit f24726d

Browse files
dmethvingibson042
authored andcommitted
Attributes: Remove undocumented .toggleClass( boolean ) signature
Fixes gh-2491 Close gh-2618 (cherry picked from commit 53f798c) Conflicts: src/attributes/classes.js
1 parent 3842246 commit f24726d

File tree

2 files changed

+22
-76
lines changed

2 files changed

+22
-76
lines changed

src/attributes/classes.js

Lines changed: 21 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -99,60 +99,29 @@ jQuery.fn.extend( {
9999
},
100100

101101
toggleClass: function( value, stateVal ) {
102-
var type = typeof value;
103-
104-
if ( typeof stateVal === "boolean" && type === "string" ) {
105-
return stateVal ? this.addClass( value ) : this.removeClass( value );
106-
}
107-
108-
if ( jQuery.isFunction( value ) ) {
109-
return this.each( function( i ) {
110-
jQuery( this ).toggleClass(
111-
value.call( this, i, getClass( this ), stateVal ),
112-
stateVal
113-
);
114-
} );
115-
}
116-
117-
return this.each( function() {
118-
var className, i, self, classNames;
119-
120-
if ( type === "string" ) {
121-
122-
// Toggle individual class names
123-
i = 0;
124-
self = jQuery( this );
125-
classNames = value.match( rnotwhite ) || [];
126-
127-
while ( ( className = classNames[ i++ ] ) ) {
128-
129-
// Check each className given, space separated list
130-
if ( self.hasClass( className ) ) {
131-
self.removeClass( className );
132-
} else {
133-
self.addClass( className );
134-
}
135-
}
136-
137-
// Toggle whole class name
138-
} else if ( value === undefined || type === "boolean" ) {
139-
className = getClass( this );
140-
if ( className ) {
102+
var type = typeof value,
103+
classNames = type === "string" ? value.match( rnotwhite ) : "",
104+
checker = typeof stateVal === "boolean" ?
105+
function() { return !stateVal; } :
106+
jQuery.fn.hasClass;
107+
108+
return this.each( function( i ) {
109+
var className,
110+
self = jQuery( this ),
111+
c = 0;
112+
113+
if ( type === "function" ) {
114+
classNames = value.call( this, i, getClass( this ), stateVal )
115+
.match( rnotwhite ) || [];
116+
}
141117

142-
// store className if set
143-
jQuery._data( this, "__className__", className );
144-
}
118+
// Toggle individual class names based on presence or stateVal
119+
while ( ( className = classNames[ c++ ] ) ) {
145120

146-
// If the element has a class name or if we're passed "false",
147-
// then remove the whole classname (if there was one, the above saved it).
148-
// Otherwise bring back whatever was previously saved (if anything),
149-
// falling back to the empty string if nothing was stored.
150-
if ( this.setAttribute ) {
151-
this.setAttribute( "class",
152-
className || value === false ?
153-
"" :
154-
jQuery._data( this, "__className__" ) || ""
155-
);
121+
if ( checker.call( self, className ) ) {
122+
self.removeClass( className );
123+
} else {
124+
self.addClass( className );
156125
}
157126
}
158127
} );

test/unit/attributes.js

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,7 @@ QUnit.test( "removeClass(undefined) is a no-op", function( assert ) {
12201220
} );
12211221

12221222
var testToggleClass = function( valueObj, assert ) {
1223-
assert.expect( 17 );
1223+
assert.expect( 9 );
12241224

12251225
var e = jQuery( "#firstp" );
12261226
assert.ok( !e.is( ".test" ), "Assert class not present" );
@@ -1244,29 +1244,6 @@ var testToggleClass = function( valueObj, assert ) {
12441244
assert.ok( ( e.is( ".testA.testC" ) && !e.is( ".testB" ) ), "Assert 1 class added, 1 class removed, and 1 class kept" );
12451245
e.toggleClass( valueObj( "testA testC" ) );
12461246
assert.ok( ( !e.is( ".testA" ) && !e.is( ".testB" ) && !e.is( ".testC" ) ), "Assert no class present" );
1247-
1248-
// toggleClass storage
1249-
e.toggleClass( true );
1250-
assert.ok( e[ 0 ].className === "", "Assert class is empty (data was empty)" );
1251-
e.addClass( "testD testE" );
1252-
assert.ok( e.is( ".testD.testE" ), "Assert class present" );
1253-
e.toggleClass();
1254-
assert.ok( !e.is( ".testD.testE" ), "Assert class not present" );
1255-
assert.ok( jQuery._data( e[ 0 ], "__className__" ) === "testD testE", "Assert data was stored" );
1256-
e.toggleClass();
1257-
assert.ok( e.is( ".testD.testE" ), "Assert class present (restored from data)" );
1258-
e.toggleClass( false );
1259-
assert.ok( !e.is( ".testD.testE" ), "Assert class not present" );
1260-
e.toggleClass( true );
1261-
assert.ok( e.is( ".testD.testE" ), "Assert class present (restored from data)" );
1262-
e.toggleClass();
1263-
e.toggleClass( false );
1264-
e.toggleClass();
1265-
assert.ok( e.is( ".testD.testE" ), "Assert class present (restored from data)" );
1266-
1267-
// Cleanup
1268-
e.removeClass( "testD" );
1269-
assert.expectJqData( this, e[ 0 ], "__className__" );
12701247
};
12711248

12721249
QUnit.test( "toggleClass(String|boolean|undefined[, boolean])", function( assert ) {

0 commit comments

Comments
 (0)