diff options
author | Timmy Willison <timmywillisn@gmail.com> | 2016-01-07 14:06:41 -0500 |
---|---|---|
committer | Timmy Willison <timmywillisn@gmail.com> | 2016-01-07 14:06:41 -0500 |
commit | a4d16a26abe3ea36337f7bfc4d1bfe13b893cd17 (patch) | |
tree | 00421e5ac4e6a5c7d857b3fd86f271078a9d5fa7 /src | |
parent | 1823a715660a5f31dd7e05672e9ad020066256a9 (diff) | |
download | jquery-a4d16a26abe3ea36337f7bfc4d1bfe13b893cd17.tar.gz jquery-a4d16a26abe3ea36337f7bfc4d1bfe13b893cd17.zip |
Revert "Attributes: Remove undocumented .toggleClass( boolean ) signature"
This reverts commit 53f798cf4d783bb813b4d1ba97411bc752b275f3.
- Turns out this is documented, even if not fully. Need to deprecate before removal.
Diffstat (limited to 'src')
-rw-r--r-- | src/attributes/classes.js | 69 |
1 files changed, 52 insertions, 17 deletions
diff --git a/src/attributes/classes.js b/src/attributes/classes.js index 6ab6f6efa..7933873c4 100644 --- a/src/attributes/classes.js +++ b/src/attributes/classes.js @@ -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__" ) || "" + ); } } } ); |