Skip to content

Lightpanda: the headless browser designed for AI and automation

emit NWD before triggering JS events

Karl Seguina77a39a42ff2
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index 05c2a1862..b70704b36 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -1046,8 +1046,14 @@ fn scheduleNavigationWithArena(originator: *Frame, arena: *lp.Arena, request_url
if (is_fragment_navigation) {
// Re-navigating to the exact current URL: since the fragment didn't
- // change, no hashchange and no new history entry either.
+ // change, no hashchange and no new history entry either. It's still a
+ // fragment navigation, which Chrome reports.
if (std.mem.eql(u8, target.url, resolved_url)) {
+ session.notification.dispatch(.frame_navigated_within_document, &.{
+ .url = target.url,
+ .frame_id = target._frame_id,
+ .navigation_type = .fragment,
+ });
arena.release();
return;
}
@@ -1060,16 +1066,16 @@ fn scheduleNavigationWithArena(originator: *Frame, arena: *lp.Arena, request_url
target.window._location.releaseRef(target.page);
target.window._location = location;
- if (target.parent == null) {
- 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,
});
+ if (target.parent == null) {
+ try session.navigation.updateEntries(target.url, opts.kind, target, true);
+ }
+
// `: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 893e04930..277db7aa7 100644
--- a/src/browser/webapi/navigation/Navigation.zig
+++ b/src/browser/webapi/navigation/Navigation.zig
@@ -423,17 +423,25 @@ pub fn navigateInner(
new_url = try arena.dupeSentinel(u8, new_url, 0);
}
- // Captured before the switch overwrites frame.url in the same_document
- // branches; used to queue the hashchange once below.
+ // Captured before a same-document navigation overwrites frame.url; used
+ // to queue the hashchange once below.
const old_url = frame.url;
const previous = self.getCurrentEntry();
+ if (is_same_document and kind != .reload) {
+ frame.url = new_url;
+
+ frame._session.notification.dispatch(.frame_navigated_within_document, &.{
+ .url = new_url,
+ .frame_id = frame._frame_id,
+ .navigation_type = .fragment,
+ });
+ }
+
switch (kind) {
.push => |state| {
if (is_same_document) {
- frame.url = new_url;
-
committed.resolve("navigation push", {});
// todo: Fire navigate event
_ = try self.pushEntry(url, .{ .source = .navigation, .value = state }, frame, true);
@@ -444,8 +452,6 @@ pub fn navigateInner(
},
.replace => |state| {
if (is_same_document) {
- frame.url = new_url;
-
committed.resolve("navigation replace", {});
// todo: Fire navigate event
_ = try self.replaceEntry(url, .{ .source = .navigation, .value = state }, frame, true);
@@ -458,7 +464,6 @@ pub fn navigateInner(
self._index = index;
if (is_same_document) {
- frame.url = new_url;
try frame.window._location._url.setHref(new_url, &frame.js.execution);
// `:target` matches off the fragment, which might have just changed.
frame.styleChanged();
@@ -476,17 +481,6 @@ 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 37753ab53..ec7a62816 100644
--- a/src/server/cdp/domains/page.zig
+++ b/src/server/cdp/domains/page.zig
@@ -2973,3 +2973,74 @@ test "cdp.frame: a same-document history traversal emits Page.navigatedWithinDoc
.url = "http://127.0.0.1:9582/src/browser/tests/hi.html",
}, .{});
}
+
+test "cdp.frame: a fragment navigation to the current URL 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();
+ // Setting location.hash to the current fragment doesn't navigate, but
+ // navigating to the current URL does.
+ _ = try ls.local.exec("location.hash = 'a'; location.hash = 'a'; location.href = '#a'", null);
+ }
+
+ try expectNavigatedWithinDocument(&ctx, &.{
+ "http://127.0.0.1:9582/src/browser/tests/hi.html#a",
+ "http://127.0.0.1:9582/src/browser/tests/hi.html#a",
+ });
+}
+
+test "cdp.frame: Page.navigatedWithinDocument precedes navigations from listeners" {
+ 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(
+ \\navigation.addEventListener('currententrychange', () => {
+ \\ if (location.hash === '#a') location.hash = 'b';
+ \\});
+ \\navigation.addEventListener('navigatesuccess', () => {
+ \\ location.hash = 'd';
+ \\}, { once: true });
+ \\location.hash = 'a';
+ \\navigation.navigate('#c');
+ , null);
+ }
+
+ try expectNavigatedWithinDocument(&ctx, &.{
+ "http://127.0.0.1:9582/src/browser/tests/hi.html#a",
+ "http://127.0.0.1:9582/src/browser/tests/hi.html#b",
+ "http://127.0.0.1:9582/src/browser/tests/hi.html#c",
+ "http://127.0.0.1:9582/src/browser/tests/hi.html#d",
+ });
+}
+
+// The subset matcher can't check order or count; look at the events directly.
+fn expectNavigatedWithinDocument(ctx: *testing.TestContext, expected: []const []const u8) !void {
+ try ctx.expectSentEvent("Page.navigatedWithinDocument", .{ .url = expected[expected.len - 1] }, .{});
+
+ var i: usize = 0;
+ for (ctx.received.items) |received| {
+ const method = received.object.get("method") orelse continue;
+ if (!std.mem.eql(u8, method.string, "Page.navigatedWithinDocument")) continue;
+ try testing.expect(i < expected.len);
+ const url = received.object.get("params").?.object.get("url").?.string;
+ try testing.expectEqualSlices(u8, expected[i], url);
+ i += 1;
+ }
+ try testing.expectEqual(expected.len, i);
+}