3 "../core/stripAndCollapse",
5 "../var/rnothtmlwhite",
6 "../data/var/dataPriv",
8 ], function( jQuery, stripAndCollapse, isFunction, rnothtmlwhite, dataPriv ) {
12 function getClass( elem ) {
13 return elem.getAttribute && elem.getAttribute( "class" ) || "";
16 function classesToArray( value ) {
17 if ( Array.isArray( value ) ) {
20 if ( typeof value === "string" ) {
21 return value.match( rnothtmlwhite ) || [];
27 addClass: function( value ) {
28 var classes, elem, cur, curValue, clazz, j, finalValue,
31 if ( isFunction( value ) ) {
32 return this.each( function( j ) {
33 jQuery( this ).addClass( value.call( this, j, getClass( this ) ) );
37 classes = classesToArray( value );
39 if ( classes.length ) {
40 while ( ( elem = this[ i++ ] ) ) {
41 curValue = getClass( elem );
42 cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );
46 while ( ( clazz = classes[ j++ ] ) ) {
47 if ( cur.indexOf( " " + clazz + " " ) < 0 ) {
52 // Only assign if different to avoid unneeded rendering.
53 finalValue = stripAndCollapse( cur );
54 if ( curValue !== finalValue ) {
55 elem.setAttribute( "class", finalValue );
64 removeClass: function( value ) {
65 var classes, elem, cur, curValue, clazz, j, finalValue,
68 if ( isFunction( value ) ) {
69 return this.each( function( j ) {
70 jQuery( this ).removeClass( value.call( this, j, getClass( this ) ) );
74 if ( !arguments.length ) {
75 return this.attr( "class", "" );
78 classes = classesToArray( value );
80 if ( classes.length ) {
81 while ( ( elem = this[ i++ ] ) ) {
82 curValue = getClass( elem );
84 // This expression is here for better compressibility (see addClass)
85 cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );
89 while ( ( clazz = classes[ j++ ] ) ) {
91 // Remove *all* instances
92 while ( cur.indexOf( " " + clazz + " " ) > -1 ) {
93 cur = cur.replace( " " + clazz + " ", " " );
97 // Only assign if different to avoid unneeded rendering.
98 finalValue = stripAndCollapse( cur );
99 if ( curValue !== finalValue ) {
100 elem.setAttribute( "class", finalValue );
109 toggleClass: function( value, stateVal ) {
110 var type = typeof value,
111 isValidValue = type === "string" || Array.isArray( value );
113 if ( typeof stateVal === "boolean" && isValidValue ) {
114 return stateVal ? this.addClass( value ) : this.removeClass( value );
117 if ( isFunction( value ) ) {
118 return this.each( function( i ) {
119 jQuery( this ).toggleClass(
120 value.call( this, i, getClass( this ), stateVal ),
126 return this.each( function() {
127 var className, i, self, classNames;
129 if ( isValidValue ) {
131 // Toggle individual class names
133 self = jQuery( this );
134 classNames = classesToArray( value );
136 while ( ( className = classNames[ i++ ] ) ) {
138 // Check each className given, space separated list
139 if ( self.hasClass( className ) ) {
140 self.removeClass( className );
142 self.addClass( className );
146 // Toggle whole class name
147 } else if ( value === undefined || type === "boolean" ) {
148 className = getClass( this );
151 // Store className if set
152 dataPriv.set( this, "__className__", className );
155 // If the element has a class name or if we're passed `false`,
156 // then remove the whole classname (if there was one, the above saved it).
157 // Otherwise bring back whatever was previously saved (if anything),
158 // falling back to the empty string if nothing was stored.
159 if ( this.setAttribute ) {
160 this.setAttribute( "class",
161 className || value === false ?
163 dataPriv.get( this, "__className__" ) || ""
170 hasClass: function( selector ) {
174 className = " " + selector + " ";
175 while ( ( elem = this[ i++ ] ) ) {
176 if ( elem.nodeType === 1 &&
177 ( " " + stripAndCollapse( getClass( elem ) ) + " " ).indexOf( className ) > -1 ) {