Skip to content

Lightpanda: the headless browser designed for AI and automation

document: keep an aborted parser from restarting after navigation

Scott Taylor39dde6db7c5a
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index 96504e3e2..a0172772b 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -988,7 +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;
+ target.abortDocumentLoad();
session.browser.http_client.abortRequests(&target._http_owner);
// Capture the originating frame's URL as the Referer for this
@@ -1110,6 +1110,20 @@ pub fn stopLoading(self: *Frame) void {
http_client.cancelRequests(&self._http_owner);
}
+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;
+ }
+}
+
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;
@@ -3919,6 +3933,47 @@ test "Frame: document.open cancels the queued navigation without reviving load"
try testing.expectEqual("", try (try ls.local.exec("events.join('|')", null)).toStringSlice());
}
+test "Frame: document.open after inline navigation does not restart the parser" {
+ const page = try testing.pageTest("fixtures/navigation_open.html", .{});
+ defer page.close();
+
+ try testing.expect(std.mem.endsWith(u8, page.frame().?.url, "/hi.html"));
+}
+
+test "Frame: document.open can cancel navigation once parsing has finished" {
+ inline for (.{ "?interactive", "?dcl" }) |query| {
+ const page = try testing.pageTest("fixtures/navigation_open.html" ++ query, .{});
+ defer page.close();
+ const frame = page.frame().?;
+
+ try testing.expect(std.mem.endsWith(u8, frame.url, "/navigation_open.html" ++ query));
+ try testing.expectEqual(false, frame.document._active_parser_aborted);
+ try testing.expectEqual(null, frame._queued_navigation);
+ try testing.expectEqual("Rewritten", (try frame.getTitle()).?);
+ }
+}
+
+test "Frame: cancelling navigation does not revive an aborted parser" {
+ const page = try testing.pageTest("fixtures/navigation_open.html?cancel", .{});
+ defer page.close();
+ const frame = page.frame().?;
+
+ try testing.expect(std.mem.endsWith(u8, frame.url, "/navigation_open.html?cancel"));
+ var ls: JS.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+ // The original parse has unwound, but the active-parser-was-aborted flag
+ // must still prevent open/write/writeln from replacing the document.
+ try ls.local.eval(
+ \\document.open();
+ \\document.write('<title>Later write</title>');
+ \\document.writeln('<title>Later writeln</title>');
+ \\document.close();
+ , null);
+ try testing.expectEqual("Original", try (try ls.local.exec("document.title", null)).toStringSlice());
+ try testing.expectEqual(null, frame.document._script_created_parser);
+}
+
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 6108dc18e..b1f35a95a 100644
--- a/src/browser/Session.zig
+++ b/src/browser/Session.zig
@@ -881,7 +881,7 @@ pub fn initiateRootNavigation(self: *Session, frame_id: u32, url: [:0]const u8,
errdefer self.queuePageDestruction(page);
// The old document's load stays aborted even if the pending request fails.
- live.frame.document._load_aborted = true;
+ live.frame.abortDocumentLoad();
// Reuses `live`'s frame_id: the replacement IS the same browsing context.
// `replaces` keeps `live` addressable until commit; the `replacement`
diff --git a/src/browser/tests/fixtures/navigation_open.html b/src/browser/tests/fixtures/navigation_open.html
new file mode 100644
index 000000000..f915c3a0d
--- /dev/null
+++ b/src/browser/tests/fixtures/navigation_open.html
@@ -0,0 +1,21 @@
+<!doctype html>
+<title>Original</title>
+<script>
+function navigateAndOpen() {
+ location.assign('../hi.html');
+ if (location.search === '?cancel') window.stop();
+ document.open();
+ document.write('<title>Rewritten</title>');
+ document.close();
+}
+
+if (location.search === '?interactive') {
+ document.addEventListener('readystatechange', () => {
+ if (document.readyState === 'interactive') navigateAndOpen();
+ });
+} else if (location.search === '?dcl') {
+ document.addEventListener('DOMContentLoaded', navigateAndOpen);
+} else {
+ navigateAndOpen();
+}
+</script>
diff --git a/src/browser/webapi/Document.zig b/src/browser/webapi/Document.zig
index 2927295ed..0a283a8e2 100644
--- a/src/browser/webapi/Document.zig
+++ b/src/browser/webapi/Document.zig
@@ -66,6 +66,8 @@ _content_type: ?[]const u8 = null,
_charset: ?[]const u8 = null,
_ready_state: ReadyState = .loading,
_load_aborted: bool = false,
+// HTML's "active parser was aborted" flag also makes open/write no-ops.
+_active_parser_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
@@ -997,6 +999,8 @@ fn writeInternal(self: *Document, text: []const []const u8, append_newline: bool
return error.InvalidStateError;
}
+ if (self._active_parser_aborted) return;
+
const html = blk: {
var joined: std.ArrayList(u8) = .empty;
for (text) |str| {
@@ -1123,7 +1127,7 @@ pub fn open(self: *Document, call_frame: *Frame) !*Document {
return error.InvalidStateError;
}
- if (frame._load_state == .parsing) {
+ if (self._active_parser_aborted or frame._load_state == .parsing) {
return self;
}