Lightpanda: the headless browser designed for AI and automation
history: traverse within a document by entry, not by URL
Navid EMAD84d421393d4c
diff --git a/src/browser/tests/history_traverse.html b/src/browser/tests/history_traverse.htmlnew file mode 100644index 000000000..d9211f469--- /dev/null+++ b/src/browser/tests/history_traverse.html@@ -0,0 +1,37 @@+<!DOCTYPE html>+<script src="testing.js"></script>++<script id=traverse_entries_of_one_document>+ {+ // Entries pushed by this document belong to it whatever their path or+ // query: traversing between them must stay in the document (location+ // updated synchronously, popstate fired) instead of loading the URL.+ const original = location.href;+ const path = location.pathname;+ const pops = [];+ const onpop = (e) => pops.push([location.pathname + location.search, e.state]);+ window.addEventListener('popstate', onpop);++ history.pushState({n: 1}, '', '?n=1');+ history.pushState({n: 2}, '', '/history-traverse-path');++ history.back();+ testing.expectEqual(path + '?n=1', location.pathname + location.search);+ testing.expectEqual({n: 1}, history.state);++ history.forward();+ testing.expectEqual('/history-traverse-path', location.pathname);+ testing.expectEqual({n: 2}, history.state);++ testing.expectEqual([+ [path + '?n=1', {n: 1}],+ ['/history-traverse-path', {n: 2}],+ ], pops);++ const pushed = navigation.entries().find((e) => e.url.endsWith('/history-traverse-path'));+ testing.expectEqual(true, pushed.sameDocument);++ window.removeEventListener('popstate', onpop);+ history.replaceState(null, '', original);+ }+</script>diff --git a/src/browser/webapi/History.zig b/src/browser/webapi/History.zigindex 67717c23a..54ad05396 100644--- a/src/browser/webapi/History.zig+++ b/src/browser/webapi/History.zig@@ -109,11 +109,13 @@ fn goInner(delta: i32, frame: *Frame) !void {const index = @as(usize, @intCast(index_s));const entry = frame._session.navigation._entries.items[index];+ // popstate fires on the document that stays; a traversal to another+ // document replaces this one instead.+ const same_document = entry.sameDocument(frame);_ = try frame._session.navigation.navigateInner(entry._url, .{ .traverse = index }, frame);- const url = entry._url orelse return;- if (frame.isSameOrigin(url)) {+ if (same_document) {const target = frame.window.asEventTarget();if (frame._event_manager.hasDirectListeners(target, "popstate", frame.window._on_popstate)) {const event = (try PopStateEvent.initTrusted(comptime .wrap("popstate"), .{ .state = entry._state.value }, frame)).asEvent();@@ -158,4 +160,5 @@ const testing = @import("../../testing.zig");test "WebApi: History" {try testing.htmlRunner("history.html", .{});try testing.htmlRunner("history_url_update.html", .{});+ try testing.htmlRunner("history_traverse.html", .{});}diff --git a/src/browser/webapi/navigation/Navigation.zig b/src/browser/webapi/navigation/Navigation.zigindex f9892674c..903650ac6 100644--- a/src/browser/webapi/navigation/Navigation.zig+++ b/src/browser/webapi/navigation/Navigation.zig@@ -196,6 +196,20 @@ pub fn commitNavigation(self: *Navigation, frame: *Frame) !void {try self.updateEntries(url, kind, frame, false);+ // A traversal or reload recreates the document of the entry it lands on.+ // Every entry that shared the old document now belongs to the new one.+ switch (kind) {+ .traverse, .reload => {+ const old_loader_id = self.getCurrentEntry()._loader_id;+ for (self._entries.items) |entry| {+ if (entry._loader_id == old_loader_id) {+ entry._loader_id = frame._loader_id;+ }+ }+ },+ .push, .replace => {},+ }+self._activation = NavigationActivation{// If we are navigating away from the initial about:blank, we have no from.._from = if (was_initial_entry) null else from_entry,@@ -252,6 +266,7 @@ pub fn pushEntry(._key = id_str,._url = url,._state = state,+ ._loader_id = frame._loader_id,},});@@ -293,6 +308,7 @@ pub fn replaceEntry(._key = previous._key,._url = url,._state = state,+ ._loader_id = frame._loader_id,},});@@ -397,7 +413,14 @@ pub fn navigateInner(const finished = local.createPromiseResolver();var new_url = try URL.resolve(arena.allocator(), frame.url, url, .{});- const is_same_document = URL.eqlDocument(new_url, frame.url);+ const is_same_url = URL.eqlDocument(new_url, frame.url);+ // navigate() stays in the document only for a fragment change, but a+ // traversal stays in it whenever the entry belongs to it, whatever URL+ // pushState gave the entry.+ const is_same_document = switch (kind) {+ .traverse => |index| self._entries.items[index].sameDocument(frame),+ else => is_same_url,+ };// In case of navigation to the same document, we force an url duplication.// Keeping the same url generates a crash during WPT test navigate-history-push-same-url.html.@@ -459,7 +482,7 @@ pub fn navigateInner(},}- if (is_same_document and !std.mem.eql(u8, old_url, new_url)) {+ if (is_same_document and is_same_url and !std.mem.eql(u8, old_url, new_url)) {try frame.queueHashChange(old_url, new_url);}diff --git a/src/browser/webapi/navigation/NavigationHistoryEntry.zig b/src/browser/webapi/navigation/NavigationHistoryEntry.zigindex 466dfc6f3..615dbac39 100644--- a/src/browser/webapi/navigation/NavigationHistoryEntry.zig+++ b/src/browser/webapi/navigation/NavigationHistoryEntry.zig@@ -17,7 +17,6 @@// along with this program. If not, see <https://www.gnu.org/licenses/>.const std = @import("std");-const URL = @import("../URL.zig");const NavigationState = @import("root.zig").NavigationState;const Event = @import("../Event.zig");const EventTarget = @import("../EventTarget.zig");@@ -34,6 +33,10 @@ _id: []const u8,_key: []const u8,_url: ?[:0]const u8,_state: NavigationState,+// The document this entry belongs to, as that document's frame loader id.+// Entries created by pushState/replaceState or a fragment navigation share+// their document's id; traversing between them stays in that document.+_loader_id: u32,_on_dispose: ?js.Function.Global = null,@@ -61,9 +64,12 @@ pub fn key(self: *const NavigationHistoryEntry) []const u8 {return self._key;}-fn sameDocument(self: *const NavigationHistoryEntry, frame: *Frame) bool {- const got_url = self._url orelse return false;- return URL.eqlDocument(got_url, frame.base());+// https://html.spec.whatwg.org/multipage/browsing-the-web.html#traverse-the-history+// Same document means the entry's document is the frame's current one, not+// that the URLs match: pushState can change the path or query of an entry+// without leaving its document.+pub fn sameDocument(self: *const NavigationHistoryEntry, frame: *Frame) bool {+ return self._loader_id == frame._loader_id;}pub fn url(self: *const NavigationHistoryEntry) ?[:0]const u8 {