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.
This commit is contained in:
Andi Gallo 2023-06-28 05:12:57 +00:00 committed by Andreas Kling
parent 50d6b1ba19
commit 4596d41291
Notes: sideshowbarker 2024-07-17 00:25:35 +09:00
3 changed files with 72 additions and 2 deletions

View file

@ -0,0 +1,38 @@
Viewport <#document> at (0,0) content-size 800x600 children: not-inline
BlockContainer <html> at (0,0) content-size 800x600 [BFC] children: not-inline
BlockContainer <body> 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 <table> at (9,9) content-size 91.328125x21.46875 table-box [TFC] children: not-inline
BlockContainer <(anonymous)> (not painted) children: inline
TextNode <#text>
Box <tbody> at (9,9) content-size 91.328125x21.46875 table-row-group children: not-inline
BlockContainer <(anonymous)> (not painted) children: inline
TextNode <#text>
Box <tr> at (9,9) content-size 91.328125x21.46875 table-row children: not-inline
BlockContainer <(anonymous)> (not painted) children: inline
TextNode <#text>
BlockContainer <td> 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 <td> 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 <td> 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>

View file

@ -0,0 +1,21 @@
<style>
table {
border: 1px solid;
border-collapse: collapse;
text-align: center;
}
td {
border: 1px solid;
}
</style>
<table>
<tbody>
<tr>
<td style="width: 20%;">A</td>
<td style="width: 60%;">B</td>
<td style="width: 20%;">C</td>
</tr>
</tbody>
</table>

View file

@ -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 tables 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-roots width property has a computed value (resolving to
// resolved-table-width) other than auto, the used width is the greater