Bubble events to the Window

This commit is contained in:
Karl Seguin
2025-09-15 22:24:35 +08:00
parent 0f13e062fe
commit 4f127c9de3
3 changed files with 25 additions and 3 deletions

View File

@@ -148,8 +148,22 @@ pub const EventTarget = struct {
);
}
pub fn _dispatchEvent(self: *parser.EventTarget, event: *parser.Event) !bool {
return try parser.eventTargetDispatchEvent(self, event);
pub fn _dispatchEvent(self: *parser.EventTarget, event: *parser.Event, page: *Page) !bool {
const res = try parser.eventTargetDispatchEvent(self, event);
// TODO: If we get this working, we should also create a getter to check
// stopPropagation and not bubble to window if true.
if (!parser.eventBubbles(event)) {
return res;
}
// I think this mutates `event`, which means any JavaScript that captured
// it will be mutated incorrectly.
const Window = @import("../html/window.zig").Window;
return parser.eventTargetDispatchEvent(
parser.toEventTarget(Window, &page.window),
event,
);
}
};

View File

@@ -604,7 +604,7 @@ fn eventTargetVtable(et: *EventTarget) c.dom_event_target_vtable {
return @as([*c]const c.dom_event_target_vtable, @ptrCast(vtable_aligned)).*;
}
pub inline fn toEventTarget(comptime T: type, v: *T) *EventTarget {
pub fn toEventTarget(comptime T: type, v: *T) *EventTarget {
if (comptime eventTargetTBaseFieldName(T)) |field| {
const et_aligned: *align(@alignOf(EventTarget)) EventTargetTBase = @alignCast(&@field(v, field));
return @as(*EventTarget, @ptrCast(et_aligned));

View File

@@ -13,4 +13,12 @@
el.dispatchEvent(new CustomEvent('c2', {detail: {over: 9000}}));
testing.expectEqual("c2-9000", capture);
let window_calls = 0;
window.addEventListener('c1', () => {
window_calls += 1;
});
el.dispatchEvent(new CustomEvent('c1', {bubbles: true}));
testing.expectEqual(1, window_calls);
</script>