diff --git a/moli-renderer-v8/src/runtime/page_vm/tests/computed_size.rs b/moli-renderer-v8/src/runtime/page_vm/tests/computed_size.rs index 88fe49b57e1..3fb8a65b055 100644 --- a/moli-renderer-v8/src/runtime/page_vm/tests/computed_size.rs +++ b/moli-renderer-v8/src/runtime/page_vm/tests/computed_size.rs @@ -367,3 +367,30 @@ globalThis.movedStyle = freshFrame.contentWindow.getComputedStyle(moved); .await .expect("computed size should use only the owning Document's existing snapshot"); } + +#[tokio::test(flavor = "current_thread")] +async fn computed_size_rem_lengths_follow_root_font_size_across_device_rebuilds() { + run_page_vm_async_test(async move { + let mut page_vm = + page_with_size_fixture("
")?; + page_vm.vm_mut().eval("document.documentElement.style.fontSize='10px';'set'")?; + let font_size = "id=>getComputedStyle(document.getElementById(id)).fontSize"; + assert_eq!(read_sizes(&mut page_vm, &["s"])?, json!({"s":["20px","1px"]})); + publish_size_layout(&mut page_vm)?; + assert_eq!(page_vm.vm_mut().eval("String(s.getBoundingClientRect().width)")?, "20"); + + page_vm.vm_mut().eval("document.documentElement.style.fontSize='20px';'set'")?; + assert_eq!(page_vm.vm_mut().eval(&format!("({font_size})('s')"))?, "40px"); + + // A viewport change rebuilds the Stylo Device; elements resolved + // afterwards must still see the current root font size. + publish_size_layout_at(&mut page_vm, 400, 300)?; + page_vm.vm_mut().eval( + "const t=document.createElement('div');t.id='t';t.style.fontSize='3rem';document.body.appendChild(t);'added'", + )?; + assert_eq!(page_vm.vm_mut().eval(&format!("({font_size})('t')"))?, "60px"); + Ok::<_, anyhow::Error>(()) + }) + .await + .expect("rem lengths should resolve against the root element font size"); +} diff --git a/moli-renderer-v8/src/style_engine/computed.rs b/moli-renderer-v8/src/style_engine/computed.rs index b26bcdb0605..494f0136a9f 100644 --- a/moli-renderer-v8/src/style_engine/computed.rs +++ b/moli-renderer-v8/src/style_engine/computed.rs @@ -10,6 +10,7 @@ use style::{ SharedStyleContext, StyleContext, StyleSystemOptions, ThreadLocalStyleContext, }, data::ElementStyles, + device::Device, dom::{TElement, TNode}, properties::{ ComputedValues, PropertyId, @@ -51,6 +52,7 @@ use super::{ cache::ComputedElementStyleCacheKey, document_world::DocumentStyleWorld, lazy_invalidation::StyleValidationPathEntry, + retained::synchronize_root_style_device, source_lifecycle::StyleSourceDocumentContext, world_key::StyleWorldKey, world_lifecycle::{ @@ -1000,10 +1002,21 @@ fn clear_element_styles_for_resolution(world: &DocumentStyleWorld, fn publish_resolved_element_styles( world: &DocumentStyleWorld, + device: &Device, element: E, styles: ElementStyles, required_generation: u64, ) { + if let Some(root_style) = styles.primary.as_ref().filter(|style| { + style + .flags + .contains(ComputedValueFlags::IS_ROOT_ELEMENT_STYLE) + }) { + synchronize_root_style_device(device, root_style); + world.document_state.with_retained_style_system(|retained| { + *retained.root_style.borrow_mut() = Some(root_style.clone()); + }); + } unsafe { let mut data = element.ensure_data(); data.styles = styles; @@ -1053,7 +1066,13 @@ fn resolve_element_styles<'dom>( .lazy_invalidation_roots .required_generation_for_checked_element(target) .unwrap_or_default(); - publish_resolved_element_styles(world, element, styles.clone(), required_generation); + publish_resolved_element_styles( + world, + context.shared.stylist.device(), + element, + styles.clone(), + required_generation, + ); world .document_state .note_element_style_resolutions(ancestor_resolution_count.saturating_add(1)); @@ -1139,7 +1158,13 @@ pub(super) fn prepare_style_observation_targets( .lazy_invalidation_roots .required_generation_for_checked_element(handle) .unwrap_or_default(); - publish_resolved_element_styles(&world, element, styles, generation); + publish_resolved_element_styles( + &world, + context.shared.stylist.device(), + element, + styles, + generation, + ); inherited_change = true; resolutions += 1; } @@ -1198,7 +1223,13 @@ where clear_element_styles_for_resolution(world, ancestor); let styles = resolve_style(context, ancestor, RuleInclusion::All, None, None); resolution_count = resolution_count.saturating_add(1); - publish_resolved_element_styles(world, ancestor, styles, required_generation); + publish_resolved_element_styles( + world, + context.shared.stylist.device(), + ancestor, + styles, + required_generation, + ); force_descendants = true; } resolution_count diff --git a/moli-renderer-v8/src/style_engine/retained.rs b/moli-renderer-v8/src/style_engine/retained.rs index 51fd1b51c83..202e10e13c1 100644 --- a/moli-renderer-v8/src/style_engine/retained.rs +++ b/moli-renderer-v8/src/style_engine/retained.rs @@ -2,7 +2,9 @@ use std::collections::HashSet; use std::sync::atomic::{AtomicU64, Ordering}; use style::{ author_styles::AuthorStyles, + device::Device, invalidation::stylesheets::StylesheetInvalidationSet, + properties::ComputedValues, servo_arc::Arc as ServoArc, shared_lock::{SharedRwLock, StylesheetGuards}, stylesheets::{CustomMediaMap, DocumentStyleSheet, Origin, OriginSet, UrlExtraData}, @@ -165,6 +167,7 @@ pub(super) fn build_retained_style_system( stylist_identity: NEXT_STYLIST_IDENTITY.fetch_add(1, Ordering::Relaxed), key, stylist, + root_style: Default::default(), document_stylesheets, shadow_scopes, stylesheet_resources, @@ -383,6 +386,9 @@ fn update_document_scope( if device_changed { let device = new_style_device_with_viewport(key.style_viewport(), key.environment, key.quirks_mode); + if let Some(root_style) = retained.root_style.borrow().as_ref() { + synchronize_root_style_device(&device, root_style); + } let guard = shared_lock.read(); let guards = StylesheetGuards::same(&guard); device_affected_origins = retained.stylist.set_device(device, &guards); @@ -437,6 +443,26 @@ fn update_document_scope( } } +/// Applies the root element style to the Device state behind `rem`, `rlh` +/// and other root-relative units. Stylo's full traversal does this in +/// `matching.rs`; Moli's single-node `resolve_style` path must do it itself. +pub(super) fn synchronize_root_style_device( + device: &Device, + root_style: &ServoArc, +) { + device.set_root_style(root_style); + let font = root_style.get_font(); + let font_size = font.clone_font_size().computed_size(); + device.set_root_font_size(root_style.effective_zoom.unzoom(font_size.px())); + let line_height = device + .calc_line_height(font, root_style.writing_mode, None) + .0; + device.set_root_line_height(root_style.effective_zoom.unzoom(line_height.px())); + if device.used_root_font_metrics() { + device.update_root_font_metrics(); + } +} + fn refresh_retained_derived_state( retained: &mut RetainedStyleSystem, device_changed: bool, diff --git a/moli-renderer-v8/src/style_engine/state.rs b/moli-renderer-v8/src/style_engine/state.rs index 9c657afffd2..da810b5b543 100644 --- a/moli-renderer-v8/src/style_engine/state.rs +++ b/moli-renderer-v8/src/style_engine/state.rs @@ -6,6 +6,7 @@ use std::{ use moli_selector::StyloSourceInvalidationFallbackReason; use selectors::context::SelectorCaches; use style::{ + properties::ComputedValues, servo_arc::Arc as ServoArc, stylist::{CascadeData, Stylist}, }; @@ -31,6 +32,10 @@ pub(super) struct RetainedStyleSystem { pub(super) stylist_identity: u64, pub(super) key: StyleWorldKey, pub(super) stylist: Stylist, + /// Last resolved root element style. Moli resolves elements one at a time, + /// so it mirrors Stylo traversal's root-unit Device update and replays it + /// onto a rebuilt Device. + pub(super) root_style: RefCell>>, pub(super) document_stylesheets: ActiveStylesheetCollection, pub(super) shadow_scopes: Vec, pub(super) stylesheet_resources: StylesheetResourceManifest,