SitePoint Sponsor

User Tag List

View Poll Results: What are the best names for these methods?

Voters
8. You may not vote on this poll
  • get(), getAll()

    1 12.50%
  • match(), matchAll()

    1 12.50%
  • getElementBySelector(), getElementsBySelector()

    6 75.00%
  • getElementBySelector(), getElementListBySelector()

    0 0%
Results 1 to 20 of 20
  1. #1
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  3. #3
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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);
    Birnam wood is come to Dunsinane

  5. #5
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  6. #6
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?
    Birnam wood is come to Dunsinane

  7. #7
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Netherlands
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  9. #9
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  10. #10
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  11. #11
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  12. #12
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.

  13. #13
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  14. #14
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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?

  15. #15
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  16. #16
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    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.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  17. #17
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    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)
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  18. #18
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  19. #19
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    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.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  20. #20
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •