PDA

View Full Version : DomQuery: id select with explicit root searches entire doc



mauritius
13 Apr 2007, 9:40 AM
I've been trying to do the following:


result = Ext.DomQuery.select("#foo", bar);

where bar is some DOM element.

The search eventually ends up in


function quickId(ns, mode, root, id){
if(ns == root){
var d = root.ownerDocument || root;
return d.getElementById(id);
}
ns = getNodes(ns, mode, "*");
return byId(ns, null, id);
}

with BOTH ns and root set to bar. Consequently, the first conditional evaluates to true, and the _entire document_ gets searched for the id in question, not only bar. I don't understand the semantics of ns in this context, so I'm not really sure why this goes wrong, but it should be undisputed that this is not the desired behavior.

Apart from this bug, thanks for the fast DomQuery.js!

Joe
13 Apr 2007, 12:43 PM
I'd imaging the team will chime in - but here is a quick note from some Joe programmer.

In testing - the results you see only happen looking for id (worked fine looking for 'div' for example).

I am pretty sure Ext caches elements by id. So the fact that is does a document wide search when looking for an ID may be working as designed.

But the team will be able to say for sure.

mauritius
13 Apr 2007, 12:52 PM
In testing - the results you see only happen looking for id (worked fine looking for 'div' for example).

yeah, sure, that's also what I'd expect from looking at the code. That quickId function is obviously only used for id queries.


I am pretty sure Ext caches elements by id. So the fact that is does a document wide search when looking for an ID may be working as designed.

DomQuery caches the compiled query strings, but not the results. The cached query function is called with the appropriate root object. So the observed behavior is definitely not correct.

jack.slocum
13 Apr 2007, 1:07 PM
The root property does not limit id queries. The only way to limit an id query is to explicitly make it part of the query process:

Ext.select('#foo #bar');

mauritius
13 Apr 2007, 1:18 PM
The root property does not limit id queries. The only way to limit an id query is to explicitly make it part of the query process:

Ext.select('#foo #bar');

Thanks for that answer. Is there any rationale behind this inconsistency, is it considered a bug/desired behavior, is it going to be fixed?

The reason I'm asking is twofold. First of all, we used Ext.query as a drop-in replacement for the venerable but slow cssQuery and found that Ext.query breaks our app in all sorts of unexpected places because of this behavior.

Second, we have a situation where the root is not so easily found via another DOM selector, so the solution you're proposing is not applicable.

mauritius
13 Apr 2007, 5:41 PM
to (partly) answer myself: One could probably argue that an id is supposed to be unique across the document if one follows the W3C, despite common practice to violate this rule... If this assumption is fulfilled, then this "bug" becomes moot.