Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/browser/tests/history_url_update.html
Original file line number Diff line number Diff line change
@@ -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>
19 changes: 17 additions & 2 deletions src/browser/webapi/History.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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", .{});
}
Loading