Core: Use named exports in `src/`
[jquery.git] / src / attributes / classes.js
blobfa2b7d9cd1c579c28d3773748ed335472ada3186
1 import { jQuery } from "../core.js";
2 import { stripAndCollapse } from "../core/stripAndCollapse.js";
3 import { rnothtmlwhite } from "../var/rnothtmlwhite.js";
5 import "../core/init.js";
7 function getClass( elem ) {
8         return elem.getAttribute && elem.getAttribute( "class" ) || "";
11 function classesToArray( value ) {
12         if ( Array.isArray( value ) ) {
13                 return value;
14         }
15         if ( typeof value === "string" ) {
16                 return value.match( rnothtmlwhite ) || [];
17         }
18         return [];
21 jQuery.fn.extend( {
22         addClass: function( value ) {
23                 var classNames, cur, curValue, className, i, finalValue;
25                 if ( typeof value === "function" ) {
26                         return this.each( function( j ) {
27                                 jQuery( this ).addClass( value.call( this, j, getClass( this ) ) );
28                         } );
29                 }
31                 classNames = classesToArray( value );
33                 if ( classNames.length ) {
34                         return this.each( function() {
35                                 curValue = getClass( this );
36                                 cur = this.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );
38                                 if ( cur ) {
39                                         for ( i = 0; i < classNames.length; i++ ) {
40                                                 className = classNames[ i ];
41                                                 if ( cur.indexOf( " " + className + " " ) < 0 ) {
42                                                         cur += className + " ";
43                                                 }
44                                         }
46                                         // Only assign if different to avoid unneeded rendering.
47                                         finalValue = stripAndCollapse( cur );
48                                         if ( curValue !== finalValue ) {
49                                                 this.setAttribute( "class", finalValue );
50                                         }
51                                 }
52                         } );
53                 }
55                 return this;
56         },
58         removeClass: function( value ) {
59                 var classNames, cur, curValue, className, i, finalValue;
61                 if ( typeof value === "function" ) {
62                         return this.each( function( j ) {
63                                 jQuery( this ).removeClass( value.call( this, j, getClass( this ) ) );
64                         } );
65                 }
67                 if ( !arguments.length ) {
68                         return this.attr( "class", "" );
69                 }
71                 classNames = classesToArray( value );
73                 if ( classNames.length ) {
74                         return this.each( function() {
75                                 curValue = getClass( this );
77                                 // This expression is here for better compressibility (see addClass)
78                                 cur = this.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );
80                                 if ( cur ) {
81                                         for ( i = 0; i < classNames.length; i++ ) {
82                                                 className = classNames[ i ];
84                                                 // Remove *all* instances
85                                                 while ( cur.indexOf( " " + className + " " ) > -1 ) {
86                                                         cur = cur.replace( " " + className + " ", " " );
87                                                 }
88                                         }
90                                         // Only assign if different to avoid unneeded rendering.
91                                         finalValue = stripAndCollapse( cur );
92                                         if ( curValue !== finalValue ) {
93                                                 this.setAttribute( "class", finalValue );
94                                         }
95                                 }
96                         } );
97                 }
99                 return this;
100         },
102         toggleClass: function( value, stateVal ) {
103                 var classNames, className, i, self;
105                 if ( typeof value === "function" ) {
106                         return this.each( function( i ) {
107                                 jQuery( this ).toggleClass(
108                                         value.call( this, i, getClass( this ), stateVal ),
109                                         stateVal
110                                 );
111                         } );
112                 }
114                 if ( typeof stateVal === "boolean" ) {
115                         return stateVal ? this.addClass( value ) : this.removeClass( value );
116                 }
118                 classNames = classesToArray( value );
120                 if ( classNames.length ) {
121                         return this.each( function() {
123                                 // Toggle individual class names
124                                 self = jQuery( this );
126                                 for ( i = 0; i < classNames.length; i++ ) {
127                                         className = classNames[ i ];
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                 }
139                 return this;
140         },
142         hasClass: function( selector ) {
143                 var className, elem,
144                         i = 0;
146                 className = " " + selector + " ";
147                 while ( ( elem = this[ i++ ] ) ) {
148                         if ( elem.nodeType === 1 &&
149                                 ( " " + stripAndCollapse( getClass( elem ) ) + " " ).indexOf( className ) > -1 ) {
150                                 return true;
151                         }
152                 }
154                 return false;
155         }
156 } );