]> source.dussan.org Git - jquery.git/commitdiff
Revert "Attributes: Remove undocumented .toggleClass( boolean ) signature"
authorTimmy Willison <timmywillisn@gmail.com>
Thu, 7 Jan 2016 19:06:41 +0000 (14:06 -0500)
committerTimmy Willison <timmywillisn@gmail.com>
Thu, 7 Jan 2016 19:06:41 +0000 (14:06 -0500)
This reverts commit 53f798cf4d783bb813b4d1ba97411bc752b275f3.

- Turns out this is documented, even if not fully. Need to deprecate before removal.

src/attributes/classes.js
test/unit/attributes.js

index 6ab6f6efa5d41f9d75e208dc480caa500af52590..7933873c4f97939d8318548990598aafd91f091e 100644 (file)
@@ -1,8 +1,9 @@
 define( [
        "../core",
        "../var/rnotwhite",
+       "../data/var/dataPriv",
        "../core/init"
-], function( jQuery, rnotwhite ) {
+], function( jQuery, rnotwhite, dataPriv ) {
 
 var rclass = /[\t\r\n\f]/g;
 
@@ -96,26 +97,60 @@ jQuery.fn.extend( {
        },
 
        toggleClass: function( value, stateVal ) {
-               var type = typeof value,
-                       classNames = type === "string" ? value.match( rnotwhite ) : [];
+               var type = typeof value;
 
-               return this.each( function( i ) {
-                       var className,
-                               self = jQuery( this ),
-                               c = 0;
+               if ( typeof stateVal === "boolean" && type === "string" ) {
+                       return stateVal ? this.addClass( value ) : this.removeClass( value );
+               }
 
-                       if ( type === "function" ) {
-                               classNames = value.call( this, i, getClass( this ), stateVal )
-                                       .match( rnotwhite ) || [];
-                       }
+               if ( jQuery.isFunction( value ) ) {
+                       return this.each( function( i ) {
+                               jQuery( this ).toggleClass(
+                                       value.call( this, i, getClass( this ), stateVal ),
+                                       stateVal
+                               );
+                       } );
+               }
+
+               return this.each( function() {
+                       var className, i, self, classNames;
+
+                       if ( type === "string" ) {
 
-                       // Toggle individual class names based on presence or stateVal
-                       while ( ( className = classNames[ c++ ] ) ) {
+                               // Toggle individual class names
+                               i = 0;
+                               self = jQuery( this );
+                               classNames = value.match( rnotwhite ) || [];
+
+                               while ( ( className = classNames[ i++ ] ) ) {
+
+                                       // Check each className given, space separated list
+                                       if ( self.hasClass( className ) ) {
+                                               self.removeClass( className );
+                                       } else {
+                                               self.addClass( className );
+                                       }
+                               }
+
+                       // Toggle whole class name
+                       } else if ( value === undefined || type === "boolean" ) {
+                               className = getClass( this );
+                               if ( className ) {
+
+                                       // Store className if set
+                                       dataPriv.set( this, "__className__", className );
+                               }
 
-                               if ( stateVal === false || stateVal !== true && self.hasClass( className ) ) {
-                                       self.removeClass( className );
-                               } else {
-                                       self.addClass( className );
+                               // If the element has a class name or if we're passed `false`,
+                               // then remove the whole classname (if there was one, the above saved it).
+                               // Otherwise bring back whatever was previously saved (if anything),
+                               // falling back to the empty string if nothing was stored.
+                               if ( this.setAttribute ) {
+                                       this.setAttribute( "class",
+                                               className || value === false ?
+                                               "" :
+                                               dataPriv.get( this, "__className__" ) || ""
+                                       );
                                }
                        }
                } );
index cbf83d0ec46008771b31ce1683e23e45702a1138..7f92d3505b8faed0312d531ab4dbf8f85bdaaac0 100644 (file)
@@ -1239,7 +1239,7 @@ QUnit.test( "removeClass(undefined) is a no-op", function( assert ) {
 } );
 
 var testToggleClass = function( valueObj, assert ) {
-       assert.expect( 11 );
+       assert.expect( 19 );
 
        var e = jQuery( "#firstp" );
        assert.ok( !e.is( ".test" ), "Assert class not present" );
@@ -1267,6 +1267,29 @@ var testToggleClass = function( valueObj, assert ) {
        assert.ok( ( e.is( ".testA.testC" ) && !e.is( ".testB" ) ), "Assert 1 class added, 1 class removed, and 1 class kept" );
        e.toggleClass( valueObj( "testA testC" ) );
        assert.ok( ( !e.is( ".testA" ) && !e.is( ".testB" ) && !e.is( ".testC" ) ), "Assert no class present" );
+
+       // toggleClass storage
+       e.toggleClass( true );
+       assert.ok( e[ 0 ].className === "", "Assert class is empty (data was empty)" );
+       e.addClass( "testD testE" );
+       assert.ok( e.is( ".testD.testE" ), "Assert class present" );
+       e.toggleClass();
+       assert.ok( !e.is( ".testD.testE" ), "Assert class not present" );
+       assert.ok( jQuery._data( e[ 0 ], "__className__" ) === "testD testE", "Assert data was stored" );
+       e.toggleClass();
+       assert.ok( e.is( ".testD.testE" ), "Assert class present (restored from data)" );
+       e.toggleClass( false );
+       assert.ok( !e.is( ".testD.testE" ), "Assert class not present" );
+       e.toggleClass( true );
+       assert.ok( e.is( ".testD.testE" ), "Assert class present (restored from data)" );
+       e.toggleClass();
+       e.toggleClass( false );
+       e.toggleClass();
+       assert.ok( e.is( ".testD.testE" ), "Assert class present (restored from data)" );
+
+       // Cleanup
+       e.removeClass( "testD" );
+       assert.expectJqData( this, e[ 0 ], "__className__" );
 };
 
 QUnit.test( "toggleClass(String|boolean|undefined[, boolean])", function( assert ) {