Skip to content

Lightpanda: the headless browser designed for AI and automation

Narrow frame suspension

Karl Seguin21a210fe1631
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index a0172772b..9282ca0c4 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -1031,7 +1031,7 @@ fn scheduleNavigationWithArena(originator: *Frame, arena: *lp.Arena, request_url
target._queued_navigation = qn;
try session.scheduleNavigation(target);
- target.documentIsComplete();
+ target.abortedDocumentIsComplete();
}
// A script can have multiple competing navigation events, say it starts off
@@ -1101,6 +1101,16 @@ pub fn stopLoading(self: *Frame) void {
self.cancelQueuedNavigation();
+ // HTML's "active parser was aborted" flag. Stopping is the only thing that
+ // actually kills the parser: a merely *scheduled* navigation leaves it
+ // running until the replacement commits, and Chrome keeps honouring
+ // document.write until then.
+ if (self.parserIsRunning()) {
+ self.document._active_parser_aborted = true;
+ } else if (self.document._script_created_parser) |parser| {
+ if (parser.handle != null) self.document._active_parser_aborted = true;
+ }
+
const http_client = &self._session.browser.http_client;
if (http_client.findTransfer(self._req_id)) |transfer| {
// the main navigation is still transfering, force it to finish now,
@@ -1110,18 +1120,38 @@ pub fn stopLoading(self: *Frame) void {
http_client.cancelRequests(&self._http_owner);
}
+// A cross-document navigation has been scheduled (or started) for this frame:
+// its current document is superseded and must never fire DOMContentLoaded or
+// load, even if the replacement is discarded or fails. Deliberately does NOT
+// touch _load_state — the parser can still be on the stack, and open/write/
+// maybeCheckpoint key off it.
pub fn abortDocumentLoad(self: *Frame) void {
self.document._load_aborted = true;
+}
- // readyState will become complete even if the parser is still on the
- // stack. Remember its aborted state separately so document.open/write cannot
- // start another parser, even after the original parse has unwound.
- if (self._load_state == .parsing and !self._script_manager.base.static_scripts_done) {
- self.document._active_parser_aborted = true;
- }
- if (self.document._script_created_parser) |parser| {
- if (parser.handle != null) self.document._active_parser_aborted = true;
+// The navigation parser is on the stack, i.e. an inline script is running from
+// inside parser.parse(). Narrower than `_load_state == .parsing`, which stays
+// true through deferred and async scripts.
+fn parserIsRunning(self: *const Frame) bool {
+ return switch (self._parse_state) {
+ .html => true,
+ else => false,
+ };
+}
+
+// Chrome moves a superseded document's readyState to "complete" but never
+// fires DOMContentLoaded or load. `_load_aborted` suppresses the events; this
+// is the readyState half, run as soon as the navigation is scheduled. The
+// guard makes it idempotent: a handler that renavigates lands here again.
+pub fn abortedDocumentIsComplete(self: *Frame) void {
+ if (self.document._ready_state == .complete) {
+ return;
}
+ self.document._ready_state = .complete;
+ self.dispatchReadyStateChange() catch |err| switch (err) {
+ error.JsException => {}, // already logged
+ else => log.err(.frame, "aborted document is complete", .{ .err = err, .type = self._type, .url = self.url }),
+ };
}
fn loadEventsAborted(self: *const Frame) bool {
@@ -1138,6 +1168,27 @@ pub fn cancelQueuedNavigation(self: *Frame) void {
}
qn.arena.release();
self._queued_navigation = null;
+
+ // Our own load is aborted and the replacement that would have completed it
+ // is now gone, so _documentIsComplete will never reach the parent. Release
+ // the parent's load delay here or it waits forever.
+ if (self.document._load_aborted) {
+ self.releaseParentLoadDelay();
+ }
+}
+
+// Stop delaying the parent's load event without dispatching the iframe
+// element's load event: that event belongs to a document that actually
+// finished loading, and this one never will.
+fn releaseParentLoadDelay(self: *Frame) void {
+ const parent = self.parent orelse return;
+ if (self._parent_notified) {
+ return;
+ }
+ self._parent_notified = true;
+ if (self._delays_parent_load) {
+ parent.pendingLoadCompleted();
+ }
}
pub fn documentIsLoaded(self: *Frame) void {
@@ -1267,8 +1318,11 @@ pub fn documentIsComplete(self: *Frame) void {
}
fn _documentIsComplete(self: *Frame) !void {
- self.document._ready_state = .complete;
- try self.dispatchReadyStateChange();
+ // abortedDocumentIsComplete may already have done this half.
+ if (self.document._ready_state != .complete) {
+ self.document._ready_state = .complete;
+ try self.dispatchReadyStateChange();
+ }
if (self.loadEventsAborted()) return;
// Run element load/error events before window.load.
@@ -3953,6 +4007,22 @@ test "Frame: document.open can cancel navigation once parsing has finished" {
}
}
+test "Frame: a scheduled navigation does not abort the parser" {
+ const page = try testing.pageTest("fixtures/navigation_open.html?write", .{});
+ defer page.close();
+ const frame = page.frame().?;
+
+ try testing.expect(std.mem.endsWith(u8, frame.url, "/navigation_open.html?write"));
+ var ls: JS.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+ // The write landed: only window.stop() sets the active-parser-was-aborted
+ // flag, scheduling the navigation doesn't.
+ const late = try ls.local.exec("document.getElementById('late').textContent", null);
+ try testing.expectEqual("late", try late.toStringSlice());
+ try testing.expectEqual(true, frame.document._active_parser_aborted);
+}
+
test "Frame: cancelling navigation does not revive an aborted parser" {
const page = try testing.pageTest("fixtures/navigation_open.html?cancel", .{});
defer page.close();
diff --git a/src/browser/Runner.zig b/src/browser/Runner.zig
index 2f0820fd0..e85dc09b4 100644
--- a/src/browser/Runner.zig
+++ b/src/browser/Runner.zig
@@ -559,6 +559,25 @@ test "Runner: lazy iframe does not delay the load event" {
try testing.expectEqual(true, lazy_child._parent_notified);
}
+test "Runner: iframe that cancels its own navigation stops delaying the parent" {
+ const page = try testing.pageTest("runner/iframe_nav_cancel.html", .{ .wait_until_done = false });
+ defer page.close();
+
+ var runner = page.session.runner(.{});
+ try runner.waitForFrame(page.frame_id, 2000, .{ .until = .load });
+
+ const frame = page.frame().?;
+ try testing.expectEqual(true, frame._load_state == .complete);
+ try testing.expectEqual(0, frame._pending_loads);
+
+ // The child aborted its load for a navigation it then cancelled, so no
+ // replacement frame will ever notify the parent on its behalf.
+ const child = frame.child_frames.items[0];
+ try testing.expectEqual(true, child.document._load_aborted);
+ try testing.expectEqual(null, child._queued_navigation);
+ try testing.expectEqual(true, child._parent_notified);
+}
+
test "Runner: idle notifications advance past a resolved condition" {
const page = try testing.pageTest("runner/runner1.html", .{});
defer page.close();
diff --git a/src/browser/Session.zig b/src/browser/Session.zig
index b1f35a95a..402b4892d 100644
--- a/src/browser/Session.zig
+++ b/src/browser/Session.zig
@@ -880,9 +880,6 @@ 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.abortDocumentLoad();
-
// 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
@@ -907,7 +904,9 @@ 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();
+
+ live.frame.abortDocumentLoad();
+ live.frame.abortedDocumentIsComplete();
}
// Promote a pending replacement Page to be the live Page.
diff --git a/src/browser/tests/fixtures/navigation_open.html b/src/browser/tests/fixtures/navigation_open.html
index f915c3a0d..0ad8d2750 100644
--- a/src/browser/tests/fixtures/navigation_open.html
+++ b/src/browser/tests/fixtures/navigation_open.html
@@ -15,6 +15,13 @@ if (location.search === '?interactive') {
});
} else if (location.search === '?dcl') {
document.addEventListener('DOMContentLoaded', navigateAndOpen);
+} else if (location.search === '?write') {
+ // Scheduling a navigation does not abort the parser: Chrome keeps honouring
+ // document.write until the replacement commits. window.stop() cancels the
+ // navigation so the test can see what landed.
+ location.assign('../hi.html');
+ document.write('<p id="late">late</p>');
+ window.stop();
} else {
navigateAndOpen();
}
diff --git a/src/browser/tests/runner/iframe_nav_cancel.html b/src/browser/tests/runner/iframe_nav_cancel.html
new file mode 100644
index 000000000..1b3a6e50f
--- /dev/null
+++ b/src/browser/tests/runner/iframe_nav_cancel.html
@@ -0,0 +1,3 @@
+<!DOCTYPE html>
+<meta charset="UTF-8">
+<iframe src="iframe_nav_cancel_child.html"></iframe>
diff --git a/src/browser/tests/runner/iframe_nav_cancel_child.html b/src/browser/tests/runner/iframe_nav_cancel_child.html
new file mode 100644
index 000000000..2645550af
--- /dev/null
+++ b/src/browser/tests/runner/iframe_nav_cancel_child.html
@@ -0,0 +1,7 @@
+<!doctype html>
+<script>
+// Abort our own load for a navigation, then cancel the navigation. Nothing
+// will ever complete this document, so it must stop delaying the parent.
+location.assign('runner1.html');
+window.stop();
+</script>