-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Attributes: Remove undocumented .toggleClass( boolean ) signature #2618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
src/attributes/classes.js
Outdated
There was a problem hiding this comment.
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 ).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 )
}a2b6b06 to
bf95f42
Compare
There was a problem hiding this comment.
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 || ?
There was a problem hiding this comment.
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. 😸
There was a problem hiding this comment.
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 || hereSorry, I'm not used with the || mixed with && without parentheses ^^ (in C this is throw a warning :D)
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:Ref jquery/api.jquery.com#781