@@ -99,60 +99,29 @@ jQuery.fn.extend( {
9999 } ,
100100
101101 toggleClass : function ( value , stateVal ) {
102- var type = typeof value ;
103-
104- if ( typeof stateVal === "boolean" && type === "string" ) {
105- return stateVal ? this . addClass ( value ) : this . removeClass ( value ) ;
106- }
107-
108- if ( jQuery . isFunction ( value ) ) {
109- return this . each ( function ( i ) {
110- jQuery ( this ) . toggleClass (
111- value . call ( this , i , getClass ( this ) , stateVal ) ,
112- stateVal
113- ) ;
114- } ) ;
115- }
116-
117- return this . each ( function ( ) {
118- var className , i , self , classNames ;
119-
120- if ( type === "string" ) {
121-
122- // Toggle individual class names
123- i = 0 ;
124- self = jQuery ( this ) ;
125- classNames = value . match ( rnotwhite ) || [ ] ;
126-
127- while ( ( className = classNames [ i ++ ] ) ) {
128-
129- // Check each className given, space separated list
130- if ( self . hasClass ( className ) ) {
131- self . removeClass ( className ) ;
132- } else {
133- self . addClass ( className ) ;
134- }
135- }
136-
137- // Toggle whole class name
138- } else if ( value === undefined || type === "boolean" ) {
139- className = getClass ( this ) ;
140- if ( className ) {
102+ var type = typeof value ,
103+ classNames = type === "string" ? value . match ( rnotwhite ) : "" ,
104+ checker = typeof stateVal === "boolean" ?
105+ function ( ) { return ! stateVal ; } :
106+ jQuery . fn . hasClass ;
107+
108+ return this . each ( function ( i ) {
109+ var className ,
110+ self = jQuery ( this ) ,
111+ c = 0 ;
112+
113+ if ( type === "function" ) {
114+ classNames = value . call ( this , i , getClass ( this ) , stateVal )
115+ . match ( rnotwhite ) || [ ] ;
116+ }
141117
142- // store className if set
143- jQuery . _data ( this , "__className__" , className ) ;
144- }
118+ // Toggle individual class names based on presence or stateVal
119+ while ( ( className = classNames [ c ++ ] ) ) {
145120
146- // If the element has a class name or if we're passed "false",
147- // then remove the whole classname (if there was one, the above saved it).
148- // Otherwise bring back whatever was previously saved (if anything),
149- // falling back to the empty string if nothing was stored.
150- if ( this . setAttribute ) {
151- this . setAttribute ( "class" ,
152- className || value === false ?
153- "" :
154- jQuery . _data ( this , "__className__" ) || ""
155- ) ;
121+ if ( checker . call ( self , className ) ) {
122+ self . removeClass ( className ) ;
123+ } else {
124+ self . addClass ( className ) ;
156125 }
157126 }
158127 } ) ;
0 commit comments