backport Location getHash

This commit is contained in:
Muki Kiboigo
2025-12-08 05:16:47 -08:00
parent 907298c6b1
commit 9d7b80c1ac
3 changed files with 45 additions and 2 deletions

View File

@@ -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;
}

View File

@@ -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);
</script>
<script id=location_hash>
location.hash = "";
testing.expectEqual("", location.hash);
testing.expectEqual('http://127.0.0.1:9582/src/browser/tests/window/location.html', location.href);
location.hash = "#abcdef";
testing.expectEqual("#abcdef", location.hash);
testing.expectEqual('http://127.0.0.1:9582/src/browser/tests/window/location.html#abcdef', location.href);
location.hash = "xyzxyz";
testing.expectEqual("#xyzxyz", location.hash);
testing.expectEqual('http://127.0.0.1:9582/src/browser/tests/window/location.html#xyzxyz', location.href);
location.hash = "";
testing.expectEqual("", location.hash);
testing.expectEqual('http://127.0.0.1:9582/src/browser/tests/window/location.html', location.href);
</script>

View File

@@ -16,6 +16,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
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, .{});