Go Back   SitePoint Forums > Forum Index > Program Your Site > JavaScript
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
View Poll Results: What are the best names for these methods?
get(), getAll() 1 12.50%
match(), matchAll() 1 12.50%
getElementBySelector(), getElementsBySelector() 6 75.00%
getElementBySelector(), getElementListBySelector() 0 0%
Voters: 8. You may not vote on this poll

Reply
 
Thread Tools Display Modes
Old Jan 25, 2007, 13:03   #1
zcorpan
bronze trophy
 
Join Date: Dec 2004
Location: Sweden
Posts: 2,512
Influence a future W3C Standard: Selectors API

Hey,

There has been a lot of discussion on the public-webapi@w3.org mailing list about what names these methods should have. I was interested in what you guys think are the best names.

What do they do?
Simply put, you can get either the first element in the document, or a static nodelist of all elements in the document, that match a group of selectors, using two different methods. They can both be used on Documents and on Elements. Example (using "XXX" and "YYY" as method names here, respectively):
Code:
var e = document.XXX("#nav ul");
var items = e.YYY("li"); // static nodelist
This will be supported natively in browsers in due course so you don't need to use libraries for this anymore.

See the working draft of the spec, or the editor's draft.

So what do you think are appropriate names for these two methods? Why?
__________________
Simon Pieters
(works for Opera Software, though represents just himself)
zcorpan is offline   Reply With Quote
Old Jan 25, 2007, 13:17   #2
zcorpan
bronze trophy
 
Join Date: Dec 2004
Location: Sweden
Posts: 2,512
Oops. The last two should have been:
  • getElementBySelectors(), getElementsBySelectors()
  • getElementBySelectors(), getElementListBySelectors()
...that is, with an s on the end. Sorry for the typo.
__________________
Simon Pieters
(works for Opera Software, though represents just himself)
zcorpan is offline   Reply With Quote
Old Jan 25, 2007, 22:39   #3
Kravvitz
CSS & JS/DOM Adept
bronze trophy
 
Join Date: Mar 2005
Location: USA
Posts: 5,486
Why would there be two different methods? Why would you want a method that could only ever return a single element?
__________________
We miss you, Dan Schulz.
Learn CSS. | X/HTML Validator | CSS validator
Dynamic Site Solutions
Code for Firefox/Safari/Opera, add fixes for IE, not vice versa.
Kravvitz is offline   Reply With Quote
Old Jan 26, 2007, 03:07   #4
AutisticCuckoo
SitePoint Author
silver trophybronze trophy
 
Join Date: Nov 2004
Location: Åsnorrbodarna
Posts: 12,249
For efficiency reasons, probably. A simple ID selector can never match more than one element, so returning an array or node list is unnecessary.

Returning a single element node would let us skip the array index reference. Compare to these existing functions:
Code:
var e1 = document.getElementById("foo");
var e2 = document.getElementsByTagName("body").item(0);
__________________
Tommy Olsson The Autistic Cuckoo
Ultimate CSS Reference: book & site
Be like the 22nd elephant with heated value in space – bark!
(gotta end this ding dong for the freesias)
AutisticCuckoo is offline   Reply With Quote
Old Jan 26, 2007, 03:36   #5
zcorpan
bronze trophy
 
Join Date: Dec 2004
Location: Sweden
Posts: 2,512
Yeah, it's for performance. The second method will have to walk the entire tree, and the common case is probably that you just want one element matching the selector.
__________________
Simon Pieters
(works for Opera Software, though represents just himself)
zcorpan is offline   Reply With Quote
Old Jan 26, 2007, 03:43   #6
AutisticCuckoo
SitePoint Author
silver trophybronze trophy
 
Join Date: Nov 2004
Location: Åsnorrbodarna
Posts: 12,249
Off Topic:

Quote:
Originally Posted by zcorpan View Post
walk the entire tree
Random Friday thought: why is it called 'walking' a tree? Shouldn't it really be 'climbing' a tree?
__________________
Tommy Olsson The Autistic Cuckoo
Ultimate CSS Reference: book & site
Be like the 22nd elephant with heated value in space – bark!
(gotta end this ding dong for the freesias)
AutisticCuckoo is offline   Reply With Quote
Old Jan 26, 2007, 03:44   #7
mw22
SitePoint Member
 
Join Date: Aug 2003
Location: Netherlands
Posts: 13
Well, personally I prefer getElementsBySelector() instead of getElementListBySelectors(), but I think it is at least a lot better than getAll(), which I don't like at all.

I think the getElementBySelectors() method is totally unnecessary.
mw22 is offline   Reply With Quote
Old Jan 26, 2007, 07:36   #8
zcorpan
bronze trophy
 
Join Date: Dec 2004
Location: Sweden
Posts: 2,512
Quote:
Originally Posted by AutisticCuckoo View Post
Off Topic:


Random Friday thought: why is it called 'walking' a tree? Shouldn't it really be 'climbing' a tree?
Off Topic:

Climbing sounds more like an all-out effort, while walking the tree is actually a pretty simple task.

Well except the trees that come from IE, you can get around in circles in those and see the same things twice! More like a bush full of weeds and fungus.
__________________
Simon Pieters
(works for Opera Software, though represents just himself)
zcorpan is offline   Reply With Quote
Old Jan 26, 2007, 12:37   #9
MikeFoster
I'll take mine raw
silver trophy
 
MikeFoster's Avatar
 
Join Date: Dec 2002
Location: Alabama, USA
Posts: 2,562
getElementsBySelectors() is more in line with the existing naming convention - even tho matchOne/matchAll is certainly easier to type. But having only one letter difference between two (long) function names is just asking for typo bugs

Seeing as how IE only just now supports CSS1 - I think we'll be using our own Js implementations of these functions for a long time to come
__________________
Cross-Browser.com, Home of the X Library
MikeFoster is offline   Reply With Quote
Old Jan 26, 2007, 13:08   #10
Kravvitz
CSS & JS/DOM Adept
bronze trophy
 
Join Date: Mar 2005
Location: USA
Posts: 5,486
Quote:
Originally Posted by AutisticCuckoo View Post
For efficiency reasons, probably. A simple ID selector can never match more than one element, so returning an array or node list is unnecessary.
For an ID, why wouldn't you just use getElementById(). I suppose it would be useful if you use :first-child or :last-child though.

Quote:
Originally Posted by zcorpan View Post
Yeah, it's for performance. The second method will have to walk the entire tree, and the common case is probably that you just want one element matching the selector.
Even if an ID selector was used at the beginning of the selector chain?
__________________
We miss you, Dan Schulz.
Learn CSS. | X/HTML Validator | CSS validator
Dynamic Site Solutions
Code for Firefox/Safari/Opera, add fixes for IE, not vice versa.
Kravvitz is offline   Reply With Quote
Old Jan 26, 2007, 14:38   #11
zcorpan
bronze trophy
 
Join Date: Dec 2004
Location: Sweden
Posts: 2,512
Quote:
Originally Posted by Kravvitz View Post
Even if an ID selector was used at the beginning of the selector chain?
Yeah. Multiple elements can have the same ID. It is invalid, mind you, but it would "work" just like it already does in CSS.
__________________
Simon Pieters
(works for Opera Software, though represents just himself)
zcorpan is offline   Reply With Quote
Old Jan 26, 2007, 15:03   #12
Raffles
Islomaniac
silver trophybronze trophy
SitePoint Award Recipient
 
Raffles's Avatar
 
Join Date: Sep 2005
Location: Cambridge, UK
Posts: 4,218
This is great, but when you say browsers will implement it, has Microsoft made any noise about when they're going to update their javascript engine?

getElementListBySelector() is a bit of a mouthful and get()/getAll() sounds Microsoftish (like document.all) so I agree with the others that it's best to stick with the name convention - I vote for option 3.
Raffles is offline   Reply With Quote
Old Jan 26, 2007, 15:06   #13
Kravvitz
CSS & JS/DOM Adept
bronze trophy
 
Join Date: Mar 2005
Location: USA
Posts: 5,486
Perhaps getOneElementBySelectors() and getElementsBySelectors() would be less prone to unnoticed typos.
__________________
We miss you, Dan Schulz.
Learn CSS. | X/HTML Validator | CSS validator
Dynamic Site Solutions
Code for Firefox/Safari/Opera, add fixes for IE, not vice versa.
Kravvitz is offline   Reply With Quote
Old Jan 26, 2007, 15:18   #14
Raffles
Islomaniac
silver trophybronze trophy
SitePoint Award Recipient
 
Raffles's Avatar
 
Join Date: Sep 2005
Location: Cambridge, UK
Posts: 4,218
What about merging both into one: getElement(s)BySelector(s)()?

On a more serious note, am I right in thinking that getElementBySelectors('#nav li.even') would only match the first li.even in the ul? Or would it be the last?
Raffles is offline   Reply With Quote
Old Jan 26, 2007, 16:46   #15
zcorpan
bronze trophy
 
Join Date: Dec 2004
Location: Sweden
Posts: 2,512
Quote:
Originally Posted by Raffles View Post
On a more serious note, am I right in thinking that getElementBySelectors('#nav li.even') would only match the first li.even in the ul? Or would it be the last?
The first.
Quote:
Originally Posted by Selectors API, Editor's Draft 9 January 2007
The get(selectors, nsresolver) method, when invoked, must return the first Element node in document order (using depth-first pre-order traversal) that matches the group of selectors (selectors), if any. Otherwise it must return null.
__________________
Simon Pieters
(works for Opera Software, though represents just himself)
zcorpan is offline   Reply With Quote
Old Jan 27, 2007, 01:15   #16
felgall
Programming Since 1978
silver trophybronze trophy
SitePoint Award Recipient
 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 12,867
Quote:
Originally Posted by zcorpan View Post
Yeah. Multiple elements can have the same ID. It is invalid, mind you, but it would "work" just like it already does in CSS.
Since having a second element with the same ID is invalid there is no need for a browser to continue looking once it finds the first one since the subsequent ones shouldn't be there and it is therefore more correct to ignore them and assume that they don't exist than it would be to continue searching for something which in a valid page wont be there. Any browser that continues searching once it finds the first occurrence of an id is just being inefficient.
felgall is offline   Reply With Quote
Old Jan 27, 2007, 01:19   #17
felgall
Programming Since 1978
silver trophybronze trophy
SitePoint Award Recipient
 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 12,867
Quote:
Originally Posted by Raffles View Post
This is great, but when you say browsers will implement it, has Microsoft made any noise about when they're going to update their javascript engine?

getElementListBySelector() is a bit of a mouthful and get()/getAll() sounds Microsoftish (like document.all) so I agree with the others that it's best to stick with the name convention - I vote for option 3.
At Microsoft's current rate of progress of implementing CSS I would say that if a standard were adopted for this TODAY then it would probably make it into Internet Explorer 14 (which Microsoft will probably release in approximately 2084 - assuming of course that they still exist by then)
felgall is offline   Reply With Quote
Old Jan 27, 2007, 10:25   #18
zcorpan
bronze trophy
 
Join Date: Dec 2004
Location: Sweden
Posts: 2,512
Quote:
Originally Posted by felgall View Post
Since having a second element with the same ID is invalid there is no need for a browser to continue looking once it finds the first one since the subsequent ones shouldn't be there and it is therefore more correct to ignore them and assume that they don't exist than it would be to continue searching for something which in a valid page wont be there. Any browser that continues searching once it finds the first occurrence of an id is just being inefficient.
Authoring conformance requirements and UA conformance requirements are two completely separate things. UAs are not free to do whatever they want when faced with something that authors are not allowed to do. They must still follow the spec, and now the spec says that UAs must catch all elements matching the group of selectors. If there are two elements matching the selector "#foo" then they must both be catched.

However, I've asked the WG about this case. If it ends up making the spec clearer then that's a good thing.

__________________
Simon Pieters
(works for Opera Software, though represents just himself)
zcorpan is offline   Reply With Quote
Old Jan 27, 2007, 17:06   #19
felgall
Programming Since 1978
silver trophybronze trophy
SitePoint Award Recipient
 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 12,867
Presumably after determining that there are multiple elements with the same id on a web page the browser will then report an ERROR and refuse to process any of them since the page is invalid and to do anything else would be compounding the error. Only by IGNORING any subsequent occurrences and treating the page as if only the first one exists is any consistent approach that doesn't involve giving an error possible. So presumably if the search for subsequent occurrences is required to continue past the first occurrence of an ID the browser will be required to abort the display of the page as soon as an invalid second occurrence is found. At least aborting the display of the page will make it more obvious to the author of the page that they have made a stupid error.
felgall is offline   Reply With Quote
Old Jan 27, 2007, 20:24   #20
MikeFoster
I'll take mine raw
silver trophy
 
MikeFoster's Avatar
 
Join Date: Dec 2002
Location: Alabama, USA
Posts: 2,562
Quote:
If there are two elements matching the selector "#foo" then they must both be catched.
That makes about as much sense as event.button == 0 for left click
__________________
Cross-Browser.com, Home of the X Library
MikeFoster is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

 
Forum Jump


All times are GMT -7. The time now is 05:31.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved