add NavigationKind to navigate

This commit is contained in:
Muki Kiboigo
2025-12-08 05:16:48 -08:00
parent 01d71323fc
commit ac85341cab
11 changed files with 42 additions and 26 deletions

View File

@@ -58,6 +58,7 @@ const IntersectionObserver = @import("webapi/IntersectionObserver.zig");
const CustomElementDefinition = @import("webapi/CustomElementDefinition.zig");
const storage = @import("webapi/storage/storage.zig");
const PageTransitionEvent = @import("webapi/event/PageTransitionEvent.zig");
const NavigationKind = @import("webapi/navigation/root.zig").NavigationKind;
const timestamp = @import("../datetime.zig").timestamp;
const milliTimestamp = @import("../datetime.zig").milliTimestamp;
@@ -270,7 +271,7 @@ fn registerBackgroundTasks(self: *Page) !void {
}.runMessageLoop, 250, .{ .name = "page.messageLoop" });
}
pub fn navigate(self: *Page, request_url: [:0]const u8, opts: NavigateOpts) !void {
pub fn navigate(self: *Page, request_url: [:0]const u8, opts: NavigateOpts, kind: NavigationKind) !void {
const session = self._session;
const resolved_url = try URL.resolve(
@@ -292,7 +293,7 @@ pub fn navigate(self: *Page, request_url: [:0]const u8, opts: NavigateOpts) !voi
self.window._location = try Location.init(self.url, self);
self.document._location = self.window._location;
try session.navigation.updateEntries("", .{ .push = null }, self, true);
try session.navigation.updateEntries(resolved_url, kind, self, true);
return;
}
}
@@ -353,6 +354,8 @@ pub fn navigate(self: *Page, request_url: [:0]const u8, opts: NavigateOpts) !voi
.timestamp = timestamp(.monotonic),
});
session.navigation._current_navigation_kind = kind;
http_client.request(.{
.ctx = self,
.url = self.url,

View File

@@ -191,7 +191,11 @@ fn processQueuedNavigation(self: *Session) !bool {
return err;
};
page.navigate(qn.url, qn.opts) catch |err| {
page.navigate(
qn.url,
qn.opts,
self.navigation._current_navigation_kind orelse .{ .push = null },
) catch |err| {
log.err(.browser, "queued navigation error", .{ .err = err, .url = qn.url });
return err;
};

View File

@@ -81,7 +81,7 @@ pub fn setHash(_: *const Location, hash: []const u8, page: *Page) !void {
};
const duped_hash = try page.arena.dupeZ(u8, normalized_hash);
return page.navigate(duped_hash, .{ .reason = .script });
return page.navigate(duped_hash, .{ .reason = .script }, .{ .replace = null });
}
pub fn toString(self: *const Location, page: *const Page) ![:0]const u8 {

View File

@@ -92,7 +92,6 @@ pub fn back(self: *Navigation, page: *Page) !NavigationReturn {
const new_index = self._index - 1;
const next_entry = self._entries.items[new_index];
self._index = new_index;
return self.navigateInner(next_entry._url, .{ .traverse = new_index }, page);
}
@@ -108,7 +107,6 @@ pub fn forward(self: *Navigation, page: *Page) !NavigationReturn {
const new_index = self._index + 1;
const next_entry = self._entries.items[new_index];
self._index = new_index;
return self.navigateInner(next_entry._url, .{ .traverse = new_index }, page);
}
@@ -132,7 +130,10 @@ pub fn updateEntries(self: *Navigation, url: [:0]const u8, kind: NavigationKind,
// This is only really safe to run in the `pageDoneCallback` where we can guarantee that the URL and NavigationKind are correct.
pub fn processNavigation(self: *Navigation, page: *Page) !void {
const url = page.url;
const kind: NavigationKind = self._current_navigation_kind orelse .{ .push = null };
defer self._current_navigation_kind = null;
try self.updateEntries(url, kind, page, false);
}
@@ -247,9 +248,11 @@ pub fn navigateInner(
const committed = try page.js.createPromiseResolver(.page);
const finished = try page.js.createPromiseResolver(.page);
const new_url = try URL.resolve(arena, url, page.url, .{});
const new_url = try URL.resolve(arena, page.url, url, .{});
const is_same_document = URL.eqlDocument(new_url, page.url);
const previous = self.getCurrentEntry();
switch (kind) {
.push => |state| {
if (is_same_document) {
@@ -261,8 +264,7 @@ pub fn navigateInner(
_ = try self.pushEntry(url, .{ .source = .navigation, .value = state }, page, true);
} else {
// try page.navigate(url, .{ .reason = .navigation }, kind);
try page.navigate(url, .{ .reason = .navigation });
try page.navigate(url, .{ .reason = .navigation }, kind);
}
},
.replace => |state| {
@@ -275,8 +277,7 @@ pub fn navigateInner(
_ = try self.replaceEntry(url, .{ .source = .navigation, .value = state }, page, true);
} else {
// try page.navigate(url, .{ .reason = .navigation }, kind);
try page.navigate(url, .{ .reason = .navigation });
try page.navigate(url, .{ .reason = .navigation }, kind);
}
},
.traverse => |index| {
@@ -289,16 +290,22 @@ pub fn navigateInner(
// todo: Fire navigate event
finished.resolve("navigation traverse", {});
} else {
// try page.navigate(url, .{ .reason = .navigation }, kind);
try page.navigate(url, .{ .reason = .navigation });
try page.navigate(url, .{ .reason = .navigation }, kind);
}
},
.reload => {
// try page.navigate(url, .{ .reason = .navigation }, kind);
try page.navigate(url, .{ .reason = .navigation });
try page.navigate(url, .{ .reason = .navigation }, kind);
},
}
// If we haven't navigated off, let us fire off an a currententrychange.
const event = try NavigationCurrentEntryChangeEvent.init(
"currententrychange",
.{ .from = previous, .navigationType = @tagName(kind) },
page,
);
try self._proto.dispatch(.{ .currententrychange = event }, page);
return .{
.committed = committed.promise(),
.finished = finished.promise(),

View File

@@ -183,7 +183,7 @@ fn navigate(cmd: anytype) !void {
try page.navigate(params.url, .{
.reason = .address_bar,
.cdp_id = cmd.input.id,
});
}, .{ .push = null });
}
pub fn pageNavigate(arena: Allocator, bc: anytype, event: *const Notification.PageNavigate) !void {

View File

@@ -179,9 +179,11 @@ fn createTarget(cmd: anytype) !void {
try doAttachtoTarget(cmd, target_id);
}
try page.navigate(params.url, .{
.reason = .address_bar,
});
try page.navigate(
params.url,
.{ .reason = .address_bar },
.{ .push = null },
);
try cmd.sendResult(.{
.targetId = target_id,

View File

@@ -127,10 +127,10 @@ const TestContext = struct {
const full_url = try std.fmt.allocPrintSentinel(
self.arena.allocator(),
"http://127.0.0.1:9582/src/browser/tests/{s}",
.{ url },
.{url},
0,
);
try page.navigate(full_url, .{});
try page.navigate(full_url, .{}, .{ .push = null });
bc.session.fetchWait(2000);
}
return bc;

View File

@@ -60,7 +60,7 @@ pub fn fetch(app: *App, url: [:0]const u8, opts: FetchOpts) !void {
// }
// }
_ = try page.navigate(url, .{});
_ = try page.navigate(url, .{}, .{ .push = null });
_ = session.fetchWait(opts.wait_ms);
const writer = opts.writer orelse return;

View File

@@ -85,7 +85,7 @@ pub fn run(allocator: Allocator, file: []const u8, session: *lp.Session) !void {
try_catch.init(js_context);
defer try_catch.deinit();
try page.navigate(url, .{});
try page.navigate(url, .{}, .{ .push = null });
session.fetchWait(2000);
page._session.browser.runMicrotasks();

View File

@@ -114,7 +114,7 @@ fn run(
defer session.removePage();
const url = try std.fmt.allocPrintSentinel(arena, "http://localhost:9582/{s}", .{test_file}, 0);
try page.navigate(url, .{});
try page.navigate(url, .{}, .{ .push = null });
_ = page.wait(2000);

View File

@@ -403,7 +403,7 @@ fn runWebApiTest(test_file: [:0]const u8) !void {
try_catch.init(js_context);
defer try_catch.deinit();
try page.navigate(url, .{});
try page.navigate(url, .{}, .{ .push = null });
test_session.fetchWait(2000);
page._session.browser.runMicrotasks();
@@ -428,7 +428,7 @@ pub fn pageTest(comptime test_file: []const u8) !*Page {
0,
);
try page.navigate(url, .{});
try page.navigate(url, .{}, .{ .push = null });
test_session.fetchWait(2000);
return page;
}