Closed Bug 814090 Opened 7 years ago Closed 7 years ago

[classList] implements the new toggle spec


(Core :: DOM: Core & HTML, defect)

Not set





(Reporter: julienw, Assigned: julienw)




(Keywords: doc-bug-filed)


(1 file, 1 obsolete file)

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.
OS: Linux → All
Hardware: x86_64 → All
See Also: → 814014
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.
Keywords: dev-doc-needed
Assignee: nobody → felash
Attached patch patch v1 (obsolete) — Splinter Review
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)
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.
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);
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 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+
(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 ?
Did you test an opt build or debug build?
I had no .mozconfig so I think it was an opt build. I'm building again with the debug option. Thanks a lot.
I get the assert failure while running the mochitests with a debug build.
Attached patch patch v2Splinter Review
carrying r=bzbarsky

try is
Attachment #760342 - Attachment is obsolete: true
Attachment #762917 - Flags: review+
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
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
Closed: 7 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla24
Depends on: 885279
The specification has been updated with clearer wording:
Flags: needinfo?(annevk)
Blocks: 814014
You need to log in before you can comment on or make changes to this bug.