diff options
author | Dave Methvin <dave.methvin@gmail.com> | 2015-09-25 17:11:01 -0400 |
---|---|---|
committer | Timmy Willison <timmywillisn@gmail.com> | 2015-10-18 12:38:59 -0400 |
commit | 53f798cf4d783bb813b4d1ba97411bc752b275f3 (patch) | |
tree | 9b4181c376b5e67b4f3966f8909108f78353d5ab /src/attributes/classes.js | |
parent | 67d7a2eefee768b59eb3d51cb1fb2c671873e58a (diff) | |
download | jquery-53f798cf4d783bb813b4d1ba97411bc752b275f3.tar.gz jquery-53f798cf4d783bb813b4d1ba97411bc752b275f3.zip |
Attributes: Remove undocumented .toggleClass( boolean ) signature
Fixes gh-2491
Close gh-2618
Diffstat (limited to 'src/attributes/classes.js')
-rw-r--r-- | src/attributes/classes.js | 76 |
1 files changed, 22 insertions, 54 deletions
diff --git a/src/attributes/classes.js b/src/attributes/classes.js index 0ba7b86af..5308ad463 100644 --- a/src/attributes/classes.js +++ b/src/attributes/classes.js @@ -1,9 +1,8 @@ define( [ "../core", "../var/rnotwhite", - "../data/var/dataPriv", "../core/init" -], function( jQuery, rnotwhite, dataPriv ) { +], function( jQuery, rnotwhite ) { var rclass = /[\t\r\n\f]/g; @@ -100,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 - dataPriv.set( 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 ? - "" : - dataPriv.get( this, "__className__" ) || "" - ); + if ( checker.call( self, className ) ) { + self.removeClass( className ); + } else { + self.addClass( className ); } } } ); |