Skip to content

Lightpanda: the headless browser designed for AI and automation

session: call navigationFor once per function

Pierre Tachoire130f8f670bf8
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index 81b12f4cb..067862531 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -854,8 +854,9 @@ pub fn navigate(self: *Frame, request_url: [:0]const u8, opts: NavigateOpts) !vo
});
if (self.parent == null) {
- session.navigationFor(self)._current_navigation_kind = opts.kind;
- try session.navigationFor(self).commitNavigation(self);
+ const navigation = session.navigationFor(self);
+ navigation._current_navigation_kind = opts.kind;
+ try navigation.commitNavigation(self);
}
self.documentIsComplete();
diff --git a/src/browser/Session.zig b/src/browser/Session.zig
index f0b025f4a..36b28fcbd 100644
--- a/src/browser/Session.zig
+++ b/src/browser/Session.zig
@@ -1178,7 +1178,9 @@ test "Session: each page has its own session history" {
const a = (try session.createPage()).frame().?;
const b = (try session.createPage()).frame().?;
- try testing.expect(session.navigationFor(a) != session.navigationFor(b));
+ const nav_a = session.navigationFor(a);
+ const nav_b = session.navigationFor(b);
+ try testing.expect(nav_a != nav_b);
const js = @import("js/js.zig");
var ls: js.Local.Scope = undefined;
@@ -1186,8 +1188,8 @@ test "Session: each page has its own session history" {
defer ls.deinit();
_ = try ls.local.exec("history.pushState(null, '', '#one'); history.pushState(null, '', '#two')", null);
- try testing.expectEqual(3, session.navigationFor(a)._entries.items.len);
- try testing.expectEqual(1, session.navigationFor(b)._entries.items.len);
+ try testing.expectEqual(3, nav_a._entries.items.len);
+ try testing.expectEqual(1, nav_b._entries.items.len);
}
test "Session: a popup has its own session history" {
@@ -1196,7 +1198,9 @@ test "Session: a popup has its own session history" {
const frame = (try session.createPage()).frame().?;
const popup = try frame.openPopup(.{ .url = "about:blank", .name = "", .opener = frame.window });
- try testing.expect(session.navigationFor(frame) != session.navigationFor(popup));
+ const frame_nav = session.navigationFor(frame);
+ const popup_nav = session.navigationFor(popup);
+ try testing.expect(frame_nav != popup_nav);
const js = @import("js/js.zig");
var ls: js.Local.Scope = undefined;
@@ -1204,9 +1208,9 @@ test "Session: a popup has its own session history" {
defer ls.deinit();
_ = try ls.local.exec("history.pushState(null, '', '#one')", null);
- try testing.expectEqual(1, session.navigationFor(frame)._entries.items.len);
- try testing.expectEqual("about:blank", session.navigationFor(frame).getCurrentEntry()._url.?);
- try testing.expectEqual("about:blank#one", session.navigationFor(popup).getCurrentEntry()._url.?);
+ try testing.expectEqual(1, frame_nav._entries.items.len);
+ try testing.expectEqual("about:blank", frame_nav.getCurrentEntry()._url.?);
+ try testing.expectEqual("about:blank#one", popup_nav.getCurrentEntry()._url.?);
}
test "Session: console capture runs no page JS" {
diff --git a/src/browser/webapi/History.zig b/src/browser/webapi/History.zig
index bc6697b47..e012e5a43 100644
--- a/src/browser/webapi/History.zig
+++ b/src/browser/webapi/History.zig
@@ -101,19 +101,20 @@ fn replaceState(_: *History, state: js.Value, _: ?[]const u8, _url: ?[]const u8,
fn goInner(delta: i32, frame: *Frame) !void {
// 0 behaves the same as no argument, both reloading the frame.
- const current = frame._session.navigationFor(frame)._index;
+ const navigation = frame._session.navigationFor(frame);
+ const current = navigation._index;
const index_s: i64 = @intCast(@as(i64, @intCast(current)) + @as(i64, @intCast(delta)));
- if (index_s < 0 or index_s > frame._session.navigationFor(frame)._entries.items.len - 1) {
+ if (index_s < 0 or index_s > navigation._entries.items.len - 1) {
return;
}
const index = @as(usize, @intCast(index_s));
- const entry = frame._session.navigationFor(frame)._entries.items[index];
+ const entry = 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.navigationFor(frame).navigateInner(entry._url, .{ .traverse = index }, frame);
+ _ = try navigation.navigateInner(entry._url, .{ .traverse = index }, frame);
if (same_document) {
try firePopState(frame, entry._state.value);