From 243d4cc83cf15f30db1e09d9374c265a1513b18d Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Tue, 27 Nov 2012 09:46:06 -0500 Subject: [PATCH] Don't let removeClass create attributes --- src/attributes.js | 21 ++++++++++++--------- test/unit/attributes.js | 40 +++++++++++++++++++--------------------- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/src/attributes.js b/src/attributes.js index ac4b3c866..bc065481c 100644 --- a/src/attributes.js +++ b/src/attributes.js @@ -51,11 +51,12 @@ jQuery.fn.extend({ for ( ; i < len; i++ ) { elem = this[ i ]; + cur = elem.nodeType === 1 && ( elem.className ? + ( " " + elem.className + " " ).replace( rclass, " " ) : + " " + ); - if ( elem.nodeType === 1 ) { - cur = elem.className ? - ( " " + elem.className + " " ).replace( rclass, " " ) : - " "; + if ( cur ) { j = 0; while ( (clazz = classes[j++]) ) { if ( cur.indexOf( " " + clazz + " " ) < 0 ) { @@ -87,11 +88,13 @@ jQuery.fn.extend({ for ( ; i < len; i++ ) { elem = this[ i ]; - if ( elem.nodeType === 1 ) { - // This expression is here for better compressibility (see addClass) - cur = elem.className ? - ( " " + elem.className + " " ).replace( rclass, " " ) : - " "; + // This expression is here for better compressibility (see addClass) + cur = elem.nodeType === 1 && ( elem.className ? + ( " " + elem.className + " " ).replace( rclass, " " ) : + "" + ); + + if ( cur ) { j = 0; while ( (clazz = classes[j++]) ) { // Remove *all* instances diff --git a/test/unit/attributes.js b/test/unit/attributes.js index 4e3ae6e4a..7b2f21ad0 100644 --- a/test/unit/attributes.js +++ b/test/unit/attributes.js @@ -1052,38 +1052,36 @@ test( "addClass(Function) with incoming value", function() { }); var testRemoveClass = function(valueObj) { - expect( 7 ); - - var $divs = jQuery("div"); + expect( 8 ); - $divs.addClass("test").removeClass( valueObj("test") ); + var $set = jQuery("div"), + div = document.createElement("div"); - ok( !$divs.is(".test"), "Remove Class" ); + $set.addClass("test").removeClass( valueObj("test") ); - QUnit.reset(); - $divs = jQuery("div"); + ok( !$set.is(".test"), "Remove Class" ); - $divs.addClass("test").addClass("foo").addClass("bar"); - $divs.removeClass( valueObj("test") ).removeClass( valueObj("bar") ).removeClass( valueObj("foo") ); + $set.addClass("test").addClass("foo").addClass("bar"); + $set.removeClass( valueObj("test") ).removeClass( valueObj("bar") ).removeClass( valueObj("foo") ); - ok( !$divs.is(".test,.bar,.foo"), "Remove multiple classes" ); - - QUnit.reset(); - $divs = jQuery("div"); + ok( !$set.is(".test,.bar,.foo"), "Remove multiple classes" ); // Make sure that a null value doesn't cause problems - $divs.eq( 0 ).addClass("test").removeClass( valueObj( null ) ); - ok( $divs.eq( 0 ).is(".test"), "Null value passed to removeClass" ); + $set.eq( 0 ).addClass("expected").removeClass( valueObj( null ) ); + ok( $set.eq( 0 ).is(".expected"), "Null value passed to removeClass" ); - $divs.eq( 0 ).addClass("test").removeClass( valueObj("") ); - ok( $divs.eq( 0 ).is(".test"), "Empty string passed to removeClass" ); + $set.eq( 0 ).addClass("expected").removeClass( valueObj("") ); + ok( $set.eq( 0 ).is(".expected"), "Empty string passed to removeClass" ); // using contents will get regular, text, and comment nodes - var j = jQuery("#nonnodes").contents(); - j.removeClass( valueObj("asdf") ); - ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" ); + $set = jQuery("#nonnodes").contents(); + $set.removeClass( valueObj("asdf") ); + ok( !$set.hasClass("asdf"), "Check node,textnode,comment for removeClass" ); + + + jQuery( div ).removeClass( valueObj("foo") ); + strictEqual( jQuery( div ).attr("class"), undefined, "removeClass doesn't create a class attribute" ); - var div = document.createElement("div"); div.className = " test foo "; jQuery( div ).removeClass( valueObj("foo") ); -- 2.39.5