]> source.dussan.org Git - jquery.git/commitdiff
Attributes: Remove undocumented .toggleClass( boolean ) signature
authorDave Methvin <dave.methvin@gmail.com>
Fri, 25 Sep 2015 21:11:01 +0000 (17:11 -0400)
committerRichard Gibson <richard.gibson@gmail.com>
Sun, 25 Oct 2015 19:00:48 +0000 (15:00 -0400)
Fixes gh-2491
Close gh-2618

(cherry picked from commit 53f798cf4d783bb813b4d1ba97411bc752b275f3)

Conflicts:
src/attributes/classes.js

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

index 4e2d2adf305ca71fc67294f38224507b33e1277b..1d24a1851ee179a6e35ecf4cd94b2beb5d3e37a7 100644 (file)
@@ -99,60 +99,29 @@ jQuery.fn.extend( {
        },
 
        toggleClass: function( value, stateVal ) {
-               var type = typeof value;
-
-               if ( typeof stateVal === "boolean" && type === "string" ) {
-                       return stateVal ? this.addClass( value ) : this.removeClass( value );
-               }
-
-               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
-                               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 ) {
+               var type = typeof value,
+                       classNames = type === "string" ? value.match( rnotwhite ) : "",
+                       checker = typeof stateVal === "boolean" ?
+                               function() { return !stateVal; } :
+                               jQuery.fn.hasClass;
+
+               return this.each( function( i ) {
+                       var className,
+                               self = jQuery( this ),
+                               c = 0;
+
+                       if ( type === "function" ) {
+                               classNames = value.call( this, i, getClass( this ), stateVal )
+                                       .match( rnotwhite ) || [];
+                       }
 
-                                       // store className if set
-                                       jQuery._data( this, "__className__", className );
-                               }
+                       // Toggle individual class names based on presence or stateVal
+                       while ( ( className = classNames[ c++ ] ) ) {
 
-                               // 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 ?
-                                               "" :
-                                               jQuery._data( this, "__className__" ) || ""
-                                       );
+                               if ( checker.call( self, className ) ) {
+                                       self.removeClass( className );
+                               } else {
+                                       self.addClass( className );
                                }
                        }
                } );
index f8ded417a8d2d92ad3402a86f4493d80a60d0edc..028931c70794f613c04d3d6db850c6d50c352379 100644 (file)
@@ -1220,7 +1220,7 @@ QUnit.test( "removeClass(undefined) is a no-op", function( assert ) {
 } );
 
 var testToggleClass = function( valueObj, assert ) {
-       assert.expect( 17 );
+       assert.expect( 9 );
 
        var e = jQuery( "#firstp" );
        assert.ok( !e.is( ".test" ), "Assert class not present" );
@@ -1244,29 +1244,6 @@ 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 ) {