Skip to content

Truly independent web browser

LibWeb: Let size containers settle while their font-size transitions

sideshowbarker0e835f6ac2f4
diff --git a/Libraries/LibWeb/Rust/src/css/style/inputs.rs b/Libraries/LibWeb/Rust/src/css/style/inputs.rs
index 4aed3b98015..dc491a78bf6 100644
--- a/Libraries/LibWeb/Rust/src/css/style/inputs.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/inputs.rs
@@ -1248,18 +1248,11 @@ impl RetainedState {
) {
let style_record = record.as_deref().map_or(0, bridge::host_record_identity);
let previous = self.held_style_records.set(node, record);
- // NB: Font-relative size queries can change even when the container's box keeps the same dimensions.
- let font_group = crate::css::table_group_builder::group_index::FONT;
- let font_inputs_changed = self.size_container_queries.is_queried_container(node)
- && previous.is_some_and(|previous| {
- self.held_style_records.get(node).is_some_and(|current| {
- previous.payloads[font_group].as_ptr() != current.payloads[font_group].as_ptr()
- })
- });
- self.set_element_container_query_inputs(node, style_record);
- if font_inputs_changed {
- self.size_container_queries.note_font_input_change(node);
+ if let (Some(previous), Some(current)) = (previous, self.held_style_records.get(node)) {
+ self.size_container_queries
+ .note_held_record_change(node, previous, current);
}
+ self.set_element_container_query_inputs(node, style_record);
// A `rem` the host resolves reads the font of the record it holds for the document element,
// which it can install in the middle of a style update.
if self.projection.adjustment_facts(node) & bridge::element_adjustment_fact::IS_DOCUMENT_ELEMENT != 0 {
diff --git a/Libraries/LibWeb/Rust/src/css/style/size_container_invalidation.rs b/Libraries/LibWeb/Rust/src/css/style/size_container_invalidation.rs
index 8d07c1b05af..fe9b8f0f9fa 100644
--- a/Libraries/LibWeb/Rust/src/css/style/size_container_invalidation.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/size_container_invalidation.rs
@@ -8,8 +8,11 @@
use super::bridge::FfiContainerEffectKind;
use super::container_queries::ContainerVerdict;
-use super::fast_hash::FastSet as HashSet;
+use super::fast_hash::{FastMap as HashMap, FastSet as HashSet};
+use super::pipeline::Record;
use super::{RetainedState, StyleEngine, StyleNodeID};
+use std::collections::hash_map::Entry;
+use std::sync::Arc;
/// What the host learned about size container queries while it computed styles: which elements
/// were asked about, which elements asked, and which containers had no box to answer with yet.
@@ -24,6 +27,10 @@ pub(super) struct SizeContainerQueryFacts {
/// Containers a style computation asked about before they had a committed box. The layout
/// that gives them one is where their dependents move.
needing_evaluation_after_layout: HashSet<StyleNodeID>,
+ /// Queried containers whose font moved since their dependents were last recorded, each with the record it held
+ /// before the move. An animated container passes through its base record within every update, on its way to the
+ /// record with its animated values, so only a font that differs from the one before it counts.
+ font_moves: HashMap<StyleNodeID, Arc<Record>>,
/// A container's eligibility changed, so units can newly select it even when it was not queried before.
availability_changed: HashSet<StyleNodeID>,
/// The size containers whose records say they answer size queries, as the records they were last published
@@ -38,8 +45,23 @@ impl SizeContainerQueryFacts {
self.queried_containers.contains(&node)
}
- pub(super) fn note_font_input_change(&mut self, node: StyleNodeID) {
- self.needing_evaluation_after_layout.insert(node);
+ /// Font-relative size queries can change even when the container's box keeps the same dimensions.
+ pub(super) fn note_held_record_change(&mut self, node: StyleNodeID, previous: Arc<Record>, current: &Record) {
+ if !self.is_queried_container(node) {
+ return;
+ }
+ match self.font_moves.entry(node) {
+ Entry::Occupied(entry) => {
+ if same_font(entry.get(), current) {
+ entry.remove();
+ }
+ }
+ Entry::Vacant(entry) => {
+ if !same_font(&previous, current) {
+ entry.insert(previous);
+ }
+ }
+ }
}
/// Follows whether `node` answers size queries, noting where that changed.
@@ -60,11 +82,22 @@ impl SizeContainerQueryFacts {
self.queried_containers.remove(&node);
self.dependents.remove(&node);
self.needing_evaluation_after_layout.remove(&node);
+ self.font_moves.remove(&node);
self.availability_changed.remove(&node);
self.available.remove(&node);
}
}
+fn same_font(first: &Record, second: &Record) -> bool {
+ let font_group = crate::css::table_group_builder::group_index::FONT;
+ let (first, second) = (
+ first.payloads[font_group].as_ptr(),
+ second.payloads[font_group].as_ptr(),
+ );
+ // SAFETY: Both records hold a reference to their font payloads.
+ first == second || unsafe { crate::css::computed_values::style_group_payloads_equal(font_group, first, second) }
+}
+
fn set_contains(set: &mut HashSet<StyleNodeID>, node: StyleNodeID, contains: bool) {
if contains {
set.insert(node);
@@ -101,6 +134,7 @@ impl StyleEngine {
.size_container_queries
.needing_evaluation_after_layout
.is_empty()
+ || !self.retained.size_container_queries.font_moves.is_empty()
|| !self.retained.size_container_queries.availability_changed.is_empty()
}
@@ -152,6 +186,7 @@ impl StyleEngine {
let mut containers = std::mem::take(&mut self.retained.size_container_queries.needing_evaluation_after_layout);
let availability_changed = std::mem::take(&mut self.retained.size_container_queries.availability_changed);
containers.extend(availability_changed.iter().copied());
+ containers.extend(std::mem::take(&mut self.retained.size_container_queries.font_moves).into_keys());
for container in containers {
// A container that has left the document has no dependents left to move.
if self.retained.projection.is_live(container) {
diff --git a/Tests/LibWeb/Text/expected/css/size-container-font-size-transition-settles.txt b/Tests/LibWeb/Text/expected/css/size-container-font-size-transition-settles.txt
new file mode 100644
index 00000000000..d602441fea8
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/size-container-font-size-transition-settles.txt
@@ -0,0 +1 @@
+item height after resize: 574
diff --git a/Tests/LibWeb/Text/input/css/size-container-font-size-transition-settles.html b/Tests/LibWeb/Text/input/css/size-container-font-size-transition-settles.html
new file mode 100644
index 00000000000..c4cf169166e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/size-container-font-size-transition-settles.html
@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<iframe id="frame" style="width: 900px; height: 600px; border: 0"></iframe>
+<script>
+ // A font-size transition on size containers, which a resize starts, must not keep the style and layout update from
+ // settling.
+ promiseTest(async () => {
+ const frame = document.getElementById("frame");
+ frame.srcdoc = `<!doctype html>
+<style>
+ body { font-size: 18px; }
+ @media (max-width: 800px) { body { font-size: 16px; } }
+ .wrapper { container-type: inline-size; }
+ ul { --min: max(200px, calc(33.33cqw - 0.67em)); display: grid; grid-template-columns: repeat(auto-fill, minmax(var(--min), 1fr)); gap: 1em; list-style: none; margin: 0; padding: 0; }
+ li { container-type: inline-size; display: flex; position: relative; background: #9cf; transition: font-size 0.5s; }
+ li::before { content: ""; display: block; width: 1px; margin-left: -1px; min-height: var(--min); padding-top: 100%; }
+</style>
+<div class="wrapper"><ul><li></li><li></li><li></li><li></li><li></li><li></li></ul></div>`;
+ await new Promise(resolve => frame.addEventListener("load", resolve, { once: true }));
+ await animationFrame();
+ frame.style.width = "390px";
+ await new Promise(resolve => setTimeout(resolve, 1000));
+ println(`item height after resize: ${Math.round(frame.contentDocument.querySelector("li").getBoundingClientRect().height)}`);
+ });
+</script>