Skip to content
Permalink
Browse files

Don't let removeClass create attributes

  • Loading branch information
gibson042 committed Nov 27, 2012
1 parent b9dfcfd commit 243d4cc83cf15f30db1e09d9374c265a1513b18d
Showing with 31 additions and 30 deletions.
  1. +12 −9 src/attributes.js
  2. +19 −21 test/unit/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
@@ -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") );

0 comments on commit 243d4cc

Please sign in to comment.
You can’t perform that action at this time.