Skip to content

Truly independent web browser

LibWeb: Recascade monospace font sizes over animated ancestors

Andreas Klingaced801465d5
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/drive.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/drive.rs
index 7e1712c870e..e2910073155 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/drive.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/drive.rs
@@ -179,14 +179,36 @@ impl RetainedState {
};
let default_size = CssPixels::from_integer(13).raw_value();
let document_inputs = FontSizeRecascadeDocumentInputs::from_document(inputs);
+ // NB: The size an ancestor's animations give it wins over what the cascade of its ancestors decides, so the
+ // recascade goes on from that ancestor's computed size.
+ let animated = views.iter().enumerate().rev().find_map(|(index, (_, view))| {
+ let view = view.as_ref()?;
+ view.longhands()
+ .overlay_value(
+ view.animated_overlay,
+ crate::css::property_metadata::property_id::FONT_SIZE,
+ )
+ .map(|_| (index, view))
+ });
+ let (start_index, start_size_raw, start_depends_on_viewport) =
+ animated.map_or((0, default_size, false), |(index, view)| {
+ (
+ index + 1,
+ crate::css::computed_value_views::ComputedValuesView::of(view.payloads)
+ .font()
+ .font_size
+ .raw_value(),
+ view.dependency_flags & FONT_METRICS_DEPEND_ON_VIEWPORT_METRICS != 0,
+ )
+ });
let mut batch = recascade_font_size_batch(
views.len(),
value_at,
|index| zooms[index],
applies_zoom_at,
- 0,
- default_size,
- false,
+ start_index,
+ start_size_raw,
+ start_depends_on_viewport,
default_size,
document_inputs,
None,
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/live.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/live.rs
index 51655e616c8..4ea645e0886 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/live.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/live.rs
@@ -726,6 +726,36 @@ impl Live {
}
}
+ /// Restyle, in the next pass, the elements and pseudo-elements below `node` whose `font-family: monospace` recascades
+ /// their font size, which goes on from the computed size of the nearest ancestor whose animations give it one, where
+ /// such animations of `node` came or went. What they inherit need not have moved.
+ fn note_animated_font_size_came_or_went(&mut self, state: &RetainedState, node: StyleNodeID) {
+ let is_monospace = |record: &Record| {
+ record
+ .longhand(crate::css::computed_values::LonghandID::new(
+ crate::css::property_metadata::property_id::FONT_FAMILY,
+ ))
+ .optional_data()
+ .is_some_and(crate::css::style_compute::font_family_is_monospace)
+ };
+ self.hints.reserve(state.projection.element_slot_count());
+ for descendant in state.projection.style_preorder(node).skip(1) {
+ let Some(record) = self.records.get(descendant) else {
+ continue;
+ };
+ if is_monospace(record) {
+ self.hints.mark(descendant, RESTYLE_SELF | PARENT_CHANGED);
+ } else if self
+ .records
+ .pseudo_element_records(descendant)
+ .iter()
+ .any(|(_, record)| is_monospace(record))
+ {
+ self.hints.mark(descendant, RESTYLE_PSEUDO_ELEMENTS);
+ }
+ }
+ }
+
/// Restyle, in the next pass, the elements whose records or pseudo-element records name a
/// counter-style registry their style scope no longer has. A record reading the counter-style
/// environment names the registry it was computed against, and the host minting a new one for
@@ -4170,6 +4200,7 @@ impl StyleEngine {
let display = |payloads: &crate::css::computed_values::StyleGroups| {
crate::css::computed_value_views::ComputedValuesView::of(payloads).display()
};
+ let font_size_animated = HeldComposition::animates_font_size(&view);
let impact =
match custom_properties == held.custom_properties && display(view.payloads) == display(&held.payloads) {
true => ChildImpact::of_groups(view.payloads, &held.payloads),
@@ -4178,6 +4209,9 @@ impl StyleEngine {
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);
+ }
self.retained.live_style = Some(live);
}
}
@@ -4191,7 +4225,7 @@ impl RetainedState {
.element_custom_property_data
.get(&node)
.and_then(|held| held.sampled_over.map(|_| held.identity));
- Some(HeldComposition::new(view.payloads, custom_properties))
+ Some(HeldComposition::new(&view, custom_properties))
}
/// Restyle the elements naming a counter-style registry their style scope no longer has, in
@@ -4390,15 +4424,27 @@ fn resolve_waits(
pub(in super::super) struct HeldComposition {
payloads: crate::css::computed_values::StyleGroups,
custom_properties: Option<u64>,
+ /// Whether its animations gave it a font size.
+ font_size_animated: bool,
}
impl HeldComposition {
- fn new(payloads: &crate::css::computed_values::StyleGroups, custom_properties: Option<u64>) -> Self {
+ fn new(view: &computed::StyleRecordView<'_>, custom_properties: Option<u64>) -> Self {
Self {
- payloads: payloads.clone(),
+ payloads: view.payloads.clone(),
custom_properties,
+ font_size_animated: Self::animates_font_size(view),
}
}
+
+ fn animates_font_size(view: &computed::StyleRecordView<'_>) -> bool {
+ view.longhands()
+ .overlay_value(
+ view.animated_overlay,
+ crate::css::property_metadata::property_id::FONT_SIZE,
+ )
+ .is_some()
+ }
}
impl RetainedState {
diff --git a/Tests/LibWeb/Text/expected/css/monospace-follows-animated-ancestor-font.txt b/Tests/LibWeb/Text/expected/css/monospace-follows-animated-ancestor-font.txt
index bc26f9ed5ed..d1efe9398a5 100644
--- a/Tests/LibWeb/Text/expected/css/monospace-follows-animated-ancestor-font.txt
+++ b/Tests/LibWeb/Text/expected/css/monospace-follows-animated-ancestor-font.txt
@@ -1,6 +1,7 @@
no animation: 20px, inheriting 20px, monospace 20px, below 20px
-running animation: 30px, inheriting 30px, monospace 20px, below 20px
+running animation: 30px, inheriting 30px, monospace 30px, below 30px
canceled animation: 20px, inheriting 20px, monospace 20px, below 20px
-transition at its start: 20px, inheriting 20px, monospace 40px, below 20px
-transition halfway: 30px, inheriting 30px, monospace 40px, below 40px
+transition at its start: 20px, inheriting 20px, monospace 20px, below 20px
+transition halfway: 30px, inheriting 30px, monospace 30px, below 30px
transition finished: 40px, inheriting 40px, monospace 40px, below 40px
+running animation under a zoom: 30px, inheriting 30px, monospace 30px, below 30px
diff --git a/Tests/LibWeb/Text/expected/css/monospace-follows-animation-at-ancestor-own-font-size.txt b/Tests/LibWeb/Text/expected/css/monospace-follows-animation-at-ancestor-own-font-size.txt
new file mode 100644
index 00000000000..cc246bcb5ab
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/monospace-follows-animation-at-ancestor-own-font-size.txt
@@ -0,0 +1,5 @@
+no animation: ancestor 16px, its ::before 13px, monospace 13px, ::before 13px, ::after 13px, new monospace 13px
+running animation: ancestor 16px, its ::before 16px, monospace 16px, ::before 16px, ::after 16px, new monospace 16px
+canceled animation: ancestor 16px, its ::before 13px, monospace 13px, ::before 13px, ::after 13px, new monospace 13px
+running again: ancestor 16px, its ::before 16px, monospace 16px, ::before 16px, ::after 16px, new monospace 16px
+finished animation: ancestor 16px, its ::before 13px, monospace 13px, ::before 13px, ::after 13px, new monospace 13px
diff --git a/Tests/LibWeb/Text/input/css/monospace-follows-animated-ancestor-font.html b/Tests/LibWeb/Text/input/css/monospace-follows-animated-ancestor-font.html
index 19d5b1238fb..8e48997aff1 100644
--- a/Tests/LibWeb/Text/input/css/monospace-follows-animated-ancestor-font.html
+++ b/Tests/LibWeb/Text/input/css/monospace-follows-animated-ancestor-font.html
@@ -50,5 +50,10 @@
dump("transition halfway");
transition.finish();
dump("transition finished");
+
+ ancestor.className = "";
+ ancestor.style.zoom = 2;
+ ancestor.classList.add("animated");
+ dump("running animation under a zoom");
});
</script>
diff --git a/Tests/LibWeb/Text/input/css/monospace-follows-animation-at-ancestor-own-font-size.html b/Tests/LibWeb/Text/input/css/monospace-follows-animation-at-ancestor-own-font-size.html
new file mode 100644
index 00000000000..2b62dcab3e4
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/monospace-follows-animation-at-ancestor-own-font-size.html
@@ -0,0 +1,52 @@
+<!DOCTYPE html>
+<style>
+ #ancestor {
+ font-size: medium;
+ }
+ .monospace {
+ font-family: monospace;
+ margin: 0;
+ }
+ #ancestor::before, #before::before, #after::after {
+ content: "x";
+ font-family: monospace;
+ }
+</style>
+<div id="ancestor">
+ <div><span class="monospace" id="target"></span><span id="before"></span><span id="after"></span></div>
+</div>
+<script src="../include.js"></script>
+<script>
+ // An animation of an ancestor's font size at the size the ancestor has already moves no value its descendants
+ // inherit, but a monospace font size goes on from an animated size where it would recascade otherwise.
+ asyncTest(async done => {
+ const nextFrame = () => new Promise(resolve => requestAnimationFrame(resolve));
+ const dump = label => {
+ const fresh = target.parentElement.appendChild(document.createElement("span"));
+ fresh.className = "monospace";
+ const size = (element, pseudo) => getComputedStyle(element, pseudo).fontSize;
+ println(`${label}: ancestor ${size(ancestor)}, its ::before ${size(ancestor, "::before")}, monospace ${size(target)}, ::before ${size(before, "::before")}, ::after ${size(after, "::after")}, new monospace ${size(fresh)}`);
+ fresh.remove();
+ };
+ await nextFrame();
+ await nextFrame();
+ dump("no animation");
+ const animation = ancestor.animate({ fontSize: ["16px", "16px"] }, { duration: 1000000 });
+ await nextFrame();
+ await nextFrame();
+ dump("running animation");
+ animation.cancel();
+ await nextFrame();
+ await nextFrame();
+ dump("canceled animation");
+ const finished = ancestor.animate({ fontSize: ["16px", "16px"] }, { duration: 1000000 });
+ await nextFrame();
+ await nextFrame();
+ dump("running again");
+ finished.finish();
+ await nextFrame();
+ await nextFrame();
+ dump("finished animation");
+ done();
+ });
+</script>