Tuesday, August 02, 2005

Subtleties of getElementsBySelector

I should point out that my implementation of getElementsBySelector corrects a couple of subtle bugs in the original version. For one thing, the original version searches for nodes of the form #foo by using document.getElementById; this results in finding the outermost node in the entire document whose ID matches, even if searching a subtree. Since ID's are not guaranteed to be unique, this can produce the wrong node.

An even more subtle bug in the original version is that multiple copies of the same node may appear in the result array. For example, the selector '.foo .bar .mumble', when applied to the following tree:
<div class="foo">
<div class="bar">
<div class="bar">
<div class="mumble">mumble</div>
...produces two copies of the mumble node, because it finds it by following two different paths. By contrast, my implementation visits each node in the entire document tree exactly once, so it is guaranteed to produce either 0 or 1 copy of each node in the result array.


Anonymous said...

"this results in finding the outermost node in the entire document whose ID matches, even if searching a subtree. Since ID's are not guaranteed to be unique, this can produce the wrong node."

Are you kidding??

Speaking of the "id" attribute, W3C has this to say: "This attribute assigns a name to an element. This name must be unique in a document."

(See the spec)

This functionality is further underscored by the fact that there is no "node.getElementById()" method available, it always must be used as "document.getElementById()".

The purpose of "id" is to assign a unique identity to an element. If you don't want uniqueness, that's what "class" is for.

The bottom line is that IDs are guaranteed to be unique, unless you make a practice of breaking the rules of valid XHTML. If you run into a case where getElementsBySelector() catches the wrong element by ID, then the proper place to fix the problem is to make sure all of your ID attributes are uniquely-named, not in fixing the "bug" which pointed out your error.

Don't fix what isn't broken. If anything, your changes to this function simply make is easier for people to ignore the standards which have made the web relatively simple to code for in recent years. That's not good!


Unknown said...

Wow!! Really a nice Article. Thank you so much for your efforts. Definitely, it will be helpful for others. I would like to follow your blog..Artificial Intelligence Training in Bangalore. Keep sharing your information regularly for my future reference. Thanks Again.

Abarth Automotive said...

Thanks for sharing your valuable information. If you are looking for car mechanic in south Melbourne Please Visit us: Abarth Automotive

Anbarasan14 said...

I am happy reading your blog, I gained a piece of knowledge. Kindly continue the work.
Spoken English Classes in Chennai
Spoken English in Chennai
Top 10 Spoken English Classes in Chennai
Best IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
English Classes in Mumbai
English Speaking Classes in Mumbai
Best IELTS Coaching in Mumbai
IELTS Coaching in Mumbai
Spoken English Class in Anna Nagar

english speaking classes said...

Thanks for sharing wonderful information blog, its such a great info. keep update.
Intensive english program
learn english america
English conversation course
Best english programs

Unknown said...
This comment has been removed by the author.
carnegieautomotive said...

Thanks for sharing the great information. Car Service Bentleigh | Mechanic Hughesdale

Singhstyresauto said...

This article looks great thanks for the information Car Service Clayton South | Tyres Clayton

sammad ali said...

واتساب بلس
واتس اب جي بي
واتس اب الذهبي

Fouadwhatsappdownload said...


timmykeels said...

Hello my family member! I want to say that this article is awesome, great written and include almost all important infos. I’d like to see more posts like this .
Do check
Paypal Login
gemini login

jack peterson said...

Hello very cool site!! Man .. Excellent .. Superb .. I’ll bookmark your blog and take the feeds additionally¡KI am glad to search out so many helpful info right here within the post, we want develop extra strategies in this regard, thank you for sharing. . . . . .
Do check
Telstra email login

APKVOW Apps Store 2 said...

Thanks for sharing this post. Check this:
Sex oil
sex coundom

Majortotosite Pro said...

Good post. I study one thing more difficult on totally different blogs everyday. It can all the time be stimulating to learn content from different writers and follow a bit one thing from their store. 토토사이트

Racesite Pro said...

Thanks a bunch for sharing this with all people you actually recognise what you’re speaking approximately! Bookmarked.

Oncasinosite Net said...

There may be noticeably a bundle to know about this. I assume you made certain good factors in options also. 바카라

Totopick Pro said...

After study a few of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Pls check out my web site as well and let me know what you think. 토토사이트

Should I Enroll My Child in Driving Lessons said...

Carlton Driving School
Ringwood Driving School
bundoora Driving School

CasinoMecca said...


Royalcasino888 said...