W3C home > Mailing lists > Public > public-webapps@w3.org > April to June 2012

Re: [DOM3 Events/DOM4] re-dispatching trusted events with initEvent

From: Boris Zbarsky <bzbarsky@MIT.EDU>
Date: Tue, 24 Apr 2012 17:02:22 -0400
Message-ID: <4F9714DE.4030005@mit.edu>
To: public-webapps@w3.org
On 4/24/12 4:34 PM, Glenn Maynard wrote:
> This is a common misconception of how events work.  If you're a browser,
> default events do not--except for one or two web-compat exceptions--look
> like this:
>
> browse_button.addEventListener("click", function(e) { if(e.isTrusted)
> openFilePicker(); }, false);
> browse_button.dispatchEvent(clickEvent);
>
> Rather, they look like this:
>
> if(browse_button.dispatchEvent(clickEvent))
>      openFilePicker();

Actually, no.  The way events work, at least in Gecko, is more like this 
(conceptually; the actual implementation is somewhat different):

   browse_button.addEventListenerInSystemEventGroup("click",
     function(e) {
       if (e.isTrusted && !e.defaultPrevented) {
         e.preventDefault();
         openFilePicker();
       }
     }, false);

This is needed because there might be multiple things that might wish to 
perform a default action based on a click (in particular, every single 
thing the click bubbles through), and the code that actually dispatches 
a click can't possibly have an idea of the full set of default actions 
involved.  The dispatch code then looks like this:

   someNode.dispatchEvent(clickEvent);

and has no idea whether it's going to a browse_button or to a <span> 
which has an <a href> on the ancestor chain.

> The "default action"--openFilePicker()--is not part of the event
> dispatch process; it's part of the caller of the dispatch.

That's a convenient spec fiction, but actually hard to implement that 
way; it requires some way for the event dispatch process to communicate 
to the caller of the dispatch the set of desired default actions.  See 
the hoops that the spec has to jump through to deal with nested 
activation behavior, for example....

Oh, and that's before we get into default actions implemented by extensions.

> (DOM3's language
> about "default actions" confuses this; I suggest reading DOM4's event
> section to get a good picture of how this actually works.)

Or rather how the DOM4 editor is choosing to conceptualize it, which may 
not have much bearing on how it actually works in actual browsers.

> (The main exception is the "click" event

And a rather big exception it is!

-Boris
Received on Tuesday, 24 April 2012 21:02:56 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 18:49:52 GMT