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 ) ) {
15 if ( typeof value === "string" ) {
16 return value.match( rnothtmlwhite ) || [];
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 ) ) );
31 classNames = classesToArray( value );
33 if ( classNames.length ) {
34 return this.each( function() {
35 curValue = getClass( this );
36 cur = this.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );
39 for ( i = 0; i < classNames.length; i++ ) {
40 className = classNames[ i ];
41 if ( cur.indexOf( " " + className + " " ) < 0 ) {
42 cur += className + " ";
46 // Only assign if different to avoid unneeded rendering.
47 finalValue = stripAndCollapse( cur );
48 if ( curValue !== finalValue ) {
49 this.setAttribute( "class", finalValue );
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 ) ) );
67 if ( !arguments.length ) {
68 return this.attr( "class", "" );
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 ) + " " );
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 + " ", " " );
90 // Only assign if different to avoid unneeded rendering.
91 finalValue = stripAndCollapse( cur );
92 if ( curValue !== finalValue ) {
93 this.setAttribute( "class", finalValue );
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 ),
114 if ( typeof stateVal === "boolean" ) {
115 return stateVal ? this.addClass( value ) : this.removeClass( value );
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 );
133 self.addClass( className );
142 hasClass: function( selector ) {
146 className = " " + selector + " ";
147 while ( ( elem = this[ i++ ] ) ) {
148 if ( elem.nodeType === 1 &&
149 ( " " + stripAndCollapse( getClass( elem ) ) + " " ).indexOf( className ) > -1 ) {