From 23151de217fb2826624cdc56d65e391df3280a14 Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Fri, 14 Mar 2025 21:50:37 +0100 Subject: [PATCH] LibWeb/Layout: Fix min{max}-content size calculation for grid items Instead of directly invoking `to_px()`, `calculate_min_content_contribution()` needs to use `calculate_inner_width()` and `calculate_inner_height()`, which are aware of how to correctly handle `min-content` and `max-content` values. Fixes https://github.com/LadybirdBrowser/ladybird/issues/3469 --- .../LibWeb/Layout/GridFormattingContext.cpp | 10 +++++----- .../grid/item-with-min-content-width.txt | 15 +++++++++++++++ .../input/grid/item-with-min-content-width.html | 17 +++++++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 Tests/LibWeb/Layout/expected/grid/item-with-min-content-width.txt create mode 100644 Tests/LibWeb/Layout/input/grid/item-with-min-content-width.html diff --git a/Libraries/LibWeb/Layout/GridFormattingContext.cpp b/Libraries/LibWeb/Layout/GridFormattingContext.cpp index fd231868507..2768d4832c5 100644 --- a/Libraries/LibWeb/Layout/GridFormattingContext.cpp +++ b/Libraries/LibWeb/Layout/GridFormattingContext.cpp @@ -2438,12 +2438,12 @@ CSSPixels GridFormattingContext::calculate_min_content_contribution(GridItem con } auto preferred_size = get_item_preferred_size(item, dimension); - if (dimension == GridDimension::Row && preferred_size.is_auto() && item.box->has_preferred_aspect_ratio()) { - return m_state.get(item.box).content_width() / item.box->preferred_aspect_ratio().value(); + if (dimension == GridDimension::Column) { + auto width = calculate_inner_width(item.box, m_available_space->width, preferred_size); + return min(item.add_margin_box_sizes(width, dimension), maxium_size); } - auto containing_block_size = containing_block_size_for_item(item, dimension); - auto result = item.add_margin_box_sizes(preferred_size.to_px(grid_container(), containing_block_size), dimension); - return min(result, maxium_size); + auto height = calculate_inner_height(item.box, *m_available_space, preferred_size); + return min(item.add_margin_box_sizes(height, dimension), maxium_size); } CSSPixels GridFormattingContext::calculate_max_content_contribution(GridItem const& item, GridDimension const dimension) const diff --git a/Tests/LibWeb/Layout/expected/grid/item-with-min-content-width.txt b/Tests/LibWeb/Layout/expected/grid/item-with-min-content-width.txt new file mode 100644 index 00000000000..f71566b0656 --- /dev/null +++ b/Tests/LibWeb/Layout/expected/grid/item-with-min-content-width.txt @@ -0,0 +1,15 @@ +Viewport <#document> at (0,0) content-size 800x600 children: not-inline + BlockContainer at (0,0) content-size 800x33 [BFC] children: not-inline + Box at (8,8) content-size 784x17 [GFC] children: not-inline + Box at (8,8) content-size 100x17 flex-container(row) [FFC] children: not-inline + BlockContainer at (8,8) content-size 100x17 flex-item [BFC] children: inline + frag 0 from TextNode start: 0, length: 3, rect: [8,8 26.953125x17] baseline: 13.296875 + "whf" + TextNode <#text> + +ViewportPaintable (Viewport<#document>) [0,0 800x600] + PaintableWithLines (BlockContainer) [0,0 800x33] + PaintableBox (Box) [8,8 784x17] + PaintableBox (Box
.main) [8,8 100x17] + PaintableWithLines (BlockContainer
.player) [8,8 100x17] + TextPaintable (TextNode<#text>) diff --git a/Tests/LibWeb/Layout/input/grid/item-with-min-content-width.html b/Tests/LibWeb/Layout/input/grid/item-with-min-content-width.html new file mode 100644 index 00000000000..71329c8e773 --- /dev/null +++ b/Tests/LibWeb/Layout/input/grid/item-with-min-content-width.html @@ -0,0 +1,17 @@ +
whf \ No newline at end of file