Skip to content

Truly independent web browser

LibWeb: Grow a column flex container to its content via aspect-ratio

sideshowbarker5b0bb7fa7758
diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
index a257f652574..8366e35c716 100644
--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
@@ -3840,6 +3840,30 @@ impl<'pass> FlexFormattingContext<'pass> {
}
}
+ /// Grows the block size of a column container, where it was transferred through the container's preferred aspect
+ /// ratio, to the automatic minimum size that its content gives it, before its items flex within it. So, they flex
+ /// against the grown size.
+ /// https://drafts.csswg.org/css-sizing-4/#aspect-ratio-minimum
+ fn grow_block_size_transferred_through_aspect_ratio_to_content(&mut self) {
+ let facts = self.facts(self.run.box_);
+ // https://drafts.csswg.org/css-contain-2/#containment-size
+ // NB: A size-contained container is sized as if it had no content, so its content can't grow it.
+ if self.is_row_layout() || !facts.has_preferred_aspect_ratio() || facts.node_has_size_containment() {
+ return;
+ }
+ let layout_input = self.layout_input.as_ref().unwrap();
+ let constraints = layout_input.containing_block_constraints;
+ let sizing = self.sizing();
+ let available_space =
+ sizing.available_space_for_block_size_resolution(self.run.box_, layout_input.available_space, constraints);
+ sizing.apply_automatic_minimum_block_size_from_aspect_ratio(
+ sizing_context::SizedCells::new(self.run.box_, &self.container_cells, self.run.pass.frame),
+ available_space,
+ constraints,
+ Some(self.largest_outer_hypothetical_main_size_of_lines()),
+ );
+ }
+
fn resolve_own_auto_block_size_from_line_cross_sizes(&mut self) {
if self.cross_axis_is_horizontal() {
return;
@@ -4227,6 +4251,7 @@ impl<'pass> FlexFormattingContext<'pass> {
// 5. Collect flex items into flex lines:
// After this step no additional items are to be added to flex_lines or any of its items!
self.collect_flex_items_into_flex_lines();
+ self.grow_block_size_transferred_through_aspect_ratio_to_content();
// 6. Resolve the flexible lengths
self.resolve_flexible_lengths();
// Cross Size Determination
@@ -4408,7 +4433,11 @@ impl<'pass> FlexFormattingContext<'pass> {
self.place_item(index, placed_item, fragment_arrival);
}
if self.facts(self.run.box_).has_preferred_aspect_ratio() {
- self.in_flow_items_margin_box_block_extent = self.margin_box_block_extent_of_placed_in_flow_items();
+ self.in_flow_items_margin_box_block_extent = if self.is_row_layout() {
+ self.margin_box_block_extent_of_placed_in_flow_items()
+ } else {
+ Some(self.largest_outer_hypothetical_main_size_of_lines())
+ };
}
// https://drafts.csswg.org/css-flexbox-1/#flex-baselines
// If any of the flex items on the flex container's startmost/endmost flex line participate in baseline
@@ -4468,6 +4497,24 @@ impl<'pass> FlexFormattingContext<'pass> {
}
}
+ /// The block size a column flex container's content would take up if its items didn't flex: the sum of the outer
+ /// hypothetical main sizes of the items on its longest line, and the gaps between them. That's the min-content
+ /// block size that the automatic minimum size of a box with a preferred aspect ratio grows to, whereas the extent
+ /// of the placed items is only what's left of it after they shrank to fit the block size transferred through the
+ /// ratio.
+ /// https://drafts.csswg.org/css-sizing-4/#aspect-ratio-minimum
+ fn largest_outer_hypothetical_main_size_of_lines(&self) -> CssPixels {
+ let main_gap = self.main_gap();
+ self.flex_lines
+ .iter()
+ .map(|line| {
+ line.items.iter().fold(CssPixels::default(), |sum, index| {
+ sum + self.flex_items[index].outer_hypothetical_main_size()
+ }) + main_gap * line.items.len().saturating_sub(1)
+ })
+ .fold(CssPixels::default(), CssPixels::max)
+ }
+
/// The block size of the margin boxes of the container's in-flow children, once its items are placed, unless a box
/// among them is not an item. A run of collapsible white space is no item and renders nothing.
fn margin_box_block_extent_of_placed_in_flow_items(&self) -> Option<CssPixels> {
diff --git a/Tests/LibWeb/Text/expected/Layout/flex-column-aspect-ratio-container-grows-to-its-content.txt b/Tests/LibWeb/Text/expected/Layout/flex-column-aspect-ratio-container-grows-to-its-content.txt
new file mode 100644
index 00000000000..31e36a2a76d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Layout/flex-column-aspect-ratio-container-grows-to-its-content.txt
@@ -0,0 +1,17 @@
+item: 200; items 200
+flexible-item: 200; items 200
+scrollable-item: 200; items 200
+two-items: 200; items 150, 50
+two-items-with-gap: 130; items 60, 60
+item-with-min-height-zero: 200; items 200
+short-item: 100; items 50
+container-overflow-hidden: 100; items 100
+container-min-height-zero: 100; items 100
+container-contain-size: 100; items 200
+container-contain-inline-size: 200; items 200
+container-max-height: 150; items 150
+wrap: 100; items 60, 60
+column-reverse: 200; items 200
+row: 200; items 200
+flex-item-card: 200; items 200
+abspos-card: 200; items 200
diff --git a/Tests/LibWeb/Text/input/Layout/flex-column-aspect-ratio-container-grows-to-its-content.html b/Tests/LibWeb/Text/input/Layout/flex-column-aspect-ratio-container-grows-to-its-content.html
new file mode 100644
index 00000000000..5fb162799ab
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Layout/flex-column-aspect-ratio-container-grows-to-its-content.html
@@ -0,0 +1,41 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body { margin: 0; }
+ .card { width: 100px; aspect-ratio: 1 / 1; float: left; margin: 0 8px 8px 0; }
+ .column { display: flex; flex-direction: column; }
+ .tall { height: 200px; }
+</style>
+<!-- A box with a preferred aspect ratio and an automatic block size grows to its min-content block size, which for a
+ column flex container is what its items take up before they shrink. Its items then flex within the grown size.
+ Each line gives the height of the container, and then of each of its items. -->
+<div class="card column" id="item"><div class="tall"></div></div>
+<div class="card column" id="flexible-item"><div class="tall" style="flex: 1 1 auto"></div></div>
+<div class="card column" id="scrollable-item"><div style="flex: 1 1 auto; overflow: hidden"><div class="tall"></div></div></div>
+<div class="card column" id="two-items"><div style="height: 150px"></div><div style="height: 50px"></div></div>
+<div class="card column" id="two-items-with-gap" style="gap: 10px"><div style="height: 60px"></div><div style="height: 60px"></div></div>
+<div class="card column" id="item-with-min-height-zero"><div class="tall" style="min-height: 0"></div></div>
+<div class="card column" id="short-item"><div style="height: 50px"></div></div>
+<div class="card column" id="container-overflow-hidden" style="overflow: hidden"><div class="tall"></div></div>
+<div class="card column" id="container-min-height-zero" style="min-height: 0"><div class="tall"></div></div>
+<div class="card column" id="container-contain-size" style="contain: size"><div class="tall" style="flex: none"></div></div>
+<div class="card column" id="container-contain-inline-size" style="contain: inline-size"><div class="tall" style="flex: none"></div></div>
+<div class="card column" id="container-max-height" style="max-height: 150px"><div class="tall"></div></div>
+<div class="card column" id="wrap" style="flex-wrap: wrap"><div style="height: 60px; width: 40px"></div><div style="height: 60px; width: 40px"></div></div>
+<div class="card column" id="column-reverse" style="flex-direction: column-reverse"><div class="tall"></div></div>
+<div class="card" id="row" style="display: flex"><div class="tall" style="width: 50px"></div></div>
+<div style="display: flex; align-items: flex-start"><div class="card column" id="flex-item-card"><div class="tall"></div></div></div>
+<div style="position: relative; height: 220px"><div class="card column" id="abspos-card" style="position: absolute"><div class="tall"></div></div></div>
+<script>
+ function run(report) {
+ const height = element => element.getBoundingClientRect().height;
+ for (const card of document.querySelectorAll(".card"))
+ report(`${card.id}: ${height(card)}; items ${[...card.children].map(height).join(", ")}`);
+ }
+</script>
+<script>
+ if (window.asyncTest)
+ asyncTest(done => addEventListener("load", () => { run(println); done(); }));
+ else
+ addEventListener("load", () => { run(line => console.log(line)); window.engineCompareDone = true; });
+</script>