base2.DOM and the Selectors API
The Selectors API is a fairly simple specification as far as W3C specs go. It defines two methods for element retrieval. One method returns a single element, the other returns all matching elements. The only debate was what to call these methods. Finally, the debating is over, and in keeping with W3C tradition the editors have selected the longest names possible:
querySelector
querySelectorAll
I like the new names really.
I am going to rename the base2.DOM methods to stay in line with the standard. If you are using base2.DOM then this is a heads up.
I am also removing support for getElementsByClassName()
. This is because I cannot support it fully. getElementsByClassName()
is supposed to return a live node list. This is virtually impossible to replicate in JavaScript.
You can get identical functionality using the Selectors API:
var elements = document.getElementsByClassName("my-class");
is equivalent to:
var elements = document.querySelectorAll(".my-class");
NB: JavaScript implementations of getElementsByClassName()
are notoriously slow so it is best to just avoid this selector entirely.
If you want to continue using getElementsByClassName()
or the old Selectors API methods then include this function in your page:
(function() { eval(base2.namespace); eval(DOM.namespace); var OldNodeSelector = Interface.extend({ "@!(element.getElementsByClassName)": { getElementsByClassName: function(node, className) { if (instanceOf(className, Array)) { className = className.join("."); } return this.querySelectorAll(node, "." + className); } }, matchAll: NodeSelector.querySelectorAll, matchSingle: NodeSelector.querySelector }); Document.implement(OldNodeSelector); Element.implement(OldNodeSelector); })();
If you are using the base2 core (you shouldn’t as it is not supported) then I have also renamed the Hash class and its methods to stay in line with the JS2 Map object which has very similar functionality.
These naming issues were preventing me from fully releasing base2.DOM, so expect a full release pretty soon. The code will be served from a permanent URL on Google code. By permanent, I mean that it will never be overwritten. The version number will form part of the URL somehow. I’m still undecided exactly how to do this as I also want the directory structure to mirror the base2 API.
Subscribe to the base2 Google group for further news about base2.
Update: It seems that the names have changed again! For clarification I emailed the editor of the Selectors API specification, Lachlan Hunt:
I got overruled! We ended up holding a working group vote and
querySelector
/querySelectorAll
won.http://lists.w3.org/Archives/Public/public-webapi/2007Aug/0066.html
I’ve updated this post to reflect the latest names.
Comments (13)
Leave a comment
Comment: #1
I’m utterly confused.
In fact, it was only last night that I was attempting to patch a local copy of Base2 so that I could start using
get()
andgetAll()!
http://dev.w3.org/cvsweb/2006/webapi/selectors-api/Overview.html?rev=1.16
More seriously, though, the latest version in cvs
http://dev.w3.org/cvsweb/2006/webapi/selectors-api/Overview.html?rev=1.27
has
querySelector()
andquerySelectorAll()
. Nor is that a mistakehttp://lists.w3.org/Archives/Public/public-webapi/2007Aug/0061.html
http://lists.w3.org/Archives/Public/public-webapi/2007Aug/0066.html
Phew!
Mind you, back in June, Lachlan said that he was now prepared to go with
cssQuery()
…http://lists.w3.org/Archives/Public/public-webapi/2007Jun/0120.html
Comment: #2
I would have been flattered, but ultimately I think this is a bad name as it ties the Selectors API to CSS, which is not the greatest tool for this particular job.
Comment: #3
Damn. Now I really need to buy that widescreen iMac. I hoped I could use my goold old 15″ iMac for another year
Comment: #4
[…] UPDATE: Dean has actually implemented the Selectors API in his base2.DOM library and has taken out his getElementsByClassName implementation (as he can’t return a live node list). All you have to do is s/document.getElementsByClassName(”my-class”);/document.selectAllElements(”.my-class”);/ so to speak. […]
Comment: #5
I just announced this result on my own blog. http://lachy.id.au/log/2007/08/naming-debate-revisited
Comment: #6
Is this final? or is there any chance president Bush will overrule this voting
Comment: #7
Doeke, no, I suspect this is final. Note that originally there was a vote for getElementsBySelector, but there were a bunch of people that simply refused to use that name. That’s why they had to revote again, afaict.
Comment: #8
It’s pretty curious the debate between “Select” and “Choose”. I can agree that “Select” is more precise then “Choose”, but it doesn’t seem to be shorter nor longer.
Anyway the most important thing is to have a stable name.
Comment: #9
Nearly one year from the release of the draft, until the winners were announced, is a looong time.
Comment: #10
Thank you for “querySelectorAll” attribute. I didn’t know about it
Comment: #11
I think ‘select’ is more precise, ‘choose’ is more on decision making.
Comment: #12
I think anne and Lachy made some good-faith decisions. Why the lowest score won the API naming?
Comment: #13
[…] base2.DOM is now ready for a beta release. As previously discussed, I’ve renamed the Selectors API methods to keep in line with the standard. […]
Comments are closed.