Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(page-visibility): keep synthetic iframe documents hidden
ldm08fb77fbe9768
diff --git a/moli-renderer-v8/src/native_bridge/document.rs b/moli-renderer-v8/src/native_bridge/document.rsindex e819bbb62..42dd23d95 100644--- a/moli-renderer-v8/src/native_bridge/document.rs+++ b/moli-renderer-v8/src/native_bridge/document.rs@@ -1113,6 +1113,18 @@ fn current_script_belongs_to_document(.is_some_and(|owner_document| owner_document == document_handle)}+fn document_has_browsing_context(runtime: &JsContextHost, handle: DomHandle) -> bool {+ // Detached iframe compatibility windows can become a document's defaultView+ // without registering a browsing context. They must not make it visible.+ runtime.dom_host().document_handle() == handle+ || runtime+ .child_browsing_context_host_for_document_handle(handle)+ .is_some()+ || runtime+ .lightweight_popup_id_for_document_handle(handle)+ .is_some()+}+fn document_hidden_getter_function<'s>(scope: &mut v8::PinScope<'s, '_>,args: v8::FunctionCallbackArguments<'s>,@@ -1123,8 +1135,8 @@ fn document_hidden_getter_function<'s>(rv.set_undefined();return;};- let visible = document_associated_window_for_object(scope, runtime_ptr, handle, args.this())- .is_some_and(|_| unsafe { &*runtime_ptr }.document_activity().visible);+ let visible = unsafe { &*runtime_ptr }.document_activity().visible+ && document_has_browsing_context(unsafe { &*runtime_ptr }, handle);rv.set_bool(!visible);}@@ -1138,8 +1150,8 @@ fn document_visibility_state_getter_function<'s>(rv.set_undefined();return;};- let state = if document_associated_window_for_object(scope, runtime_ptr, handle, args.this())- .is_some_and(|_| unsafe { &*runtime_ptr }.document_activity().visible)+ let state = if unsafe { &*runtime_ptr }.document_activity().visible+ && document_has_browsing_context(unsafe { &*runtime_ptr }, handle){"visible"} else {diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/detached.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/detached.rsindex cf32cd18b..526519ba9 100644--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/detached.rs+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/detached.rs@@ -8776,3 +8776,55 @@ fn domparser_xml_preserves_requested_content_type_for_success_and_error_documentr#"[["text/xml","text/xml","html"],["application/xml","application/xml","html"],["application/xhtml+xml","application/xhtml+xml","html"],["image/svg+xml","image/svg+xml","html"]]"#);}++#[test]+fn detached_iframe_windows_do_not_change_document_visibility() {+ let mut vm = new_storage_test_vm("https://detached-iframe-visibility.test/");++ let result = vm+ .eval(+ r#"+(() => {+ const errors = [];+ const check = (condition, message) => { if (!condition) errors.push(message); };+ const factories = [+ ["createHTMLDocument", () => document.implementation.createHTMLDocument("")],+ ["DOMParser", () => new DOMParser().parseFromString("<body></body>", "text/html")],+ ["createDocument", () => document.implementation.createDocument("urn:test", "root", null)]+ ];+ for (const [name, create] of factories) {+ for (const first of ["document", "window"]) {+ const outer = create();+ const frame = outer.createElementNS("http://www.w3.org/1999/xhtml", "iframe");+ frame.srcdoc = "<p>hello</p>";+ outer.documentElement.appendChild(frame);+ const label = `${name}/${first}`;+ check(outer.defaultView === null, `${label}: windowless owner`);+ let child;+ if (first === "document") {+ child = frame.contentDocument;+ check(child.hidden && child.visibilityState === "hidden", `${label}: before contentWindow`);+ }+ const view = frame.contentWindow;+ child ||= frame.contentDocument;+ check(view !== null && view.document === child, `${label}: synthetic window`);+ check(child.defaultView === view, `${label}: associated window retained`);+ check(child.hidden && child.visibilityState === "hidden", `${label}: after contentWindow`);+ check(outer.hidden && outer.visibilityState === "hidden", `${label}: owner stays hidden`);+ const nested = child.createElement("iframe");+ nested.srcdoc = "<p>nested</p>";+ child.body.appendChild(nested);+ const nestedChild = nested.contentDocument;+ check(nestedChild.hidden && nestedChild.visibilityState === "hidden", `${label}: nested before contentWindow`);+ check(nested.contentWindow.document === nestedChild, `${label}: nested window`);+ check(nestedChild.hidden && nestedChild.visibilityState === "hidden", `${label}: nested after contentWindow`);+ }+ }+ return JSON.stringify(errors);+})()+"#,+ )+ .expect("detached iframe visibility probe should evaluate");++ assert_eq!(result, "[]");+}diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document.rsindex c268cd70f..bf3a70d53 100644--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document.rs+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document.rs@@ -6539,3 +6539,62 @@ fn inline_style_has_not_any_link_invalidates_on_plain_child_insertion() {assert_eq!(result, "rgb(0, 0, 255)|rgb(128, 128, 128)|rgb(0, 0, 255)");}++#[test]+fn document_visibility_and_default_view_follow_receiver_across_realms() {+ let mut vm = new_storage_test_vm("https://document-visibility-realms.test/");++ vm.eval(+ r#"+(() => {+ const frame = document.createElement("iframe");+ frame.id = "visibility-live-frame";+ (document.body || document.documentElement || document).appendChild(frame);+})()+"#,+ )+ .expect("visibility child frame should be created");+ materialize_single_child_default_realm_for_test(+ &mut vm,+ "document visibility and defaultView child Realm",+ );++ let result = vm+ .eval(+ r#"+(() => {+ const errors = [];+ const check = (condition, message) => { if (!condition) errors.push(message); };+ const childWindow = document.getElementById("visibility-live-frame").contentWindow;+ const child = childWindow.document;+ const parentView = Object.getOwnPropertyDescriptor(Document.prototype, "defaultView").get;+ const childView = Object.getOwnPropertyDescriptor(childWindow.Document.prototype, "defaultView").get;+ const parentHidden = Object.getOwnPropertyDescriptor(Document.prototype, "hidden").get;+ const childHidden = Object.getOwnPropertyDescriptor(childWindow.Document.prototype, "hidden").get;+ const parentVisibility = Object.getOwnPropertyDescriptor(Document.prototype, "visibilityState").get;+ const childVisibility = Object.getOwnPropertyDescriptor(childWindow.Document.prototype, "visibilityState").get;+ check(parentView.call(document) === window, "parent defaultView");+ check(parentView.call(child) === childWindow, "parent getter on child defaultView");+ check(childView.call(document) === window, "child getter on parent defaultView");+ check(childView.call(child) === childWindow, "child defaultView");+ check(!parentHidden.call(child) && parentVisibility.call(child) === "visible", "child is visible through parent getters");+ check(!childHidden.call(document) && childVisibility.call(document) === "visible", "parent is visible through child getters");+ const windowless = child.implementation.createHTMLDocument("");+ check(childView.call(windowless) === null, "windowless defaultView through child getter");+ check(parentHidden.call(windowless) && parentVisibility.call(windowless) === "hidden", "windowless remains hidden through parent getters");+ const popup = window.open("about:blank", "visibility-popup");+ check(popup !== null, "popup created");+ try {+ check(!popup.document.hidden && popup.document.visibilityState === "visible", "live popup is visible");+ check(parentView.call(popup.document) === popup, "popup defaultView through parent getter");+ } finally {+ popup.close();+ }+ return JSON.stringify(errors);+})()+"#,+ )+ .expect("cross-realm document visibility and view probe should evaluate");++ assert_eq!(result, "[]");+}