[classList] implements the new toggle spec

RESOLVED FIXED in mozilla24

Status

()

Core
DOM: Core & HTML
RESOLVED FIXED
5 years ago
4 years ago

People

(Reporter: julienw, Assigned: julienw)

Tracking

({doc-bug-filed})

Trunk
mozilla24
doc-bug-filed
Points:
---
Dependency tree / graph
Bug Flags:
in-testsuite +

Firefox Tracking Flags

(Not tracked)

Details

(URL)

Attachments

(1 attachment, 1 obsolete attachment)

(Assignee)

Description

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.
(Assignee)

Updated

5 years ago
OS: Linux → All
Hardware: x86_64 → All
(Assignee)

Updated

5 years ago
See Also: → bug 814014
(Assignee)

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 <http://mxr.mozilla.org/mozilla-central/source/dom/webidl/DOMTokenList.webidl>, <http://mxr.mozilla.org/mozilla-central/source/content/base/src/nsDOMTokenList.h>, <http://mxr.mozilla.org/mozilla-central/source/content/base/src/nsDOMTokenList.cpp>. Relevant documentation is <https://developer.mozilla.org/en/Mozilla/WebIDL_bindings>. #content will be happy to help if you run into issues.

Updated

4 years ago
Keywords: dev-doc-needed
(Assignee)

Updated

4 years ago
Assignee: nobody → felash
(Assignee)

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)
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.
(Assignee)

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) {
    this.contactPickButton.classList.add('disabled');
  } else {
    this.contactPickButton.classList.remove('disabled');
  }

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,

aForce.

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

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 ?
Did you test an opt build or debug build?
(Assignee)

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.
(Assignee)

Comment 12

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

Comment 13

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

carrying r=bzbarsky

try is https://tbpl.mozilla.org/?tree=Try&rev=85804cbf8361
Attachment #760342 - Attachment is obsolete: true
Attachment #762917 - Flags: review+
(Assignee)

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
(Assignee)

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
https://hg.mozilla.org/integration/mozilla-inbound/rev/dccd693173c9
Flags: in-testsuite+
Keywords: checkin-needed
https://hg.mozilla.org/mozilla-central/rev/dccd693173c9
Status: NEW → RESOLVED
Last Resolved: 4 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla24
Depends on: 885279
Follow up for documentation: https://bugzilla.mozilla.org/show_bug.cgi?id=885279
Keywords: dev-doc-needed → doc-bug-filed

Comment 21

4 years ago
The specification has been updated with clearer wording: https://www.w3.org/Bugs/Public/show_bug.cgi?id=22321
Flags: needinfo?(annevk)
(Assignee)

Updated

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