Skip to content

Conversation

@dmethvin
Copy link
Member

Fixes #2491

I may have gone a little crazy with the refactoring here, so feel free to yell if something looks off. It passes unit tests, and I removed the tests that dealt with the className storage.

The commit messages needs a little work, I guess I'd describe this as "removing the incorrectly documented .toggleClass( [ boolean ] ) signature. Here is what we currently say which did not match the old implementation:

As of jQuery 1.4, if no arguments are passed to .toggleClass(), all class names on the element the first time .toggleClass() is called will be toggled.

Ref jquery/api.jquery.com#781

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd get rid of checker, leaving this test something like stateVal === false || self.hasClass( className ).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gibson042 .toggleClass("cls", true) needs to add the class if it's there and avoid removing it. If stateVal is true and the element currently had the class, it would be removed rather than kept. It could be done with a few extra conditionals though, instead of the call.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right, we'd need two checks against stateVal. It might still be preferable, but I'm now less enthusiastic.

if ( stateVal !== true && self.hasClass( className ) ) {
    self.removeClass( className );
} else if ( stateVal !== false ) {
    self.addClass( className )
}

@dmethvin dmethvin force-pushed the 2491-toggleClass-undoc branch from a2b6b06 to bf95f42 Compare October 14, 2015 22:47
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the use of stateVal === false || ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In .toggleClass() the stateVal can be either true, false or undefined. The boolean cases indicate the caller wants to force the class to be added or removed. For the undefined case, the class should be added if it does not have the class and removed if it does have the class. So if you walk through the three states you can double-check that expression works for all the cases. I added a few extra tests below to catch some of the possible mistakes someone could make in editing this line. 😸

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, my bad... I realize now the 6th case here, doesn't work without your ===false||, you're right.

$( ".class" ).toggleClass( "class" )        -> removeClass
$( ".class" ).toggleClass( "class", false ) -> removeClass
$( ".class" ).toggleClass( "class", true  ) -> addClass

$( ":not(.class)" ).toggleClass( "class" )        -> addClass
$( ":not(.class)" ).toggleClass( "class", true  ) -> addClass
$( ":not(.class)" ).toggleClass( "class", false ) -> removeClass // need stateVal === false || here

Sorry, I'm not used with the || mixed with && without parentheses ^^ (in C this is throw a warning :D)

@dmethvin dmethvin closed this in 53f798c Oct 18, 2015
@dmethvin dmethvin deleted the 2491-toggleClass-undoc branch October 20, 2015 16:47
dmethvin added a commit that referenced this pull request Oct 25, 2015
Fixes gh-2491
Close gh-2618

(cherry picked from commit 53f798c)

Conflicts:
	src/attributes/classes.js
@lock lock bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

7 participants