Skip to content

Commit 61e5272

Browse files
committed
LibWeb: Resolve percentage cell widths properly in fixed layout tables
In fixed table layouts, cell percentage widths were incorrectly being resolved against the containing block of the table wrapper (the page) instead of the table's computed width. This caused cells with percentage widths to evaluate to incorrect absolute dimensions and throw off the table column distribution logic. This commit properly queries the table's computed width first and uses that as the reference width when resolving percentage widths in TableFormattingContext::compute_cell_measures(), ensuring that the math adheres to the CSS Tables Level 3 Specification. Test baselines have been updated to match the correct output.
1 parent 8a657a9 commit 61e5272

3 files changed

Lines changed: 85 additions & 70 deletions

File tree

Libraries/LibWeb/Layout/TableFormattingContext.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,18 @@ void TableFormattingContext::compute_cell_measures()
180180
// in which case they are resolved based on the table width (if it is definite, otherwise use 0).
181181
auto width_is_specified_length_or_percentage = computed_values.width().is_length() || computed_values.width().is_percentage();
182182
if (use_fixed_mode_layout()) {
183-
min_content_width = max_content_width = width_is_specified_length_or_percentage ? computed_values.width().to_px(cell.box, containing_block_width) : 0;
183+
if (computed_values.width().is_percentage()) {
184+
CSSPixels reference_width = 0;
185+
auto table_width = table_box().computed_values().width();
186+
if (table_width.is_length() || table_width.is_percentage())
187+
reference_width = table_width.to_px(table_box(), containing_block_width);
188+
min_content_width = max_content_width = computed_values.width().to_px(cell.box, reference_width);
189+
} else if (computed_values.width().is_length()) {
190+
min_content_width = max_content_width = computed_values.width().to_px(cell.box, containing_block_width);
191+
} else {
192+
min_content_width = max_content_width = 0;
193+
}
194+
184195
if (computed_values.box_sizing() == CSS::BoxSizing::BorderBox)
185196
min_content_width = max_content_width = max(CSSPixels(0), min_content_width - padding_left - padding_right - border_left - border_right);
186197
}

Tests/LibWeb/Layout/expected/table/fixed-layout-percentage-width-all-columns.txt

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,59 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
55
Box <table> at [9,9] table-box [0+1+0 598 0+1+0] [0+1+0 44 0+1+0] [TFC] children: not-inline
66
BlockContainer <(anonymous)> (not painted) children: inline
77
TextNode <#text> (not painted)
8-
Box <tbody> at [9,9] table-row-group [0+0+0 598 0+0+0] [0+0+0 44 0+0+0] children: not-inline
9-
Box <tr> at [9,9] table-row [0+0+0 598 0+0+0] [0+0+0 22 0+0+0] children: not-inline
8+
Box <tbody> at [9,9] table-row-group [0+0+0 597.984375 0+0+0] [0+0+0 44 0+0+0] children: not-inline
9+
Box <tr> at [9,9] table-row [0+0+0 597.984375 0+0+0] [0+0+0 22 0+0+0] children: not-inline
1010
BlockContainer <(anonymous)> (not painted) children: inline
1111
TextNode <#text> (not painted)
12-
BlockContainer <td> at [11,11] table-cell [0+1+1 62.4375 1+1+0] [0+1+1 18 1+1+0] [BFC] children: inline
12+
BlockContainer <td> at [11,11] table-cell [0+1+1 64.828125 1+1+0] [0+1+1 18 1+1+0] [BFC] children: inline
1313
frag 0 from TextNode start: 0, length: 4, rect: [11,11 26.078125x18] baseline: 13.796875
1414
"cell"
1515
TextNode <#text> (not painted)
1616
BlockContainer <(anonymous)> (not painted) children: inline
1717
TextNode <#text> (not painted)
18-
BlockContainer <td> at [77.4375,11] table-cell [0+1+1 62.4375 1+1+0] [0+1+1 18 1+1+0] [BFC] children: inline
19-
frag 0 from TextNode start: 0, length: 4, rect: [77.4375,11 26.078125x18] baseline: 13.796875
18+
BlockContainer <td> at [79.828125,11] table-cell [0+1+1 64.828125 1+1+0] [0+1+1 18 1+1+0] [BFC] children: inline
19+
frag 0 from TextNode start: 0, length: 4, rect: [79.828125,11 26.078125x18] baseline: 13.796875
2020
"cell"
2121
TextNode <#text> (not painted)
2222
BlockContainer <(anonymous)> (not painted) children: inline
2323
TextNode <#text> (not painted)
24-
BlockContainer <td> at [143.875,11] table-cell [0+1+1 128.890625 1+1+0] [0+1+1 18 1+1+0] [BFC] children: inline
25-
frag 0 from TextNode start: 0, length: 12, rect: [143.875,11 94.96875x18] baseline: 13.796875
24+
BlockContainer <td> at [148.65625,11] table-cell [0+1+1 129.359375 1+1+0] [0+1+1 18 1+1+0] [BFC] children: inline
25+
frag 0 from TextNode start: 0, length: 12, rect: [148.65625,11 94.96875x18] baseline: 13.796875
2626
"A table cell"
2727
TextNode <#text> (not painted)
2828
BlockContainer <(anonymous)> (not painted) children: inline
2929
TextNode <#text> (not painted)
30-
BlockContainer <td> at [276.765625,11] table-cell [0+1+1 328.234375 1+1+0] [0+1+1 18 1+1+0] [BFC] children: inline
31-
frag 0 from TextNode start: 0, length: 12, rect: [276.765625,11 94.96875x18] baseline: 13.796875
30+
BlockContainer <td> at [282.015625,11] table-cell [0+1+1 322.96875 1+1+0] [0+1+1 18 1+1+0] [BFC] children: inline
31+
frag 0 from TextNode start: 0, length: 12, rect: [282.015625,11 94.96875x18] baseline: 13.796875
3232
"A table cell"
3333
TextNode <#text> (not painted)
3434
BlockContainer <(anonymous)> (not painted) children: inline
3535
TextNode <#text> (not painted)
3636
BlockContainer <(anonymous)> (not painted) children: inline
3737
TextNode <#text> (not painted)
38-
Box <tr> at [9,31] table-row [0+0+0 598 0+0+0] [0+0+0 22 0+0+0] children: not-inline
38+
Box <tr> at [9,31] table-row [0+0+0 597.984375 0+0+0] [0+0+0 22 0+0+0] children: not-inline
3939
BlockContainer <(anonymous)> (not painted) children: inline
4040
TextNode <#text> (not painted)
41-
BlockContainer <td> at [11,33] table-cell [0+1+1 62.4375 1+1+0] [0+1+1 18 1+1+0] [BFC] children: inline
41+
BlockContainer <td> at [11,33] table-cell [0+1+1 64.828125 1+1+0] [0+1+1 18 1+1+0] [BFC] children: inline
4242
frag 0 from TextNode start: 0, length: 4, rect: [11,33 26.078125x18] baseline: 13.796875
4343
"cell"
4444
TextNode <#text> (not painted)
4545
BlockContainer <(anonymous)> (not painted) children: inline
4646
TextNode <#text> (not painted)
47-
BlockContainer <td> at [77.4375,33] table-cell [0+1+1 62.4375 1+1+0] [0+1+1 18 1+1+0] [BFC] children: inline
48-
frag 0 from TextNode start: 0, length: 4, rect: [77.4375,33 26.078125x18] baseline: 13.796875
47+
BlockContainer <td> at [79.828125,33] table-cell [0+1+1 64.828125 1+1+0] [0+1+1 18 1+1+0] [BFC] children: inline
48+
frag 0 from TextNode start: 0, length: 4, rect: [79.828125,33 26.078125x18] baseline: 13.796875
4949
"cell"
5050
TextNode <#text> (not painted)
5151
BlockContainer <(anonymous)> (not painted) children: inline
5252
TextNode <#text> (not painted)
53-
BlockContainer <td> at [143.875,33] table-cell [0+1+1 128.890625 1+1+0] [0+1+1 18 1+1+0] [BFC] children: inline
54-
frag 0 from TextNode start: 0, length: 12, rect: [143.875,33 94.96875x18] baseline: 13.796875
53+
BlockContainer <td> at [148.65625,33] table-cell [0+1+1 129.359375 1+1+0] [0+1+1 18 1+1+0] [BFC] children: inline
54+
frag 0 from TextNode start: 0, length: 12, rect: [148.65625,33 94.96875x18] baseline: 13.796875
5555
"A table cell"
5656
TextNode <#text> (not painted)
5757
BlockContainer <(anonymous)> (not painted) children: inline
5858
TextNode <#text> (not painted)
59-
BlockContainer <td> at [276.765625,33] table-cell [0+1+1 328.234375 1+1+0] [0+1+1 18 1+1+0] [BFC] children: inline
60-
frag 0 from TextNode start: 0, length: 12, rect: [276.765625,33 94.96875x18] baseline: 13.796875
59+
BlockContainer <td> at [282.015625,33] table-cell [0+1+1 322.96875 1+1+0] [0+1+1 18 1+1+0] [BFC] children: inline
60+
frag 0 from TextNode start: 0, length: 12, rect: [282.015625,33 94.96875x18] baseline: 13.796875
6161
"A table cell"
6262
TextNode <#text> (not painted)
6363
BlockContainer <(anonymous)> (not painted) children: inline
@@ -70,24 +70,24 @@ ViewportPaintable (Viewport<#document>) [0,0 800x600]
7070
PaintableWithLines (BlockContainer<BODY>) [8,8 784x46]
7171
PaintableWithLines (TableWrapper(anonymous)) [8,8 600x46]
7272
PaintableBox (Box<TABLE>) [8,8 600x46]
73-
PaintableBox (Box<TBODY>) [9,9 598x44]
74-
PaintableBox (Box<TR>) [9,9 598x22]
75-
PaintableWithLines (BlockContainer<TD>) [9,9 66.4375x22]
73+
PaintableBox (Box<TBODY>) [9,9 597.984375x44]
74+
PaintableBox (Box<TR>) [9,9 597.984375x22]
75+
PaintableWithLines (BlockContainer<TD>) [9,9 68.828125x22]
7676
TextPaintable (TextNode<#text>)
77-
PaintableWithLines (BlockContainer<TD>) [75.4375,9 66.4375x22]
77+
PaintableWithLines (BlockContainer<TD>) [77.828125,9 68.828125x22]
7878
TextPaintable (TextNode<#text>)
79-
PaintableWithLines (BlockContainer<TD>) [141.875,9 132.890625x22]
79+
PaintableWithLines (BlockContainer<TD>) [146.65625,9 133.359375x22]
8080
TextPaintable (TextNode<#text>)
81-
PaintableWithLines (BlockContainer<TD>) [274.765625,9 332.234375x22]
81+
PaintableWithLines (BlockContainer<TD>) [280.015625,9 326.96875x22]
8282
TextPaintable (TextNode<#text>)
83-
PaintableBox (Box<TR>) [9,31 598x22]
84-
PaintableWithLines (BlockContainer<TD>) [9,31 66.4375x22]
83+
PaintableBox (Box<TR>) [9,31 597.984375x22]
84+
PaintableWithLines (BlockContainer<TD>) [9,31 68.828125x22]
8585
TextPaintable (TextNode<#text>)
86-
PaintableWithLines (BlockContainer<TD>) [75.4375,31 66.4375x22]
86+
PaintableWithLines (BlockContainer<TD>) [77.828125,31 68.828125x22]
8787
TextPaintable (TextNode<#text>)
88-
PaintableWithLines (BlockContainer<TD>) [141.875,31 132.890625x22]
88+
PaintableWithLines (BlockContainer<TD>) [146.65625,31 133.359375x22]
8989
TextPaintable (TextNode<#text>)
90-
PaintableWithLines (BlockContainer<TD>) [274.765625,31 332.234375x22]
90+
PaintableWithLines (BlockContainer<TD>) [280.015625,31 326.96875x22]
9191
TextPaintable (TextNode<#text>)
9292

9393
SC for Viewport<#document> [0,0 800x600] [children: 1] (z-index: auto)

0 commit comments

Comments
 (0)