[Bug 17708] DOMTokenList: Ability to swap a class

https://www.w3.org/Bugs/Public/show_bug.cgi?id=17708

--- Comment #2 from Marcos Caceres <w3c@marcosc.com> 2012-07-07 10:33:39 UTC ---
(In reply to comment #1)
> (In reply to comment #0)
> > $("article").removeClass("closed").addClass("opened");
> > 
> > If developers are going to swap CSS classes around in the method above, then
> > they might as well have an efficient means to do that. 
> 
> What do you mean by "efficient"?  I doubt there's any performance issue here. 

There is potentially a significant performance gain. Check this out: 
http://youtu.be/hZJacl2VkKo 

(not that swap could also have been used there... but also toggle())

> If you mean "less typing", this is already pretty concise (even without the
> chaining shortcut).

Nope, raw performance gains. As shown in the video. 

> > Or other similar code. We also saw evidence that developers assume that class
> > lists retain order:
> > 
> > if(elem.classList === "x y") {
> >    .... 
> > } 
> > 
> > This could lead to confusion when using DOMTokenList toggle(), as toggle
> > destroys the ordering of CSS class list names of an attribute.  
> > 
> > Given the above, I'll like to request we add swap() method to DOMTokenList().
> > It would swap one token for another while retaining item order.
> 
> This seems to be increasing the surface area of the API just to make it easier
> to get bad practices to work.  It encourages people to write more code that
> depends on the order of the class list--which should be discouraged--instead of
> fixing the real problem (use classList.contains(), not string comparison).

To do the queries that you suggest is more annoying. You would need to do the
following:

if(elem.classList.contains("x") && elem.classList.contains("y"))

When what you really want is:
if(elem.classList.contains("x", "y", "z")); 

or 
(elem.classList.contains(["x", "y", "z"]))

Getting off topic.

-- 
Configure bugmail: https://www.w3.org/Bugs/Public/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.

Received on Saturday, 7 July 2012 10:33:53 UTC