support CustomEvent in createEvent

This commit is contained in:
nikneym
2025-09-09 21:44:09 +03:00
parent 2ed8a1c0ec
commit e1c765e78a

View File

@@ -36,6 +36,8 @@ const CSSStyleSheet = @import("../cssom/CSSStyleSheet.zig");
const NodeIterator = @import("node_iterator.zig").NodeIterator; const NodeIterator = @import("node_iterator.zig").NodeIterator;
const Range = @import("range.zig").Range; const Range = @import("range.zig").Range;
const CustomEvent = @import("../events/custom_event.zig").CustomEvent;
const Env = @import("../env.zig").Env; const Env = @import("../env.zig").Env;
const DOMImplementation = @import("implementation.zig").DOMImplementation; const DOMImplementation = @import("implementation.zig").DOMImplementation;
@@ -110,13 +112,21 @@ pub const Document = struct {
return try parser.documentGetDoctype(self); return try parser.documentGetDoctype(self);
} }
pub fn _createEvent(_: *parser.Document, eventCstr: []const u8) !*parser.Event { pub fn _createEvent(_: *parser.Document, eventCstr: []const u8) !union(enum) {
// TODO: for now only "Event" constructor is supported base: *parser.Event,
// see table on https://dom.spec.whatwg.org/#dom-document-createevent $2 custom: CustomEvent,
} {
if (std.ascii.eqlIgnoreCase(eventCstr, "Event") or std.ascii.eqlIgnoreCase(eventCstr, "Events")) { if (std.ascii.eqlIgnoreCase(eventCstr, "Event") or std.ascii.eqlIgnoreCase(eventCstr, "Events")) {
return try parser.eventCreate(); return .{ .base = try parser.eventCreate() };
} }
return parser.DOMError.NotSupported;
// Not documented in MDN but supported in Chrome.
// This is actually both instance of `Event` and `CustomEvent`.
if (std.ascii.eqlIgnoreCase(eventCstr, "CustomEvent")) {
return .{ .custom = try CustomEvent.constructor(eventCstr, null) };
}
return error.NotSupported;
} }
pub fn _getElementById(self: *parser.Document, id: []const u8) !?ElementUnion { pub fn _getElementById(self: *parser.Document, id: []const u8) !?ElementUnion {