|
| 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 | +} |
0 commit comments