From 4596d41291135a65199a5c444c5e8f805c5af378 Mon Sep 17 00:00:00 2001 From: Andi Gallo Date: Wed, 28 Jun 2023 05:12:57 +0000 Subject: [PATCH] LibWeb: Make cell percentage widths relative to table Cell percentage widths are relative to table width, not containing block width. If the table width is auto, there isn't a normative specification, only a brief mention that the user agent should try to meet it. As a starting point, we increase the width of the table such that it's sufficient to cover min-width of cells with a percentage width. This matches the behavior of other browsers, at least for simple cases. --- .../table/percentage-width-columns.txt | 38 +++++++++++++++++++ .../input/table/percentage-width-columns.html | 21 ++++++++++ .../LibWeb/Layout/TableFormattingContext.cpp | 15 +++++++- 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 Tests/LibWeb/Layout/expected/table/percentage-width-columns.txt create mode 100644 Tests/LibWeb/Layout/input/table/percentage-width-columns.html diff --git a/Tests/LibWeb/Layout/expected/table/percentage-width-columns.txt b/Tests/LibWeb/Layout/expected/table/percentage-width-columns.txt new file mode 100644 index 00000000000..2eb400581b8 --- /dev/null +++ b/Tests/LibWeb/Layout/expected/table/percentage-width-columns.txt @@ -0,0 +1,38 @@ +Viewport <#document> at (0,0) content-size 800x600 children: not-inline + BlockContainer at (0,0) content-size 800x600 [BFC] children: not-inline + BlockContainer at (8,8) content-size 784x23.46875 children: not-inline + TableWrapper <(anonymous)> at (8,8) content-size 93.328125x23.46875 [BFC] children: not-inline + Box at (9,9) content-size 91.328125x21.46875 table-box [TFC] children: not-inline + BlockContainer <(anonymous)> (not painted) children: inline + TextNode <#text> + Box at (9,9) content-size 91.328125x21.46875 table-row-group children: not-inline + BlockContainer <(anonymous)> (not painted) children: inline + TextNode <#text> + Box at (9,9) content-size 91.328125x21.46875 table-row children: not-inline + BlockContainer <(anonymous)> (not painted) children: inline + TextNode <#text> + BlockContainer
at (11,11) content-size 14.265625x17.46875 table-cell [BFC] children: inline + line 0 width: 14.265625, height: 17.46875, bottom: 17.46875, baseline: 13.53125 + frag 0 from TextNode start: 0, length: 1, rect: [11,11 14.265625x17.46875] + "A" + TextNode <#text> + BlockContainer <(anonymous)> (not painted) children: inline + TextNode <#text> + BlockContainer at (29.265625,11) content-size 50.796875x17.46875 table-cell [BFC] children: inline + line 0 width: 9.34375, height: 17.46875, bottom: 17.46875, baseline: 13.53125 + frag 0 from TextNode start: 0, length: 1, rect: [50.265625,11 9.34375x17.46875] + "B" + TextNode <#text> + BlockContainer <(anonymous)> (not painted) children: inline + TextNode <#text> + BlockContainer at (84.0625,11) content-size 14.265625x17.46875 table-cell [BFC] children: inline + line 0 width: 10.3125, height: 17.46875, bottom: 17.46875, baseline: 13.53125 + frag 0 from TextNode start: 0, length: 1, rect: [86.0625,11 10.3125x17.46875] + "C" + TextNode <#text> + BlockContainer <(anonymous)> (not painted) children: inline + TextNode <#text> + BlockContainer <(anonymous)> (not painted) children: inline + TextNode <#text> + BlockContainer <(anonymous)> (not painted) children: inline + TextNode <#text> diff --git a/Tests/LibWeb/Layout/input/table/percentage-width-columns.html b/Tests/LibWeb/Layout/input/table/percentage-width-columns.html new file mode 100644 index 00000000000..dfb8a131eca --- /dev/null +++ b/Tests/LibWeb/Layout/input/table/percentage-width-columns.html @@ -0,0 +1,21 @@ + + + + + + + + + + +
ABC
\ No newline at end of file diff --git a/Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp index a1d72f86260..8db618b861f 100644 --- a/Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp @@ -180,7 +180,7 @@ void TableFormattingContext::compute_cell_measures(AvailableSpace const& availab CSSPixels border_right = is_collapse ? cell_state.border_right : computed_values.border_right().width; auto height = computed_values.height().to_px(cell.box, containing_block.content_height()); - auto width = computed_values.width().to_px(cell.box, containing_block.content_width()); + auto width = computed_values.width().is_length() ? computed_values.width().to_px(cell.box, containing_block.content_width()) : 0; auto min_content_height = calculate_min_content_height(cell.box, available_space.width); auto max_content_height = calculate_max_content_height(cell.box, available_space.width); auto min_content_width = calculate_min_content_width(cell.box); @@ -194,7 +194,7 @@ void TableFormattingContext::compute_cell_measures(AvailableSpace const& availab min_width = max(min_width, computed_values.min_width().to_px(cell.box, containing_block.content_width())); CSSPixels max_height = computed_values.height().is_auto() ? max_content_height : height; - CSSPixels max_width = computed_values.width().is_auto() ? max_content_width : width; + CSSPixels max_width = computed_values.width().is_length() ? width : max_content_width; if (!should_treat_max_height_as_none(cell.box, available_space.height)) max_height = min(max_height, computed_values.max_height().to_px(cell.box, containing_block.content_height())); if (!should_treat_max_width_as_none(cell.box, available_space.width)) @@ -392,6 +392,17 @@ void TableFormattingContext::compute_table_width() // If the table-root has 'width: auto', the used width is the greater of // min(GRIDMAX, the table’s containing block width), the used min-width of the table. used_width = max(min(grid_max, width_of_table_containing_block), used_min_width); + // https://www.w3.org/TR/CSS22/tables.html#auto-table-layout + // A percentage value for a column width is relative to the table width. If the table has 'width: auto', + // a percentage represents a constraint on the column's width, which a UA should try to satisfy. + CSSPixels adjusted_used_width = 0; + for (auto& cell : m_cells) { + auto const& cell_width = cell.box->computed_values().width(); + if (cell_width.is_percentage()) { + adjusted_used_width = 100 / cell_width.percentage().value() * cell.min_width; + used_width = min(max(used_width, adjusted_used_width), width_of_table_containing_block); + } + } } else { // If the table-root’s width property has a computed value (resolving to // resolved-table-width) other than auto, the used width is the greater