Skip to content

Commit c6f59a7

Browse files
authored
Merge pull request #804 from lightpanda-io/add_error_event_web_api
add ErrorEvent webapi
2 parents bdb2338 + bf296ad commit c6f59a7

File tree

5 files changed

+125
-8
lines changed

5 files changed

+125
-8
lines changed

src/browser/events/event.zig

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,10 @@ const EventTargetUnion = @import("../dom/event_target.zig").Union;
3131
const CustomEvent = @import("custom_event.zig").CustomEvent;
3232
const ProgressEvent = @import("../xhr/progress_event.zig").ProgressEvent;
3333
const MouseEvent = @import("mouse_event.zig").MouseEvent;
34+
const ErrorEvent = @import("../html/error_event.zig").ErrorEvent;
3435

3536
// Event interfaces
36-
pub const Interfaces = .{
37-
Event,
38-
CustomEvent,
39-
ProgressEvent,
40-
MouseEvent,
41-
};
37+
pub const Interfaces = .{ Event, CustomEvent, ProgressEvent, MouseEvent, ErrorEvent };
4238

4339
pub const Union = generate.Union(Interfaces);
4440

@@ -62,6 +58,7 @@ pub const Event = struct {
6258
.custom_event => .{ .CustomEvent = @as(*CustomEvent, @ptrCast(evt)).* },
6359
.progress_event => .{ .ProgressEvent = @as(*ProgressEvent, @ptrCast(evt)).* },
6460
.mouse_event => .{ .MouseEvent = @as(*parser.MouseEvent, @ptrCast(evt)) },
61+
.error_event => .{ .ErrorEvent = @as(*ErrorEvent, @ptrCast(evt)).* },
6562
};
6663
}
6764

src/browser/html/error_event.zig

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright (C) 2023-2024 Lightpanda (Selecy SAS)
2+
//
3+
// Francis Bouvier <francis@lightpanda.io>
4+
// Pierre Tachoire <pierre@lightpanda.io>
5+
//
6+
// This program is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU Affero General Public License as
8+
// published by the Free Software Foundation, either version 3 of the
9+
// License, or (at your option) any later version.
10+
//
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU Affero General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU Affero General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
const Env = @import("../env.zig").Env;
19+
const parser = @import("../netsurf.zig");
20+
21+
// https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent
22+
pub const ErrorEvent = struct {
23+
pub const prototype = *parser.Event;
24+
pub const union_make_copy = true;
25+
26+
proto: parser.Event,
27+
message: []const u8,
28+
filename: []const u8,
29+
lineno: i32,
30+
colno: i32,
31+
@"error": ?Env.JsObject,
32+
33+
const ErrorEventInit = struct {
34+
message: []const u8 = "",
35+
filename: []const u8 = "",
36+
lineno: i32 = 0,
37+
colno: i32 = 0,
38+
@"error": ?Env.JsObject = null,
39+
};
40+
41+
pub fn constructor(event_type: []const u8, opts: ?ErrorEventInit) !ErrorEvent {
42+
const event = try parser.eventCreate();
43+
defer parser.eventDestroy(event);
44+
try parser.eventInit(event, event_type, .{});
45+
try parser.eventSetInternalType(event, .event);
46+
47+
const o = opts orelse ErrorEventInit{};
48+
49+
return .{
50+
.proto = event.*,
51+
.message = o.message,
52+
.filename = o.filename,
53+
.lineno = o.lineno,
54+
.colno = o.colno,
55+
.@"error" = if (o.@"error") |e| try e.persist() else null,
56+
};
57+
}
58+
59+
pub fn get_message(self: *const ErrorEvent) []const u8 {
60+
return self.message;
61+
}
62+
63+
pub fn get_filename(self: *const ErrorEvent) []const u8 {
64+
return self.filename;
65+
}
66+
67+
pub fn get_lineno(self: *const ErrorEvent) i32 {
68+
return self.lineno;
69+
}
70+
71+
pub fn get_colno(self: *const ErrorEvent) i32 {
72+
return self.colno;
73+
}
74+
75+
const ErrorValue = union(enum) {
76+
obj: Env.JsObject,
77+
undefined: void,
78+
};
79+
pub fn get_error(self: *const ErrorEvent) ErrorValue {
80+
if (self.@"error") |e| {
81+
return .{ .obj = e };
82+
}
83+
return .{ .undefined = {} };
84+
}
85+
};
86+
87+
const testing = @import("../../testing.zig");
88+
test "Browser.HTML.ErrorEvent" {
89+
var runner = try testing.jsRunner(testing.tracking_allocator, .{ .html = "<div id=c></div>" });
90+
defer runner.deinit();
91+
92+
try runner.testCases(&.{
93+
.{ "let e1 = new ErrorEvent('err1')", null },
94+
.{ "e1.message", "" },
95+
.{ "e1.filename", "" },
96+
.{ "e1.lineno", "0" },
97+
.{ "e1.colno", "0" },
98+
.{ "e1.error", "undefined" },
99+
100+
.{
101+
\\ let e2 = new ErrorEvent('err1', {
102+
\\ message: 'm1',
103+
\\ filename: 'fx19',
104+
\\ lineno: 443,
105+
\\ colno: 8999,
106+
\\ error: 'under 9000!',
107+
\\
108+
\\})
109+
,
110+
null,
111+
},
112+
.{ "e2.message", "m1" },
113+
.{ "e2.filename", "fx19" },
114+
.{ "e2.lineno", "443" },
115+
.{ "e2.colno", "8999" },
116+
.{ "e2.error", "under 9000!" },
117+
}, .{});
118+
}

src/browser/html/html.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ pub const Interfaces = .{
3737
Location,
3838
MediaQueryList,
3939
@import("screen.zig").Interfaces,
40+
@import("error_event.zig").ErrorEvent,
4041
};

src/browser/netsurf.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ pub const EventType = enum(u8) {
525525
progress_event = 1,
526526
custom_event = 2,
527527
mouse_event = 3,
528+
error_event = 4,
528529
};
529530

530531
pub const MutationEvent = c.dom_mutation_event;

src/browser/xhr/progress_event.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ pub const ProgressEvent = struct {
3737
loaded: u64 = 0,
3838
total: u64 = 0,
3939

40-
pub fn constructor(eventType: []const u8, opts: ?EventInit) !ProgressEvent {
40+
pub fn constructor(event_type: []const u8, opts: ?EventInit) !ProgressEvent {
4141
const event = try parser.eventCreate();
4242
defer parser.eventDestroy(event);
43-
try parser.eventInit(event, eventType, .{});
43+
try parser.eventInit(event, event_type, .{});
4444
try parser.eventSetInternalType(event, .progress_event);
4545

4646
const o = opts orelse EventInit{};

0 commit comments

Comments
 (0)