Skip to content

Commit

Permalink
Attributes: Use simpler boolean check vs a function call
Browse files Browse the repository at this point in the history
  • Loading branch information
dmethvin authored and timmywil committed Oct 18, 2015
1 parent 53f798c commit 4bf1a09
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
7 changes: 2 additions & 5 deletions src/attributes/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 );
Expand Down
6 changes: 5 additions & 1 deletion test/unit/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
Expand All @@ -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" );

Expand Down

0 comments on commit 4bf1a09

Please sign in to comment.