Lightpanda: the headless browser designed for AI and automation
browser: fire popstate on a fragment navigation
Navid EMAD09b0cf2d5319
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zigindex e616826fc..6e8a4eead 100644--- a/src/browser/Frame.zig+++ b/src/browser/Frame.zig@@ -46,6 +46,7 @@ const HtmlElement = @import("webapi/element/Html.zig");const Window = @import("webapi/Window.zig");const Cookie = @import("webapi/storage/Cookie.zig");const Location = @import("webapi/Location.zig");+const History = @import("webapi/History.zig");const Document = @import("webapi/Document.zig");const ShadowRoot = @import("webapi/ShadowRoot.zig");const Performance = @import("webapi/Performance.zig");@@ -1067,6 +1068,10 @@ fn scheduleNavigationWithArena(originator: *Frame, arena: *lp.Arena, request_url// `:target` matches off the fragment, which just changed.target.styleChanged();+ // Updating the document for the new entry fires popstate right away+ // (the entry's state is null), then hashchange in a queued task.+ // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate-fragid+ try History.firePopState(target, null);try target.queueHashChange(old_url, target.url);// don't defer this, the caller is responsible for freeing it on errordiff --git a/src/browser/tests/event/hashchange.html b/src/browser/tests/event/hashchange.htmlindex acf66ccb6..29f399ebe 100644--- a/src/browser/tests/event/hashchange.html+++ b/src/browser/tests/event/hashchange.html@@ -114,3 +114,34 @@});}</script>++<script id=popStateFiresOnFragmentNavigation type=module>+{+ // A fragment navigation fires popstate synchronously, with the new entry's+ // (null) state, before the assignment returns; hashchange follows in a+ // queued task.+ const state = await testing.async();+ const log = [];+ const onpop = (e) => log.push(['popstate', e.state, location.hash]);+ const onhash = (e) => {+ if (!e.newURL.endsWith('#popstate-check')) return;+ log.push(['hashchange']);+ state.resolve();+ };+ window.addEventListener('hashchange', onhash);++ window.addEventListener('popstate', onpop);+ location.hash = '#popstate-check';+ log.push(['after assignment']);+ window.removeEventListener('popstate', onpop);++ await state.done(() => {+ testing.expectEqual([+ ['popstate', null, '#popstate-check'],+ ['after assignment'],+ ['hashchange'],+ ], log);+ window.removeEventListener('hashchange', onhash);+ });+}+</script>diff --git a/src/browser/webapi/History.zig b/src/browser/webapi/History.zigindex 45d324fb3..32c275037 100644--- a/src/browser/webapi/History.zig+++ b/src/browser/webapi/History.zig@@ -116,15 +116,22 @@ fn goInner(delta: i32, frame: *Frame) !void {_ = try frame._session.navigation.navigateInner(entry._url, .{ .traverse = index }, frame);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();- try frame._event_manager.dispatchDirect(target, event, frame.window._on_popstate, .{ .context = "Pop State" });- }+ try firePopState(frame, entry._state.value);// hashchange is queued by navigateInner.}}+// Fires popstate at the frame's window, synchronously: a same-document+// traversal or a fragment navigation updating the document for its new+// history entry. `state` is the entry's serialized state.+pub fn firePopState(frame: *Frame, state: ?[]const u8) !void {+ 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 = state }, frame)).asEvent();+ try frame._event_manager.dispatchDirect(target, event, frame.window._on_popstate, .{ .context = "Pop State" });+ }+}+pub fn back(_: *History, frame: *Frame) !void {try goInner(-1, frame);}