Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(style): refresh live viewport-dependent styles

ldm0d92c6385925a
diff --git a/moli-renderer-v8/src/native_bridge/context_host/layout.rs b/moli-renderer-v8/src/native_bridge/context_host/layout.rs
index 33ddda09d..cb12bac1e 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/layout.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/layout.rs
@@ -142,6 +142,7 @@ impl JsContextHost {
.frame_viewport(frame)
}
+ #[cfg(debug_assertions)]
pub(crate) fn style_viewport_generation(&self) -> u64 {
self.style_viewport_generation.get()
}
diff --git a/moli-renderer-v8/src/native_bridge/element/styles.rs b/moli-renderer-v8/src/native_bridge/element/styles.rs
index 29be7c925..ce303b36a 100644
--- a/moli-renderer-v8/src/native_bridge/element/styles.rs
+++ b/moli-renderer-v8/src/native_bridge/element/styles.rs
@@ -340,8 +340,32 @@ fn style_object_computation_context<'s>(
style: v8::Local<'s, v8::Object>,
) -> StyleComputationContext {
let read_document = style_object_read_document(scope, style);
- StyleComputationContext::new(style_object_viewport(scope, style))
- .with_read_document(read_document)
+ let viewport = bridge_handle_from_object(scope, style)
+ .ok()
+ .and_then(|(runtime_ptr, bridge_handle)| {
+ let runtime = unsafe { &*runtime_ptr };
+ match bridge_handle {
+ BridgeHandle::ComputedStyle(handle, descriptor) => match descriptor.target {
+ ComputedStyleTargetKey::Dynamic => runtime
+ .dom_host()
+ .owner_document_handle(handle)
+ .or(read_document)
+ .map(|document| style_viewport_for_document(runtime, document)),
+ ComputedStyleTargetKey::ChildFrame(frame_handle) => {
+ iframe_handle_viewport(runtime, frame_handle)
+ }
+ ComputedStyleTargetKey::DetachedIframe(_)
+ | ComputedStyleTargetKey::PopupDocument(_) => None,
+ },
+ BridgeHandle::Window
+ | BridgeHandle::Node(_)
+ | BridgeHandle::ClassList(_, _)
+ | BridgeHandle::Dataset(_)
+ | BridgeHandle::Style(_) => None,
+ }
+ })
+ .unwrap_or_else(|| style_object_viewport(scope, style));
+ StyleComputationContext::new(viewport).with_read_document(read_document)
}
fn style_object_property_count_with_context<'s>(
diff --git a/moli-renderer-v8/src/native_bridge/element/styles/declaration/observation.rs b/moli-renderer-v8/src/native_bridge/element/styles/declaration/observation.rs
index 8f418ea54..164993d32 100644
--- a/moli-renderer-v8/src/native_bridge/element/styles/declaration/observation.rs
+++ b/moli-renderer-v8/src/native_bridge/element/styles/declaration/observation.rs
@@ -85,6 +85,7 @@ pub(super) struct StyleObservationInputs<'a> {
runtime: &'a JsContextHost,
key: StyleObservationKey,
context: StyleComputationContext,
+ #[cfg(debug_assertions)]
tracks_persistent_world: bool,
prepared_update: RefCell<Option<Rc<PreparedStyleWorldUpdate>>>,
stylesheet_query_snapshot: RefCell<Option<Rc<FullStyleWorldSnapshot>>>,
@@ -95,13 +96,14 @@ impl<'a> StyleObservationInputs<'a> {
runtime: &'a JsContextHost,
source_document: Option<DomHandle>,
context: StyleComputationContext,
- tracks_persistent_world: bool,
+ _tracks_persistent_world: bool,
) -> Self {
Self {
runtime,
key: StyleObservationKey::for_document(runtime, source_document),
context,
- tracks_persistent_world,
+ #[cfg(debug_assertions)]
+ tracks_persistent_world: _tracks_persistent_world,
prepared_update: RefCell::new(None),
stylesheet_query_snapshot: RefCell::new(None),
}
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
index 9f6255847..63f4af6eb 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
@@ -4528,6 +4528,339 @@ fn computed_style_child_document_viewport_units_use_css_iframe_size() {
assert_eq!(result, "100px|100px|200px");
}
+#[test]
+fn held_main_document_computed_styles_follow_repeated_viewport_surface_changes() {
+ let mut vm = new_storage_test_vm("https://held-main-viewport-units.test/");
+ let surface = |inner_width, inner_height| crate::protocol_types::ViewportSurface {
+ inner_width,
+ inner_height,
+ outer_width: inner_width,
+ outer_height: inner_height,
+ device_pixel_ratio: 1.0,
+ screen_width: 1920,
+ screen_height: 1080,
+ screen_avail_width: 1920,
+ screen_avail_height: 1040,
+ };
+ vm.set_viewport_surface(Some(surface(1000, 800)))
+ .expect("initial viewport surface should update");
+
+ let before = vm
+ .eval(
+ r#"
+(() => {
+ const style = document.createElement('style');
+ style.textContent = `
+ #held-main-viewport-target {
+ position: absolute;
+ width: 10vw;
+ height: 10vh;
+ --pseudo-width: 5vw;
+ }
+ #held-main-viewport-target::before {
+ content: "viewport";
+ width: var(--pseudo-width);
+ height: 2vmin;
+ }
+ @media (min-width: 1px) {
+ #held-main-viewport-target { left: 50vw; }
+ }`;
+ (document.head || document.documentElement || document).appendChild(style);
+ const target = document.createElement('div');
+ target.id = 'held-main-viewport-target';
+ target.style.paddingLeft = '12vw';
+ (document.body || document.documentElement || document).appendChild(target);
+ const held = getComputedStyle(target);
+ const heldBefore = getComputedStyle(target, '::before');
+ globalThis.__heldMainViewportStyles = { target, held, heldBefore };
+ return [
+ innerWidth,
+ innerHeight,
+ held.width,
+ held.height,
+ held.paddingLeft,
+ held.left,
+ heldBefore.width,
+ heldBefore.height
+ ].join('|');
+})()
+"#,
+ )
+ .expect("main-document viewport-unit styles should be cached");
+ assert_eq!(before, "1000|800|100px|80px|120px|500px|50px|16px");
+ let document = vm.document_handle_for_test();
+ let updates = vm.retained_style_system_update_count_for_document_for_test(document);
+ let stylist = vm.retained_stylist_identity_for_document_for_test(document);
+
+ vm.set_viewport_surface(Some(surface(500, 400)))
+ .expect("smaller viewport surface should update");
+ let smaller = vm
+ .eval(
+ r#"
+(() => {
+ const { target, held, heldBefore } = __heldMainViewportStyles;
+ const fresh = getComputedStyle(target);
+ const freshBefore = getComputedStyle(target, '::before');
+ return [
+ innerWidth,
+ innerHeight,
+ held.width,
+ held.height,
+ held.paddingLeft,
+ held.left,
+ heldBefore.width,
+ heldBefore.height,
+ fresh.width,
+ freshBefore.width
+ ].join('|');
+})()
+"#,
+ )
+ .expect("held styles should observe the smaller viewport");
+ assert_eq!(smaller, "500|400|50px|40px|60px|250px|25px|8px|50px|25px");
+ assert_eq!(
+ vm.retained_style_system_update_count_for_document_for_test(document),
+ updates + 1,
+ "one viewport transition should update the retained document world once",
+ );
+ assert_eq!(
+ vm.retained_stylist_identity_for_document_for_test(document),
+ stylist,
+ "viewport changes must retain the Document Stylist",
+ );
+
+ vm.set_viewport_surface(Some(surface(1000, 800)))
+ .expect("restored viewport surface should update");
+ let restored = vm
+ .eval(
+ r#"
+(() => {
+ const { held, heldBefore } = __heldMainViewportStyles;
+ return [
+ innerWidth,
+ innerHeight,
+ held.width,
+ held.height,
+ held.paddingLeft,
+ held.left,
+ heldBefore.width,
+ heldBefore.height
+ ].join('|');
+})()
+"#,
+ )
+ .expect("held styles should observe the restored viewport");
+ assert_eq!(restored, "1000|800|100px|80px|120px|500px|50px|16px");
+ assert_eq!(
+ vm.retained_style_system_update_count_for_document_for_test(document),
+ updates + 2,
+ "each distinct viewport transition should produce one retained update",
+ );
+ assert_eq!(
+ vm.retained_stylist_identity_for_document_for_test(document),
+ stylist,
+ );
+}
+
+#[test]
+fn cached_child_viewport_units_update_after_the_iframe_viewport_changes() {
+ let mut vm = new_storage_test_vm("https://cached-child-viewport-units.test/");
+ vm.force_fresh_layout_reads_for_test();
+
+ let before = vm
+ .eval(
+ r#"
+(() => {
+ const frame = document.createElement('iframe');
+ frame.id = 'cached-viewport-unit-frame';
+ frame.style.cssText = 'width:200px;height:160px';
+ (document.body || document.documentElement || document).appendChild(frame);
+ const childWindow = frame.contentWindow;
+ const childDocument = childWindow.document;
+ childDocument.open();
+ childDocument.write(`
+ <style>
+ #target {
+ position: absolute;
+ width: 10vw;
+ height: 10vh;
+ padding-left: calc(5vw + 8px);
+ }
+ @media (min-width: 1px) {
+ #target { left: 50vw; }
+ }
+ </style>
+ <body><div id="target"></div></body>`);
+ childDocument.close();
+ const target = childDocument.getElementById('target');
+ const held = childWindow.getComputedStyle(target);
+ frame.getBoundingClientRect();
+ globalThis.__cachedViewportUnitFixture = { frame, childWindow, target, held };
+ return [
+ childWindow.innerWidth,
+ childWindow.innerHeight,
+ held.width,
+ held.height,
+ held.paddingLeft,
+ held.left
+ ].join('|');
+})()
+"#,
+ )
+ .expect("the child viewport-unit style should be cached before resize");
+ assert_eq!(before, "200|160|20px|16px|18px|100px");
+ let child_document = child_document_handle_for_frame_id(&vm, "cached-viewport-unit-frame");
+ let updates = vm.retained_style_system_update_count_for_document_for_test(child_document);
+
+ let after = vm
+ .eval(
+ r#"
+(() => {
+ const { frame, childWindow, target, held } = __cachedViewportUnitFixture;
+ frame.style.width = '100px';
+ frame.style.height = '80px';
+ const frameWidth = frame.getBoundingClientRect().width;
+ const heldValues = [held.width, held.height, held.paddingLeft, held.left];
+ const fresh = childWindow.getComputedStyle(target);
+ return [
+ frameWidth,
+ childWindow.innerWidth,
+ childWindow.innerHeight,
+ ...heldValues,
+ fresh.width,
+ fresh.height
+ ].join('|');
+})()
+"#,
+ )
+ .expect("the same cached child element should observe the resized iframe viewport");
+ assert_eq!(after, "100|100|80|10px|8px|13px|50px|10px|8px");
+ assert_eq!(
+ vm.retained_style_system_update_count_for_document_for_test(child_document),
+ updates + 1,
+ "one iframe viewport change should update its retained style world exactly once",
+ );
+}
+
+#[test]
+fn nested_iframe_cached_viewport_units_follow_recursive_frame_resizes() {
+ let mut vm = new_storage_test_vm("https://nested-cached-viewport-units.test/");
+ vm.force_fresh_layout_reads_for_test();
+
+ let before = vm
+ .eval(
+ r#"
+(() => {
+ const outerFrame = document.createElement('iframe');
+ outerFrame.id = 'nested-viewport-outer-frame';
+ outerFrame.style.cssText = 'width:200px;height:160px;border:0';
+ (document.body || document.documentElement || document).appendChild(outerFrame);
+
+ const outerWindow = outerFrame.contentWindow;
+ const outerDocument = outerWindow.document;
+ outerDocument.open();
+ outerDocument.write(`
+ <style>
+ html, body { margin: 0; width: 100%; height: 100%; }
+ #nested-viewport-inner-frame {
+ display: block;
+ width: 50%;
+ height: 50%;
+ border: 0;
+ }
+ </style>
+ <body><iframe id="nested-viewport-inner-frame"></iframe></body>`);
+ outerDocument.close();
+
+ const innerFrame = outerDocument.getElementById('nested-viewport-inner-frame');
+ const innerWindow = innerFrame.contentWindow;
+ const innerDocument = innerWindow.document;
+ innerDocument.open();
+ innerDocument.write(`
+ <style>
+ #nested-viewport-target {
+ width: 10vw;
+ height: 10vh;
+ padding-left: 5vmin;
+ }
+ </style>
+ <body><div id="nested-viewport-target"></div></body>`);
+ innerDocument.close();
+
+ outerFrame.getBoundingClientRect();
+ innerFrame.getBoundingClientRect();
+ const target = innerDocument.getElementById('nested-viewport-target');
+ const held = innerWindow.getComputedStyle(target);
+ globalThis.__nestedViewportUnitFixture = {
+ outerFrame,
+ innerFrame,
+ outerWindow,
+ innerWindow,
+ target,
+ held
+ };
+ return [
+ outerWindow.innerWidth,
+ outerWindow.innerHeight,
+ innerWindow.innerWidth,
+ innerWindow.innerHeight,
+ held.width,
+ held.height,
+ held.paddingLeft
+ ].join('|');
+})()
+"#,
+ )
+ .expect("nested viewport-unit styles should be cached");
+ assert_eq!(before, "200|160|100|80|10px|8px|4px");
+ let inner_document = child_document_handle_for_frame_id(&vm, "nested-viewport-inner-frame");
+ let updates = vm.retained_style_system_update_count_for_document_for_test(inner_document);
+ let stylist = vm.retained_stylist_identity_for_document_for_test(inner_document);
+
+ let after = vm
+ .eval(
+ r#"
+(() => {
+ const {
+ outerFrame,
+ innerFrame,
+ outerWindow,
+ innerWindow,
+ target,
+ held
+ } = __nestedViewportUnitFixture;
+ outerFrame.style.width = '100px';
+ outerFrame.style.height = '80px';
+ outerFrame.getBoundingClientRect();
+ innerFrame.getBoundingClientRect();
+ const heldValues = [held.width, held.height, held.paddingLeft];
+ const fresh = innerWindow.getComputedStyle(target);
+ return [
+ outerWindow.innerWidth,
+ outerWindow.innerHeight,
+ innerWindow.innerWidth,
+ innerWindow.innerHeight,
+ ...heldValues,
+ fresh.width,
+ fresh.height
+ ].join('|');
+})()
+"#,
+ )
+ .expect("nested held styles should observe recursive iframe resize");
+ assert_eq!(after, "100|80|50|40|5px|4px|2px|5px|4px");
+ assert_eq!(
+ vm.retained_style_system_update_count_for_document_for_test(inner_document),
+ updates + 1,
+ "the innermost Document should consume one recursive viewport transition",
+ );
+ assert_eq!(
+ vm.retained_stylist_identity_for_document_for_test(inner_document),
+ stylist,
+ "recursive iframe resizing must retain the innermost Stylist",
+ );
+}
+
#[test]
fn child_document_mixed_style_observations_do_not_churn_the_retained_world() {
let mut vm = new_storage_test_vm("https://child-style-observation-context.test/");
diff --git a/moli-renderer-v8/src/style_engine/tests/subtree_cache.rs b/moli-renderer-v8/src/style_engine/tests/subtree_cache.rs
index 107a746f3..0e0f68aaa 100644
--- a/moli-renderer-v8/src/style_engine/tests/subtree_cache.rs
+++ b/moli-renderer-v8/src/style_engine/tests/subtree_cache.rs
@@ -5720,6 +5720,364 @@ fn one_shadow_stylesheet_change_preserves_the_other_scope_cascade_data() {
);
}
+#[test]
+fn viewport_resize_recascades_cached_viewport_units_without_media_match_changes() {
+ reset_source_cascade_rebuild_count_for_test();
+ let mut host = test_host();
+ let document = host.document_handle();
+ let rule_target = host.create_element("div");
+ assert!(host.set_attribute(rule_target, "class", "rule-target stable-media-target"));
+ assert!(host.append_child(document, rule_target));
+ let inline_target = host.create_element("div");
+ assert!(host.set_attribute(inline_target, "style", "width: 12vw; height: 25vh",));
+ assert!(host.append_child(document, inline_target));
+ let shadow_host = host.create_element("section");
+ assert!(host.append_child(document, shadow_host));
+ let shadow_root = host
+ .attach_shadow_root(shadow_host, "open")
+ .expect("the viewport-unit host should accept a shadow root");
+ let shadow_target = host.create_element("span");
+ assert!(host.set_attribute(shadow_target, "class", "shadow-target"));
+ assert!(host.append_child(shadow_root, shadow_target));
+
+ let document_url = url::Url::parse("https://example.test/viewport-units.html").unwrap();
+ let mut inputs = FullStyleWorldSnapshot::default();
+ inputs.document_stylesheet_sources.push(
+ StyloStylesheetSource::new(
+ r#"
+ .rule-target {
+ position: absolute;
+ width: 10vw;
+ height: 10vh;
+ min-width: 2vmin;
+ max-width: 2vmax;
+ font-size: 2vmin;
+ padding-left: calc(5vw + 8px);
+ top: 10dvh;
+ right: 10svh;
+ bottom: 10lvh;
+ }
+ @media (min-width: 1px) {
+ .stable-media-target { left: 50vw; }
+ }
+ "#
+ .into(),
+ document_url.clone(),
+ )
+ .with_source_id(Some(StyleSourceId::document_adopted_style_sheet(
+ document, 81,
+ ))),
+ );
+ inputs.shadow_stylesheet_sources.push((
+ shadow_root,
+ vec![
+ StyloStylesheetSource::new(
+ ".shadow-target { width: 25vw; height: 25vh; }".into(),
+ document_url.clone(),
+ )
+ .with_source_id(Some(StyleSourceId::shadow_root_adopted_style_sheet(
+ shadow_root,
+ 82,
+ ))),
+ ],
+ ));
+
+ let engine = MoliStyleEngine::new();
+ let viewport_1000 = StyleViewport::new(Some(1000.0), Some(800.0));
+ let read = |target, property, viewport| {
+ engine
+ .computed_style_property_value(
+ &host,
+ &document_url,
+ target,
+ property,
+ None,
+ &inputs,
+ viewport,
+ )
+ .unwrap_or_else(|| panic!("{property} should have a computed value"))
+ };
+ let rule_properties = [
+ "width",
+ "height",
+ "min-width",
+ "max-width",
+ "font-size",
+ "padding-left",
+ "top",
+ "right",
+ "bottom",
+ "left",
+ ];
+ assert_eq!(
+ rule_properties.map(|property| read(rule_target, property, viewport_1000)),
+ [
+ "100px", "80px", "16px", "20px", "16px", "58px", "80px", "80px", "80px", "500px",
+ ],
+ );
+ assert_eq!(read(inline_target, "width", viewport_1000), "120px");
+ assert_eq!(read(inline_target, "height", viewport_1000), "200px");
+ assert_eq!(read(shadow_target, "width", viewport_1000), "250px");
+ assert_eq!(read(shadow_target, "height", viewport_1000), "200px");
+ let style_before = retained_primary_style_for_test(&engine, &host, rule_target)
+ .expect("the initial viewport-relative style should be retained");
+ let document_flushes = engine.retained_stylist_flush_count_for_document_for_test(document);
+ let shadow_flushes = engine
+ .retained_shadow_scope_flush_count_for_document_for_test(document, shadow_root)
+ .expect("the shadow scope should be retained");
+ let source_rebuilds = source_cascade_rebuild_count_for_test();
+
+ let viewport_500 = StyleViewport::new(Some(500.0), Some(400.0));
+ assert_eq!(
+ rule_properties.map(|property| read(rule_target, property, viewport_500)),
+ [
+ "50px", "40px", "8px", "10px", "8px", "33px", "40px", "40px", "40px", "250px",
+ ],
+ "the same cached element must recascade every viewport-relative unit",
+ );
+ assert_eq!(read(inline_target, "width", viewport_500), "60px");
+ assert_eq!(read(inline_target, "height", viewport_500), "100px");
+ assert_eq!(read(shadow_target, "width", viewport_500), "125px");
+ assert_eq!(read(shadow_target, "height", viewport_500), "100px");
+ let style_after = retained_primary_style_for_test(&engine, &host, rule_target)
+ .expect("the resized viewport-relative style should be retained");
+ assert!(
+ !ServoArc::ptr_eq(&style_before, &style_after),
+ "viewport invalidation must replace the canonical ComputedValues on the same element",
+ );
+ assert_eq!(
+ engine.retained_stylist_flush_count_for_document_for_test(document),
+ document_flushes,
+ "an always-matching media query must not be used to hide viewport-unit invalidation",
+ );
+ assert_eq!(
+ engine.retained_shadow_scope_flush_count_for_document_for_test(document, shadow_root),
+ Some(shadow_flushes),
+ "viewport units alone must not flush the ShadowRoot AuthorStyles",
+ );
+ assert_eq!(
+ source_cascade_rebuild_count_for_test(),
+ source_rebuilds,
+ "viewport units alone must not rebuild source-local cascade data",
+ );
+}
+
+#[test]
+fn repeated_viewport_resizes_recascade_only_dependent_cached_elements() {
+ let mut host = test_host();
+ let document = host.document_handle();
+ let dependent = host.create_element("div");
+ let independent = host.create_element("div");
+ assert!(host.set_attribute(dependent, "class", "viewport-dependent"));
+ assert!(host.set_attribute(independent, "class", "viewport-independent"));
+ assert!(host.append_child(document, dependent));
+ assert!(host.append_child(document, independent));
+
+ let document_url = url::Url::parse("https://example.test/repeated-resize.html").unwrap();
+ let mut inputs = FullStyleWorldSnapshot::default();
+ inputs.document_stylesheet_sources.push(
+ StyloStylesheetSource::new(
+ r#"
+ .viewport-dependent { width: 10vw; height: 10vh; }
+ .viewport-independent { width: 123px; height: 45px; }
+ "#
+ .into(),
+ document_url.clone(),
+ )
+ .with_source_id(Some(StyleSourceId::document_adopted_style_sheet(
+ document, 83,
+ ))),
+ );
+
+ let engine = MoliStyleEngine::new();
+ let read = |target, property, viewport| {
+ engine
+ .computed_style_property_value(
+ &host,
+ &document_url,
+ target,
+ property,
+ None,
+ &inputs,
+ viewport,
+ )
+ .unwrap_or_else(|| panic!("{property} should have a computed value"))
+ };
+ let large = StyleViewport::new(Some(800.0), Some(600.0));
+ assert_eq!(read(dependent, "width", large), "80px");
+ assert_eq!(read(independent, "width", large), "123px");
+ let first_dependent = retained_primary_style_for_test(&engine, &host, dependent).unwrap();
+ let first_independent = retained_primary_style_for_test(&engine, &host, independent).unwrap();
+ let flushes = engine.retained_stylist_flush_count_for_document_for_test(document);
+
+ let small = StyleViewport::new(Some(400.0), Some(300.0));
+ assert_eq!(read(dependent, "width", small), "40px");
+ assert_eq!(read(dependent, "height", small), "30px");
+ assert_eq!(read(independent, "width", small), "123px");
+ let second_dependent = retained_primary_style_for_test(&engine, &host, dependent).unwrap();
+ let second_independent = retained_primary_style_for_test(&engine, &host, independent).unwrap();
+ assert!(!ServoArc::ptr_eq(&first_dependent, &second_dependent));
+ assert!(
+ ServoArc::ptr_eq(&first_independent, &second_independent),
+ "a viewport-independent cached element must survive a resize unchanged",
+ );
+
+ assert_eq!(read(dependent, "width", large), "80px");
+ assert_eq!(read(dependent, "height", large), "60px");
+ assert_eq!(read(independent, "height", large), "45px");
+ let third_dependent = retained_primary_style_for_test(&engine, &host, dependent).unwrap();
+ let third_independent = retained_primary_style_for_test(&engine, &host, independent).unwrap();
+ assert!(!ServoArc::ptr_eq(&second_dependent, &third_dependent));
+ assert!(
+ ServoArc::ptr_eq(&second_independent, &third_independent),
+ "reversing a resize must still preserve viewport-independent ComputedValues",
+ );
+ assert_eq!(
+ engine.retained_stylist_flush_count_for_document_for_test(document),
+ flushes,
+ "viewport-unit recascade must not flush an unchanged stylesheet",
+ );
+}
+
+#[test]
+fn viewport_resize_propagates_through_inheritance_variables_pseudos_and_nested_shadow_roots() {
+ let mut host = test_host();
+ let document = host.document_handle();
+ let parent = host.create_element("section");
+ let child = host.create_element("span");
+ let pseudo_target = host.create_element("div");
+ assert!(host.set_attribute(parent, "class", "viewport-parent"));
+ assert!(host.set_attribute(child, "class", "viewport-child"));
+ assert!(host.set_attribute(pseudo_target, "class", "viewport-pseudo"));
+ assert!(host.append_child(document, parent));
+ assert!(host.append_child(parent, child));
+ assert!(host.append_child(document, pseudo_target));
+
+ let outer_host = host.create_element("article");
+ assert!(host.append_child(document, outer_host));
+ let outer_root = host
+ .attach_shadow_root(outer_host, "open")
+ .expect("outer host should accept a shadow root");
+ let outer_target = host.create_element("div");
+ let inner_host = host.create_element("aside");
+ assert!(host.set_attribute(outer_target, "class", "outer-viewport-target"));
+ assert!(host.append_child(outer_root, outer_target));
+ assert!(host.append_child(outer_root, inner_host));
+ let inner_root = host
+ .attach_shadow_root(inner_host, "open")
+ .expect("a host inside a ShadowRoot should accept a nested shadow root");
+ let inner_target = host.create_element("span");
+ assert!(host.set_attribute(inner_target, "class", "inner-viewport-target"));
+ assert!(host.append_child(inner_root, inner_target));
+
+ let document_url = url::Url::parse("https://example.test/viewport-dependencies.html").unwrap();
+ let mut inputs = FullStyleWorldSnapshot::default();
+ inputs.document_stylesheet_sources.push(
+ StyloStylesheetSource::new(
+ r#"
+ .viewport-parent { font-size: 2vmin; --viewport-gap: 10vw; }
+ .viewport-child { font-size: 2em; padding-left: var(--viewport-gap); }
+ .viewport-pseudo::before {
+ content: "viewport";
+ width: 10vw;
+ height: 10vh;
+ }
+ "#
+ .into(),
+ document_url.clone(),
+ )
+ .with_source_id(Some(StyleSourceId::document_adopted_style_sheet(
+ document, 84,
+ ))),
+ );
+ inputs.shadow_stylesheet_sources.push((
+ outer_root,
+ vec![
+ StyloStylesheetSource::new(
+ ".outer-viewport-target { width: 20vw; }".into(),
+ document_url.clone(),
+ )
+ .with_source_id(Some(StyleSourceId::shadow_root_adopted_style_sheet(
+ outer_root, 85,
+ ))),
+ ],
+ ));
+ inputs.shadow_stylesheet_sources.push((
+ inner_root,
+ vec![
+ StyloStylesheetSource::new(
+ ".inner-viewport-target { height: 15vh; }".into(),
+ document_url.clone(),
+ )
+ .with_source_id(Some(StyleSourceId::shadow_root_adopted_style_sheet(
+ inner_root, 86,
+ ))),
+ ],
+ ));
+
+ let engine = MoliStyleEngine::new();
+ let read = |target, property, pseudo, viewport| {
+ engine
+ .computed_style_property_value(
+ &host,
+ &document_url,
+ target,
+ property,
+ pseudo,
+ &inputs,
+ viewport,
+ )
+ .unwrap_or_else(|| panic!("{property} should have a computed value"))
+ };
+ let large = StyleViewport::new(Some(1000.0), Some(800.0));
+ assert_eq!(read(parent, "font-size", None, large), "16px");
+ assert_eq!(read(child, "font-size", None, large), "32px");
+ assert_eq!(read(child, "padding-left", None, large), "100px");
+ assert_eq!(read(pseudo_target, "width", Some("before"), large), "100px");
+ assert_eq!(read(pseudo_target, "height", Some("before"), large), "80px");
+ assert_eq!(read(outer_target, "width", None, large), "200px");
+ assert_eq!(read(inner_target, "height", None, large), "120px");
+ let child_before = retained_primary_style_for_test(&engine, &host, child).unwrap();
+ let nested_before = retained_primary_style_for_test(&engine, &host, inner_target).unwrap();
+ let outer_flushes = engine
+ .retained_shadow_scope_flush_count_for_document_for_test(document, outer_root)
+ .expect("outer shadow scope should be retained");
+ let inner_flushes = engine
+ .retained_shadow_scope_flush_count_for_document_for_test(document, inner_root)
+ .expect("inner shadow scope should be retained");
+
+ let small = StyleViewport::new(Some(500.0), Some(400.0));
+ assert_eq!(read(parent, "font-size", None, small), "8px");
+ assert_eq!(
+ read(child, "font-size", None, small),
+ "16px",
+ "viewport invalidation must propagate through inherited font metrics",
+ );
+ assert_eq!(
+ read(child, "padding-left", None, small),
+ "50px",
+ "a viewport unit substituted through an inherited custom property must recascade",
+ );
+ assert_eq!(read(pseudo_target, "width", Some("before"), small), "50px");
+ assert_eq!(read(pseudo_target, "height", Some("before"), small), "40px");
+ assert_eq!(read(outer_target, "width", None, small), "100px");
+ assert_eq!(read(inner_target, "height", None, small), "60px");
+ let child_after = retained_primary_style_for_test(&engine, &host, child).unwrap();
+ let nested_after = retained_primary_style_for_test(&engine, &host, inner_target).unwrap();
+ assert!(!ServoArc::ptr_eq(&child_before, &child_after));
+ assert!(!ServoArc::ptr_eq(&nested_before, &nested_after));
+ assert_eq!(
+ engine.retained_shadow_scope_flush_count_for_document_for_test(document, outer_root),
+ Some(outer_flushes),
+ "viewport dependency invalidation must not rebuild the outer AuthorStyles",
+ );
+ assert_eq!(
+ engine.retained_shadow_scope_flush_count_for_document_for_test(document, inner_root),
+ Some(inner_flushes),
+ "viewport dependency invalidation must not rebuild nested AuthorStyles",
+ );
+}
+
#[test]
fn device_changes_keep_media_sheets_installed_and_flush_only_affected_tree_scopes() {
reset_source_cascade_rebuild_count_for_test();