Skip to content

Lightpanda: the headless browser designed for AI and automation

frame: stop a superseded document's load events once navigation begins

Scott Taylor1a9557e41ee1
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index ebf0b9119..96504e3e2 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -988,6 +988,7 @@ fn scheduleNavigationWithArena(originator: *Frame, arena: *lp.Arena, request_url
// Navigation: kill in-flight HTTP transfers, but leave WebSockets
// alive — they're cross-document by spec.
+ target.document._load_aborted = true;
session.browser.http_client.abortRequests(&target._http_owner);
// Capture the originating frame's URL as the Referer for this
@@ -1029,7 +1030,8 @@ fn scheduleNavigationWithArena(originator: *Frame, arena: *lp.Arena, request_url
}
target._queued_navigation = qn;
- return session.scheduleNavigation(target);
+ try session.scheduleNavigation(target);
+ target.documentIsComplete();
}
// A script can have multiple competing navigation events, say it starts off
@@ -1097,14 +1099,7 @@ pub fn stopLoading(self: *Frame) void {
self.child_frames.items[i].stopLoading();
}
- if (self._queued_navigation) |qn| {
- const queued = self.page.queued_navigation;
- if (std.mem.indexOfScalar(*Frame, queued.items, self)) |idx| {
- _ = queued.swapRemove(idx);
- }
- qn.arena.release();
- self._queued_navigation = null;
- }
+ self.cancelQueuedNavigation();
const http_client = &self._session.browser.http_client;
if (http_client.findTransfer(self._req_id)) |transfer| {
@@ -1115,8 +1110,24 @@ pub fn stopLoading(self: *Frame) void {
http_client.cancelRequests(&self._http_owner);
}
+fn loadEventsAborted(self: *const Frame) bool {
+ if (self.document._load_aborted or self.js.env.terminatePending()) return true;
+ const parent = self.parent orelse return false;
+ return parent.loadEventsAborted();
+}
+
+pub fn cancelQueuedNavigation(self: *Frame) void {
+ const qn = self._queued_navigation orelse return;
+ const queued = self.page.queued_navigation;
+ if (std.mem.indexOfScalar(*Frame, queued.items, self)) |idx| {
+ _ = queued.swapRemove(idx);
+ }
+ qn.arena.release();
+ self._queued_navigation = null;
+}
+
pub fn documentIsLoaded(self: *Frame) void {
- if (self._load_state != .parsing) {
+ if (self._load_state != .parsing or self.loadEventsAborted()) {
// Ideally, documentIsLoaded would only be called once, but if a
// script is dynamically added from an async script after
// documentIsLoaded is already called, then ScriptManager will call
@@ -1134,6 +1145,7 @@ pub fn documentIsLoaded(self: *Frame) void {
fn _documentIsLoaded(self: *Frame) !void {
try self.dispatchReadyStateChange();
+ if (self.loadEventsAborted()) return;
const event = try Event.initTrusted(.wrap("DOMContentLoaded"), .{ .bubbles = true }, self.page);
try self._event_manager.dispatch(
@@ -1225,6 +1237,7 @@ pub fn documentIsComplete(self: *Frame) void {
// documentIsLoaded, if there were _only_ async scripts
if (self._load_state == .parsing) {
self.documentIsLoaded();
+ if (self._load_state == .complete) return;
}
self._load_state = .complete;
@@ -1233,7 +1246,7 @@ pub fn documentIsComplete(self: *Frame) void {
else => log.err(.frame, "document is complete", .{ .err = err, .type = self._type, .url = self.url }),
};
- if (self._maybe_meta_refresh) {
+ if (self._maybe_meta_refresh and !self.loadEventsAborted()) {
self._maybe_meta_refresh = false;
self.metaRefreshOnLoad();
}
@@ -1242,9 +1255,11 @@ pub fn documentIsComplete(self: *Frame) void {
fn _documentIsComplete(self: *Frame) !void {
self.document._ready_state = .complete;
try self.dispatchReadyStateChange();
+ if (self.loadEventsAborted()) return;
// Run element load/error events before window.load.
try self.dispatchQueuedEvents();
+ if (self.loadEventsAborted()) return;
// Dispatch window.load event.
const window_target = self.window.asEventTarget();
@@ -3790,6 +3805,120 @@ test "Page: isSameOrigin" {
try testing.expectEqual(false, frame.isSameOrigin("//origin.com/foo"));
}
+test "Frame: superseded documents omit DOMContentLoaded and load" {
+ const cases = [_]struct { trigger: []const u8, expected: []const u8 }{
+ .{ .trigger = "location.assign('/next');", .expected = "complete" },
+ .{
+ .trigger = "document.addEventListener('readystatechange', () => { if (document.readyState === 'interactive') location.assign('/next'); });",
+ .expected = "interactive|complete",
+ },
+ .{
+ .trigger = "document.addEventListener('DOMContentLoaded', () => location.assign('/next'));",
+ .expected = "interactive|dcl|complete",
+ },
+ .{
+ .trigger = "document.addEventListener('readystatechange', () => { if (document.readyState === 'complete') location.assign('/next'); });",
+ .expected = "interactive|dcl|complete",
+ },
+ .{ .trigger = "location.hash = 'section';", .expected = "interactive|dcl|complete|load" },
+ .{ .trigger = "history.replaceState({}, '', '?same-document=1');", .expected = "interactive|dcl|complete|load" },
+ };
+ for (cases) |case| {
+ const page = try testing.pageTest("hi.html", .{});
+ defer page.close();
+ const frame = page.frame().?;
+ var ls: JS.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+ try ls.local.eval(
+ \\globalThis.events = [];
+ \\document.addEventListener('readystatechange', () => events.push(document.readyState));
+ \\document.addEventListener('DOMContentLoaded', () => events.push('dcl'));
+ \\window.addEventListener('load', () => events.push('load'));
+ , null);
+ frame._load_state = .parsing;
+ frame.document._ready_state = .loading;
+ try ls.local.eval(case.trigger, null);
+ frame.documentIsComplete();
+ const events = try ls.local.exec("events.join('|')", null);
+ try testing.expectEqual(case.expected, try events.toStringSlice());
+ }
+}
+
+test "Frame: pending or discarded replacements do not resume old load events" {
+ for ([_]bool{ false, true }) |discard| {
+ const page = try testing.pageTest("hi.html", .{});
+ defer page.close();
+ const frame = page.frame().?;
+ var ls: JS.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+ try ls.local.eval(
+ \\globalThis.events = [];
+ \\document.addEventListener('readystatechange', () => events.push(document.readyState));
+ \\document.addEventListener('DOMContentLoaded', () => events.push('dcl'));
+ \\window.addEventListener('load', () => events.push('load'));
+ , null);
+ frame._load_state = .parsing;
+ frame.document._ready_state = .loading;
+ try frame._session.initiateRootNavigation(frame._frame_id, "http://127.0.0.1:9582/src/browser/tests/hi.html?replacement", .{});
+ const replacement = frame.page.replacement.?;
+ if (discard) frame._session.discardPendingPage(replacement);
+ try testing.expectEqual(null, frame._queued_navigation);
+ frame.documentIsComplete();
+ const events = try ls.local.exec("events.join('|')", null);
+ try testing.expectEqual("complete", try events.toStringSlice());
+ if (!discard) frame._session.discardPendingPage(replacement);
+ }
+}
+
+test "Frame: readystatechange during an aborted load may renavigate or throw" {
+ const page = try testing.pageTest("hi.html", .{});
+ defer page.close();
+ const frame = page.frame().?;
+ var ls: JS.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+ try ls.local.eval(
+ \\globalThis.events = [];
+ \\document.addEventListener('readystatechange', () => {
+ \\ events.push(document.readyState);
+ \\ if (document.readyState === 'complete') { location.assign('/second'); throw new Error('handler'); }
+ \\});
+ \\window.addEventListener('load', () => events.push('load'));
+ , null);
+ frame._load_state = .parsing;
+ frame.document._ready_state = .loading;
+ testing.silenceLog(&.{ .js, .event, .frame });
+ try ls.local.eval("location.assign('/first');", null);
+ try testing.expectEqual("complete", try (try ls.local.exec("events.join('|')", null)).toStringSlice());
+ try testing.expectEqual(true, std.mem.endsWith(u8, frame._queued_navigation.?.url, "/second"));
+ try testing.expectEqual(1, frame.page.queued_navigation.items.len);
+ frame.documentIsComplete();
+ try testing.expectEqual("complete", try (try ls.local.exec("events.join('|')", null)).toStringSlice());
+}
+
+test "Frame: document.open cancels the queued navigation without reviving load" {
+ const page = try testing.pageTest("hi.html", .{});
+ defer page.close();
+ const frame = page.frame().?;
+ var ls: JS.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+ try ls.local.eval(
+ \\globalThis.events = [];
+ \\window.addEventListener('load', () => events.push('load'));
+ \\location.assign('/next');
+ \\document.open();
+ \\document.write('<title>Rewritten</title>');
+ \\document.close();
+ , null);
+ try testing.expectEqual(null, frame._queued_navigation);
+ try testing.expectEqual(0, frame.page.queued_navigation.items.len);
+ try testing.expectEqual("Rewritten", try (try ls.local.exec("document.title", null)).toStringSlice());
+ try testing.expectEqual("", try (try ls.local.exec("events.join('|')", null)).toStringSlice());
+}
+
test "Frame: static immediate meta refresh navigates" {
const page = try testing.pageTest("fixtures/meta_refresh.html", .{});
defer page.close();
diff --git a/src/browser/Session.zig b/src/browser/Session.zig
index 039c983a4..6108dc18e 100644
--- a/src/browser/Session.zig
+++ b/src/browser/Session.zig
@@ -880,6 +880,9 @@ pub fn initiateRootNavigation(self: *Session, frame_id: u32, url: [:0]const u8,
const page = try self.allocatePage(frame_id);
errdefer self.queuePageDestruction(page);
+ // The old document's load stays aborted even if the pending request fails.
+ live.frame.document._load_aborted = true;
+
// Reuses `live`'s frame_id: the replacement IS the same browsing context.
// `replaces` keeps `live` addressable until commit; the `replacement`
// back-pointer is its inverse
@@ -904,6 +907,7 @@ pub fn initiateRootNavigation(self: *Session, frame_id: u32, url: [:0]const u8,
log.err(.browser, "pending navigation start", .{ .err = err, .url = url });
return err;
};
+ live.frame.documentIsComplete();
}
// Promote a pending replacement Page to be the live Page.
diff --git a/src/browser/webapi/Document.zig b/src/browser/webapi/Document.zig
index c9ba54688..2927295ed 100644
--- a/src/browser/webapi/Document.zig
+++ b/src/browser/webapi/Document.zig
@@ -65,6 +65,7 @@ _content_type: ?[]const u8 = null,
// createDocument) are UTF-8 regardless of the frame's encoding
_charset: ?[]const u8 = null,
_ready_state: ReadyState = .loading,
+_load_aborted: bool = false,
_current_script: ?*Element.Html.Script = null,
_elements_by_id: std.StringHashMapUnmanaged(*Element) = .empty,
// Track IDs that were removed from the map - they might have duplicates in the tree
@@ -1148,6 +1149,9 @@ pub fn open(self: *Document, call_frame: *Frame) !*Document {
self._style_sheets = null;
self._implementation = null;
self._ready_state = .loading;
+ // open() cancels an ongoing navigation; the aborted document's load is
+ // gone for good, as in Chrome.
+ frame.cancelQueuedNavigation();
self._script_created_parser = Parser.Streaming.init(frame.arena, doc_node, frame, .{ .allow_declarative_shadow = true });
try self._script_created_parser.?.start();