Skip to content

Respect the force argument in DOMTokenList.toggle() - #1189

Merged
rbri merged 1 commit into
HtmlUnit:masterfrom
chiliec:fix-domtokenlist-toggle-force
Nov 9, 2026
Merged

rbri merged 1 commit into
HtmlUnit:masterfrom
chiliec:fix-domtokenlist-toggle-force

Conversation

@chiliec

@chiliec chiliec commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

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.

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
@sonarqubecloud

sonarqubecloud Bot commented Nov 9, 2026

Copy link
Copy Markdown

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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

OMG looks like the spec requires this.... will merge and lets see...

@rbri

rbri commented Nov 9, 2026

Copy link
Copy Markdown
Member

Thanks for the PR, please have a look at my comment...

@rbri

rbri commented Nov 9, 2026

Copy link
Copy Markdown
Member

and maybe you can add yourself as author to all classes (incl. tests) you touched

@rbri
rbri merged commit c8451c3 into HtmlUnit:master Nov 9, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DOMTokenList.toggle() ignores the force argument

2 participants