1616// You should have received a copy of the GNU Affero General Public License
1717// along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
19+ const std = @import ("std" );
1920const Env = @import ("../env.zig" ).Env ;
2021const parser = @import ("../netsurf.zig" );
2122const Page = @import ("../page.zig" ).Page ;
@@ -39,33 +40,30 @@ pub const EventTarget = struct {
3940 // Extend libdom event target for pure zig struct.
4041 base : parser.EventTargetTBase = parser.EventTargetTBase { .internal_target_type = .plain },
4142
42- pub fn toInterface (e : * parser.Event , et : * parser.EventTarget , page : * Page ) ! Union {
43+ pub fn toInterface (et : * parser.EventTarget , page : * Page ) ! Union {
4344 // libdom assumes that all event targets are libdom nodes. They are not.
4445
45- // The window is a common non-node target, but it's easy to handle as
46- // its a singleton.
47- if (@intFromPtr (et ) == @intFromPtr (& page .window .base )) {
48- return .{ .node = .{ .Window = & page .window } };
49- }
50-
51- if (try parser .eventTargetInternalType (et ) == .plain ) {
52- return .{ .plain = et };
53- }
54-
55- // AbortSignal is another non-node target. It has a distinct usage though
56- // so we hijack the event internal type to identity if.
57- switch (try parser .eventGetInternalType (e )) {
46+ switch (try parser .eventTargetInternalType (et )) {
47+ .libdom_node = > {
48+ return .{ .node = try nod .Node .toInterface (@as (* parser .Node , @ptrCast (et ))) };
49+ },
50+ .plain = > return .{ .plain = et },
5851 .abort_signal = > {
52+ // AbortSignal is a special case, it has its own internal type.
53+ // We return it as a node, but we need to handle it differently.
5954 return .{ .node = .{ .AbortSignal = @fieldParentPtr ("proto" , @as (* parser .EventTargetTBase , @ptrCast (et ))) } };
6055 },
61- .xhr_event = > {
56+ .window = > {
57+ // The window is a common non-node target, but it's easy to handle as its a singleton.
58+ std .debug .assert (@intFromPtr (et ) == @intFromPtr (& page .window .base ));
59+ return .{ .node = .{ .Window = & page .window } };
60+ },
61+ .xhr = > {
6262 const XMLHttpRequestEventTarget = @import ("../xhr/event_target.zig" ).XMLHttpRequestEventTarget ;
6363 const base : * XMLHttpRequestEventTarget = @fieldParentPtr ("base" , @as (* parser .EventTargetTBase , @ptrCast (et )));
6464 return .{ .xhr = @fieldParentPtr ("proto" , base ) };
6565 },
66- else = > {
67- return .{ .node = try nod .Node .toInterface (@as (* parser .Node , @ptrCast (et ))) };
68- },
66+ else = > return error .MissingEventTargetType ,
6967 }
7068 }
7169
0 commit comments