Skip to content

Commit a86fa8c

Browse files
committed
add support for CustomEvent#initCustomEvent
1 parent e1c765e commit a86fa8c

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/browser/events/custom_event.zig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
const parser = @import("../netsurf.zig");
2020
const Event = @import("event.zig").Event;
2121
const JsObject = @import("../env.zig").JsObject;
22+
const netsurf = @import("../netsurf.zig");
2223

2324
// https://dom.spec.whatwg.org/#interface-customevent
2425
pub const CustomEvent = struct {
@@ -55,6 +56,25 @@ pub const CustomEvent = struct {
5556
pub fn get_detail(self: *CustomEvent) ?JsObject {
5657
return self.detail;
5758
}
59+
60+
// Initializes an already created `CustomEvent`.
61+
// https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/initCustomEvent
62+
pub fn _initCustomEvent(
63+
self: *CustomEvent,
64+
event_type: []const u8,
65+
can_bubble: bool,
66+
cancelable: bool,
67+
detail: ?JsObject,
68+
) !void {
69+
// This function can only be called after the constructor has called.
70+
// So we assume proto is initialized already by constructor.
71+
self.proto.type = try netsurf.strFromData(event_type);
72+
self.proto.bubble = can_bubble;
73+
self.proto.cancelable = cancelable;
74+
self.proto.is_initialised = true;
75+
// Detail is stored separately.
76+
self.detail = detail;
77+
}
5878
};
5979

6080
const testing = @import("../../testing.zig");

src/browser/netsurf.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ inline fn strToData(s: *String) []const u8 {
106106
return data[0..c.dom_string_byte_length(s)];
107107
}
108108

109-
inline fn strFromData(data: []const u8) !*String {
109+
pub inline fn strFromData(data: []const u8) !*String {
110110
var s: ?*String = null;
111111
const err = c.dom_string_create(data.ptr, data.len, &s);
112112
try DOMErr(err);

0 commit comments

Comments
 (0)