W3C home > Mailing lists > Public > whatwg@whatwg.org > May 2012

[whatwg] DOMTokenList methods would be more useful with a space separated token list

From: Rick Waldron <waldron.rick@gmail.com>
Date: Wed, 2 May 2012 19:05:33 -0400
Message-ID: <8AF127D68B474128864F78F5042F5A49@gmail.com>


On Wednesday, May 2, 2012 at 6:41 PM, Ian Hickson wrote:  
> On Wed, 2 May 2012, Rick Waldron wrote:
> > On Wed, May 2, 2012 at 6:13 PM, Ian Hickson <ian at hixie.ch> wrote:
> > > On Fri, 28 Oct 2011, David H?s?ther wrote:
> > > >  
> > > > It would be more useful if the DOMTokenList methods (contains, add,  
> > > > remove, toggle) would take a space separated list of tokens. This is  
> > > > the behavior most DOM libraries have when dealing with class names.
> > > >  
> > > > So, when setting two classes, instead of
> > > >  
> > > > ele.classList.add("hey");
> > > > ele.classList.add("ho");
> > > >  
> > > > you could do
> > > >  
> > > > ele.classList.add("hey ho");
> > > >  
> > > > Currently, the latter results in an INVALID_CHARACTER_ERR.
> > > >  
> > > > The behavior of all methods should be obvious I guess.
> > >  
> > > This has now moved to DOM Core. If you still want this I recommend filing
> > > a bug on that spec.
> > >  
> > > http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html
> > >  
> > >  
> > > On Fri, 28 Oct 2011, Mike Taylor wrote:
> > > >  
> > > > I would prefer if it returned the DOMTokenList, to enable chaining like:
> > > >  
> > > > foo.toggle('bar baz').remove('bat');
> > >  
> > > That makes for very hard to read code, IMHO. I much prefer:
> > >  
> > > foo.toggle('bar');
> > > foo.remove('bat');
> > >  
> >  
> >  
> > Mike's recommendation is the same API introduced by jQuery a few years  
> > ago and since then, there have been many requests to expand the  
> > signature to other jQuery APIs. The toggle('bar baz') is not a good  
> > example, it should be illustrated with .add() and .remove():
> >  
>  
>  
> It was the chaining I was talking about with respect to Mike's example,  
> not the multiple values.
>  
>  


Great, in both cases, what _you_ prefer is not what is desired based on most common patterns and use cases.

JS APIs like this should always return the object (constructed instance or not) and therefore chain implicitly.

I'll take this to DOM Core.

Rick

>  
>  
> > Adding >1 class at a time is a _very_ common pattern in "real world" web  
> > development.
> >  
>  
>  
> As mentioned above, this API has now moved to DOM Core. If you still want  
> this I recommend filing a bug on that spec.
>  
> http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html
>  
> --  
> Ian Hickson U+1047E )\._.,--....,'``. fL
> http://ln.hixie.ch/ U+263A /, _.. \ _\ ;`._ ,.
> Things that are impossible just take longer. `._.-(,_..'--(,_..'`-.;.'
>  
>  
Received on Wednesday, 2 May 2012 16:05:33 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Wednesday, 30 January 2013 18:48:08 GMT