diff --git a/src/browser/events/custom_event.zig b/src/browser/events/custom_event.zig index 3bf8179f..f84a9805 100644 --- a/src/browser/events/custom_event.zig +++ b/src/browser/events/custom_event.zig @@ -19,6 +19,7 @@ const parser = @import("../netsurf.zig"); const Event = @import("event.zig").Event; const JsObject = @import("../env.zig").JsObject; +const netsurf = @import("../netsurf.zig"); // https://dom.spec.whatwg.org/#interface-customevent pub const CustomEvent = struct { @@ -55,6 +56,25 @@ pub const CustomEvent = struct { pub fn get_detail(self: *CustomEvent) ?JsObject { return self.detail; } + + // Initializes an already created `CustomEvent`. + // https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/initCustomEvent + pub fn _initCustomEvent( + self: *CustomEvent, + event_type: []const u8, + can_bubble: bool, + cancelable: bool, + detail: ?JsObject, + ) !void { + // This function can only be called after the constructor has called. + // So we assume proto is initialized already by constructor. + self.proto.type = try netsurf.strFromData(event_type); + self.proto.bubble = can_bubble; + self.proto.cancelable = cancelable; + self.proto.is_initialised = true; + // Detail is stored separately. + self.detail = detail; + } }; const testing = @import("../../testing.zig"); diff --git a/src/browser/netsurf.zig b/src/browser/netsurf.zig index 079dfc0c..026f5ade 100644 --- a/src/browser/netsurf.zig +++ b/src/browser/netsurf.zig @@ -106,7 +106,7 @@ inline fn strToData(s: *String) []const u8 { return data[0..c.dom_string_byte_length(s)]; } -inline fn strFromData(data: []const u8) !*String { +pub inline fn strFromData(data: []const u8) !*String { var s: ?*String = null; const err = c.dom_string_create(data.ptr, data.len, &s); try DOMErr(err);