Repository navigation
Respect the force argument in DOMTokenList.toggle() - #1189
Merged
Merged
Conversation
toggle(token, force) ignored its second argument and always flipped the token. A true force now only adds the token and a false force only removes it, and the class attribute is left untouched when the token set does not change. Without force the behavior is unchanged. Fixes HtmlUnit#1188
|
rbri
reviewed
Nov 9, 2026
| final List<String> parts = split(getValue()); | ||
| if (parts.contains(token)) { | ||
| final boolean present = parts.contains(token); | ||
| final boolean add = JavaScriptEngine.isUndefined(force) ? !present : JavaScriptEngine.toBoolean(force); |
Member
There was a problem hiding this comment.
this looks strange to me, my expectation is more like: if the value for force is not provided, then the value is false, but not dependent on the present variable. I'm wrong here?
final boolean add = JavaScriptEngine.isUndefined(force) ? false : JavaScriptEngine.toBoolean(force);
Member
There was a problem hiding this comment.
OMG looks like the spec requires this.... will merge and lets see...
Member
|
Thanks for the PR, please have a look at my comment... |
Member
|
and maybe you can add yourself as author to all classes (incl. tests) you touched |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



DOMTokenList.toggle(token, force) ignored its second argument and always flipped the token, so classList.toggle('x', false) added a missing class and classList.toggle('x', true) removed a present one.
The method now takes the force argument the same way Element.toggleAttribute already does: a true value only adds the token, a false value only removes it, and an undefined force toggles as before. The class attribute is left untouched when the token set does not change, so toggle('x', true) on a present token and toggle('x', false) on a missing one raise no MutationObserver record.
Fixes #1188
Tested with four new DOMTokenListTest cases covering force true/false on present and missing tokens (returned value, resulting token set, and attribute mutation). They fail on master and pass with this change; the existing toggle tests still pass.