From 69884b9d8d7e0f4555c9c5969f17282d25120c27 Mon Sep 17 00:00:00 2001 From: Halil Durak Date: Tue, 21 Oct 2025 16:44:29 +0300 Subject: [PATCH] `Location` changes regarding to changes in `URL` --- src/browser/html/document.zig | 4 ++-- src/browser/html/location.zig | 21 +++++++++++---------- src/browser/html/window.zig | 7 ++++++- src/browser/page.zig | 2 +- src/url.zig | 4 ---- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/browser/html/document.zig b/src/browser/html/document.zig index 4020b498..0516e76d 100644 --- a/src/browser/html/document.zig +++ b/src/browser/html/document.zig @@ -42,12 +42,12 @@ pub const HTMLDocument = struct { // JS funcs // -------- - pub fn get_domain(self: *parser.DocumentHTML, page: *Page) ![]const u8 { + pub fn get_domain(self: *parser.DocumentHTML) ![]const u8 { // libdom's document_html get_domain always returns null, this is // the way MDN recommends getting the domain anyways, since document.domain // is deprecated. const location = try parser.documentHTMLGetLocation(Location, self) orelse return ""; - return location.get_host(page); + return location.get_host(); } pub fn set_domain(_: *parser.DocumentHTML, _: []const u8) ![]const u8 { diff --git a/src/browser/html/location.zig b/src/browser/html/location.zig index 3e3e593b..1f8d134c 100644 --- a/src/browser/html/location.zig +++ b/src/browser/html/location.zig @@ -25,13 +25,14 @@ const URL = @import("../url/url.zig").URL; pub const Location = struct { url: URL, + /// Initializes the `Location` to be used in `Window`. /// Browsers give such initial values when user not navigated yet: /// Chrome -> chrome://new-tab-page/ /// Firefox -> about:newtab /// Safari -> favorites:// - pub const default = Location{ - .url = .initWithoutSearchParams(Uri.parse("about:blank") catch unreachable), - }; + pub fn init(url: []const u8) !Location { + return .{ .url = try .initForLocation(url) }; + } pub fn get_href(self: *Location, page: *Page) ![]const u8 { return self.url.get_href(page); @@ -45,16 +46,16 @@ pub const Location = struct { return self.url.get_protocol(); } - pub fn get_host(self: *Location, page: *Page) ![]const u8 { - return self.url.get_host(page); + pub fn get_host(self: *Location) []const u8 { + return self.url.get_host(); } pub fn get_hostname(self: *Location) []const u8 { return self.url.get_hostname(); } - pub fn get_port(self: *Location, page: *Page) ![]const u8 { - return self.url.get_port(page); + pub fn get_port(self: *Location) []const u8 { + return self.url.get_port(); } pub fn get_pathname(self: *Location) []const u8 { @@ -65,8 +66,8 @@ pub const Location = struct { return self.url.get_search(page); } - pub fn get_hash(self: *Location, page: *Page) ![]const u8 { - return self.url.get_hash(page); + pub fn get_hash(self: *Location) []const u8 { + return self.url.get_hash(); } pub fn get_origin(self: *Location, page: *Page) ![]const u8 { @@ -86,7 +87,7 @@ pub const Location = struct { } pub fn _toString(self: *Location, page: *Page) ![]const u8 { - return try self.get_href(page); + return self.get_href(page); } }; diff --git a/src/browser/html/window.zig b/src/browser/html/window.zig index ef015492..97aa381f 100644 --- a/src/browser/html/window.zig +++ b/src/browser/html/window.zig @@ -53,7 +53,7 @@ pub const Window = struct { document: *parser.DocumentHTML, target: []const u8 = "", - location: Location = .default, + location: Location, storage_shelf: ?*storage.Shelf = null, // counter for having unique timer ids @@ -79,6 +79,7 @@ pub const Window = struct { return .{ .document = html_doc, .target = target orelse "", + .location = try .init("about:blank"), .navigator = navigator orelse .{}, .performance = Performance.init(), }; @@ -89,6 +90,10 @@ pub const Window = struct { try parser.documentHTMLSetLocation(Location, self.document, &self.location); } + pub fn changeLocation(self: *Window, new_url: []const u8, page: *Page) !void { + return self.location.url.reinit(new_url, page); + } + pub fn replaceDocument(self: *Window, doc: *parser.DocumentHTML) !void { self.performance.reset(); // When to reset see: https://developer.mozilla.org/en-US/docs/Web/API/Performance/timeOrigin self.document = doc; diff --git a/src/browser/page.zig b/src/browser/page.zig index 833c683f..897cb414 100644 --- a/src/browser/page.zig +++ b/src/browser/page.zig @@ -859,7 +859,7 @@ pub const Page = struct { self.window.setStorageShelf( try self.session.storage_shed.getOrPut(try self.origin(self.arena)), ); - try self.window.replaceLocation(.{ .url = try self.url.toWebApi(self.arena) }); + try self.window.changeLocation(self.url.raw, self); } pub const MouseEvent = struct { diff --git a/src/url.zig b/src/url.zig index acfac256..a818327e 100644 --- a/src/url.zig +++ b/src/url.zig @@ -75,10 +75,6 @@ pub const URL = struct { return writer.writeAll(self.raw); } - pub fn toWebApi(self: *const URL, allocator: Allocator) !WebApiURL { - return WebApiURL.init(allocator, self.uri); - } - /// Properly stitches two URL fragments together. /// /// For URLs with a path, it will replace the last entry with the src.