]> source.dussan.org Git - jquery.git/commitdiff
Make removeClass smart enough to remove duplicates. Fixes #11923
authorRick Waldron <waldron.rick@gmail.com>
Sat, 23 Jun 2012 23:38:27 +0000 (19:38 -0400)
committerRick Waldron <waldron.rick@gmail.com>
Sat, 23 Jun 2012 23:38:27 +0000 (19:38 -0400)
src/attributes.js
test/unit/attributes.js

index 23a4fddd085e13e6b6ad960c474a944f3fc11d76..6ec1c7154384bf7f3764aa58ec45ac139dc680d9 100644 (file)
@@ -71,31 +71,30 @@ jQuery.fn.extend({
        },
 
        removeClass: function( value ) {
-               var classNames, i, l, elem, className, c, cl;
+               var removes, className, elem, c, cl, i, l;
 
                if ( jQuery.isFunction( value ) ) {
                        return this.each(function( j ) {
                                jQuery( this ).removeClass( value.call(this, j, this.className) );
                        });
                }
-
                if ( (value && typeof value === "string") || value === undefined ) {
-                       classNames = ( value || "" ).split( core_rspace );
+                       removes = ( value || "" ).split( core_rspace );
 
                        for ( i = 0, l = this.length; i < l; i++ ) {
                                elem = this[ i ];
-
                                if ( elem.nodeType === 1 && elem.className ) {
-                                       if ( value ) {
-                                               className = (" " + elem.className + " ").replace( rclass, " " );
-                                               for ( c = 0, cl = classNames.length; c < cl; c++ ) {
-                                                       className = className.replace(" " + classNames[ c ] + " ", " ");
-                                               }
-                                               elem.className = jQuery.trim( className );
 
-                                       } else {
-                                               elem.className = "";
+                                       className = (" " + elem.className + " ").replace( rclass, " " );
+
+                                       // loop over each item in the removal list
+                                       for ( c = 0, cl = removes.length; c < cl; c++ ) {
+                                               // Remove until there is nothing to remove,
+                                               while ( className.indexOf(" " + removes[ c ] + " ") > -1 ) {
+                                                       className = className.replace( " " + removes[ c ] + " " , " " );
+                                               }
                                        }
+                                       elem.className = value ? jQuery.trim( className ) : "";
                                }
                        }
                }
index ad7f79d1efbb2819069eba0f268dd8e3121eea7c..b6a1b5d0ee3e8493e3f78e7eb59b481ce1e5af2f 100644 (file)
@@ -1055,6 +1055,16 @@ test("removeClass(Function) with incoming value", function() {
        QUnit.reset();
 });
 
+test("removeClass() removes duplicates", function() {
+       expect(1);
+
+       var $div = jQuery( jQuery.parseHTML("<div class='x x x'></div>") );
+
+       $div.removeClass("x");
+
+       ok( !$div.hasClass("x"), "Element with multiple same classes does not escape the wrath of removeClass()" );
+});
+
 var testToggleClass = function(valueObj) {
        expect(17);