From 775742b35d24188070b704be17376945d23813ea Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Thu, 8 Jun 2023 00:54:24 +0300 Subject: [PATCH] LibWeb: Add support for horizontal margins of grid items in GFC --- .../grid-item-horizontal-margins-auto.txt | 116 ++++++++++++++++++ .../grid-item-horizontal-margins-auto.html | 40 ++++++ .../LibWeb/Layout/GridFormattingContext.cpp | 45 +++++-- 3 files changed, 193 insertions(+), 8 deletions(-) create mode 100644 Tests/LibWeb/Layout/expected/grid/grid-item-horizontal-margins-auto.txt create mode 100644 Tests/LibWeb/Layout/input/grid/grid-item-horizontal-margins-auto.html diff --git a/Tests/LibWeb/Layout/expected/grid/grid-item-horizontal-margins-auto.txt b/Tests/LibWeb/Layout/expected/grid/grid-item-horizontal-margins-auto.txt new file mode 100644 index 00000000000..406e05d3bd5 --- /dev/null +++ b/Tests/LibWeb/Layout/expected/grid/grid-item-horizontal-margins-auto.txt @@ -0,0 +1,116 @@ +Viewport <#document> at (0,0) content-size 800x600 children: not-inline + BlockContainer at (1,1) content-size 798x458.28125 [BFC] children: not-inline + BlockContainer at (10,10) content-size 780x440.28125 children: not-inline + Box at (11,11) content-size 500x438.28125 [GFC] children: not-inline + BlockContainer <(anonymous)> (not painted) [BFC] children: inline + TextNode <#text> + BlockContainer at (99.71875,12) content-size 322.5625x17.46875 [BFC] children: inline + line 0 width: 322.5625, height: 17.46875, bottom: 17.46875, baseline: 13.53125 + frag 0 from TextNode start: 0, length: 38, rect: [99.71875,12 322.5625x17.46875] + "auto horizontal margins and auto width" + TextNode <#text> + BlockContainer <(anonymous)> (not painted) [BFC] children: inline + TextNode <#text> + BlockContainer at (252.375,31.46875) content-size 257.625x17.46875 [BFC] children: inline + line 0 width: 257.625, height: 17.46875, bottom: 17.46875, baseline: 13.53125 + frag 0 from TextNode start: 0, length: 31, rect: [252.375,31.46875 257.625x17.46875] + "auto left margin and auto width" + TextNode <#text> + BlockContainer <(anonymous)> (not painted) [BFC] children: inline + TextNode <#text> + BlockContainer at (12,50.9375) content-size 268.484375x17.46875 [BFC] children: inline + line 0 width: 268.484375, height: 17.46875, bottom: 17.46875, baseline: 13.53125 + frag 0 from TextNode start: 0, length: 32, rect: [12,50.9375 268.484375x17.46875] + "auto right margin and auto width" + TextNode <#text> + BlockContainer <(anonymous)> (not painted) [BFC] children: inline + TextNode <#text> + BlockContainer at (75.257812,70.40625) content-size 371.484375x17.46875 [BFC] children: inline + line 0 width: 371.484375, height: 17.46875, bottom: 17.46875, baseline: 13.53125 + frag 0 from TextNode start: 0, length: 45, rect: [75.257812,70.40625 371.484375x17.46875] + "auto horizontal margins and fit-content width" + TextNode <#text> + BlockContainer <(anonymous)> (not painted) [BFC] children: inline + TextNode <#text> + BlockContainer at (203.453125,89.875) content-size 306.546875x17.46875 [BFC] children: inline + line 0 width: 306.546875, height: 17.46875, bottom: 17.46875, baseline: 13.53125 + frag 0 from TextNode start: 0, length: 38, rect: [203.453125,89.875 306.546875x17.46875] + "auto left margin and fit-content width" + TextNode <#text> + BlockContainer <(anonymous)> (not painted) [BFC] children: inline + TextNode <#text> + BlockContainer at (12,109.34375) content-size 317.40625x17.46875 [BFC] children: inline + line 0 width: 317.40625, height: 17.46875, bottom: 17.46875, baseline: 13.53125 + frag 0 from TextNode start: 0, length: 39, rect: [12,109.34375 317.40625x17.46875] + "auto right margin and fit-content width" + TextNode <#text> + BlockContainer <(anonymous)> (not painted) [BFC] children: inline + TextNode <#text> + BlockContainer at (236,128.8125) content-size 50x105.15625 [BFC] children: inline + line 0 width: 36.328125, height: 17.46875, bottom: 17.46875, baseline: 13.53125 + frag 0 from TextNode start: 0, length: 4, rect: [236,128.8125 36.328125x17.46875] + "auto" + line 1 width: 81.84375, height: 17.9375, bottom: 35.40625, baseline: 13.53125 + frag 0 from TextNode start: 5, length: 10, rect: [236,145.8125 81.84375x17.46875] + "horizontal" + line 2 width: 61.453125, height: 18.40625, bottom: 53.34375, baseline: 13.53125 + frag 0 from TextNode start: 16, length: 7, rect: [236,162.8125 61.453125x17.46875] + "margins" + line 3 width: 26.8125, height: 17.875, bottom: 70.28125, baseline: 13.53125 + frag 0 from TextNode start: 24, length: 3, rect: [236,180.8125 26.8125x17.46875] + "and" + line 4 width: 37.28125, height: 18.34375, bottom: 88.21875, baseline: 13.53125 + frag 0 from TextNode start: 28, length: 5, rect: [236,197.8125 37.28125x17.46875] + "fixed" + line 5 width: 39.796875, height: 17.8125, bottom: 105.15625, baseline: 13.53125 + frag 0 from TextNode start: 34, length: 5, rect: [236,215.8125 39.796875x17.46875] + "width" + TextNode <#text> + BlockContainer <(anonymous)> (not painted) [BFC] children: inline + TextNode <#text> + BlockContainer at (460,235.96875) content-size 50x105.15625 [BFC] children: inline + line 0 width: 36.328125, height: 17.46875, bottom: 17.46875, baseline: 13.53125 + frag 0 from TextNode start: 0, length: 4, rect: [460,235.96875 36.328125x17.46875] + "auto" + line 1 width: 26.25, height: 17.9375, bottom: 35.40625, baseline: 13.53125 + frag 0 from TextNode start: 5, length: 4, rect: [460,252.96875 26.25x17.46875] + "left" + line 2 width: 52.109375, height: 18.40625, bottom: 53.34375, baseline: 13.53125 + frag 0 from TextNode start: 10, length: 6, rect: [460,269.96875 52.109375x17.46875] + "margin" + line 3 width: 26.8125, height: 17.875, bottom: 70.28125, baseline: 13.53125 + frag 0 from TextNode start: 17, length: 3, rect: [460,287.96875 26.8125x17.46875] + "and" + line 4 width: 37.28125, height: 18.34375, bottom: 88.21875, baseline: 13.53125 + frag 0 from TextNode start: 21, length: 5, rect: [460,304.96875 37.28125x17.46875] + "fixed" + line 5 width: 39.796875, height: 17.8125, bottom: 105.15625, baseline: 13.53125 + frag 0 from TextNode start: 27, length: 5, rect: [460,322.96875 39.796875x17.46875] + "width" + TextNode <#text> + BlockContainer <(anonymous)> (not painted) [BFC] children: inline + TextNode <#text> + BlockContainer at (12,343.125) content-size 50x105.15625 [BFC] children: inline + line 0 width: 36.328125, height: 17.46875, bottom: 17.46875, baseline: 13.53125 + frag 0 from TextNode start: 0, length: 4, rect: [12,343.125 36.328125x17.46875] + "auto" + line 1 width: 37.109375, height: 17.9375, bottom: 35.40625, baseline: 13.53125 + frag 0 from TextNode start: 5, length: 5, rect: [12,360.125 37.109375x17.46875] + "right" + line 2 width: 52.109375, height: 18.40625, bottom: 53.34375, baseline: 13.53125 + frag 0 from TextNode start: 11, length: 6, rect: [12,377.125 52.109375x17.46875] + "margin" + line 3 width: 26.8125, height: 17.875, bottom: 70.28125, baseline: 13.53125 + frag 0 from TextNode start: 18, length: 3, rect: [12,395.125 26.8125x17.46875] + "and" + line 4 width: 37.28125, height: 18.34375, bottom: 88.21875, baseline: 13.53125 + frag 0 from TextNode start: 22, length: 5, rect: [12,412.125 37.28125x17.46875] + "fixed" + line 5 width: 39.796875, height: 17.8125, bottom: 105.15625, baseline: 13.53125 + frag 0 from TextNode start: 28, length: 5, rect: [12,430.125 39.796875x17.46875] + "width" + TextNode <#text> + BlockContainer <(anonymous)> (not painted) [BFC] children: inline + TextNode <#text> + BlockContainer <(anonymous)> at (10,450.28125) content-size 780x0 children: inline + TextNode <#text> diff --git a/Tests/LibWeb/Layout/input/grid/grid-item-horizontal-margins-auto.html b/Tests/LibWeb/Layout/input/grid/grid-item-horizontal-margins-auto.html new file mode 100644 index 00000000000..c489ed6c05a --- /dev/null +++ b/Tests/LibWeb/Layout/input/grid/grid-item-horizontal-margins-auto.html @@ -0,0 +1,40 @@ + + +
+ +
auto horizontal margins and auto width
+
auto left margin and auto width
+
auto right margin and auto width
+ +
auto horizontal margins and fit-content width
+
auto left margin and fit-content width
+
auto right margin and fit-content width
+ +
auto horizontal margins and fixed width
+
auto left margin and fixed width
+
auto right margin and fixed width
+ +
diff --git a/Userland/Libraries/LibWeb/Layout/GridFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/GridFormattingContext.cpp index 339685cdd96..969f89ec55d 100644 --- a/Userland/Libraries/LibWeb/Layout/GridFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/GridFormattingContext.cpp @@ -1367,14 +1367,35 @@ void GridFormattingContext::resolve_grid_item_widths() box_state.border_left = border_left; box_state.border_right = border_right; - auto const& computed_width = item.box().computed_values().width(); + auto const& computed_values = item.box().computed_values(); + auto const& computed_width = computed_values.width(); + + auto try_compute_width = [&](CSSPixels a_width) { + CSSPixels width = a_width; + auto underflow_px = containing_block_width - width - box_state.border_left - box_state.border_right - box_state.padding_left - box_state.padding_right - box_state.margin_left - box_state.margin_right; + if (computed_values.margin().left().is_auto() && computed_values.margin().right().is_auto()) { + auto half_of_the_underflow = underflow_px / 2; + box_state.margin_left = half_of_the_underflow; + box_state.margin_right = half_of_the_underflow; + } else if (computed_values.margin().left().is_auto()) { + box_state.margin_left = underflow_px; + } else if (computed_values.margin().right().is_auto()) { + box_state.margin_right = underflow_px; + } else if (computed_values.width().is_auto()) { + width += underflow_px; + } + + return width; + }; + CSSPixels used_width; - if (computed_width.is_auto()) - used_width = (containing_block_width - box_state.border_left - box_state.border_right - box_state.padding_left - box_state.padding_right); - else if (computed_width.is_fit_content()) - used_width = calculate_fit_content_width(item.box(), get_available_space_for_item(item)); - else - used_width = computed_width.to_px(grid_container(), containing_block_width); + if (computed_width.is_auto()) { + used_width = try_compute_width(calculate_fit_content_width(item.box(), get_available_space_for_item(item))); + } else if (computed_width.is_fit_content()) { + used_width = try_compute_width(calculate_fit_content_width(item.box(), get_available_space_for_item(item))); + } else { + used_width = try_compute_width(computed_width.to_px(grid_container(), containing_block_width)); + } box_state.set_content_width(used_width); } } @@ -1435,6 +1456,11 @@ void GridFormattingContext::run(Box const& box, LayoutMode, AvailableSpace const box_state.padding_bottom = computed_values.padding().bottom().to_px(grid_container(), 0); box_state.padding_left = computed_values.padding().left().to_px(grid_container(), 0); + box_state.margin_top = computed_values.margin().top().to_px(grid_container(), 0); + box_state.margin_right = computed_values.margin().right().to_px(grid_container(), 0); + box_state.margin_bottom = computed_values.margin().bottom().to_px(grid_container(), 0); + box_state.margin_left = computed_values.margin().left().to_px(grid_container(), 0); + box_state.border_top = computed_values.border_top().width; box_state.border_right = computed_values.border_right().width; box_state.border_bottom = computed_values.border_bottom().width; @@ -1476,7 +1502,10 @@ void GridFormattingContext::run(Box const& box, LayoutMode, AvailableSpace const y_end += m_grid_rows_and_gaps[i].base_size; } - child_box_state.offset = { x_start + child_box_state.border_left + child_box_state.padding_left, y_start + child_box_state.border_top + child_box_state.padding_top }; + child_box_state.offset = { + x_start + child_box_state.border_left + child_box_state.padding_left + child_box_state.margin_left, + y_start + child_box_state.border_top + child_box_state.padding_top + child_box_state.margin_top + }; auto available_space_for_children = AvailableSpace(AvailableSize::make_definite(child_box_state.content_width()), AvailableSize::make_definite(child_box_state.content_height())); if (auto independent_formatting_context = layout_inside(child_box, LayoutMode::Normal, available_space_for_children))