Skip to content

Commit b3e2c1b

Browse files
committed
add NavigationKind to navigate
1 parent 3658a3d commit b3e2c1b

File tree

9 files changed

+34
-20
lines changed

9 files changed

+34
-20
lines changed

src/browser/Page.zig

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const IntersectionObserver = @import("webapi/IntersectionObserver.zig");
5858
const CustomElementDefinition = @import("webapi/CustomElementDefinition.zig");
5959
const storage = @import("webapi/storage/storage.zig");
6060
const PageTransitionEvent = @import("webapi/event/PageTransitionEvent.zig");
61+
const NavigationKind = @import("webapi/navigation/root.zig").NavigationKind;
6162

6263
const timestamp = @import("../datetime.zig").timestamp;
6364
const milliTimestamp = @import("../datetime.zig").milliTimestamp;
@@ -262,7 +263,7 @@ fn registerBackgroundTasks(self: *Page) !void {
262263
}.runMessageLoop, 250, .{ .name = "page.messageLoop" });
263264
}
264265

265-
pub fn navigate(self: *Page, request_url: [:0]const u8, opts: NavigateOpts) !void {
266+
pub fn navigate(self: *Page, request_url: [:0]const u8, opts: NavigateOpts, kind: NavigationKind) !void {
266267
const session = self._session;
267268

268269
const resolved_url = try URL.resolve(
@@ -284,7 +285,7 @@ pub fn navigate(self: *Page, request_url: [:0]const u8, opts: NavigateOpts) !voi
284285
self.window._location = try Location.init(self.url, self);
285286
self.document._location = self.window._location;
286287

287-
try session.navigation.updateEntries("", .{ .push = null }, self, true);
288+
try session.navigation.updateEntries(resolved_url, kind, self, true);
288289
return;
289290
}
290291
}
@@ -345,6 +346,8 @@ pub fn navigate(self: *Page, request_url: [:0]const u8, opts: NavigateOpts) !voi
345346
.timestamp = timestamp(.monotonic),
346347
});
347348

349+
session.navigation._current_navigation_kind = kind;
350+
348351
http_client.request(.{
349352
.ctx = self,
350353
.url = self.url,

src/browser/Session.zig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,11 @@ fn processQueuedNavigation(self: *Session) !bool {
191191
return err;
192192
};
193193

194-
page.navigate(qn.url, qn.opts) catch |err| {
194+
page.navigate(
195+
qn.url,
196+
qn.opts,
197+
self.navigation._current_navigation_kind orelse .{ .push = null },
198+
) catch |err| {
195199
log.err(.browser, "queued navigation error", .{ .err = err, .url = qn.url });
196200
return err;
197201
};

src/browser/webapi/Location.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn setHash(_: *const Location, hash: []const u8, page: *Page) !void {
8181
};
8282

8383
const duped_hash = try page.arena.dupeZ(u8, normalized_hash);
84-
return page.navigate(duped_hash, .{ .reason = .script });
84+
return page.navigate(duped_hash, .{ .reason = .script }, .{ .replace = null });
8585
}
8686

8787
pub fn toString(self: *const Location, page: *const Page) ![:0]const u8 {

src/browser/webapi/navigation/Navigation.zig

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ pub fn back(self: *Navigation, page: *Page) !NavigationReturn {
9292

9393
const new_index = self._index - 1;
9494
const next_entry = self._entries.items[new_index];
95-
self._index = new_index;
9695

9796
return self.navigateInner(next_entry._url, .{ .traverse = new_index }, page);
9897
}
@@ -108,7 +107,6 @@ pub fn forward(self: *Navigation, page: *Page) !NavigationReturn {
108107

109108
const new_index = self._index + 1;
110109
const next_entry = self._entries.items[new_index];
111-
self._index = new_index;
112110

113111
return self.navigateInner(next_entry._url, .{ .traverse = new_index }, page);
114112
}
@@ -132,7 +130,10 @@ pub fn updateEntries(self: *Navigation, url: [:0]const u8, kind: NavigationKind,
132130
// This is only really safe to run in the `pageDoneCallback` where we can guarantee that the URL and NavigationKind are correct.
133131
pub fn processNavigation(self: *Navigation, page: *Page) !void {
134132
const url = page.url;
133+
135134
const kind: NavigationKind = self._current_navigation_kind orelse .{ .push = null };
135+
defer self._current_navigation_kind = null;
136+
136137
try self.updateEntries(url, kind, page, false);
137138
}
138139

@@ -247,9 +248,11 @@ pub fn navigateInner(
247248
const committed = try page.js.createPromiseResolver(.page);
248249
const finished = try page.js.createPromiseResolver(.page);
249250

250-
const new_url = try URL.resolve(arena, url, page.url, .{});
251+
const new_url = try URL.resolve(arena, page.url, url, .{});
251252
const is_same_document = URL.eqlDocument(new_url, page.url);
252253

254+
const previous = self.getCurrentEntry();
255+
253256
switch (kind) {
254257
.push => |state| {
255258
if (is_same_document) {
@@ -261,8 +264,7 @@ pub fn navigateInner(
261264

262265
_ = try self.pushEntry(url, .{ .source = .navigation, .value = state }, page, true);
263266
} else {
264-
// try page.navigate(url, .{ .reason = .navigation }, kind);
265-
try page.navigate(url, .{ .reason = .navigation });
267+
try page.navigate(url, .{ .reason = .navigation }, kind);
266268
}
267269
},
268270
.replace => |state| {
@@ -275,8 +277,7 @@ pub fn navigateInner(
275277

276278
_ = try self.replaceEntry(url, .{ .source = .navigation, .value = state }, page, true);
277279
} else {
278-
// try page.navigate(url, .{ .reason = .navigation }, kind);
279-
try page.navigate(url, .{ .reason = .navigation });
280+
try page.navigate(url, .{ .reason = .navigation }, kind);
280281
}
281282
},
282283
.traverse => |index| {
@@ -289,16 +290,22 @@ pub fn navigateInner(
289290
// todo: Fire navigate event
290291
finished.resolve("navigation traverse", {});
291292
} else {
292-
// try page.navigate(url, .{ .reason = .navigation }, kind);
293-
try page.navigate(url, .{ .reason = .navigation });
293+
try page.navigate(url, .{ .reason = .navigation }, kind);
294294
}
295295
},
296296
.reload => {
297-
// try page.navigate(url, .{ .reason = .navigation }, kind);
298-
try page.navigate(url, .{ .reason = .navigation });
297+
try page.navigate(url, .{ .reason = .navigation }, kind);
299298
},
300299
}
301300

301+
// If we haven't navigated off, let us fire off an a currententrychange.
302+
const event = try NavigationCurrentEntryChangeEvent.init(
303+
"currententrychange",
304+
.{ .from = previous, .navigationType = @tagName(kind) },
305+
page,
306+
);
307+
try self._proto.dispatch(.{ .currententrychange = event }, page);
308+
302309
return .{
303310
.committed = committed.promise(),
304311
.finished = finished.promise(),

src/cdp/domains/page.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ fn navigate(cmd: anytype) !void {
157157
try page.navigate(params.url, .{
158158
.reason = .address_bar,
159159
.cdp_id = cmd.input.id,
160-
});
160+
}, .{ .push = null });
161161
}
162162

163163
pub fn pageNavigate(arena: Allocator, bc: anytype, event: *const Notification.PageNavigate) !void {

src/lightpanda.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub fn fetch(app: *App, url: [:0]const u8, opts: FetchOpts) !void {
6060
// }
6161
// }
6262

63-
_ = try page.navigate(url, .{});
63+
_ = try page.navigate(url, .{}, .{ .push = null });
6464
_ = session.fetchWait(opts.wait_ms);
6565

6666
const writer = opts.writer orelse return;

src/main_legacy_test.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub fn run(allocator: Allocator, file: []const u8, session: *lp.Session) !void {
8585
try_catch.init(js_context);
8686
defer try_catch.deinit();
8787

88-
try page.navigate(url, .{});
88+
try page.navigate(url, .{}, .{ .push = null });
8989
session.fetchWait(2000);
9090

9191
page._session.browser.runMicrotasks();

src/main_wpt.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn run(
114114
defer session.removePage();
115115

116116
const url = try std.fmt.allocPrintSentinel(arena, "http://localhost:9582/{s}", .{test_file}, 0);
117-
try page.navigate(url, .{});
117+
try page.navigate(url, .{}, .{ .push = null });
118118

119119
_ = page.wait(2000);
120120

src/testing.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ fn runWebApiTest(test_file: [:0]const u8) !void {
402402
try_catch.init(js_context);
403403
defer try_catch.deinit();
404404

405-
try page.navigate(url, .{});
405+
try page.navigate(url, .{}, .{ .push = null });
406406
test_session.fetchWait(2000);
407407

408408
page._session.browser.runMicrotasks();

0 commit comments

Comments
 (0)