diff --git a/moli-renderer-v8/src/native_bridge/context_host/realtime_layout.rs b/moli-renderer-v8/src/native_bridge/context_host/realtime_layout.rs index 6087e5f615b..6f107c4f00b 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/realtime_layout.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/realtime_layout.rs @@ -289,21 +289,14 @@ impl PageScriptHost { .expect("Realtime policy installs its document state"); let inputs = self.screen_layout_inputs(state)?; let document = self.document_handle(); - if inputs.permits_reuse() && state.published.borrow().as_ref() == Some(&inputs) { - let reused = if inputs.documents[0].root.is_none() { - true - } else if state.scroll_projection_dirty.get() { - self.refresh_published_scroll_projection()? != ScrollProjectionRefresh::Unavailable - } else { - self.document_layout_state - .borrow() - .latest_layout(document) - .is_some() - }; - if reused { - state.scroll_projection_dirty.set(false); - return Ok(()); - } + if self.can_reuse_screen_layout(state, &inputs) + && (inputs.documents[0].root.is_none() + || !state.scroll_projection_dirty.get() + || self.refresh_published_scroll_projection()? + != ScrollProjectionRefresh::Unavailable) + { + state.scroll_projection_dirty.set(false); + return Ok(()); } // Native invalidation/geometry only: no input dispatch, callbacks, @@ -342,6 +335,35 @@ impl PageScriptHost { .is_some_and(|state| state.published.borrow().is_some()) } + pub(crate) fn realtime_screen_layout_is_current(&self) -> bool { + let Some(state) = &self.realtime_layout_state else { + return false; + }; + if !state.published.borrow().as_ref().is_some_and(|published| { + published.dom == self.dom_host().dom_version() + && Some(published.non_dom) == state.non_dom_revision.get() + }) { + return false; + } + self.screen_layout_inputs(state) + .is_ok_and(|inputs| self.can_reuse_screen_layout(state, &inputs)) + } + + fn can_reuse_screen_layout( + &self, + state: &RealtimeLayoutState, + inputs: &ScreenLayoutInputs, + ) -> bool { + inputs.permits_reuse() + && state.published.borrow().as_ref() == Some(inputs) + && (inputs.documents[0].root.is_none() + || self + .document_layout_state + .borrow() + .latest_layout(self.document_handle()) + .is_some()) + } + fn prepare_current_screen_styles(&self, inputs: &ScreenLayoutInputs) { for document in &inputs.documents { self.drain_pending_style_invalidations_for_computed_style_read_for_document( diff --git a/moli-renderer-v8/src/native_bridge/element/styles.rs b/moli-renderer-v8/src/native_bridge/element/styles.rs index ba4278a563a..69219e4bb91 100644 --- a/moli-renderer-v8/src/native_bridge/element/styles.rs +++ b/moli-renderer-v8/src/native_bridge/element/styles.rs @@ -672,7 +672,7 @@ pub(crate) fn computed_style_properties_for_inspector_handle( } // The complete computed declaration includes used dimensions. Establish // geometry before constructing its viewport-specific style context. - public_layout::prepare_public_style_property(host, handle, StyleMode::Computed, "width")?; + public_layout::prepare_public_style_property(host, handle, StyleMode::Computed, "width", None)?; let Some(DocumentSnapshotStyleComputation::Available { context }) = style_computation_context_for_document_snapshot(host, handle) else { diff --git a/moli-renderer-v8/src/native_bridge/element/styles/public_layout.rs b/moli-renderer-v8/src/native_bridge/element/styles/public_layout.rs index de4865ff3ba..5dfd56178a2 100644 --- a/moli-renderer-v8/src/native_bridge/element/styles/public_layout.rs +++ b/moli-renderer-v8/src/native_bridge/element/styles/public_layout.rs @@ -1,8 +1,8 @@ use moli_layout::LayoutError; use super::declaration::{ - property_uses_container_geometry, style_property_value_for_pseudo_with_context, - style_property_value_with_context, + StyleObservation, property_uses_container_geometry, + style_property_value_for_pseudo_with_context, style_property_value_with_context, }; use super::{ DomHandle, PageScriptHost, StyleMode, style_object_computation_context, @@ -17,6 +17,7 @@ pub(super) fn prepare_public_style_property( handle: DomHandle, mode: StyleMode, property: &str, + pseudo: Option<&str>, ) -> Result<(), LayoutError> { if mode == StyleMode::Computed && runtime.layout_policy().is_realtime() @@ -25,6 +26,7 @@ pub(super) fn prepare_public_style_property( handle, || property_may_observe_used_geometry(property), [property], + pseudo, ) { runtime.ensure_observable_layout()?; @@ -37,6 +39,7 @@ fn style_read_needs_layout<'a>( handle: DomHandle, observes_used_geometry: impl FnOnce() -> bool, properties: impl IntoIterator, + pseudo: Option<&str>, ) -> bool { runtime.dom_host().is_connected_to_document(handle) && runtime.owner_dispatch_scope_for_node(handle).is_some() @@ -50,7 +53,25 @@ fn style_read_needs_layout<'a>( }) || properties .into_iter() - .any(|property| property_uses_container_geometry(runtime, handle, property))) + .any(|property| property_uses_container_geometry(runtime, handle, property)) + || size_container_query_needs_layout(runtime, handle, pseudo)) +} + +fn size_container_query_needs_layout( + runtime: &PageScriptHost, + handle: DomHandle, + pseudo: Option<&str>, +) -> bool { + if runtime.realtime_screen_layout_is_current() { + return false; + } + runtime + .layout_document_for_source(handle) + .is_some_and(|document| { + StyleObservation::new(runtime).with_resolution_pass(document, |pass| { + pass.depends_on_size_container_query(handle, pseudo) + }) + }) } /// Deliberately conservative for used-value families. A pure root-document @@ -97,6 +118,7 @@ pub(super) fn prepare_public_style_batch( handle, || used_geometry, properties.iter().map(String::as_str), + None, ) }); if needs_layout { @@ -113,7 +135,10 @@ pub(super) fn read_live_style_property<'s>( mode: StyleMode, property: &str, ) -> Option { - if let Err(error) = prepare_public_style_property(runtime, handle, mode, property) { + let pseudo = style_object_pseudo_element(scope, style, mode); + if let Err(error) = + prepare_public_style_property(runtime, handle, mode, property, pseudo.as_deref()) + { if let Some(message) = v8_string(scope, &format!("Layout failed: {error}")) { let exception = v8::Exception::error(scope, message); scope.throw_exception(exception); @@ -122,7 +147,7 @@ pub(super) fn read_live_style_property<'s>( } // A screen layout pass can update the viewport of a child document. let context = style_object_computation_context(scope, style, runtime, handle, mode); - Some(match style_object_pseudo_element(scope, style, mode) { + Some(match pseudo { Some(pseudo) => style_property_value_for_pseudo_with_context( runtime, handle, &pseudo, property, context, ), diff --git a/moli-renderer-v8/src/script_vm/tests/realtime_cssom.rs b/moli-renderer-v8/src/script_vm/tests/realtime_cssom.rs index 2896ba33945..9fbbd49bda5 100644 --- a/moli-renderer-v8/src/script_vm/tests/realtime_cssom.rs +++ b/moli-renderer-v8/src/script_vm/tests/realtime_cssom.rs @@ -53,6 +53,38 @@ fn realtime_size_container_queries_follow_nested_content_boxes() { ); } +#[test] +fn realtime_cssom_size_container_query_refreshes_retained_color_and_display() { + let mut vm = realtime_vm( + r#"
+
"#, + ); + assert_eq!( + vm.eval("const held = getComputedStyle(target); box.offsetWidth") + .unwrap(), + "200" + ); + let builds = vm.layout_pass_observability_for_test().1; + assert_eq!( + vm.eval("box.style.width = '400px'; getComputedStyle(unrelated).color") + .unwrap(), + "rgb(0, 0, 255)" + ); + assert_eq!(vm.layout_pass_observability_for_test().1, builds); + assert_eq!(vm.eval("held.color").unwrap(), "rgb(0, 128, 0)"); + assert_eq!(vm.layout_pass_observability_for_test().1, builds + 1); + assert_eq!( + vm.eval("box.style.width = '200px'; held.getPropertyValue('display')") + .unwrap(), + "block" + ); + assert_eq!(vm.layout_pass_observability_for_test().1, builds + 2); +} + #[test] fn on_demand_cssom_size_container_query_keeps_live_inline_sizes() { let mut vm = new_parsed_test_vm( diff --git a/moli-renderer-v8/src/script_vm/tests/realtime_publication.rs b/moli-renderer-v8/src/script_vm/tests/realtime_publication.rs index e73ef5c79b4..a204eba4c5f 100644 --- a/moli-renderer-v8/src/script_vm/tests/realtime_publication.rs +++ b/moli-renderer-v8/src/script_vm/tests/realtime_publication.rs @@ -60,6 +60,26 @@ fn realtime_publication_temporary_viewport_preserves_screen_and_child_geometry() assert_eq!(vm.layout_pass_observability_for_test().1, before + 1); } +#[test] +fn realtime_publication_temporary_viewport_preserves_screen_container_query_sizes() { + let (mut vm, viewport) = fixture( + r#"
"#, + ); + assert!(viewport.css_width >= 200); + let color = "getComputedStyle(target).color"; + assert_eq!(vm.eval(color).unwrap(), "rgb(0, 128, 0)"); + let builds = vm.layout_pass_observability_for_test().1; + vm.screenshot_layout_snapshot(LayoutViewport::new(100, 120, 1.0)) + .unwrap() + .unwrap(); + assert_eq!(vm.eval(color).unwrap(), "rgb(0, 128, 0)"); + assert_eq!(vm.layout_pass_observability_for_test().1, builds + 1); +} + #[test] fn realtime_publication_transient_intent_does_not_publish_even_with_screenshot_reason() { let (mut vm, viewport) = fixture("
"); diff --git a/moli-renderer-v8/src/style_engine/computed/resolution_pass.rs b/moli-renderer-v8/src/style_engine/computed/resolution_pass.rs index 2602062c672..f87489142fd 100644 --- a/moli-renderer-v8/src/style_engine/computed/resolution_pass.rs +++ b/moli-renderer-v8/src/style_engine/computed/resolution_pass.rs @@ -1,16 +1,21 @@ use std::collections::HashMap; use moli_selector::{StyloDomHostBinding, StyloElement}; +use selectors::matching::{ + MatchingContext, MatchingForInvalidation, MatchingMode, NeedsSelectorFlags, VisitedHandlingMode, +}; use style::{ Atom, animation::DocumentAnimationSet, + applicable_declarations::ApplicableDeclarationList, + computed_value_flags::ComputedValueFlags, context::{ QuirksMode, RegisteredSpeculativePainter, RegisteredSpeculativePainters, SharedStyleContext, StyleContext, StyleSystemOptions, ThreadLocalStyleContext, }, dom::{TElement, TNode}, properties::ComputedValues, - selector_parser::{PseudoElement, SnapshotMap}, + selector_parser::{PseudoElement, SelectorImpl, SnapshotMap}, servo_arc::Arc as ServoArc, shared_lock::StylesheetGuards, style_resolver::{PseudoElementResolution, StyleResolverForElement}, @@ -83,6 +88,42 @@ impl<'dom> ResolutionContext<'_, 'dom> { None, ) } + + fn matches_size_container_query( + &mut self, + element: StyloElement<'dom>, + pseudo: Option<(&PseudoElement, &ComputedValues)>, + ) -> bool { + self.adapter.observes_query_container_size(|| { + self.with_stylo(|context| { + let mut declarations = ApplicableDeclarationList::new(); + let mut matching = MatchingContext::::new_for_visited( + if pseudo.is_some() { + MatchingMode::ForStatelessPseudoElement + } else { + MatchingMode::Normal + }, + None, + &mut context.thread_local.selector_caches, + VisitedHandlingMode::AllLinksUnvisited, + context.shared.quirks_mode(), + NeedsSelectorFlags::No, + MatchingForInvalidation::No, + ); + matching.extra_data.originating_element_style = pseudo.map(|(_, style)| style); + context.shared.stylist.push_applicable_declarations( + element, + pseudo.map(|(pseudo, _)| pseudo), + None, + None, + Default::default(), + RuleInclusion::All, + &mut declarations, + &mut matching, + ); + }); + }) + } } impl Drop for ResolutionContext<'_, '_> { @@ -188,6 +229,49 @@ impl<'dom> StyleResolutionPass<'_, 'dom> { )) } + /// Check matching conditions, including currently false conditions. A + /// declaration using only cqw units is handled by the property read path + /// and must not make an unrelated color read flush layout. + pub(crate) fn depends_on_size_container_query( + &mut self, + handle: DomHandle, + pseudo: Option<&str>, + ) -> bool { + let Some(element) = self.prepare(handle) else { + return false; + }; + let pseudo = pseudo.and_then(stylo_pseudo_element_for_computed_style); + let mut current = Some(element); + while let Some(candidate) = current { + let Some(primary) = candidate + .borrow_data() + .and_then(|data| data.styles.get_primary().cloned()) + else { + break; + }; + if !primary + .flags + .contains(ComputedValueFlags::SELF_OR_ANCESTOR_HAS_SIZE_CONTAINER_TYPE) + { + break; + } + if candidate == element + && let Some(pseudo) = pseudo.as_ref() + && self + .context + .matches_size_container_query(candidate, Some((pseudo, &primary))) + { + return true; + } + // A matching condition on an ancestor can change inherited values. + if self.context.matches_size_container_query(candidate, None) { + return true; + } + current = candidate.traversal_parent(); + } + false + } + #[cfg(feature = "devtools")] pub(crate) fn primary_style(&mut self, handle: DomHandle) -> Option> { let element = self.prepare(handle)?; diff --git a/moli-selector/src/stylo/style_traversal.rs b/moli-selector/src/stylo/style_traversal.rs index aea4b205f82..8f099bf4280 100644 --- a/moli-selector/src/stylo/style_traversal.rs +++ b/moli-selector/src/stylo/style_traversal.rs @@ -105,6 +105,7 @@ struct StyleDomState { shared_lock: SharedRwLock, element_data: StyloElementDataStore, query_container_sizes: RefCell>, + observed_query_container_size: Cell>, atom_cache: QueryAtomCache, node_data: RefCell>>, slotted_node_data: RefCell]>>>, @@ -155,6 +156,7 @@ impl StyloDomStyleAdapter { ..StyloElementDataStore::default() }, query_container_sizes: RefCell::default(), + observed_query_container_size: Cell::new(None), atom_cache: QueryAtomCache::default(), node_data: RefCell::default(), slotted_node_data: RefCell::default(), @@ -448,6 +450,15 @@ impl<'binding> StyleDomHostBinding<'binding> { &self.state.shared_lock } + /// Records container size lookups during a selector-only matching pass. + pub fn observes_query_container_size(&self, match_rules: impl FnOnce()) -> bool { + let observed = &self.state.observed_query_container_size; + debug_assert!(observed.get().is_none()); + observed.set(Some(false)); + match_rules(); + observed.replace(None).unwrap_or(false) + } + pub fn bound_node_count(&self) -> usize { self.state.node_data.borrow().len() } @@ -515,6 +526,7 @@ impl<'binding> StyleDomHostBinding<'binding> { impl Drop for StyleDomHostBinding<'_> { fn drop(&mut self) { + self.state.observed_query_container_size.set(None); self.state.clear_node_data(); self.state.clear_shadow_cascade_data_after_binding(); self.state.host.set(std::ptr::null()); @@ -2251,6 +2263,16 @@ impl<'a> TElement for StyleElement<'a> { } fn query_container_size(&self, display: &Display) -> Size2D> { + let observed = &self.style_state().observed_query_container_size; + if observed.get() == Some(false) + && self.borrow_data().is_some_and(|data| { + data.styles + .get_primary() + .is_some_and(|style| style.clone_container_type().is_size_container_type()) + }) + { + observed.set(Some(true)); + } if display.is_none() || display.is_contents() { return Size2D::new(None, None); }