return elem.getAttribute && elem.getAttribute( "class" ) || "";
}
+function classesToArray( value ) {
+ if ( Array.isArray( value ) ) {
+ return value;
+ }
+ if ( typeof value === "string" ) {
+ return value.match( rnothtmlwhite ) || [];
+ }
+ return [];
+}
+
jQuery.fn.extend( {
addClass: function( value ) {
var classes, elem, cur, curValue, clazz, j, finalValue,
} );
}
- if ( typeof value === "string" && value ) {
- classes = value.match( rnothtmlwhite ) || [];
+ classes = classesToArray( value );
+ if ( classes.length ) {
while ( ( elem = this[ i++ ] ) ) {
curValue = getClass( elem );
cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );
return this.attr( "class", "" );
}
- if ( typeof value === "string" && value ) {
- classes = value.match( rnothtmlwhite ) || [];
+ classes = classesToArray( value );
+ if ( classes.length ) {
while ( ( elem = this[ i++ ] ) ) {
curValue = getClass( elem );
},
toggleClass: function( value, stateVal ) {
- var type = typeof value;
+ var type = typeof value,
+ isValidValue = type === "string" || Array.isArray( value );
- if ( typeof stateVal === "boolean" && type === "string" ) {
+ if ( typeof stateVal === "boolean" && isValidValue ) {
return stateVal ? this.addClass( value ) : this.removeClass( value );
}
return this.each( function() {
var className, i, self, classNames;
- if ( type === "string" ) {
+ if ( isValidValue ) {
// Toggle individual class names
i = 0;
self = jQuery( this );
- classNames = value.match( rnothtmlwhite ) || [];
+ classNames = classesToArray( value );
while ( ( className = classNames[ i++ ] ) ) {
};
}
+function arrayFromString( value ) {
+ return value ? value.split( " " ) : [];
+}
+
/*
======== local reference =======
bareObj and functionReturningObj can be used to test passing functions to setters
testAddClass( functionReturningObj, assert );
} );
+QUnit.test( "addClass(Array)", function( assert ) {
+ testAddClass( arrayFromString, assert );
+} );
+
QUnit.test( "addClass(Function) with incoming value", function( assert ) {
assert.expect( 52 );
var pass, i,
testRemoveClass( functionReturningObj, assert );
} );
+QUnit.test( "removeClass(Array) - simple", function( assert ) {
+ testRemoveClass( arrayFromString, assert );
+} );
+
QUnit.test( "removeClass(Function) with incoming value", function( assert ) {
assert.expect( 52 );
testToggleClass( functionReturningObj, assert );
} );
+QUnit.test( "toggleClass(Array[, boolean])", function( assert ) {
+ testToggleClass( arrayFromString, assert );
+} );
+
QUnit.test( "toggleClass(Function[, boolean]) with incoming value", function( assert ) {
assert.expect( 14 );
"Did not find a class when not present" );
} );
+QUnit.test( "addClass, removeClass, hasClass on many elements - Array", function( assert ) {
+ assert.expect( 16 );
+
+ var elem = jQuery( "<p>p0</p><p>p1</p><p>p2</p>" );
+
+ elem.addClass( [ "hi" ] );
+ assert.equal( elem[ 0 ].className, "hi", "Check single added class" );
+ assert.equal( elem[ 1 ].className, "hi", "Check single added class" );
+ assert.equal( elem[ 2 ].className, "hi", "Check single added class" );
+
+ elem.addClass( [ "foo", "bar" ] );
+ assert.equal( elem[ 0 ].className, "hi foo bar", "Check more added classes" );
+ assert.equal( elem[ 1 ].className, "hi foo bar", "Check more added classes" );
+ assert.equal( elem[ 2 ].className, "hi foo bar", "Check more added classes" );
+
+ elem.removeClass();
+ assert.equal( elem[ 0 ].className, "", "Remove all classes" );
+ assert.equal( elem[ 1 ].className, "", "Remove all classes" );
+ assert.equal( elem[ 2 ].className, "", "Remove all classes" );
+
+ elem.addClass( [ "hi", "foo", "bar", "baz" ] );
+ elem.removeClass( [ "foo" ] );
+ assert.equal( elem[ 0 ].className, "hi bar baz", "Check removal of one class" );
+ assert.equal( elem[ 1 ].className, "hi bar baz", "Check removal of one class" );
+ assert.equal( elem[ 2 ].className, "hi bar baz", "Check removal of one class" );
+
+ elem.removeClass( [ "bar baz" ] );
+ assert.equal( elem[ 0 ].className, "hi", "Check removal of two classes" );
+ assert.equal( elem[ 1 ].className, "hi", "Check removal of two classes" );
+ assert.equal( elem[ 2 ].className, "hi", "Check removal of two classes" );
+
+ assert.ok( elem.hasClass( "hi" ), "Check has1" );
+} );
+
QUnit.test( "addClass, removeClass, hasClass on elements with classes with non-HTML whitespace (gh-3072, gh-3003)", function( assert ) {
assert.expect( 9 );