diff --git a/src/browser/Page.zig b/src/browser/Page.zig index 9da5f5d1..ce3620fa 100644 --- a/src/browser/Page.zig +++ b/src/browser/Page.zig @@ -287,8 +287,13 @@ pub fn navigate(self: *Page, request_url: [:0]const u8, opts: NavigateOpts) !voi if (!opts.force) { // If we are navigating within the same document, just change URL. if (URL.eqlDocument(self.url, resolved_url)) { + // update page url self.url = resolved_url; - // 3. change window.location + + // update location + self.window._location = try Location.init(self.url, self); + self.document._location = self.window._location; + try session.navigation.updateEntries("", .{ .push = null }, self, true); return; } diff --git a/src/browser/tests/window/location.html b/src/browser/tests/window/location.html index 01a4049d..b61808e0 100644 --- a/src/browser/tests/window/location.html +++ b/src/browser/tests/window/location.html @@ -5,3 +5,21 @@ testing.expectEqual('http://127.0.0.1:9582/src/browser/tests/window/location.html', window.location.href); testing.expectEqual(document.location, window.location); + + diff --git a/src/browser/webapi/Location.zig b/src/browser/webapi/Location.zig index e7191a13..205d1732 100644 --- a/src/browser/webapi/Location.zig +++ b/src/browser/webapi/Location.zig @@ -16,6 +16,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +const std = @import("std"); const js = @import("../js/js.zig"); const URL = @import("URL.zig"); @@ -64,6 +65,25 @@ pub fn getHash(self: *const Location) []const u8 { return self._url.getHash(); } +pub fn setHash(_: *const Location, hash: []const u8, page: *Page) !void { + const normalized_hash = blk: { + if (hash.len == 0) { + const old_url = page.url; + + break :blk if (std.mem.indexOfScalar(u8, old_url, '#')) |index| + old_url[0..index] + else + old_url; + } else if (hash[0] == '#') + break :blk hash + else + break :blk try std.fmt.allocPrint(page.arena, "#{s}", .{hash}); + }; + + const duped_hash = try page.arena.dupeZ(u8, normalized_hash); + return page.navigate(duped_hash, .{ .reason = .script }); +} + pub fn toString(self: *const Location, page: *const Page) ![:0]const u8 { return self._url.toString(page); } @@ -80,7 +100,7 @@ pub const JsApi = struct { pub const toString = bridge.function(Location.toString, .{}); pub const href = bridge.accessor(Location.toString, null, .{}); pub const search = bridge.accessor(Location.getSearch, null, .{}); - pub const hash = bridge.accessor(Location.getHash, null, .{}); + pub const hash = bridge.accessor(Location.getHash, Location.setHash, .{}); pub const pathname = bridge.accessor(Location.getPathname, null, .{}); pub const hostname = bridge.accessor(Location.getHostname, null, .{}); pub const host = bridge.accessor(Location.getHost, null, .{});