Note: There are a few cases of duplicates in user autocompletion which are being worked on.

[classList] implements the new toggle spec

RESOLVED FIXED in mozilla24



DOM: Core & HTML
5 years ago
4 years ago


(Reporter: julienw, Assigned: julienw)



Dependency tree / graph
Bug Flags:
in-testsuite +

Firefox Tracking Flags

(Not tracked)




(1 attachment, 1 obsolete attachment)



5 years ago
The new specification for classList.toggle added a second argument which can force the toggle. This is useful and can lead to clearer JS code.


5 years ago
OS: Linux → All
Hardware: x86_64 → All


5 years ago
See Also: → bug 814014

Comment 1

4 years ago
This doesn't need variadic idl support, so could we add this support ? This is so useful when developing JS using the DOM.

Developing on B2G without extra framework there is probably one situation each day when I wish I had this implemented.
We could. Patches welcome :)

Relevant code is <>, <>, <>. Relevant documentation is <>. #content will be happy to help if you run into issues.


4 years ago
Keywords: dev-doc-needed


4 years ago
Assignee: nobody → felash

Comment 3

4 years ago
Created attachment 760342 [details] [diff] [review]
patch v1

I still don't exactly get how to push to try (it finds no change when I push) but I added some tests and they all passes locally.
Attachment #760342 - Flags: review?(bzbarsky)

Comment 4

4 years ago
Before I start looking at the code... what is the point of this argument?  It looks to me like:  .toggle('foo') will add 'foo' to the list if not there, and remove it otherwise.  But .toggle('foo', true) will act just like .add('foo') and .toggle('foo', false) will act just like .remove('foo'), except for having a non-void return value.

Is my reading of the spec correct?  If so, why is this a useful thing for a method called toggle() to do?  If I'm misreading it, what am I missing?
AIUI, you're reading it correctly. It's meant to replace classList[aBooleanArg ? "add" : "remove"](), I think.

Comment 6

4 years ago
This is especially useful when you have a boolean that results from a bool calculation. This is more a sugar syntax, because this allows nothing that can't be done without it.

Compare for example (a simplified code taken from the Sms app in Gaia) :

  if (recipientCount > 0) {
  } else {

with this code, this can be simplidied like this :

  this.contactPickButton.classList.toggle('disabled', recipientCount > 0);

Comment 7

4 years ago
Hmm...  ok, I see.  Seems like slightly odd API, but I can't come up with a better one right this second.  And the spec is ... unreadable.  :(  And of course toggle('foo', true) is not specced to do the same thing as add('foo'), but we'll worry about that when tests get written for the spec.  :(
Flags: needinfo?(annevk)

Comment 8

4 years ago
Comment on attachment 760342 [details] [diff] [review]
patch v1

>+++ b/content/base/src/nsDOMTokenList.cpp
>+                       const mozilla::dom::Optional<bool>& optionalIsForce,

Please drop the "mozilla::dom::" bit.  See the "using namespace" near the top of this file.

Also, the argument should probably be called "aForce".

>+  const bool hasForceArgument = optionalIsForce.WasPassed();
>+  const bool isForce = optionalIsForce.Value();

Doing Value() if !WasPassed() should trigger a fatal assert.  Was the relevant codepath just not tested?

I think you want something more like:

  const bool forceOn = aForce.WasPassed() && aForce.Value();
  const bool forceOff = aForce.WasPassed() && !aForce.Value();

>+  bool isPresent = (attr && attr->Contains(aToken));

Please remove the extra parens.

>+    if ((hasForceArgument && !isForce) || (!hasForceArgument)) {

And then this would become:

  if (!forceOn) {

>+    if ((hasForceArgument && isForce) || (!hasForceArgument)) {

And this would become:

  if (!forceOff) {

>+++ b/content/base/src/nsDOMTokenList.h
>+              const mozilla::dom::Optional<bool>& force,


r=me with the above fixed.
Attachment #760342 - Flags: review?(bzbarsky) → review+

Comment 9

4 years ago
(In reply to Boris Zbarsky (:bz) from comment #8)
> Comment on attachment 760342 [details] [diff] [review]
> patch v1
> >+  const bool hasForceArgument = optionalIsForce.WasPassed();
> >+  const bool isForce = optionalIsForce.Value();
> Doing Value() if !WasPassed() should trigger a fatal assert.  Was the
> relevant codepath just not tested?

strangely, I'm quite sure it is in the mochitests... The tests were existing prior this patch. I also briefly tested using the Web Console.

How is it possible then ?

Comment 10

4 years ago
Did you test an opt build or debug build?

Comment 11

4 years ago
I had no .mozconfig so I think it was an opt build. I'm building again with the debug option. Thanks a lot.

Comment 12

4 years ago
I get the assert failure while running the mochitests with a debug build.

Comment 13

4 years ago
Created attachment 762917 [details] [diff] [review]
patch v2

carrying r=bzbarsky

try is
Attachment #760342 - Attachment is obsolete: true
Attachment #762917 - Flags: review+

Comment 14

4 years ago
This looks good to me.

Ryan, would you double check if try looks good and do the check in ? Thanks !
Keywords: checkin-needed
(In reply to Julien Wajsberg [:julienw] from comment #3)
> I added some tests and they all passes locally.

Looks like your added tests didn't actually make it into the patch, though.
Keywords: checkin-needed

Comment 16

4 years ago
Phil, I do see them in the patch, don't you see the changes to `test_classList.html` ?
Flags: needinfo?(philringnalda)
Reading is hard.
Flags: needinfo?(philringnalda)
Keywords: checkin-needed
Flags: in-testsuite+
Keywords: checkin-needed
Last Resolved: 4 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla24
Depends on: 885279
Follow up for documentation:
Keywords: dev-doc-needed → doc-bug-filed

Comment 21

4 years ago
The specification has been updated with clearer wording:
Flags: needinfo?(annevk)


4 years ago
Blocks: 814014
You need to log in before you can comment on or make changes to this bug.