Merge pull request #2257 from navidemad/fix-a15-location-pathname-search-setters

browser: trigger navigation when Location.pathname or .search is assigned
This commit is contained in:
Karl Seguin
2026-04-27 18:31:09 +08:00
committed by GitHub
2 changed files with 27 additions and 2 deletions

View File

@@ -23,3 +23,11 @@
testing.expectEqual("", location.hash);
testing.expectEqual(testing.BASE_URL + 'window/location.html', location.href);
</script>
<script id=location_pathname_setter>
testing.expectEqual('function', typeof Object.getOwnPropertyDescriptor(Location.prototype, 'pathname').set);
</script>
<script id=location_search_setter>
testing.expectEqual('function', typeof Object.getOwnPropertyDescriptor(Location.prototype, 'search').set);
</script>

View File

@@ -20,6 +20,7 @@ const std = @import("std");
const js = @import("../js/js.zig");
const URL = @import("URL.zig");
const U = @import("../URL.zig");
const Frame = @import("../Frame.zig");
const Location = @This();
@@ -65,6 +66,22 @@ pub fn getHash(self: *const Location) []const u8 {
return self._url.getHash();
}
pub fn setPathname(_: *const Location, pathname: []const u8, frame: *Frame) !void {
const new_url = try U.setPathname(frame.url, pathname, frame.call_arena);
return frame.scheduleNavigation(new_url, .{
.reason = .script,
.kind = .{ .push = null },
}, .{ .script = frame });
}
pub fn setSearch(_: *const Location, search: []const u8, frame: *Frame) !void {
const new_url = try U.setSearch(frame.url, search, frame.call_arena);
return frame.scheduleNavigation(new_url, .{
.reason = .script,
.kind = .{ .push = null },
}, .{ .script = frame });
}
pub fn setHash(_: *const Location, hash: []const u8, frame: *Frame) !void {
const normalized_hash = blk: {
if (hash.len == 0) {
@@ -117,9 +134,9 @@ pub const JsApi = struct {
return self.assign(url, frame);
}
pub const search = bridge.accessor(Location.getSearch, null, .{});
pub const search = bridge.accessor(Location.getSearch, Location.setSearch, .{});
pub const hash = bridge.accessor(Location.getHash, Location.setHash, .{});
pub const pathname = bridge.accessor(Location.getPathname, null, .{});
pub const pathname = bridge.accessor(Location.getPathname, Location.setPathname, .{});
pub const hostname = bridge.accessor(Location.getHostname, null, .{});
pub const host = bridge.accessor(Location.getHost, null, .{});
pub const port = bridge.accessor(Location.getPort, null, .{});