Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(cssom): keep adopted element computed styles live

ldm063063750ee5a
diff --git a/moli-renderer-v8/src/native_bridge/element/styles.rs b/moli-renderer-v8/src/native_bridge/element/styles.rs
index dea29b724..69e7bb316 100644
--- a/moli-renderer-v8/src/native_bridge/element/styles.rs
+++ b/moli-renderer-v8/src/native_bridge/element/styles.rs
@@ -117,13 +117,14 @@ fn style_object_forces_empty_computed<'s>(
}
let empty = match bridge_handle {
BridgeHandle::ComputedStyle(handle, ref descriptor) => match descriptor.target {
- ComputedStyleTargetKey::ChildFrame(frame_handle) => {
+ ComputedStyleTargetKey::ChildFrame(_) => {
let runtime = unsafe { &*runtime_ptr };
- let empty = !child_frame_target_document_is_current(runtime, handle, frame_handle)
- || style_property_value(runtime, frame_handle, StyleMode::Computed, "display")
- == "none";
+ // A live declaration follows its Element after adoption. An
+ // outgoing Document still becomes empty when it is retired.
+ let context = computed_style_target_context(scope, runtime, handle, None);
+ let empty = context.returns_empty_style();
cache_target_empty_context(scope, runtime, style, empty);
- cache_frame_viewport(scope, runtime, style, frame_handle);
+ cache_computed_viewport(scope, style, (!empty).then(|| context.viewport(runtime)));
empty
}
ComputedStyleTargetKey::DetachedIframe(frame_handle) => {
@@ -151,20 +152,6 @@ fn style_object_forces_empty_computed<'s>(
empty
}
-fn child_frame_target_document_is_current(
- runtime: &JsContextHost,
- target: DomHandle,
- frame_handle: DomHandle,
-) -> bool {
- let target_document = runtime
- .dom_host()
- .node(target)
- .and_then(crate::dom::native::Node::owner_document);
- runtime
- .child_browsing_context_document_handle(frame_handle)
- .is_some_and(|document| Some(document) == target_document)
-}
-
fn popup_document_target_is_current(
runtime: &JsContextHost,
target: DomHandle,
@@ -240,13 +227,11 @@ fn cache_target_empty_context<'s>(
);
}
-fn cache_frame_viewport<'s>(
+fn cache_computed_viewport<'s>(
scope: &mut v8::PinScope<'s, '_>,
- runtime: &JsContextHost,
style: v8::Local<'s, v8::Object>,
- frame_handle: DomHandle,
+ viewport: Option<StyleViewport>,
) {
- let viewport = iframe_handle_viewport(runtime, frame_handle);
let width = viewport
.and_then(|viewport| viewport.width)
.map(|width| v8::Number::new(scope, width).into())
@@ -378,7 +363,7 @@ fn live_computed_style_viewport<'s>(
};
let runtime = unsafe { &*runtime_ptr };
match descriptor.target {
- ComputedStyleTargetKey::Dynamic => {
+ ComputedStyleTargetKey::Dynamic | ComputedStyleTargetKey::ChildFrame(_) => {
if let Some(frame) = computed_style_target_child_frame_handle(scope, runtime, handle) {
return iframe_handle_viewport(runtime, frame);
}
@@ -388,14 +373,9 @@ fn live_computed_style_viewport<'s>(
let document = runtime.dom_host().owner_document_handle(handle)?;
Some(style_viewport_for_document(runtime, document))
}
- ComputedStyleTargetKey::ChildFrame(frame)
- if child_frame_target_document_is_current(runtime, handle, frame) =>
- {
- iframe_handle_viewport(runtime, frame)
+ ComputedStyleTargetKey::DetachedIframe(_) | ComputedStyleTargetKey::PopupDocument(_) => {
+ None
}
- ComputedStyleTargetKey::ChildFrame(_)
- | ComputedStyleTargetKey::DetachedIframe(_)
- | ComputedStyleTargetKey::PopupDocument(_) => None,
}
}
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/computed_style_access.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/computed_style_access.rs
index 3c1f9df56..ea601ba44 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/computed_style_access.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/computed_style_access.rs
@@ -1192,17 +1192,16 @@ fn detached_nested_iframe_window_get_computed_style_uses_iframe_width() {
}
#[test]
-fn detached_iframe_computed_style_keeps_pseudo_and_target_identity() {
+fn live_iframe_computed_style_keeps_pseudo_and_target_identity() {
let mut vm = new_storage_test_vm("https://detached-iframe-computed-pseudo.test/");
let result = vm
.eval(
r#"
(() => {
- const detachedDocument = document.implementation.createHTMLDocument('');
- const frame = detachedDocument.createElement('iframe');
+ const frame = document.createElement('iframe');
frame.setAttribute('width', '220');
- frame.srcdoc = `
+ const frameMarkup = `
<style>
body { color: rgb(255, 0, 0); background-color: rgb(1, 2, 3); }
body::before { content: "x"; color: rgb(0, 128, 0); }
@@ -1211,7 +1210,13 @@ fn detached_iframe_computed_style_keeps_pseudo_and_target_identity() {
}
</style>
<body>text</body>`;
- detachedDocument.body.appendChild(frame);
+ const frameRoot = document.documentElement || document.appendChild(document.createElement('html'));
+ const frameParent = document.body || frameRoot.appendChild(document.createElement('body'));
+ frameParent.appendChild(frame);
+ const initialDocument = frame.contentDocument;
+ initialDocument.open();
+ initialDocument.write(frameMarkup);
+ initialDocument.close();
const childDocument = frame.contentDocument;
const target = childDocument.body;
@@ -1227,7 +1232,7 @@ fn detached_iframe_computed_style_keeps_pseudo_and_target_identity() {
after.color,
highlight.length > 200,
highlight.color,
- topOrigin.length,
+ topOrigin.color === origin.color,
origin.color,
origin.backgroundColor,
before.color,
@@ -1237,16 +1242,16 @@ fn detached_iframe_computed_style_keeps_pseudo_and_target_identity() {
})()
"#,
)
- .expect("detached iframe computed pseudo style should evaluate");
+ .expect("live iframe computed pseudo style should evaluate");
assert_eq!(
result,
- "rgb(255, 0, 0)|rgb(1, 2, 3)|rgb(0, 128, 0)|rgb(0, 0, 255)|true|rgb(255, 0, 0)|0|rgb(255, 0, 0)|rgb(1, 2, 3)|rgb(0, 128, 0)|rgb(0, 0, 255)|true"
+ "rgb(255, 0, 0)|rgb(1, 2, 3)|rgb(0, 128, 0)|rgb(0, 0, 255)|true|rgb(255, 0, 0)|true|rgb(255, 0, 0)|rgb(1, 2, 3)|rgb(0, 128, 0)|rgb(0, 0, 255)|true"
);
}
#[test]
-fn held_detached_iframe_computed_style_uses_target_owner_sources_after_adoption() {
+fn held_live_iframe_computed_style_uses_target_owner_sources_after_adoption() {
let mut vm = new_storage_test_vm("https://detached-iframe-held-owner-source.test/");
let result = vm
@@ -1273,9 +1278,8 @@ fn held_detached_iframe_computed_style_uses_target_owner_sources_after_adoption(
`;
head.appendChild(activeStyle);
- const detachedDocument = document.implementation.createHTMLDocument('');
- const frame = detachedDocument.createElement('iframe');
- frame.srcdoc = `
+ const frame = document.createElement('iframe');
+ const frameMarkup = `
<style>
#cross-doc-target {
color: rgb(255, 0, 0);
@@ -1284,7 +1288,13 @@ fn held_detached_iframe_computed_style_uses_target_owner_sources_after_adoption(
}
</style>
<body><div id="cross-doc-target">x</div></body>`;
- detachedDocument.body.appendChild(frame);
+ const frameRoot = document.documentElement || document.appendChild(document.createElement('html'));
+ const frameParent = document.body || frameRoot.appendChild(document.createElement('body'));
+ frameParent.appendChild(frame);
+ const initialDocument = frame.contentDocument;
+ initialDocument.open();
+ initialDocument.write(frameMarkup);
+ initialDocument.close();
const childWindow = frame.contentWindow;
const childDocument = frame.contentDocument;
@@ -1306,7 +1316,7 @@ fn held_detached_iframe_computed_style_uses_target_owner_sources_after_adoption(
})()
"#,
)
- .expect("held detached iframe computed style should use target owner sources");
+ .expect("held live iframe computed style should use target owner sources");
assert_eq!(
result,
@@ -1315,7 +1325,7 @@ fn held_detached_iframe_computed_style_uses_target_owner_sources_after_adoption(
}
#[test]
-fn held_detached_iframe_custom_property_animation_uses_target_owner_registry_after_adoption() {
+fn held_live_iframe_custom_property_animation_uses_target_owner_registry_after_adoption() {
let mut vm = new_storage_test_vm("https://detached-iframe-held-custom-animation.test/");
let result = vm
@@ -1345,10 +1355,15 @@ fn held_detached_iframe_custom_property_animation_uses_target_owner_registry_aft
`;
head.appendChild(activeStyle);
- const detachedDocument = document.implementation.createHTMLDocument('');
- const frame = detachedDocument.createElement('iframe');
- frame.srcdoc = '<body><div id="held-animation-target">x</div></body>';
- detachedDocument.body.appendChild(frame);
+ const frame = document.createElement('iframe');
+ const frameMarkup = '<body><div id="held-animation-target">x</div></body>';
+ const frameRoot = document.documentElement || document.appendChild(document.createElement('html'));
+ const frameParent = document.body || frameRoot.appendChild(document.createElement('body'));
+ frameParent.appendChild(frame);
+ const initialDocument = frame.contentDocument;
+ initialDocument.open();
+ initialDocument.write(frameMarkup);
+ initialDocument.close();
const childWindow = frame.contentWindow;
const childDocument = childWindow.document;
@@ -1365,23 +1380,22 @@ fn held_detached_iframe_custom_property_animation_uses_target_owner_registry_aft
})()
"#,
)
- .expect("held detached iframe custom property animation should use target owner registry");
+ .expect("held live iframe custom property animation should use target owner registry");
assert_eq!(result, "true|20px|20px");
}
#[test]
-fn detached_iframe_attribute_mutation_targets_computed_style_cache() {
+fn live_iframe_attribute_mutation_targets_computed_style_cache() {
let mut vm = new_storage_test_vm("https://detached-iframe-attribute-style-cache.test/");
let before = vm
.eval(
r#"
(() => {
- const detachedDocument = document.implementation.createHTMLDocument('');
- const frame = detachedDocument.createElement('iframe');
+ const frame = document.createElement('iframe');
frame.id = 'detached-attribute-style-frame';
- frame.srcdoc = `
+ const frameMarkup = `
<style>
#detached-attribute-outside { color: rgb(1, 2, 3); }
#detached-attribute-target { color: rgb(7, 8, 9); }
@@ -1391,7 +1405,13 @@ fn detached_iframe_attribute_mutation_targets_computed_style_cache() {
<section><div id="detached-attribute-outside">outside</div></section>
<section><div id="detached-attribute-target">target</div></section>
</body>`;
- detachedDocument.body.appendChild(frame);
+ const frameRoot = document.documentElement || document.appendChild(document.createElement('html'));
+ const frameParent = document.body || frameRoot.appendChild(document.createElement('body'));
+ frameParent.appendChild(frame);
+ const initialDocument = frame.contentDocument;
+ initialDocument.open();
+ initialDocument.write(frameMarkup);
+ initialDocument.close();
const childDocument = frame.contentDocument;
globalThis.__detachedAttributeStyleFrame = frame;
@@ -1405,7 +1425,7 @@ fn detached_iframe_attribute_mutation_targets_computed_style_cache() {
})()
"#,
)
- .expect("detached iframe attribute style cache setup should evaluate");
+ .expect("live iframe attribute style cache setup should evaluate");
assert_eq!(before, "rgb(1, 2, 3)|rgb(7, 8, 9)");
let child_document = owner_document_handle_for_element_id(&vm, "detached-attribute-target");
@@ -1424,7 +1444,7 @@ fn detached_iframe_attribute_mutation_targets_computed_style_cache() {
})()
"#,
)
- .expect("detached iframe attribute mutation should evaluate");
+ .expect("live iframe attribute mutation should evaluate");
assert_eq!(mutation_result, "active|true|true");
let after = vm
@@ -1443,7 +1463,7 @@ fn detached_iframe_attribute_mutation_targets_computed_style_cache() {
})()
"#,
)
- .expect("detached iframe attribute style cache mutation should evaluate");
+ .expect("live iframe attribute style cache mutation should evaluate");
assert_eq!(after, "rgb(1, 2, 3)|rgb(4, 5, 6)|rgb(4, 5, 6)");
assert!(computed_style_cache_entry_count_for_document(&vm, child_document) > 0);
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/cssom_arguments.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/cssom_arguments.rs
index 2f12a1644..0717cd4c7 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/cssom_arguments.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/cssom_arguments.rs
@@ -24,19 +24,20 @@ fn computed_style_arguments_use_native_element_brands_and_preserve_exceptions()
}
#[test]
-fn detached_iframe_computed_style_uses_the_shared_argument_conversion() {
+fn live_iframe_computed_style_uses_the_shared_argument_conversion() {
let mut vm = new_storage_page_task_executor_test_vm("https://detached-style-arguments.test/");
vm.eval(
r#"
-const detachedDocument = document.implementation.createHTMLDocument('');
-const frame = detachedDocument.createElement('iframe');
-detachedDocument.body.appendChild(frame);
+const root = document.documentElement || document.appendChild(document.createElement('html'));
+const body = document.body || root.appendChild(document.createElement('body'));
+const frame = document.createElement('iframe');
+body.appendChild(frame);
const child = frame.contentWindow;
const target = child.document.createElement('select');
child.document.body.appendChild(target);
globalThis.__detachedStyleArgumentChecks = [];
const check = value => __detachedStyleArgumentChecks.push(value);
-check(child.getComputedStyle(target) instanceof CSSStyleDeclaration);
+check(child.getComputedStyle(target) instanceof child.CSSStyleDeclaration);
for (const value of [undefined, null, {}, child.document,
child.document.createTextNode('x'), Object.create(target),
new Proxy(target, {}), Object.create(Element.prototype)]) {
@@ -45,7 +46,7 @@ for (const value of [undefined, null, {}, child.document,
try {
child.getComputedStyle(value, {toString() { conversions++; return ''; }});
} catch (error) { caught = error; }
- check(caught instanceof TypeError);
+ check(caught instanceof child.TypeError);
check(conversions === 0);
}
const sentinel = {};
@@ -53,10 +54,10 @@ let caught;
try { child.getComputedStyle(target, {toString() { throw sentinel; }}); }
catch (error) { caught = error; }
check(caught === sentinel);
-check(child.getComputedStyle(target, null) instanceof CSSStyleDeclaration);
+check(child.getComputedStyle(target, null) instanceof child.CSSStyleDeclaration);
"#,
)
- .expect("synthetic iframe should share argument conversion");
+ .expect("live iframe should share argument conversion");
assert_eq!(
vm.eval("__detachedStyleArgumentChecks.length === 19 && __detachedStyleArgumentChecks.every(Boolean)")
.unwrap(),
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/cssom_identity.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/cssom_identity.rs
index 1cc47a908..baae27d79 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/cssom_identity.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style/cssom_identity.rs
@@ -102,14 +102,15 @@ globalThis.identityInline = identityGcTarget.style;
}
#[test]
-fn detached_iframe_computed_style_declarations_are_fresh_objects() {
+fn live_iframe_computed_style_declarations_are_fresh_objects() {
let mut vm = new_storage_page_task_executor_test_vm("https://detached-style-identity.test/");
assert_eq!(
vm.eval(
r#"(() => {
-const detached = document.implementation.createHTMLDocument('');
-const frame = detached.createElement('iframe');
-detached.body.appendChild(frame);
+const root = document.documentElement || document.appendChild(document.createElement('html'));
+const body = document.body || root.appendChild(document.createElement('body'));
+const frame = document.createElement('iframe');
+body.appendChild(frame);
const child = frame.contentWindow;
const target = child.document.createElement('div');
child.document.body.appendChild(target);