Lightpanda: the headless browser designed for AI and automation
webapi: guard against recursive close during load callback
Karl Seguin01f19f949c3d
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zigindex b44988899..cb003cbf2 100644--- a/src/browser/Frame.zig+++ b/src/browser/Frame.zig@@ -305,6 +305,11 @@ _parent_notified: bool = false,// load the iframe, but we won't delay the parent_delays_parent_load: bool = true,+// Set while window.load is being dispatched. A load handler that open()s,+// write()s and close()s again gets readystatechange and DOMContentLoaded, but+// not a nested load (as in Chrome).+_load_event_in_progress: bool = false,+_type: enum { root, frame }, // only used for logs right now_req_id: u32 = 0,_navigated_options: ?NavigatedOpts = null,@@ -1478,29 +1483,16 @@ pub fn pendingLoadCompleted(self: *Frame) void {// A document.open() has been closed. If it was opened after it loaded, it// will re-trigger DOMContentLoaded and load.-pub fn scriptCreatedParserDone(self: *Frame) !void {+pub fn scriptCreatedParserDone(self: *Frame) void {switch (self._load_state) {.parsing, .load => {},.waiting => self._load_state = .parsing,.complete => {- // Queued, as in Chrome: firing from within document.close() lets- // a load handler that writes + closes again recurse into itself.- return self.js.scheduler.add(self, struct {- fn run(ctx: *anyopaque) !?u32 {- const f: *Frame = @ptrCast(@alignCast(ctx));- // re-opened since: its own close() queues the events- if (f.document._script_created_parser != null) {- return null;- }- f._load_state = .parsing;- f._parent_notified = false;- // the parent was already released the first time, we don't- // want documentIsComplete to re-trigger it.- f._delays_parent_load = false;- f.documentIsComplete();- return null;- }- }.run, 0, .{ .name = "frame.scriptCreatedParserDone" });+ self._load_state = .parsing;+ self._parent_notified = false;+ // the parent was already released the first time, we don't+ // want documentIsComplete to re-trigger it.+ self._delays_parent_load = false;},}self.documentIsComplete();@@ -1550,6 +1542,10 @@ fn _documentIsComplete(self: *Frame) !void {try self.dispatchQueuedEvents();if (self.loadEventsAborted()) return;+ // A close() from within our own load handler: the outer dispatch+ // finishes the load steps.+ if (self._load_event_in_progress) return;+// Dispatch window.load event.const window_target = self.window.asEventTarget();if (self._event_manager.hasDirectListeners(window_target, "load")) {@@ -1557,6 +1553,8 @@ fn _documentIsComplete(self: *Frame) !void {// This event is weird, it's dispatched directly on the window, but// with the document as the target.event._target = self.document.asEventTarget();+ self._load_event_in_progress = true;+ defer self._load_event_in_progress = false;try self._event_manager.dispatchDirect(window_target, event, .{ .inject_target = false, .context = "page load" });}diff --git a/src/browser/tests/document/open_load_events.html b/src/browser/tests/document/open_load_events.htmlindex 45e9d8cd8..39249a4a1 100644--- a/src/browser/tests/document/open_load_events.html+++ b/src/browser/tests/document/open_load_events.html@@ -86,45 +86,76 @@}</script>+<!--+ close() on a loaded document runs readystatechange, DOMContentLoaded and load+ synchronously, as in Chrome.+-->+<script id=close_after_load_is_sync type=module>+ {+ const state = await testing.async();++ const iframe = document.createElement('iframe');+ document.body.appendChild(iframe);+ const cw = iframe.contentWindow;+ const events = [];++ setTimeout(() => {+ const doc = cw.document;+ doc.open();+ doc.addEventListener('readystatechange', () => events.push('rsc:' + doc.readyState));+ doc.addEventListener('DOMContentLoaded', () => events.push('DCL'));+ cw.addEventListener('load', () => events.push('load'));+ doc.write('<p>x</p>');+ events.push('before:' + doc.readyState);+ doc.close();+ events.push('after:' + doc.readyState);+ state.resolve();+ }, 5);++ await state.done(() => {+ testing.expectEqual('before:loading,rsc:interactive,DCL,rsc:complete,load,after:complete', events.join(','));+ });+ }+</script>+<script id=close_from_load_handler type=module>{const state = await testing.async();- // Each window load opens, writes + closes again: the next load must be- // queued, not fired from within close() (which recursed, and crashed on- // the write).+ // A load handler that opens, writes + closes again gets readystatechange+ // and DOMContentLoaded, but no nested load, now or later (as in Chrome).+ // Firing it recursed, and crashed on the write.const iframe = document.createElement('iframe');document.body.appendChild(iframe);const cw = iframe.contentWindow;- let depth = 0;- let max_depth = 0;+ const events = [];let count = 0;const rewrite = () => {const doc = cw.document;doc.open();- // open() erased the window's listeners+ // open() erased the listeners+ doc.addEventListener('readystatechange', () => events.push('rsc:' + doc.readyState));+ doc.addEventListener('DOMContentLoaded', () => events.push('DCL'));cw.addEventListener('load', onload);doc.write('<b id=n>' + count + '</b>');doc.close();};const onload = () => {- depth += 1;- max_depth = Math.max(max_depth, depth);+ events.push('load');count += 1;if (count < 3) {rewrite();- } else {- state.resolve();}- depth -= 1;};setTimeout(rewrite, 5);+ setTimeout(() => state.resolve(), 50);await state.done(() => {- testing.expectEqual(1, max_depth);- testing.expectEqual(3, count);- testing.expectEqual('2', cw.document.getElementById('n').textContent);+ testing.expectEqual(1, count);+ testing.expectEqual('rsc:interactive,DCL,rsc:complete,load,rsc:interactive,DCL,rsc:complete', events.join(','));+ testing.expectEqual('1', cw.document.getElementById('n').textContent);+ testing.expectEqual('complete', cw.document.readyState);});}</script>diff --git a/src/browser/webapi/Document.zig b/src/browser/webapi/Document.zigindex 2004f6240..82fc5611e 100644--- a/src/browser/webapi/Document.zig+++ b/src/browser/webapi/Document.zig@@ -1321,7 +1321,7 @@ fn finishScriptCreatedParser(self: *Document, frame: *Frame) !void {// iframe written into via document.write), so we can't rely on it.frame._script_manager.base.scriptCreatedParseDone();- try frame.scriptCreatedParserDone();+ frame.scriptCreatedParserDone();}fn getFirstElementChild(self: *Document) ?*Element {