|
16 | 16 | // You should have received a copy of the GNU Affero General Public License |
17 | 17 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
18 | 18 |
|
19 | | -const Page = @import("../page.zig").Page; |
| 19 | +const Uri = @import("std").Uri; |
20 | 20 |
|
| 21 | +const Page = @import("../page.zig").Page; |
21 | 22 | const URL = @import("../url/url.zig").URL; |
22 | 23 |
|
23 | 24 | // https://html.spec.whatwg.org/multipage/nav-history-apis.html#the-location-interface |
24 | 25 | pub const Location = struct { |
25 | | - url: ?URL = null, |
| 26 | + url: URL, |
| 27 | + |
| 28 | + /// Browsers give such initial values when user not navigated yet: |
| 29 | + /// Chrome -> chrome://new-tab-page/ |
| 30 | + /// Firefox -> about:newtab |
| 31 | + /// Safari -> favorites:// |
| 32 | + pub const default = Location{ |
| 33 | + .url = .initWithoutSearchParams(Uri.parse("about:blank") catch unreachable), |
| 34 | + }; |
26 | 35 |
|
27 | 36 | pub fn get_href(self: *Location, page: *Page) ![]const u8 { |
28 | | - if (self.url) |*u| return u.get_href(page); |
29 | | - return ""; |
| 37 | + return self.url.get_href(page); |
| 38 | + } |
| 39 | + |
| 40 | + pub fn set_href(_: *const Location, href: []const u8, page: *Page) !void { |
| 41 | + return page.navigateFromWebAPI(href, .{ .reason = .script }); |
30 | 42 | } |
31 | 43 |
|
32 | 44 | pub fn get_protocol(self: *Location, page: *Page) ![]const u8 { |
33 | | - if (self.url) |*u| return u.get_protocol(page); |
34 | | - return ""; |
| 45 | + return self.url.get_protocol(page); |
35 | 46 | } |
36 | 47 |
|
37 | 48 | pub fn get_host(self: *Location, page: *Page) ![]const u8 { |
38 | | - if (self.url) |*u| return u.get_host(page); |
39 | | - return ""; |
| 49 | + return self.url.get_host(page); |
40 | 50 | } |
41 | 51 |
|
42 | 52 | pub fn get_hostname(self: *Location) []const u8 { |
43 | | - if (self.url) |*u| return u.get_hostname(); |
44 | | - return ""; |
| 53 | + return self.url.get_hostname(); |
45 | 54 | } |
46 | 55 |
|
47 | 56 | pub fn get_port(self: *Location, page: *Page) ![]const u8 { |
48 | | - if (self.url) |*u| return u.get_port(page); |
49 | | - return ""; |
| 57 | + return self.url.get_port(page); |
50 | 58 | } |
51 | 59 |
|
52 | 60 | pub fn get_pathname(self: *Location) []const u8 { |
53 | | - if (self.url) |*u| return u.get_pathname(); |
54 | | - return ""; |
| 61 | + return self.url.get_pathname(); |
55 | 62 | } |
56 | 63 |
|
57 | 64 | pub fn get_search(self: *Location, page: *Page) ![]const u8 { |
58 | | - if (self.url) |*u| return u.get_search(page); |
59 | | - return ""; |
| 65 | + return self.url.get_search(page); |
60 | 66 | } |
61 | 67 |
|
62 | 68 | pub fn get_hash(self: *Location, page: *Page) ![]const u8 { |
63 | | - if (self.url) |*u| return u.get_hash(page); |
64 | | - return ""; |
| 69 | + return self.url.get_hash(page); |
65 | 70 | } |
66 | 71 |
|
67 | 72 | pub fn get_origin(self: *Location, page: *Page) ![]const u8 { |
68 | | - if (self.url) |*u| return u.get_origin(page); |
69 | | - return ""; |
| 73 | + return self.url.get_origin(page); |
70 | 74 | } |
71 | 75 |
|
72 | 76 | pub fn _assign(_: *const Location, url: []const u8, page: *Page) !void { |
|
0 commit comments