Truly independent web browser
LibWeb: Resolve cell padding percentages against the table width
Aliaksandr Kalenik3c15ff5ac622
diff --git a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rsindex beb406cf11e..4f7d1304cf6 100644--- a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs@@ -1432,7 +1432,7 @@ impl<'pass> TableFormattingContext<'pass> {participant_box.content_offset.set(content_offset);}- fn border_spacing_inline(&mut self) -> CssPixels {+ fn border_spacing_inline(&self) -> CssPixels {let style = self.style(self.run.box_);// When a table is laid out in collapsed-borders mode, the border-spacing of the table-root is ignored (as if it was set to 0px):// https://www.w3.org/TR/css-tables-3/#collapsed-style-overrides@@ -1443,7 +1443,7 @@ impl<'pass> TableFormattingContext<'pass> {}}- fn border_spacing_block(&mut self) -> CssPixels {+ fn border_spacing_block(&self) -> CssPixels {let style = self.style(self.run.box_);// When a table is laid out in collapsed-borders mode, the border-spacing of the table-root is ignored (as if it was set to 0px):// https://www.w3.org/TR/css-tables-3/#collapsed-style-overrides@@ -1830,6 +1830,13 @@ impl<'pass> TableFormattingContext<'pass> {sizing_context::SizedBox::new(self.cells[cell_index].box_, &self.cell_layouts[cell_index].box_cells)}+ /// What the padding percentages of the cells resolve against once the table is sized: its content inline size+ /// without the border spacing before the first column and after the last. While the columns are measured, they+ /// resolve against zero.+ fn cell_padding_percentage_basis(&self) -> CssPixels {+ self.table_box_cells.content_inline_size() - self.border_spacing_inline() * 2usize+ }+/// The cells of the box of the cell at `cell_index` with its paddings, whose percentages resolve against/// `inline_basis` in both axes, and in the separated borders model its borders.fn cell_box_with_resolved_sides(@@ -1865,7 +1872,7 @@ impl<'pass> TableFormattingContext<'pass> {for cell_index in 0..self.cells.len() {let cell = self.cells[cell_index];let style = self.style(cell.box_);- let cell_box = self.cell_box_with_resolved_sides(cell_index, collapsed, inline_basis);+ let cell_box = self.cell_box_with_resolved_sides(cell_index, collapsed, CssPixels::default());let inline_offsets = cell_box.border_box_left(collapsed) + cell_box.border_box_right(collapsed);let width = style.width();let max_width = style.max_width();@@ -3067,7 +3074,8 @@ impl<'pass> TableFormattingContext<'pass> {.max(height.to_px(CssPixels::default()));}}- let inline_basis = self.participant_constraints.inline_basis();+ let cell_padding_basis = self.cell_padding_percentage_basis();+ let fixed = self.use_fixed_mode_layout();let participant_block_basis = self.participant_constraints.block_basis();let collapsed = self.style(self.run.box_).border_collapse() != BORDER_COLLAPSE_SEPARATE;let inline_spacing = self.border_spacing_inline();@@ -3078,7 +3086,7 @@ impl<'pass> TableFormattingContext<'pass> {let span_inline = (0..cell.column_span).fold(CssPixels::default(), |sum, index| {sum + self.columns[cell.column_index + index].used_inline_size});- let mut cell_box = self.cell_box_with_resolved_sides(cell_index, collapsed, inline_basis);+ let mut cell_box = self.cell_box_with_resolved_sides(cell_index, collapsed, cell_padding_basis);// A cell whose columns all have 'visibility: collapse' is removed from the display along with them, as in// other engines: "This value causes the entire row or column to be removed from the display". It is laid// out like any other cell (with no inline size, see remove_collapsed_columns), and painting skips it.@@ -3110,8 +3118,10 @@ impl<'pass> TableFormattingContext<'pass> {// specified inline size (https://www.w3.org/TR/css-tables-3/#width-distribution-in-fixed-mode), so a// column can be narrower than those. The cell still occupies exactly its columns, with an empty content// box, as in other engines: shrink its used padding and borders to fit, so that the box painted for the- // cell stays within its columns.- Self::shrink_cell_offsets_to_fit(&mut cell_box, collapsed, cell_inline_size);+ // cell stays within its columns. In auto mode, only percentage paddings, which resolve against zero while+ // the columns are measured, can be wider than the columns: the end padding shrinks to fit, and the start+ // padding keeps its used value.+ Self::shrink_cell_offsets_to_fit(&mut cell_box, collapsed, cell_inline_size, fixed);cell_box.set_content_inline_size(cell_inline_size - cell_box.border_box_left(collapsed) - cell_box.border_box_right(collapsed),);@@ -3310,13 +3320,24 @@ impl<'pass> TableFormattingContext<'pass> {}/// Shrinks the used padding, then the used borders, of a cell so that they fit within `inline_size`, the inline- /// size of the columns the cell spans. The end side goes first so that the content keeps its position relative to- /// the start of the cell for as long as possible.- fn shrink_cell_offsets_to_fit(cell_box: &mut used_values::RootInputCells, collapsed: bool, inline_size: CssPixels) {+ /// size of the columns the cell spans, or only its end padding unless `shrinks_start_padding_and_borders`. The end+ /// side goes first so that the content keeps its position relative to the start of the cell for as long as+ /// possible.+ fn shrink_cell_offsets_to_fit(+ cell_box: &mut used_values::RootInputCells,+ collapsed: bool,+ inline_size: CssPixels,+ shrinks_start_padding_and_borders: bool,+ ) {let excess = |cell_box: &used_values::RootInputCells| {cell_box.border_box_left(collapsed) + cell_box.border_box_right(collapsed) - inline_size};- for is_start_side in [false, true] {+ let shrinking_sides: &[bool] = if shrinks_start_padding_and_borders {+ &[false, true]+ } else {+ &[false]+ };+ for &is_start_side in shrinking_sides {let reduction = excess(cell_box).min(if is_start_side {cell_box.padding_left@@ -3331,6 +3352,9 @@ impl<'pass> TableFormattingContext<'pass> {};*padding -= reduction;}+ if !shrinks_start_padding_and_borders {+ return;+ }for is_start_side in [false, true] {let remaining = excess(cell_box);if remaining <= CssPixels::default() {diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-tables/tentative/padding-percentage.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-tables/tentative/padding-percentage.htmlnew file mode 100644index 00000000000..886711c1a32--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-tables/tentative/padding-percentage.html@@ -0,0 +1,108 @@+<!DOCTYPE html>+<link rel="author" title="Oriol Brufau" href="[email protected]">+<link rel="match" href="../../../../../expected/wpt-import//css/reference/ref-filled-green-100px-square.xht">+<meta name="assert" content="+ When a table cell has a percentage in the inline axis of the table,+ it should resolve against zero when computing the column sizes,+ but once we know the final size of the table, it should re-resolve+ against that size minus the leading and trailing border spacing.+ This matches what Blink and Gecko do (WebKit is different).++ Note that Blink and Gecko disagree in case there is a caption:+ Blink still behaves as per above, while Gecko re-resolves against+ the sum of column sizes plus the interleaved border spacings.+ Due to the lack of interoperability, this test doesn't use captions.+">++<style>+table {+ border-spacing: 10px 0;+}+td {+ padding: 0;+}+td > div {+ width: 100px;+ height: 25px;+ background: red;+}+td > div.green {+ position: relative;+ background: green;+}+</style>++<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>++<!--+There is one column, it's 100px wide. The percentage resolves against+that amount, so the padding becomes 100px.+There is an extra spacing of 10px before the 1st column,+that's why the table has a -110px margin.+-->+<table style="margin-left: -110px">+ <td style="padding-left: 100%">+ <div class="green"></div>+ </td>+</table>++<!--+The 1st column is 110px wide, the 2nd column is 100px wide.+And they are separated by 10px, so that's a total of 220px.+The percentage resolves against that amount, so the padding+becomes 0.5 * 220px + 10px = 120px.+There is an extra spacing of 10px before the 1st column,+that's why the table has a -130px margin.+-->+<table style="margin-left: -130px">+ <td style="padding-left: calc(50% + 10px)">+ <div class="green"></div>+ </td>+ <td>+ <div></div>+ </td>+</table>++<!--+There are 3 columns, each one is 100px wide. They are separated by 10px,+so that's a total of 100px * 3 + 10px * 2 = 320px.+The percentage resolves against that amount, so the padding becomes+320px - 100px = 220px.+There is an extra spacing of 10px before the 1st column,+that's why the table has a -230px margin.+-->+<table style="margin-left: -230px">+ <td style="padding-left: calc(100% - 100px)">+ <div class="green"></div>+ </td>+ <td>+ <div></div>+ </td>+ <td>+ <div></div>+ </td>+</table>++<!--+There are 4 columns, the 1st one is 330px wide, each one of the others+is 100px wide. They are separated by 10px, so that's a total of+330px + 100px * 3 + 10px * 3 = 660px.+The percentage resolves against that amount, so the padding becomes+0.5 * 660px + 230px = 560px.+There is an extra spacing of 10px before the 1st column,+that's why the table has a -570px margin.+-->+<table style="margin-left: -570px">+ <td style="padding-left: calc(50% + 230px)">+ <div class="green"></div>+ </td>+ <td>+ <div></div>+ </td>+ <td>+ <div></div>+ </td>+ <td>+ <div></div>+ </td>+</table>diff --git a/Tests/LibWeb/Text/expected/table/cell-padding-percentage-block-basis.txt b/Tests/LibWeb/Text/expected/table/cell-padding-percentage-block-basis.txtindex 46c26aea32d..a37d37c75ac 100644--- a/Tests/LibWeb/Text/expected/table/cell-padding-percentage-block-basis.txt+++ b/Tests/LibWeb/Text/expected/table/cell-padding-percentage-block-basis.txt@@ -1,3 +1,3 @@-table in auto block: row height 130, cell padding-top 80px-table in 1000px block: row height 130, cell padding-top 80px-table in 3000px block: row height 130, cell padding-top 80px+table in auto block: row height 90, cell padding-top 40px+table in 1000px block: row height 90, cell padding-top 40px+table in 3000px block: row height 90, cell padding-top 40pxdiff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-tables/tentative/element-sizing.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-tables/tentative/element-sizing.txtnew file mode 100644index 00000000000..37c627c14b3--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-tables/tentative/element-sizing.txt@@ -0,0 +1,7 @@+Harness status: OK++Found 2 tests++2 Pass+Pass table 1+Pass table 2\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-tables/tentative/td-box-sizing-003.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-tables/tentative/td-box-sizing-003.txtindex 62f84ba479a..bdd8742b07e 100644--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-tables/tentative/td-box-sizing-003.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-tables/tentative/td-box-sizing-003.txt@@ -2,8 +2,8 @@ Harness status: OKFound 11 tests-8 Pass-3 Fail+10 Pass+1 FailPass table 1Pass table 2Pass table 3@@ -11,7 +11,7 @@ Pass table 4Pass table 5Pass table 6Pass table 7-Fail table 8-Fail table 9+Pass table 8+Pass table 9Fail table 10Pass table 11\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-tables/tentative/element-sizing.html b/Tests/LibWeb/Text/input/wpt-import/css/css-tables/tentative/element-sizing.htmlnew file mode 100644index 00000000000..8294c6337e1--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-tables/tentative/element-sizing.html@@ -0,0 +1,34 @@+<!doctype html>+<script src='../../../resources/testharness.js'></script>+<script src='../../../resources/testharnessreport.js'></script>+<script src="../../../resources/check-layout-th.js"></script>+<title>TABLE fragment sizes</title>+<link rel="author" title="Aleks Totic" href="[email protected]" />+<link rel="help" href="https://drafts.csswg.org/css-tables-3/" />++<main>+<h1>TBODY/TR Element sizes and border spacing</h1>+<p>Legacy Chrome:TR includes start/end, not before/after. 1st TBODY includes before/after, 2nd TBODY includes only after, not before.</p>+<p>Firefox: TR/TBODY do not include start/end before/after.</p>+<p>Proposal: match firefox</p>++<table style="width:100px;border-spacing:10px;background:#ddd">+ <tbody data-expected-width=80>+ <tr data-expected-width=80>+ <td data-expected-width=80>0,0</td>+ </tr>+ </tbody>+</table>++<p>TR width is used for TD's percentage resolution</p>+<table style="width:120px;border-spacing:10px;background:#ddd">+ <tbody data-expected-width=100>+ <tr data-expected-width=100>+ <td style="padding:30%" data-expected-width=100 ><div data-offset-x=30>0,0</div></td>+ </tr>+ </tbody>+</table>+</main>+<script>+ checkLayout("table");+</script>