3 "../core/stripAndCollapse",
4 "../var/rnothtmlwhite",
5 "../data/var/dataPriv",
7 ], function( jQuery, stripAndCollapse, rnothtmlwhite, dataPriv ) {
11 function getClass( elem ) {
12 return elem.getAttribute && elem.getAttribute( "class" ) || "";
16 addClass: function( value ) {
17 var classes, elem, cur, curValue, clazz, j, finalValue,
20 if ( jQuery.isFunction( value ) ) {
21 return this.each( function( j ) {
22 jQuery( this ).addClass( value.call( this, j, getClass( this ) ) );
26 if ( typeof value === "string" && value ) {
27 classes = value.match( rnothtmlwhite ) || [];
29 while ( ( elem = this[ i++ ] ) ) {
30 curValue = getClass( elem );
31 cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );
35 while ( ( clazz = classes[ j++ ] ) ) {
36 if ( cur.indexOf( " " + clazz + " " ) < 0 ) {
41 // Only assign if different to avoid unneeded rendering.
42 finalValue = stripAndCollapse( cur );
43 if ( curValue !== finalValue ) {
44 elem.setAttribute( "class", finalValue );
53 removeClass: function( value ) {
54 var classes, elem, cur, curValue, clazz, j, finalValue,
57 if ( jQuery.isFunction( value ) ) {
58 return this.each( function( j ) {
59 jQuery( this ).removeClass( value.call( this, j, getClass( this ) ) );
63 if ( !arguments.length ) {
64 return this.attr( "class", "" );
67 if ( typeof value === "string" && value ) {
68 classes = value.match( rnothtmlwhite ) || [];
70 while ( ( elem = this[ i++ ] ) ) {
71 curValue = getClass( elem );
73 // This expression is here for better compressibility (see addClass)
74 cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );
78 while ( ( clazz = classes[ j++ ] ) ) {
80 // Remove *all* instances
81 while ( cur.indexOf( " " + clazz + " " ) > -1 ) {
82 cur = cur.replace( " " + clazz + " ", " " );
86 // Only assign if different to avoid unneeded rendering.
87 finalValue = stripAndCollapse( cur );
88 if ( curValue !== finalValue ) {
89 elem.setAttribute( "class", finalValue );
98 toggleClass: function( value, stateVal ) {
99 var type = typeof value;
101 if ( typeof stateVal === "boolean" && type === "string" ) {
102 return stateVal ? this.addClass( value ) : this.removeClass( value );
105 if ( jQuery.isFunction( value ) ) {
106 return this.each( function( i ) {
107 jQuery( this ).toggleClass(
108 value.call( this, i, getClass( this ), stateVal ),
114 return this.each( function() {
115 var className, i, self, classNames;
117 if ( type === "string" ) {
119 // Toggle individual class names
121 self = jQuery( this );
122 classNames = value.match( rnothtmlwhite ) || [];
124 while ( ( className = classNames[ i++ ] ) ) {
126 // Check each className given, space separated list
127 if ( self.hasClass( className ) ) {
128 self.removeClass( className );
130 self.addClass( className );
134 // Toggle whole class name
135 } else if ( value === undefined || type === "boolean" ) {
136 className = getClass( this );
139 // Store className if set
140 dataPriv.set( this, "__className__", className );
143 // If the element has a class name or if we're passed `false`,
144 // then remove the whole classname (if there was one, the above saved it).
145 // Otherwise bring back whatever was previously saved (if anything),
146 // falling back to the empty string if nothing was stored.
147 if ( this.setAttribute ) {
148 this.setAttribute( "class",
149 className || value === false ?
151 dataPriv.get( this, "__className__" ) || ""
158 hasClass: function( selector ) {
162 className = " " + selector + " ";
163 while ( ( elem = this[ i++ ] ) ) {
164 if ( elem.nodeType === 1 &&
165 ( " " + stripAndCollapse( getClass( elem ) ) + " " ).indexOf( className ) > -1 ) {