[whatwg/dom] "When an event is dispatched to an object that p..." (#888)

https://dom.spec.whatwg.org/commit-snapshots/8f20138c9510c7f4a7d01dbb5e24175e72fc9734/#synthetic-events


> When an event is dispatched to an object that participates in a tree \(e\.g\. an element\), it can reach event listeners on that object’s ancestors too\. First all object’s ancestor event listeners whose capture variable is set to true are invoked, in tree order\. Second, object’s own event listeners are invoked\. And finally, and only if event’s bubbles attribute value is true, object’s ancestor event listeners are invoked again, but now in reverse tree order\.
> 
> Let’s look at an example of how events work in a tree:​
> 
> <\!doctype html>
> <html>
>  <head>
>   <title>Boring example</title>
>  </head>
>  <body>
>   <p>Hello <span id=​x>world</span>\!</p>
>   <script>
>    function test\(e\) \{
>      debug\(e\.target, e\.currentTarget, e\.eventPhase\)
>    \}
>    document\.addEventListener\("hey", test, \{capture:​ true\}\)
>    document\.body\.addEventListener\("hey", test\)
>    var ev =​ new Event\("hey", \{bubbles:​true\}\)
>    document\.getElementById\("x"\)\.dispatchEvent\(ev\)
>   </script>
>  </body>
> </html>
> 
> The debug function will be invoked twice. Each time the event’s target attribute value will be the span element. The first time currentTarget attribute’s value will be the document, the second time the body element. eventPhase attribute’s value switches from CAPTURING_PHASE to BUBBLING_PHASE. If an event listener was registered for the span element, eventPhase attribute’s value would have been AT_TARGET.

I find this hard to follow, so I think it could use clarification (or perhaps I am just missing something). Following the first paragraph, this is what I would expect to happen in the example:
1. > First all object’s ancestor event listeners whose capture variable is set to true are invoked, in tree order.

The object (the span element) has exactly one ancestor event listener with capture variable set to true (the document). So that listener is invoked.

2. > Second, object’s own event listeners are invoked\.

The object (the span element) has no event listeners, so nothing is invoked.

3. > And finally, and only if event’s bubbles attribute value is true, object’s ancestor event listeners are invoked again, but now in reverse tree order\.

The wording here is unclear as to whether the invoked ancestor event listeners must have capture variable set to true. The use of the word "again" suggests that this should be so. In either case, since the event listeners are to be invoked "again," I expect the event listener of the document to be invoked again. Instead, only the body element has its event listener invoked, even though it was not invoked during the capturing phase.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/888

Received on Monday, 7 September 2020 00:25:06 UTC