Truly independent web browser
LibWeb: Start container units at 0 before a container's first layout
Andreas Klinge5685afe57c0
diff --git a/Libraries/LibWeb/Rust/src/css/style/container_queries.rs b/Libraries/LibWeb/Rust/src/css/style/container_queries.rsindex 82728a53812..da7220c0be3 100644--- a/Libraries/LibWeb/Rust/src/css/style/container_queries.rs+++ b/Libraries/LibWeb/Rust/src/css/style/container_queries.rs@@ -54,7 +54,8 @@ impl ContainerVerdict {/// One physical axis' basis for a subject's container-relative lengths, as/// `Length::container_relative_length_to_px_without_rounding` finds it: the content size of the/// nearest flat-tree ancestor that accepts size queries on that axis and has a box as of the last-/// layout commit, and the viewport's size when there is no such container.+/// layout commit, and the viewport's size when there is no such container. A rendered container+/// that no layout has reached yet has a basis of 0 until one gives it a box.#[derive(Clone, Copy, Debug, PartialEq)]pub(crate) struct ContainerUnitBasis {basis: f64,@@ -302,9 +303,25 @@ impl RetainedState {continue;}let snapshot = self.layout_style_snapshot(node).unwrap_or_default();- // A container without a box, as one under an element that is not rendered has, is not eligible: Its size- // queries are unknown, so the units resolve against the next eligible container out.if !snapshot.has_committed_box {+ // A rendered container no layout has reached yet has no box only until the next one gives it one, and+ // its units are evaluated again against that box. Its size is meant not to follow its content, so the+ // units start out as small as that content can be. Starting from a larger basis can make content that+ // still moves the container's size, as content in a grid item with a preferred aspect ratio does+ // through its row, grow the container without ever settling.+ let is_not_rendered = self.size_container_queries.is_awaiting_box(node)+ || records.record_view(node, inputs).is_some_and(|view| {+ view.dependency_flags & crate::css::computed_longhand_table::IN_DISPLAY_NONE_SUBTREE != 0+ });+ if !is_not_rendered {+ return ContainerUnitBasis {+ basis: 0.0,+ container: Some(node),+ boxless_container: Some(node),+ };+ }+ // A container a layout left without a box, as one under an element that is not rendered has, is not+ // eligible: Its size queries are unknown, so the units resolve against the next eligible container out.boxless_container = Some(node);continue;}diff --git a/Libraries/LibWeb/Rust/src/css/style/size_container_invalidation.rs b/Libraries/LibWeb/Rust/src/css/style/size_container_invalidation.rsindex c73db8ae699..46484ff438f 100644--- a/Libraries/LibWeb/Rust/src/css/style/size_container_invalidation.rs+++ b/Libraries/LibWeb/Rust/src/css/style/size_container_invalidation.rs@@ -48,6 +48,11 @@ impl SizeContainerQueryFacts {self.queried_containers.contains(&node)}+ /// Whether a layout left `node` without the box a style computation asked it about.+ pub(super) fn is_awaiting_box(&self, node: StyleNodeID) -> bool {+ self.awaiting_box.contains(&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) {@@ -207,9 +212,10 @@ impl StyleEngine {}/// A layout gave the containers that had no box when they were asked about one; their- /// dependents are computed again against it. A container the layout left without a box has- /// nothing new to answer, so its dependents wait for the commit that gives it one; computing- /// them again against no box would only ask for this evaluation again, after every layout.+ /// dependents are computed again against it. A container the layout left without a box is+ /// computed against once more, as no longer eligible, and its dependents then wait for the+ /// commit that gives it one; computing them again after every layout would only ask for this+ /// evaluation again.pub fn evaluate_size_containers_needing_evaluation_after_layout(&mut self) {let mut containers = std::mem::take(&mut self.retained.size_container_queries.needing_evaluation_after_layout);containers.retain(|&container| {@@ -217,10 +223,9 @@ impl StyleEngine {.retained.layout_style_snapshot(container).is_some_and(|row| row.has_committed_box);- if !has_box {- self.retained.size_container_queries.awaiting_box.insert(container);- }- has_box+ // The units below a container the layout left without a box started out against no box at all. They are+ // computed again once, now resolving past it, and then wait for a commit that gives it a box.+ has_box || self.retained.size_container_queries.awaiting_box.insert(container)});let availability_changed = std::mem::take(&mut self.retained.size_container_queries.availability_changed);containers.extend(availability_changed.iter().copied());diff --git a/Tests/LibWeb/Text/expected/css/size-container-units-start-before-first-layout.txt b/Tests/LibWeb/Text/expected/css/size-container-units-start-before-first-layout.txtnew file mode 100644index 00000000000..efa52b95ca1--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/size-container-units-start-before-first-layout.txt@@ -0,0 +1,2 @@+item: 200x237+heading font-size: 48.98pxdiff --git a/Tests/LibWeb/Text/input/css/size-container-units-start-before-first-layout.html b/Tests/LibWeb/Text/input/css/size-container-units-start-before-first-layout.htmlnew file mode 100644index 00000000000..355405649b1--- /dev/null+++ b/Tests/LibWeb/Text/input/css/size-container-units-start-before-first-layout.html@@ -0,0 +1,18 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ body { margin: 0; }+ .grid { display: grid; grid-template-columns: repeat(4, 1fr); width: 800px; }+ .item { aspect-ratio: 354 / 419; container-type: inline-size; }+ h2 { font-size: 24.49cqw; line-height: 1; margin: 0; }+</style>+<div class="grid"><div class="item"><h2>Join the family</h2></div></div>+<script>+ // The units below a size container that no layout has reached yet start out against a basis of 0. Starting them+ // out against the viewport grows the heading past the item's aspect ratio, and the item's column with it.+ test(() => {+ const rect = document.querySelector(".item").getBoundingClientRect();+ println(`item: ${rect.width}x${Math.round(rect.height)}`);+ println(`heading font-size: ${getComputedStyle(document.querySelector("h2")).fontSize}`);+ });+</script>