X Tutup
Skip to content

Commit fea7da7

Browse files
committed
Revert "Attributes: Remove undocumented .toggleClass( boolean ) signature"
This reverts commit 53f798c.
1 parent 13d2de7 commit fea7da7

File tree

2 files changed

+76
-18
lines changed

2 files changed

+76
-18
lines changed

src/attributes/classes.js

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

78
var rclass = /[\t\r\n\f]/g;
89

@@ -96,26 +97,60 @@ jQuery.fn.extend( {
9697
},
9798

9899
toggleClass: function( value, stateVal ) {
99-
var type = typeof value,
100-
classNames = type === "string" ? value.match( rnotwhite ) : [];
100+
var type = typeof value;
101101

102-
return this.each( function( i ) {
103-
var className,
104-
self = jQuery( this ),
105-
c = 0;
102+
if ( typeof stateVal === "boolean" && type === "string" ) {
103+
return stateVal ? this.addClass( value ) : this.removeClass( value );
104+
}
106105

107-
if ( type === "function" ) {
108-
classNames = value.call( this, i, getClass( this ), stateVal )
109-
.match( rnotwhite ) || [];
110-
}
106+
if ( jQuery.isFunction( value ) ) {
107+
return this.each( function( i ) {
108+
jQuery( this ).toggleClass(
109+
value.call( this, i, getClass( this ), stateVal ),
110+
stateVal
111+
);
112+
} );
113+
}
114+
115+
return this.each( function() {
116+
var className, i, self, classNames;
117+
118+
if ( type === "string" ) {
111119

112-
// Toggle individual class names based on presence or stateVal
113-
while ( ( className = classNames[ c++ ] ) ) {
120+
// Toggle individual class names
121+
i = 0;
122+
self = jQuery( this );
123+
classNames = value.match( rnotwhite ) || [];
124+
125+
while ( ( className = classNames[ i++ ] ) ) {
126+
127+
// Check each className given, space separated list
128+
if ( self.hasClass( className ) ) {
129+
self.removeClass( className );
130+
} else {
131+
self.addClass( className );
132+
}
133+
}
134+
135+
// Toggle whole class name
136+
} else if ( value === undefined || type === "boolean" ) {
137+
className = getClass( this );
138+
if ( className ) {
139+
140+
// Store className if set
141+
dataPriv.set( this, "__className__", className );
142+
}
114143

115-
if ( stateVal === false || stateVal !== true && self.hasClass( className ) ) {
116-
self.removeClass( className );
117-
} else {
118-
self.addClass( className );
144+
// If the element has a class name or if we're passed `false`,
145+
// then remove the whole classname (if there was one, the above saved it).
146+
// Otherwise bring back whatever was previously saved (if anything),
147+
// falling back to the empty string if nothing was stored.
148+
if ( this.setAttribute ) {
149+
this.setAttribute( "class",
150+
className || value === false ?
151+
"" :
152+
dataPriv.get( this, "__className__" ) || ""
153+
);
119154
}
120155
}
121156
} );

test/unit/attributes.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ QUnit.test( "removeClass(undefined) is a no-op", function( assert ) {
12281228
} );
12291229

12301230
var testToggleClass = function( valueObj, assert ) {
1231-
assert.expect( 11 );
1231+
assert.expect( 19 );
12321232

12331233
var e = jQuery( "#firstp" );
12341234
assert.ok( !e.is( ".test" ), "Assert class not present" );
@@ -1256,6 +1256,29 @@ var testToggleClass = function( valueObj, assert ) {
12561256
assert.ok( ( e.is( ".testA.testC" ) && !e.is( ".testB" ) ), "Assert 1 class added, 1 class removed, and 1 class kept" );
12571257
e.toggleClass( valueObj( "testA testC" ) );
12581258
assert.ok( ( !e.is( ".testA" ) && !e.is( ".testB" ) && !e.is( ".testC" ) ), "Assert no class present" );
1259+
1260+
// toggleClass storage
1261+
e.toggleClass( true );
1262+
assert.ok( e[ 0 ].className === "", "Assert class is empty (data was empty)" );
1263+
e.addClass( "testD testE" );
1264+
assert.ok( e.is( ".testD.testE" ), "Assert class present" );
1265+
e.toggleClass();
1266+
assert.ok( !e.is( ".testD.testE" ), "Assert class not present" );
1267+
assert.ok( jQuery._data( e[ 0 ], "__className__" ) === "testD testE", "Assert data was stored" );
1268+
e.toggleClass();
1269+
assert.ok( e.is( ".testD.testE" ), "Assert class present (restored from data)" );
1270+
e.toggleClass( false );
1271+
assert.ok( !e.is( ".testD.testE" ), "Assert class not present" );
1272+
e.toggleClass( true );
1273+
assert.ok( e.is( ".testD.testE" ), "Assert class present (restored from data)" );
1274+
e.toggleClass();
1275+
e.toggleClass( false );
1276+
e.toggleClass();
1277+
assert.ok( e.is( ".testD.testE" ), "Assert class present (restored from data)" );
1278+
1279+
// Cleanup
1280+
e.removeClass( "testD" );
1281+
assert.expectJqData( this, e[ 0 ], "__className__" );
12591282
};
12601283

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

0 commit comments

Comments
 (0)
X Tutup