Skip to content

Truly independent web browser

LibWeb: Size a percentage-height cell as auto in an auto-height table

sideshowbarker1e6ee891c39e
diff --git a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
index 8a397070cf6..81811540a74 100644
--- a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
@@ -1711,6 +1711,19 @@ impl<'pass> TableFormattingContext<'pass> {
}
}
+ /// A cell's percentage block size resolves against the table's block size only when the table's own block size
+ /// isn't auto, just like the percentage block sizes of its participants; otherwise the cell is sized as if its
+ /// block size were auto. (CSS 2.1 leaves percentage cell heights undefined: "CSS 2.1 does not define how the height
+ /// of table cells and table rows is calculated when their height is specified using percentage values.")
+ /// https://www.w3.org/TR/CSS21/tables.html#height-layout
+ fn cell_has_resolvable_percentage_block_size(&self, cell_box: Node) -> bool {
+ self.style(cell_box).height().is_percentage() && !self.table_block_size_is_auto()
+ }
+
+ fn table_block_size_is_auto(&self) -> bool {
+ self.style(self.run.box_).height().is_auto()
+ }
+
fn use_fixed_mode_layout(&self) -> bool {
// Implements https://www.w3.org/TR/css-tables-3/#in-fixed-mode.
// A table-root is said to be laid out in fixed mode whenever the computed value of the table-layout property is equal to fixed, and the
@@ -1899,8 +1912,11 @@ impl<'pass> TableFormattingContext<'pass> {
let max_height = style.max_height();
// The specified block size takes part in the row measures as an outer size (see
// initialize_table_measures); a percentage resolves against the basis the table forwards from its
- // containing block.
- let specified_outer = if height.is_auto() {
+ // containing block, and a block size that contains a percentage takes no part at all when the table's
+ // own block size is auto (see cell_has_resolvable_percentage_block_size).
+ let specified_outer = if height.is_auto()
+ || (height.contains_percentage() && self.table_block_size_is_auto())
+ {
CssPixels::default()
} else {
self.cell_specified_outer_block_size(cell.box_, &style, height.to_px(block_basis), block_offsets)
@@ -3139,10 +3155,10 @@ impl<'pass> TableFormattingContext<'pass> {
// At this point, percentage cell block sizes can be resolved because the final table block size is calculated.
for cell_index in 0..self.cells.len() {
let cell = self.cells[cell_index];
- let style = self.style(cell.box_);
- if !style.height().is_percentage() {
+ if !self.cell_has_resolvable_percentage_block_size(cell.box_) {
continue;
}
+ let style = self.style(cell.box_);
let mut cell_box = self.cell_box_cells[cell_index];
let offsets = cell_box.border_box_top(collapsed) + cell_box.border_box_bottom(collapsed);
let cell_size = self.cell_specified_outer_block_size(
@@ -3393,7 +3409,7 @@ impl<'pass> TableFormattingContext<'pass> {
continue;
}
let cell = self.cells[cell_index];
- let adopt_automatic_content_block_size = !self.style(cell.box_).height().is_percentage();
+ let adopt_automatic_content_block_size = !self.cell_has_resolvable_percentage_block_size(cell.box_);
let intrinsic_block_padding = self.cell_intrinsic_block_padding(cell_index, collapsed);
let measured_content_block_size = self.cell_box_cells[cell_index].content_block_size;
if let Some(measurement) = self.cell_measurements[cell_index]
diff --git a/Tests/LibWeb/Text/expected/table-cell-percentage-height-in-auto-height-table.txt b/Tests/LibWeb/Text/expected/table-cell-percentage-height-in-auto-height-table.txt
new file mode 100644
index 00000000000..1d500ee1158
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/table-cell-percentage-height-in-auto-height-table.txt
@@ -0,0 +1,16 @@
+middle 1: row height 24, cell height 24, content offset 0
+middle 2: row height 24, cell height 24, content offset 0
+middle 3: row height 24, cell height 24, content offset 0
+middle 4: row height 24, cell height 24, content offset 0
+middle table height 121
+bottom 1: row height 24, cell height 24, content offset 0
+bottom 2: row height 24, cell height 24, content offset 0
+bottom 3: row height 24, cell height 24, content offset 0
+bottom 4: row height 24, cell height 24, content offset 0
+bottom table height 121
+single 1: row height 24, cell height 24, content offset 0
+single table height 34
+50% td: row height 24, cell height 24, content offset 0
+50% td table height 34
+calc(50% + 1px) td: row height 24, cell height 24, content offset 0
+calc(50% + 1px) td table height 34
diff --git a/Tests/LibWeb/Text/expected/table-measurement-reuse-geometry.txt b/Tests/LibWeb/Text/expected/table-measurement-reuse-geometry.txt
index 2e775aebe86..be5e4bb5e3d 100644
--- a/Tests/LibWeb/Text/expected/table-measurement-reuse-geometry.txt
+++ b/Tests/LibWeb/Text/expected/table-measurement-reuse-geometry.txt
@@ -5,16 +5,16 @@ top/percentage-child: 160,72,46,3,0 -> 160,92,66,3,0
top/percentage-min-height: 160,72,46,3,20 -> 160,92,66,3,20
middle/auto: 160,72,46,13,20 -> 160,92,66,23,20
middle/rowspan: 160,72,72,26,20 -> 160,92,92,36,20
-middle/percentage-cell: 160,72,46,5,20 -> 160,92,66,10,20
+middle/percentage-cell: 160,72,46,13,20 -> 160,92,66,23,20
middle/percentage-child: 160,72,46,23,0 -> 160,92,66,33,0
middle/percentage-min-height: 160,72,46,13,20 -> 160,92,66,23,20
bottom/auto: 160,72,46,23,20 -> 160,92,66,43,20
bottom/rowspan: 160,72,72,49,20 -> 160,92,92,69,20
-bottom/percentage-cell: 160,72,46,7,20 -> 160,92,66,17,20
+bottom/percentage-cell: 160,72,46,23,20 -> 160,92,66,43,20
bottom/percentage-child: 160,72,46,43,0 -> 160,92,66,63,0
bottom/percentage-min-height: 160,72,46,23,20 -> 160,92,66,43,20
baseline/auto: 160,72,46,23,20 -> 160,92,66,43,20
baseline/rowspan: 160,72,72,23,20 -> 160,92,92,43,20
-baseline/percentage-cell: 160,72,46,7,20 -> 160,92,66,17,20
+baseline/percentage-cell: 160,72,46,23,20 -> 160,92,66,43,20
baseline/percentage-child: 160,72,46,43,0 -> 160,92,66,63,0
baseline/percentage-min-height: 160,72,46,23,20 -> 160,92,66,43,20
diff --git a/Tests/LibWeb/Text/input/layout-run-cache-table-percentage-cell-height-misses.html b/Tests/LibWeb/Text/input/layout-run-cache-table-percentage-cell-height-misses.html
index f55b5faf8fb..217ae8a97aa 100644
--- a/Tests/LibWeb/Text/input/layout-run-cache-table-percentage-cell-height-misses.html
+++ b/Tests/LibWeb/Text/input/layout-run-cache-table-percentage-cell-height-misses.html
@@ -6,6 +6,7 @@
#container { width: 300px; height: 100px; }
table { border-spacing: 0; width: 100px; }
td { padding: 0; }
+ #percentage-table { height: 100%; }
#percentage-cell { height: 50%; }
#fixed-cell { height: 20px; }
</style>
@@ -21,28 +22,30 @@
name: "only the fixed-height table is reused",
mutate: () => {
heightBefore = document.getElementById("percentage-table").getBoundingClientRect().height;
- // The percentage-height cell observes the basis forwarded by its table.
+ // The percentage-height cell observes the basis forwarded by its table, whose own percentage height
+ // follows the container.
container.style.height = "200px";
},
expect: `
layout FULL
@viewport/block RUN (cache=bypass)
BlockContainer<HTML>/block RUN (cache=miss)
- BlockContainer<TD>#percentage-cell MEASURE min-content inline (cache=hit)
+ BlockContainer<TD>#percentage-cell MEASURE min-content inline (cache=miss) FROM ITEMS
TableWrapper(anonymous)/block RUN (cache=miss)
Box<TABLE>#percentage-table/table RUN (cache=miss)
BlockContainer<TD>#percentage-cell MEASURE min-content inline (cache=hit)
+ BlockContainer<TD>#percentage-cell MEASURE max-content block (cache=miss)
+ BlockContainer<TD>#percentage-cell/block RUN (cache=miss)
BlockContainer<TD>#percentage-cell MEASURE max-content block (cache=hit)
- BlockContainer<TD>#percentage-cell MEASURE max-content block (cache=hit)
- BlockContainer<TD>#percentage-cell/block READ RESULT AHEAD OF INTRINSIC BLOCK PADDING
BlockContainer<TD>#percentage-cell/block RUN AHEAD OF INTRINSIC BLOCK PADDING
+ BlockContainer<TD>#percentage-cell/block READ RESULT AHEAD OF INTRINSIC BLOCK PADDING
BlockContainer<TD>#percentage-cell/block REPLAY FRAGMENTS
BlockContainer<TD>#fixed-cell MEASURE min-content inline (cache=hit)
TableWrapper(anonymous)/block REUSE SUBTREE
`,
check: equal => {
- equal(heightBefore, 50, "percentage table height before");
- equal(document.getElementById("percentage-table").getBoundingClientRect().height, 100, "percentage table height after");
+ equal(heightBefore, 100, "percentage table height before");
+ equal(document.getElementById("percentage-table").getBoundingClientRect().height, 200, "percentage table height after");
equal(document.getElementById("fixed-table").getBoundingClientRect().height, 20, "fixed table height");
},
},
diff --git a/Tests/LibWeb/Text/input/table-cell-percentage-height-in-auto-height-table.html b/Tests/LibWeb/Text/input/table-cell-percentage-height-in-auto-height-table.html
new file mode 100644
index 00000000000..e5c0b334bab
--- /dev/null
+++ b/Tests/LibWeb/Text/input/table-cell-percentage-height-in-auto-height-table.html
@@ -0,0 +1,47 @@
+<!doctype html>
+<script src="include.js"></script>
+<style>
+ body { margin: 0; }
+ .table { display: table; border-spacing: 5px; }
+ .row { display: table-row; }
+ .label, .cell { display: table-cell; }
+ .cell { height: 100%; }
+ .box { height: 24px; width: 200px; }
+ .container { height: 100px; }
+ table { border-spacing: 5px; }
+ td { padding: 0; }
+</style>
+<div id="middle" class="table"></div>
+<div id="bottom" class="table"></div>
+<div id="single" class="table"></div>
+<div class="container"><table><tr><td id="half" style="height: 50%; vertical-align: middle"><div class="box"></div></td></tr></table></div>
+<div class="container"><table><tr><td id="calc" style="height: calc(50% + 1px); vertical-align: middle"><div class="box"></div></td></tr></table></div>
+<script>
+ function fill(table, rows, alignment) {
+ for (let i = 0; i < rows; ++i)
+ table.insertAdjacentHTML("beforeend", `<div class="row"><div class="label">Row ${i + 1}</div><div class="cell" style="vertical-align: ${alignment}"><div class="box"></div></div></div>`);
+ }
+
+ function report(name, cell) {
+ const row = cell.parentElement.getBoundingClientRect();
+ const cellRect = cell.getBoundingClientRect();
+ const box = cell.firstElementChild.getBoundingClientRect();
+ println(`${name}: row height ${row.height}, cell height ${cellRect.height}, content offset ${box.top - cellRect.top}`);
+ }
+
+ test(() => {
+ fill(document.getElementById("middle"), 4, "middle");
+ fill(document.getElementById("bottom"), 4, "bottom");
+ fill(document.getElementById("single"), 1, "middle");
+ for (const id of ["middle", "bottom", "single"]) {
+ document.getElementById(id).querySelectorAll(".cell").forEach((cell, index) => report(`${id} ${index + 1}`, cell));
+ println(`${id} table height ${document.getElementById(id).getBoundingClientRect().height}`);
+ }
+ const half = document.getElementById("half");
+ report("50% td", half);
+ println(`50% td table height ${half.closest("table").getBoundingClientRect().height}`);
+ const calc = document.getElementById("calc");
+ report("calc(50% + 1px) td", calc);
+ println(`calc(50% + 1px) td table height ${calc.closest("table").getBoundingClientRect().height}`);
+ });
+</script>