This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.
Consider this script: var d = document.createElement("div"); d.classList.add("a", "a"); If I read the spec right, the result of this will serialize as: <div class="a a"></div> which seems slightly odd.
Do we want to catch this? Is the extra validation step negligible?
I don't know. None of this stuff is exactly super-fast, as currently specced. How UAs optimize it is an interesting question; Gecko doesn't try very hard at the moment afaict, though I might change that. One related question is whether we want: d.classList.add.apply(d.classList, args); to have different behavior from: args.map(function(x) { d.classList.add(x); }); and in general what invariants we want this token list stuff to preserve.
Optimizing the existing stuff for performance seems better to me than slowing things down even more. I don't have any concrete suggestions though.
Would there be a problem with changing the model of DOMTokenList to an ordered set instead? The moment you start modifying class="" through DOMTokenList, class="" would simply become a simple serialization of DOMTokenList rather than preserving all the things it tries to do now.
The whole idea of trying to preserve the whitespaces of the class attribute when we mutate the DOMTokenList is something that has been bothered all along. Let's remove duplicates and simply serialize by adding a space between each token. This is what JS libs did for years and polyfills still do so it should be safe.
The following is my plan: DOMTokenList represents an ordered set (with set guaranteeing uniqueness, of course). Manipulating its associated attribute (e.g. class="") seeds DOMTokenList. Manipulating DOMTokenList seeds its associated attribute (a simple serialization of the ordered set, tokens separated by U+0020). Somehow we avoid loops. DOMSettableTokenList does the same.
To have sane toggle handling we need to remove duplicates.
DOMTokenList is now defined as representing an ordered set. See bug 20105 for how that serializes to a string and parses from a string.