This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 25869 - DOMClassList.toggle 'force' parameter is "wonky API design"
Summary: DOMClassList.toggle 'force' parameter is "wonky API design"
Status: RESOLVED WONTFIX
Alias: None
Product: WebAppsWG
Classification: Unclassified
Component: DOM (show other bugs)
Version: unspecified
Hardware: PC Linux
: P2 normal
Target Milestone: ---
Assignee: Anne
QA Contact: public-webapps-bugzilla
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-05-23 00:27 UTC by Robert O'Callahan (Mozilla)
Modified: 2014-08-14 14:57 UTC (History)
8 users (show)

See Also:


Attachments

Description Robert O'Callahan (Mozilla) 2014-05-23 00:27:51 UTC
The "force" parameter makes the toggle() API not toggle. This seems wrong.

DOMClassList.set(classname, bool) would be much better.

#whatwg feedback, FWIW:
http://krijnhoetmer.nl/irc-logs/whatwg/20140523#l-109
Comment 1 Anne 2014-05-23 06:49:05 UTC
This is shipping in browsers. E.g. try

<script>
document.body.classList.toggle("test", false); document.body.classList.toggle("x", true); 
</script>

in Live DOM Viewer or some such. Seems like a pain to change now.
Comment 2 Robert O'Callahan (Mozilla) 2014-05-23 09:19:01 UTC
OK, but is it used yet?
Comment 3 Anne 2014-05-23 09:21:08 UTC
I guess someone would need to add telemetry / use counters to browsers. I can't answer that question.
Comment 4 Domenic Denicola 2014-05-30 05:51:47 UTC
I strongly disagree with this bug. "Toggling something to true" is a very common phrase in English. Even if it were not, jQuery's usage of it as such for many years has made it a common developer expectation. In fact, I remember specific developer complaints that classList's toggle did *not* have this functionality, back in the day: a kind of "why didn't they just copy jQuery; they still Just Don't Get It" diatribe.
Comment 5 Glenn Maynard 2014-05-30 14:02:21 UTC
No, it's definitely a bad, unintuitive API.  I would never expect x.toggle(true) to mean "set x to true".  It should have been "x.set(true/false)".  We shouldn't copy bad APIs from jQuery because we think people will scoff at us for doing things better.

However, I don't know if it's worth the effort it would take to find out if we can change this, and then to change it, with it already implemented in multiple browsers.  It's probably just another ugly API that we'll have to live with.

We could add set(value) and leave toggle(value) in, but then we pay the cost of redundant APIs, which is probably worse.
Comment 6 Glenn Maynard 2014-05-30 14:38:47 UTC
(In reply to Domenic Denicola from comment #4)
> I strongly disagree with this bug. "Toggling something to true" is a very
> common phrase in English.

To clarify, "toggle something to X" isn't what "toggle" actions mean; the word programmers use is "set" (or "assign", or occasionally "put" or "write", but never "toggle").  "Toggle" means a change based on the previous state.
Comment 7 Domenic Denicola 2014-05-30 14:42:44 UTC
I disagree that the word programmers use is "set." The word programmers use is often "toggle," especially in the case of classes, visibility, or animations.
Comment 8 Anne 2014-06-03 13:49:14 UTC
This was not fixed. It might be WONTFIX, but that depends on research per comment 3.