Skip to content

Commit 903168b

Browse files
committed
Support the capture field of the addEventListener option
addEventListener can take a boolean (capture, already supported) or an object of options. This adds union support for the two, but only supports the `capture` field of the options object. The other fields are not supported by netsurf.
1 parent b2605dd commit 903168b

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/browser/dom/event_target.zig

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,38 @@ pub const EventTarget = struct {
4242
// JS funcs
4343
// --------
4444

45+
const AddEventListenerOpts = union(enum) {
46+
opts: Opts,
47+
capture: bool,
48+
49+
const Opts = struct {
50+
capture: ?bool,
51+
once: ?bool, // currently does nothing
52+
passive: ?bool, // currently does nothing
53+
signal: ?bool, // currently does nothing
54+
};
55+
};
56+
4557
pub fn _addEventListener(
4658
self: *parser.EventTarget,
4759
typ: []const u8,
4860
cbk: Env.Callback,
49-
capture: ?bool,
61+
opts_: ?AddEventListenerOpts,
5062
state: *SessionState,
51-
// TODO: hanle EventListenerOptions
52-
// see #https://github.com/lightpanda-io/jsruntime-lib/issues/114
5363
) !void {
64+
var capture = false;
65+
if (opts_) |opts| {
66+
switch (opts) {
67+
.capture => |c| capture = c,
68+
.opts => |o| capture = o.capture orelse false,
69+
}
70+
}
71+
5472
// check if event target has already this listener
5573
const lst = try parser.eventTargetHasListener(
5674
self,
5775
typ,
58-
capture orelse false,
76+
capture,
5977
cbk.id,
6078
);
6179
if (lst != null) {
@@ -68,7 +86,7 @@ pub const EventTarget = struct {
6886
self,
6987
typ,
7088
&eh.node,
71-
capture orelse false,
89+
capture,
7290
);
7391
}
7492

@@ -172,7 +190,7 @@ test "Browser.DOM.EventTarget" {
172190

173191
try runner.testCases(&.{
174192
.{ "nb = 0", "0" },
175-
.{ "content.removeEventListener('basic', cbk, true)", "undefined" },
193+
.{ "content.removeEventListener('basic', cbk, {capture: true})", "undefined" },
176194
.{ "content.dispatchEvent(new Event('basic'))", "true" },
177195
.{ "nb", "0" },
178196
}, .{});

0 commit comments

Comments
 (0)