Skip to content

Lightpanda: the headless browser designed for AI and automation

cdp: emit Page.navigatedWithinDocument for fragment navigations and traversals

Navid EMAD443b10f51d70
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index e616826fc..05c2a1862 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -1064,6 +1064,12 @@ fn scheduleNavigationWithArena(originator: *Frame, arena: *lp.Arena, request_url
try session.navigation.updateEntries(target.url, opts.kind, target, true);
}
+ session.notification.dispatch(.frame_navigated_within_document, &.{
+ .url = target.url,
+ .frame_id = target._frame_id,
+ .navigation_type = .fragment,
+ });
+
// `:target` matches off the fragment, which just changed.
target.styleChanged();
diff --git a/src/browser/webapi/navigation/Navigation.zig b/src/browser/webapi/navigation/Navigation.zig
index b24a2340c..893e04930 100644
--- a/src/browser/webapi/navigation/Navigation.zig
+++ b/src/browser/webapi/navigation/Navigation.zig
@@ -476,6 +476,17 @@ pub fn navigateInner(
},
}
+ // Like Chrome, every same-document navigation other than pushState and
+ // replaceState (which History reports itself) is a fragment one,
+ // traversals included.
+ if (is_same_document and kind != .reload) {
+ frame._session.notification.dispatch(.frame_navigated_within_document, &.{
+ .url = new_url,
+ .frame_id = frame._frame_id,
+ .navigation_type = .fragment,
+ });
+ }
+
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/server/cdp/domains/page.zig b/src/server/cdp/domains/page.zig
index 755bf3b51..37753ab53 100644
--- a/src/server/cdp/domains/page.zig
+++ b/src/server/cdp/domains/page.zig
@@ -2920,3 +2920,56 @@ test "cdp.frame: history.pushState emits Page.navigatedWithinDocument" {
.url = "http://127.0.0.1:9582/next",
}, .{});
}
+
+test "cdp.frame: a fragment navigation emits Page.navigatedWithinDocument" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ var bc = try ctx.loadBrowserContext(.{ .id = "BID-9", .url = "hi.html", .target_id = "FID-000000000X".* });
+
+ const frame = bc.mainFrame() orelse unreachable;
+
+ {
+ var ls: js.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+ _ = try ls.local.exec("location.hash = 'a'", null);
+ }
+
+ // Chrome reports location.hash (and same-document anchor clicks) as a
+ // "fragment" navigation.
+ try ctx.expectSentEvent("Page.navigatedWithinDocument", .{
+ .frameId = "FID-0000000001",
+ .navigationType = "fragment",
+ .url = "http://127.0.0.1:9582/src/browser/tests/hi.html#a",
+ }, .{});
+}
+
+test "cdp.frame: a same-document history traversal emits Page.navigatedWithinDocument" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ var bc = try ctx.loadBrowserContext(.{ .id = "BID-9", .url = "hi.html", .target_id = "FID-000000000X".* });
+
+ const frame = bc.mainFrame() orelse unreachable;
+
+ {
+ var ls: js.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+ _ = try ls.local.exec("history.pushState({}, '', '/next'); history.back()", null);
+ }
+
+ try ctx.expectSentEvent("Page.navigatedWithinDocument", .{
+ .frameId = "FID-0000000001",
+ .navigationType = "historyApi",
+ .url = "http://127.0.0.1:9582/next",
+ }, .{});
+ // Going back to the pushState entry stays in the document. Chrome reports
+ // a traversal as "fragment" whatever the entry's URL.
+ try ctx.expectSentEvent("Page.navigatedWithinDocument", .{
+ .frameId = "FID-0000000001",
+ .navigationType = "fragment",
+ .url = "http://127.0.0.1:9582/src/browser/tests/hi.html",
+ }, .{});
+}