Merge pull request #2131 from lightpanda-io/pushstate-pathname

update page URL and location on pushState/replaceState
This commit is contained in:
Pierre Tachoire
2026-04-10 17:37:43 +02:00
committed by GitHub
2 changed files with 36 additions and 2 deletions

View File

@@ -0,0 +1,19 @@
<!DOCTYPE html>
<script src="testing.js"></script>
<script id=url_updates>
{
const originalUrl = location.href;
// pushState updates the URL
history.pushState(null, '', '/test-push-path');
testing.expectEqual('/test-push-path', location.pathname);
// replaceState updates the URL
history.replaceState(null, '', '/test-replace-path');
testing.expectEqual('/test-replace-path', location.pathname);
// Restore original URL so other tests aren't affected
history.replaceState(null, '', originalUrl);
}
</script>

View File

@@ -20,7 +20,9 @@ const std = @import("std");
const js = @import("../js/js.zig");
const Page = @import("../Page.zig");
const Location = @import("Location.zig");
const PopStateEvent = @import("event/PopStateEvent.zig");
const URL = @import("URL.zig");
const History = @This();
@@ -51,18 +53,30 @@ pub fn setScrollRestoration(self: *History, str: []const u8) void {
pub fn pushState(_: *History, state: js.Value, _: ?[]const u8, _url: ?[]const u8, page: *Page) !void {
const arena = page._session.arena;
const url = if (_url) |u| try arena.dupeZ(u8, u) else try arena.dupeZ(u8, page.url);
const url = if (_url) |u|
try @import("../URL.zig").resolve(arena, page.url, u, .{ .always_dupe = true })
else
try arena.dupeZ(u8, page.url);
const json = state.toJson(arena) catch return error.DataClone;
_ = try page._session.navigation.pushEntry(url, .{ .source = .history, .value = json }, page, true);
page.url = url;
page.window._location._url = try URL.init(url, null, page);
}
pub fn replaceState(_: *History, state: js.Value, _: ?[]const u8, _url: ?[]const u8, page: *Page) !void {
const arena = page._session.arena;
const url = if (_url) |u| try arena.dupeZ(u8, u) else try arena.dupeZ(u8, page.url);
const url = if (_url) |u|
try @import("../URL.zig").resolve(arena, page.url, u, .{ .always_dupe = true })
else
try arena.dupeZ(u8, page.url);
const json = state.toJson(arena) catch return error.DataClone;
_ = try page._session.navigation.replaceEntry(url, .{ .source = .history, .value = json }, page, true);
page.url = url;
page.window._location = try Location.init(url, page);
}
fn goInner(delta: i32, page: *Page) !void {
@@ -124,4 +138,5 @@ pub const JsApi = struct {
const testing = @import("../../testing.zig");
test "WebApi: History" {
try testing.htmlRunner("history.html", .{});
try testing.htmlRunner("history_url_update.html", .{});
}