Truly independent web browser
LibWeb: Lay out again before painting a screenshot
sideshowbarker304212972f5c
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cppindex e857f818b04..66063af6404 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp@@ -7431,6 +7431,10 @@ void LocalNavigable::render_screenshot_of_painted_subtree(NonnullRefPtr<Gfx::Bit// The compositor composes the display lists this process last published for the navigables of the subtree. A// descendant that has not painted since it changed, such as one whose document has just loaded, would be missing// from the screenshot, so paint it first as the rendering update would, after preparing the canvases.+ // NB: The wait for the remote descendants lets tasks run, and one can change the document after the layout that+ // the screenshot was asked for with, so it's laid out again before it's painted.+ if (auto document = active_document())+ document->update_layout(DOM::UpdateLayoutReason::ProcessScreenshot);page().prepare_canvas_contexts_for_compositing();auto navigables = hosted_inclusive_descendant_navigables();for (auto& navigable : navigables.in_reverse()) {diff --git a/Tests/LibWebView/CMakeLists.txt b/Tests/LibWebView/CMakeLists.txtindex 874f7952e87..97e10b90b1b 100644--- a/Tests/LibWebView/CMakeLists.txt+++ b/Tests/LibWebView/CMakeLists.txt@@ -33,6 +33,7 @@ if (BUILD_TESTING AND NOT WIN32)if (PDFJS_FOUND)ladybird_python_test(test-webdriver-pdf-viewer-ignores-the-csp-of-its-pdf.py ARGS $<TARGET_FILE:WebDriver>)endif()+ ladybird_python_test(test-webdriver-screenshot-with-cross-site-iframe.py ARGS $<TARGET_FILE:WebDriver>)ladybird_python_test(test-webdriver-search-engine-settings.py ARGS $<TARGET_FILE:WebDriver>)ladybird_python_test(test-webdriver-set-permission.py ARGS $<TARGET_FILE:WebDriver>)ladybird_python_test(test-webdriver-settings-changes.py ARGS $<TARGET_FILE:WebDriver>)diff --git a/Tests/LibWebView/test-webdriver-screenshot-with-cross-site-iframe.py b/Tests/LibWebView/test-webdriver-screenshot-with-cross-site-iframe.pynew file mode 100755index 00000000000..dcec84f9ac7--- /dev/null+++ b/Tests/LibWebView/test-webdriver-screenshot-with-cross-site-iframe.py@@ -0,0 +1,82 @@+#!/usr/bin/env python3+#+# Copyright (c) 2026-present, the Ladybird developers.+#+# SPDX-License-Identifier: BSD-2-Clause++import http.server+import runpy+import subprocess+import sys+import tempfile+import threading++from pathlib import Path++helpers = runpy.run_path(str(Path(__file__).with_name("test-webdriver-delete-session.py")))++# The page changes its DOM from a timer, so tasks that run while a screenshot waits on the frame's process leave its+# layout out of date.+PAGE = b"""<!doctype html>+<div id="text">0</div>+<iframe id="frame"></iframe>+<script>+ frame.src = location.href.replace("127.0.0.1", "localhost") + "frame";+ let count = 0;+ setInterval(() => { text.textContent = String(++count); text.style.paddingTop = (count % 7) + "px"; }, 1);+</script>+"""++FRAME = b"<!doctype html><p>frame</p>"+++class Handler(http.server.BaseHTTPRequestHandler):+ def do_GET(self):+ if self.path == "/favicon.ico":+ self.send_error(404)+ return+ self.send_response(200)+ self.send_header("Content-Type", "text/html")+ self.send_header("Cache-Control", "no-store")+ self.end_headers()+ self.wfile.write(FRAME if self.path.endswith("frame") else PAGE)++ def log_message(self, format, *args):+ pass+++def main():+ server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Handler)+ threading.Thread(target=server.serve_forever, daemon=True).start()+ with tempfile.TemporaryDirectory() as directory:+ process, port = helpers["launch_webdriver"](sys.argv[1], "--headless", "--profile-path", directory)+ session = None+ try:+ session = helpers["create_session"](port)++ def command(method, path, body=None):+ status, payload, raw = helpers["request"](port, method, f"/session/{session}/{path}", body)+ assert status == 200, raw+ return payload["value"]++ # localhost and 127.0.0.1 are different sites, so the frame's document is in a process of its own.+ command("POST", "url", {"url": f"http://127.0.0.1:{server.server_port}/"})+ for _ in range(10):+ assert command("GET", "screenshot"), "The screenshot is empty"+ print("PASS: screenshots of a page with a cross-site frame and a changing document")+ finally:+ try:+ if session is not None:+ helpers["request"](port, "DELETE", f"/session/{session}")+ finally:+ process.terminate()+ try:+ process.wait(timeout=5)+ except subprocess.TimeoutExpired:+ process.kill()+ process.wait()+ server.shutdown()+ server.server_close()+++main()