|
| 1 | +// Copyright (C) 2023-2024 Lightpanda (Selecy SAS) |
| 2 | +// |
| 3 | +// Francis Bouvier <[email protected]> |
| 4 | +// Pierre Tachoire <[email protected]> |
| 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 | + |
| 19 | +const std = @import("std"); |
| 20 | + |
| 21 | +const builtin = @import("builtin"); |
| 22 | +const jsruntime = @import("jsruntime"); |
| 23 | + |
| 24 | +const URL = @import("../url/url.zig").URL; |
| 25 | + |
| 26 | +const Case = jsruntime.test_utils.Case; |
| 27 | +const checkCases = jsruntime.test_utils.checkCases; |
| 28 | + |
| 29 | +// https://html.spec.whatwg.org/multipage/nav-history-apis.html#the-location-interface |
| 30 | +pub const Location = struct { |
| 31 | + pub const mem_guarantied = true; |
| 32 | + |
| 33 | + url: ?*URL = null, |
| 34 | + |
| 35 | + pub fn deinit(_: *Location, _: std.mem.Allocator) void {} |
| 36 | + |
| 37 | + pub fn get_href(self: *Location, alloc: std.mem.Allocator) ![]const u8 { |
| 38 | + if (self.url) |u| return u.get_href(alloc); |
| 39 | + |
| 40 | + return ""; |
| 41 | + } |
| 42 | + |
| 43 | + pub fn get_protocol(self: *Location, alloc: std.mem.Allocator) ![]const u8 { |
| 44 | + if (self.url) |u| return u.get_protocol(alloc); |
| 45 | + |
| 46 | + return ""; |
| 47 | + } |
| 48 | + |
| 49 | + pub fn get_host(self: *Location, alloc: std.mem.Allocator) ![]const u8 { |
| 50 | + if (self.url) |u| return u.get_host(alloc); |
| 51 | + |
| 52 | + return ""; |
| 53 | + } |
| 54 | + |
| 55 | + pub fn get_hostname(self: *Location) []const u8 { |
| 56 | + if (self.url) |u| return u.get_hostname(); |
| 57 | + |
| 58 | + return ""; |
| 59 | + } |
| 60 | + |
| 61 | + pub fn get_port(self: *Location, alloc: std.mem.Allocator) ![]const u8 { |
| 62 | + if (self.url) |u| return u.get_port(alloc); |
| 63 | + |
| 64 | + return ""; |
| 65 | + } |
| 66 | + |
| 67 | + pub fn get_pathname(self: *Location) []const u8 { |
| 68 | + if (self.url) |u| return u.get_pathname(); |
| 69 | + |
| 70 | + return ""; |
| 71 | + } |
| 72 | + |
| 73 | + pub fn get_search(self: *Location, alloc: std.mem.Allocator) ![]const u8 { |
| 74 | + if (self.url) |u| return u.get_search(alloc); |
| 75 | + |
| 76 | + return ""; |
| 77 | + } |
| 78 | + |
| 79 | + pub fn get_hash(self: *Location, alloc: std.mem.Allocator) ![]const u8 { |
| 80 | + if (self.url) |u| return u.get_hash(alloc); |
| 81 | + |
| 82 | + return ""; |
| 83 | + } |
| 84 | + |
| 85 | + pub fn get_origin(self: *Location, alloc: std.mem.Allocator) ![]const u8 { |
| 86 | + if (self.url) |u| return u.get_origin(alloc); |
| 87 | + |
| 88 | + return ""; |
| 89 | + } |
| 90 | + |
| 91 | + // TODO |
| 92 | + pub fn _assign(_: *Location, url: []const u8) !void { |
| 93 | + _ = url; |
| 94 | + } |
| 95 | + |
| 96 | + // TODO |
| 97 | + pub fn _replace(_: *Location, url: []const u8) !void { |
| 98 | + _ = url; |
| 99 | + } |
| 100 | + |
| 101 | + // TODO |
| 102 | + pub fn _reload(_: *Location) !void {} |
| 103 | + |
| 104 | + pub fn _toString(self: *Location, alloc: std.mem.Allocator) ![]const u8 { |
| 105 | + return try self.get_href(alloc); |
| 106 | + } |
| 107 | +}; |
| 108 | + |
| 109 | +// Tests |
| 110 | +// ----- |
| 111 | + |
| 112 | +pub fn testExecFn( |
| 113 | + _: std.mem.Allocator, |
| 114 | + js_env: *jsruntime.Env, |
| 115 | +) anyerror!void { |
| 116 | + var location = [_]Case{ |
| 117 | + .{ .src = "location.href", .ex = "https://lightpanda.io/opensource-browser/" }, |
| 118 | + .{ .src = "document.location.href", .ex = "https://lightpanda.io/opensource-browser/" }, |
| 119 | + |
| 120 | + .{ .src = "location.host", .ex = "lightpanda.io" }, |
| 121 | + .{ .src = "location.hostname", .ex = "lightpanda.io" }, |
| 122 | + .{ .src = "location.origin", .ex = "https://lightpanda.io" }, |
| 123 | + .{ .src = "location.pathname", .ex = "/opensource-browser/" }, |
| 124 | + .{ .src = "location.hash", .ex = "" }, |
| 125 | + .{ .src = "location.port", .ex = "" }, |
| 126 | + .{ .src = "location.search", .ex = "" }, |
| 127 | + }; |
| 128 | + try checkCases(js_env, &location); |
| 129 | +} |
0 commit comments