From 932762594580794f69c7ea2d31f7ce7d7415361c Mon Sep 17 00:00:00 2001 From: Francis Bouvier Date: Thu, 18 Jan 2024 20:20:07 +0100 Subject: [PATCH] Fix dispatch event for capturing/bubbling phases for own target MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The standard is not very clear about that. On the general description of events (https://dom.spec.whatwg.org/#events) it says "all the object’s inclusive ancestor event listeners whose capture is true are invoked" for capturing and bubbling phases. So the target itself *should be included*. But later when describing the phases (https://dom.spec.whatwg.org/#dom-event-eventphase) it says that capturing phase is _before_ the target and bubbling phase is _after_ the target. So the target itself *should not be included*. The behavior of modern web browsers (Chrome/Firefox) do not include the target in capturing/bubbling phase so let's follow that. Signed-off-by: Francis Bouvier --- src/events/event_target.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/events/event_target.c b/src/events/event_target.c index bd7e67b..eafe490 100644 --- a/src/events/event_target.c +++ b/src/events/event_target.c @@ -227,11 +227,15 @@ dom_exception _dom_event_target_dispatch(dom_event_target *et, if (dom_string_isequal(le->type, evt->type)) { assert(le->listener->handler != NULL); + bool ancestor = evt->current != evt->target; + if ((le->capture && - phase == DOM_CAPTURING_PHASE) || + phase == DOM_CAPTURING_PHASE && + ancestor) || (le->capture == false && evt->bubble && - phase == DOM_BUBBLING_PHASE) || + phase == DOM_BUBBLING_PHASE && + ancestor) || (evt->target == evt->current && phase == DOM_AT_TARGET)) { le->listener->handler(evt,