Skip to content

Truly independent web browser

LibWeb: Resolve rem and rlh against the animated root font

Andreas Klingc345bbd6d72f
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/live.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/live.rs
index 4ea645e0886..9a4d8c739f5 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/live.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/live.rs
@@ -4206,12 +4206,26 @@ impl StyleEngine {
true => ChildImpact::of_groups(view.payloads, &held.payloads),
false => ChildImpact::All,
};
+ // What resolved root-relative lengths against the font of the document element resolves them again, which the
+ // next pass tells as it restyles the element.
+ let root_font = super::compute::RootFontInputs::from_payloads(view.payloads, view.dependency_flags);
+ let root_font_moved = traversal::is_document_element(&self.retained, node)
+ && self
+ .retained
+ .live_style
+ .as_ref()
+ .and_then(|live| live.records.kept_root_font(node))
+ .is_some_and(|kept| kept != root_font);
if let Some(mut live) = self.retained.live_style.take() {
live.generation += 1;
live.note_composition_moved(&self.retained, node, impact);
if font_size_animated != held.font_size_animated {
live.note_animated_font_size_came_or_went(&self.retained, node);
}
+ if root_font_moved {
+ live.hints.reserve(self.retained.projection.element_slot_count());
+ live.hints.mark(node, RESTYLE_PSEUDO_ELEMENTS);
+ }
self.retained.live_style = Some(live);
}
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/records.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/records.rs
index 9c4bd5dfe5a..b365946bdc9 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/records.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/records.rs
@@ -1261,6 +1261,12 @@ impl RetainedRecords {
self.root_font = Some((node, root_font));
}
+ /// The root font inputs the last pass that styled the document element `node` resolved against, where one did.
+ pub(super) fn kept_root_font(&self, node: StyleNodeID) -> Option<super::compute::RootFontInputs> {
+ let (kept, root_font) = self.root_font?;
+ (kept == node).then_some(root_font)
+ }
+
/// Whether the style of `node`, its own or one of its pseudo-elements', derives from the
/// metrics of a first available font, or is not known not to, having no record.
pub(super) fn reads_font_metrics(&self, node: StyleNodeID) -> bool {
@@ -1636,8 +1642,7 @@ impl<'a> Records<'a> {
/// The root font inputs the last pass that styled the document element `node` resolved
/// against, where one did.
pub(super) fn kept_root_font(&self, node: StyleNodeID) -> Option<super::compute::RootFontInputs> {
- let (kept, root_font) = self.previous?.root_font?;
- (kept == node).then_some(root_font)
+ self.previous?.kept_root_font(node)
}
pub(super) fn new(state: &'a RetainedState, cells: &'a RecordCells, previous: Option<&'a RetainedRecords>) -> Self {
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/traversal.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/traversal.rs
index f8badfa93ea..afd9c3a8f98 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/traversal.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/traversal.rs
@@ -349,11 +349,18 @@ pub(super) fn is_document_element(state: &RetainedState, node: StyleNodeID) -> b
state.projection.adjustment_facts(node) & bridge::element_adjustment_fact::IS_DOCUMENT_ELEMENT != 0
}
-fn root_font_of(record: &Record) -> RootFontInputs {
- RootFontInputs::from_payloads(
- &record.payloads,
- super::super::computed::record_dependency_flags(&record.facts(), &record.payloads),
- )
+// https://drafts.csswg.org/css-values-4/#rem
+// Equal to the computed value of the em unit on the root element.
+// NB: The computed value holds what the root element's animations apply, which the composition of
+// them over its record has where the host composed one.
+fn root_font_of(records: &Records<'_>, node: StyleNodeID, record: &Record) -> RootFontInputs {
+ match records.composition(node) {
+ Some(view) => RootFontInputs::from_payloads(view.payloads, view.dependency_flags),
+ None => RootFontInputs::from_payloads(
+ &record.payloads,
+ super::super::computed::record_dependency_flags(&record.facts(), &record.payloads),
+ ),
+ }
}
/// The document element, where `root` is it or its parent.
@@ -379,7 +386,7 @@ pub(super) fn held_root_font(
Some(
records
.kept_root_font(document_element)
- .unwrap_or_else(|| root_font_of(record)),
+ .unwrap_or_else(|| root_font_of(records, document_element, record)),
)
}
@@ -410,7 +417,7 @@ impl PassContext<'_> {
let Some(record) = self.records.get(node) else {
return;
};
- let root_font = root_font_of(record);
+ let root_font = root_font_of(self.records, node, record);
let previous = self
.held_root_font
.unwrap_or_else(|| RootFontInputs::from_document(&self.state.document_style_computation_inputs));
diff --git a/Tests/LibWeb/Text/expected/css/rem-follows-animated-root-font.txt b/Tests/LibWeb/Text/expected/css/rem-follows-animated-root-font.txt
index f344974cd48..22f7b22f035 100644
--- a/Tests/LibWeb/Text/expected/css/rem-follows-animated-root-font.txt
+++ b/Tests/LibWeb/Text/expected/css/rem-follows-animated-root-font.txt
@@ -8,11 +8,11 @@ no animation:
running animation:
root font: 30px/40px
em, rem on the root itself: 20px, 20px
- rem, rlh: 200px, 25px, laid out 200
- rem, rlh on ::before: 40px, 50px
+ rem, rlh: 300px, 40px, laid out 300
+ rem, rlh on ::before: 60px, 80px
em below the root: 90px
- rem, em below a fixed font: 100px, 20px
- rem on an element added meanwhile: 20px
+ rem, em below a fixed font: 150px, 20px
+ rem on an element added meanwhile: 30px
canceled animation:
root font: 20px/25px
em, rem on the root itself: 20px, 20px
@@ -30,10 +30,10 @@ script animation halfway:
script animation further:
root font: 25px/25px
em, rem on the root itself: 20px, 20px
- rem, rlh: 200px, 25px, laid out 200
- rem, rlh on ::before: 40px, 50px
+ rem, rlh: 250px, 25px, laid out 250
+ rem, rlh on ::before: 50px, 50px
em below the root: 75px
- rem, em below a fixed font: 100px, 20px
+ rem, em below a fixed font: 125px, 20px
script animation canceled:
root font: 20px/25px
em, rem on the root itself: 20px, 20px
@@ -51,14 +51,14 @@ transition at its start:
transition halfway:
root font: 30px/40px
em, rem on the root itself: 40px, 40px
- rem, rlh: 400px, 55px, laid out 400
- rem, rlh on ::before: 80px, 110px
+ rem, rlh: 300px, 40px, laid out 300
+ rem, rlh on ::before: 60px, 80px
em below the root: 90px
- rem, em below a fixed font: 100px, 20px
+ rem, em below a fixed font: 150px, 20px
transition finished:
root font: 40px/55px
em, rem on the root itself: 40px, 40px
rem, rlh: 400px, 55px, laid out 400
rem, rlh on ::before: 80px, 110px
em below the root: 120px
- rem, em below a fixed font: 100px, 20px
+ rem, em below a fixed font: 200px, 20px