[Dojo-interest] Finding a matching ancestor

Michael Schuerig michael at schuerig.de
Wed Apr 22 15:13:09 EDT 2009


On Wednesday 22 April 2009, Peter E Higgins wrote:
> I've been meaning to put this kind of snippet in plugd under
> dojo.NodeList.parents(".someSelector") ... Your code is almost
> exactly the same as has been roughly implemented in a couple of
> places (in #dojo I know for certain), I just cannot bring myself to
> condone the potential perf hit here. With some unit tests, and a CLA
> in place, I would be happy to implement your ancestor function
> somewhere (plugd seems most appripriate, a whole dojox/ project for a
> nodelist extension seems excessive)

I didn't search the code long enough, then. I thought plugd was a subset 
of dojo proper and didn't look any close. Apparently I was wrong and 
have to look again.

When I've figured out DOH, how to integrate plugd in a dojo project, and 
finally prevented the epson SANE driver from hosing my system, I'll 
submit the snippet along with tests and CLA as a plugd ticket.

Regarding performance: Yes, I literally had to swallow before writing 
this code, but I think in practice it's not that bad, at least for my 
purposes. I intend to use it to dispatch bubbled events appropriately 
and in such a case there's always a root note.

Michael

> >   ancestor = function(
> >       /*String|DOMNode*/startNode,
> >       /*String*/query,
> >       /*String|DOMNode?*/root) {
> >     var node = dojo.byId(startNode);
> >     var candidates = dojo.query(query, root);
> >     root = root ? root : dojo.doc;
> >     while (node && node !== root) {
> >       if (candidates.indexOf(node) >= 0) {
> >         return node;
> >       }
> >       node = node.parentNode
> >     }
> >     return null;
> >   };

-- 
Michael Schuerig
mailto:michael at schuerig.de
http://www.schuerig.de/michael/



More information about the Dojo-interest mailing list