Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions moli-renderer-v8/src/runtime/page_vm/tests/computed_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("<div id=s style=\"width:2rem;height:1px;font-size:2rem\"></div>")?;
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");
}
37 changes: 34 additions & 3 deletions moli-renderer-v8/src/style_engine/computed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use style::{
SharedStyleContext, StyleContext, StyleSystemOptions, ThreadLocalStyleContext,
},
data::ElementStyles,
device::Device,
dom::{TElement, TNode},
properties::{
ComputedValues, PropertyId,
Expand Down Expand Up @@ -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::{
Expand Down Expand Up @@ -1000,10 +1002,21 @@ fn clear_element_styles_for_resolution<E: TElement>(world: &DocumentStyleWorld,

fn publish_resolved_element_styles<E: TElement>(
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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions moli-renderer-v8/src/style_engine/retained.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<ComputedValues>,
) {
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,
Expand Down
5 changes: 5 additions & 0 deletions moli-renderer-v8/src/style_engine/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand All @@ -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<Option<ServoArc<ComputedValues>>>,
pub(super) document_stylesheets: ActiveStylesheetCollection,
pub(super) shadow_scopes: Vec<ShadowScopeStyles>,
pub(super) stylesheet_resources: StylesheetResourceManifest,
Expand Down
Loading