From 4bf1a09522955eb52de1fafb4ee1ecc5982b7a3e Mon Sep 17 00:00:00 2001 From: Dave Methvin Date: Wed, 14 Oct 2015 15:07:53 -0400 Subject: [PATCH] Attributes: Use simpler boolean check vs a function call Ref gh-2491 --- src/attributes/classes.js | 7 ++----- test/unit/attributes.js | 6 +++++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/attributes/classes.js b/src/attributes/classes.js index 5308ad463..4a6ea3b2e 100644 --- a/src/attributes/classes.js +++ b/src/attributes/classes.js @@ -100,10 +100,7 @@ jQuery.fn.extend( { toggleClass: function( value, stateVal ) { var type = typeof value, - classNames = type === "string" ? value.match( rnotwhite ) : "", - checker = typeof stateVal === "boolean" ? - function() { return !stateVal; } : - jQuery.fn.hasClass; + classNames = type === "string" ? value.match( rnotwhite ) : []; return this.each( function( i ) { var className, @@ -118,7 +115,7 @@ jQuery.fn.extend( { // Toggle individual class names based on presence or stateVal while ( ( className = classNames[ c++ ] ) ) { - if ( checker.call( self, className ) ) { + if ( stateVal === false || stateVal !== true && self.hasClass( className ) ) { self.removeClass( className ); } else { self.addClass( className ); diff --git a/test/unit/attributes.js b/test/unit/attributes.js index 30131b08c..6e9f44c9f 100644 --- a/test/unit/attributes.js +++ b/test/unit/attributes.js @@ -1221,7 +1221,7 @@ QUnit.test( "removeClass(undefined) is a no-op", function( assert ) { } ); var testToggleClass = function( valueObj, assert ) { - assert.expect( 9 ); + assert.expect( 11 ); var e = jQuery( "#firstp" ); assert.ok( !e.is( ".test" ), "Assert class not present" ); @@ -1233,8 +1233,12 @@ var testToggleClass = function( valueObj, assert ) { // class name with a boolean e.toggleClass( valueObj( "test" ), false ); assert.ok( !e.is( ".test" ), "Assert class not present" ); + e.toggleClass( valueObj( "test" ), false ); + assert.ok( !e.is( ".test" ), "Assert class still not present" ); e.toggleClass( valueObj( "test" ), true ); assert.ok( e.is( ".test" ), "Assert class present" ); + e.toggleClass( valueObj( "test" ), true ); + assert.ok( e.is( ".test" ), "Assert class still present" ); e.toggleClass( valueObj( "test" ), false ); assert.ok( !e.is( ".test" ), "Assert class not present" ); -- 2.39.5