Skip to content

Commit 5db1e05

Browse files
mr21timmywil
authored andcommitted
Attributes: removeClass() -> attr("class", "")
- Classes simpliciation Close gh-2465
1 parent 4bf1a09 commit 5db1e05

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

src/attributes/classes.js

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,18 @@ function getClass( elem ) {
1313
jQuery.fn.extend( {
1414
addClass: function( value ) {
1515
var classes, elem, cur, curValue, clazz, j, finalValue,
16-
proceed = typeof value === "string" && value,
17-
i = 0,
18-
len = this.length;
16+
i = 0;
1917

2018
if ( jQuery.isFunction( value ) ) {
2119
return this.each( function( j ) {
2220
jQuery( this ).addClass( value.call( this, j, getClass( this ) ) );
2321
} );
2422
}
2523

26-
if ( proceed ) {
24+
if ( typeof value === "string" && value ) {
25+
classes = value.match( rnotwhite ) || [];
2726

28-
// The disjunction here is for better compressibility (see removeClass)
29-
classes = ( value || "" ).match( rnotwhite ) || [];
30-
31-
for ( ; i < len; i++ ) {
32-
elem = this[ i ];
27+
while ( ( elem = this[ i++ ] ) ) {
3328
curValue = getClass( elem );
3429
cur = elem.nodeType === 1 &&
3530
( " " + curValue + " " ).replace( rclass, " " );
@@ -56,20 +51,22 @@ jQuery.fn.extend( {
5651

5752
removeClass: function( value ) {
5853
var classes, elem, cur, curValue, clazz, j, finalValue,
59-
proceed = arguments.length === 0 || typeof value === "string" && value,
60-
i = 0,
61-
len = this.length;
54+
i = 0;
6255

6356
if ( jQuery.isFunction( value ) ) {
6457
return this.each( function( j ) {
6558
jQuery( this ).removeClass( value.call( this, j, getClass( this ) ) );
6659
} );
6760
}
68-
if ( proceed ) {
69-
classes = ( value || "" ).match( rnotwhite ) || [];
7061

71-
for ( ; i < len; i++ ) {
72-
elem = this[ i ];
62+
if ( !arguments.length ) {
63+
return this.attr( "class", "" );
64+
}
65+
66+
if ( typeof value === "string" && value ) {
67+
classes = value.match( rnotwhite ) || [];
68+
69+
while ( ( elem = this[ i++ ] ) ) {
7370
curValue = getClass( elem );
7471

7572
// This expression is here for better compressibility (see addClass)
@@ -87,7 +84,7 @@ jQuery.fn.extend( {
8784
}
8885

8986
// Only assign if different to avoid unneeded rendering.
90-
finalValue = value ? jQuery.trim( cur ) : "";
87+
finalValue = jQuery.trim( cur );
9188
if ( curValue !== finalValue ) {
9289
elem.setAttribute( "class", finalValue );
9390
}
@@ -125,12 +122,13 @@ jQuery.fn.extend( {
125122
},
126123

127124
hasClass: function( selector ) {
128-
var className = " " + selector + " ",
129-
i = 0,
130-
l = this.length;
131-
for ( ; i < l; i++ ) {
132-
if ( this[ i ].nodeType === 1 &&
133-
( " " + getClass( this[ i ] ) + " " ).replace( rclass, " " )
125+
var className, elem,
126+
i = 0;
127+
128+
className = " " + selector + " ";
129+
while ( ( elem = this[ i++ ] ) ) {
130+
if ( elem.nodeType === 1 &&
131+
( " " + getClass( elem ) + " " ).replace( rclass, " " )
134132
.indexOf( className ) > -1
135133
) {
136134
return true;

0 commit comments

Comments
 (0)