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...