add support for CustomEvent#initCustomEvent

This commit is contained in:
nikneym
2025-09-09 21:44:51 +03:00
parent e1c765e78a
commit a86fa8cc37
2 changed files with 21 additions and 1 deletions

View File

@@ -19,6 +19,7 @@
const parser = @import("../netsurf.zig"); const parser = @import("../netsurf.zig");
const Event = @import("event.zig").Event; const Event = @import("event.zig").Event;
const JsObject = @import("../env.zig").JsObject; const JsObject = @import("../env.zig").JsObject;
const netsurf = @import("../netsurf.zig");
// https://dom.spec.whatwg.org/#interface-customevent // https://dom.spec.whatwg.org/#interface-customevent
pub const CustomEvent = struct { pub const CustomEvent = struct {
@@ -55,6 +56,25 @@ pub const CustomEvent = struct {
pub fn get_detail(self: *CustomEvent) ?JsObject { pub fn get_detail(self: *CustomEvent) ?JsObject {
return self.detail; 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"); const testing = @import("../../testing.zig");

View File

@@ -106,7 +106,7 @@ inline fn strToData(s: *String) []const u8 {
return data[0..c.dom_string_byte_length(s)]; 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; var s: ?*String = null;
const err = c.dom_string_create(data.ptr, data.len, &s); const err = c.dom_string_create(data.ptr, data.len, &s);
try DOMErr(err); try DOMErr(err);