You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
Second, object’s own event listeners are invoked.
The object (the span element) has no event listeners, so nothing is 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.
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.
The text was updated successfully, but these errors were encountered:
Thank you for taking the time to report this @kevinsung. It was indeed wrong. I put up a suggested replacement over at #890 and would appreciate your review.
https://dom.spec.whatwg.org/commit-snapshots/8f20138c9510c7f4a7d01dbb5e24175e72fc9734/#synthetic-events
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:
The object (the span element) has exactly one ancestor event listener with capture variable set to true (the document). So that listener is invoked.
The object (the span element) has no event listeners, so nothing is invoked.
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.
The text was updated successfully, but these errors were encountered: