External selectors

0 views
Skip to first unread message

henrib

unread,
Dec 12, 2008, 6:23:39 AM12/12/08
to reglib
In some occasions, it can be very hard (or impossible) to match
elements through a css-like expression.
Say, you want to match elements that have a class name which is the
same as their parent for instance.
In those cases, having 'external matchers', arbitrary functions that
tell if an element matches or not a given condition could be a
solution.
I've been prototyping some code - (posted through issue #10 "external
selectors").

The syntax is (un)inspired by JSP/EL; an external selector can be
referenced through ${name-of-selector}; for instance:
reg.click("${mySelector}", myClickHandler);

An external selector is thus a function that first needs to be given a
name in reglib through a call to register$:
reg.register$("mySelector", mySelectorFunction);
This exposes "mySelector" as a usable name in a selector expression.

The companion functions are:
reg.unregister$(matcher-name) which removes the mapping between the
selector name & the function
reg.extern$(matcher-name) which returns the selector function
associated with that name if it exists.

An external selector signature is:
function selector(elt, context) {...}
The 'context' is an empty object that is created before calling the
external selector and can be modified by it.
The event handler is also receiving this context object as a second
parameter:
function handler(event, context) {...}
This way, the matcher can prepare/allocate some objects that can be
further reused by the event handler.

Comments welcome.

Greg

unread,
Dec 13, 2008, 3:49:54 PM12/13/08
to reglib
It's an interesting idea. Would it be possible to say for example:
"div.foo > ${mySelector} + ul" ?

Right now there are a few different directions reglib's selectors
could go. There is John Resig's Sizzle lib, which will soon land in
the jQuery trunk. There is the question of whether to stay feature-for-
feature compatible with CSS3. I think the best approach would be to
just make the selector engine pluggable, then people could go crazy
with alternative or extended syntaxes.

Greg

henrib

unread,
Dec 13, 2008, 7:43:59 PM12/13/08
to reglib

> It's an interesting idea. Would it be possible to say for example:
> "div.foo > ${mySelector} + ul" ?
Yes, it should; I believe the prototype allows it although I have not
verified it; I only played with simple external selector.

But you are right; side stepping from CSS3 is debatable and being able
to plugin the selector engine is a much better way.

The other idea in the prototype is to pass a context filled by the
selector to the event handler; but it can probably be accomplished by
augmenting the event with user properties. And since you were
mentioning the possibility of normalizing the event in another issue,
this is a moot point :-)

Finally, the code that made addEvent work for multiple handlers in //
& IE6 delegation is I believe just a notch more efficient @ runtime.
Cheers
Henri
Reply all
Reply to author
Forward
0 new messages