[whatwg/dom] Dispatching events section may not match browsers (#746)

I found this out while investigating this WPT test:
- http://w3c-test.org/dom/events/EventTarget-dispatchEvent.html (last check: "Event listeners should be called in order of addition")

This check is passing in Chrome and Firefox and failing in Safari so I investigated it. The test looks like so:
`
var results = []
  var b = document.createElement("b")
  b.addEventListener("x", function() {
    results.push(1)
  ), true)
  b.addEventListener("x", function() {
    results.push(2)
  }, false)
  b.addEventListener("x", function() {
    results.push(3)
  }, true)
  b.dispatchEvent(new Event("x"))
  assert_array_equals(results, [1, 2, 3])
`

Chrome and Firefox get result === [1, 2, 3]. Safari gets result = [1, 3, 2].

Either I am misinterpreting the DOM spec or WebKit's implementation actually matches the specification though, which would be sad.

Notice that the second event listener has capturing=false while the other ones have capturing=true.

Per DOM Spec [1]:
- Step 14 says to call "invoke" with phase="capturing" for every struct in event path, in reverse order.
- Step 15 says to call "invoke" with phase="bubbling" for every struct in event path

"invoke" ends up calling "Inner invoke" [2]. Notice steps 3 and 4 of "inner invoke":
- If phase is "capturing" and listener’s capture is false, then continue.
- If phase is "bubbling" and listener’s capture is true, then continue.

So with my understanding of the specification, the order the event listeners were added could not possibly be maintained here when dispatching the event since some of them use capturing=true and some other use capturing=false.

I think Blink may be using the event's phase (which may be AT_TARGET) in its "inner invoke" implementation, instead of the phase passed to the "inner invoke" algorithm which is either "capturing" or "bubbling" but never at_target.

If I am misinterpreting the specification, please let me know. If not, then I would not be opposed to aligning WebKit with Blink/Gecko as long as the specification gets updated accordingly.

[1] https://dom.spec.whatwg.org/#dispatching-events (steps 13 & 14)
[2] https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke


-- 
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/746

Received on Monday, 1 April 2019 21:42:33 UTC