Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event target type #367

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/dom/event_target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const EventHandler = @import("../events/event.zig").EventHandler;

const DOMException = @import("exceptions.zig").DOMException;
const Nod = @import("node.zig");
const Window = @import("../html/window.zig").Window;

// EventTarget interfaces
pub const Union = Nod.Union;
Expand All @@ -40,9 +41,10 @@ pub const EventTarget = struct {
pub const mem_guarantied = true;

pub fn toInterface(et: *parser.EventTarget) !Union {
// NOTE: for now we state that all EventTarget are Nodes
// TODO: handle other types (eg. Window)
return Nod.Node.toInterface(@as(*parser.Node, @ptrCast(et)));
return switch (try parser.eventTargetGetType(et)) {
.window => .{ .Window = @as(*Window, @ptrCast(et)) },
.node => Nod.Node.toInterface(@as(*parser.Node, @ptrCast(et))),
};
}

// JS funcs
Expand Down
1 change: 1 addition & 0 deletions src/html/window.zig
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub const Window = struct {
return Window{
.target = target orelse "",
.navigator = navigator orelse .{},
.base = .{ .et_type = @intFromEnum(parser.EventTargetType.window) },
};
}

Expand Down
26 changes: 26 additions & 0 deletions src/netsurf/netsurf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,11 @@ fn eventListenerGetData(lst: *EventListener) ?*anyopaque {
}

// EventTarget
pub const EventTargetType = enum(u4) {
node = c.DOM_EVENT_TARGET_NODE,
window = 2,
};

pub const EventTarget = c.dom_event_target;

pub fn eventTargetToNode(et: *EventTarget) *Node {
Expand Down Expand Up @@ -801,6 +806,14 @@ pub fn eventTargetDispatchEvent(et: *EventTarget, event: *Event) !bool {
return res;
}

pub fn eventTargetGetType(et: *EventTarget) !EventTargetType {
var res: c.dom_event_target_type = undefined;
const err = eventTargetVtable(et).dom_event_target_get_type.?(et, &res);
try DOMErr(err);

return @enumFromInt(res);
}

pub fn eventTargetTBaseFieldName(comptime T: type) ?[]const u8 {
std.debug.assert(@inComptime());
switch (@typeInfo(T)) {
Expand All @@ -824,8 +837,10 @@ pub const EventTargetTBase = extern struct {
.remove_event_listener = remove_event_listener,
.add_event_listener = add_event_listener,
.iter_event_listener = iter_event_listener,
.dom_event_target_get_type = dom_event_target_get_type,
},
eti: c.dom_event_target_internal = c.dom_event_target_internal{ .listeners = null },
et_type: c.dom_event_target_type = @intFromEnum(EventTargetType.node),

pub fn add_event_listener(et: [*c]c.dom_event_target, t: [*c]c.dom_string, l: ?*c.struct_dom_event_listener, capture: bool) callconv(.C) c.dom_exception {
const self = @as(*Self, @ptrCast(et));
Expand Down Expand Up @@ -858,6 +873,17 @@ pub const EventTargetTBase = extern struct {
const self = @as(*Self, @ptrCast(et));
return c._dom_event_target_iter_event_listener(self.eti, t, capture, cur, next, l);
}

pub fn dom_event_target_get_type(
et: [*c]c.dom_event_target,
res: [*c]c.dom_event_target_type,
) callconv(.C) c.dom_exception {
const self = @as(*Self, @ptrCast(et));

res.* = self.et_type;

return c.DOM_NO_ERR;
}
};

// NodeType
Expand Down
Loading