Skip to content

Commit 53f798c

Browse files
dmethvintimmywil
authored andcommitted
Attributes: Remove undocumented .toggleClass( boolean ) signature
Fixes gh-2491 Close gh-2618
1 parent 67d7a2e commit 53f798c

File tree

2 files changed

+23
-78
lines changed

2 files changed

+23
-78
lines changed

src/attributes/classes.js

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
define( [
22
"../core",
33
"../var/rnotwhite",
4-
"../data/var/dataPriv",
54
"../core/init"
6-
], function( jQuery, rnotwhite, dataPriv ) {
5+
], function( jQuery, rnotwhite ) {
76

87
var rclass = /[\t\r\n\f]/g;
98

@@ -100,60 +99,29 @@ jQuery.fn.extend( {
10099
},
101100

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

143-
// Store className if set
144-
dataPriv.set( this, "__className__", className );
145-
}
118+
// Toggle individual class names based on presence or stateVal
119+
while ( ( className = classNames[ c++ ] ) ) {
146120

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

test/unit/attributes.js

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

12231223
var testToggleClass = function( valueObj, assert ) {
1224-
assert.expect( 17 );
1224+
assert.expect( 9 );
12251225

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

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

0 commit comments

Comments
 (0)