Skip to content

Truly independent web browser

LibWeb: Align the contents of percentage-height table cells

sideshowbarker48e5220b97e0
diff --git a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
index 4aa46d8fce3..51203465cc6 100644
--- a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
@@ -3205,9 +3205,16 @@ impl<'pass> TableFormattingContext<'pass> {
let inner = cell_box.available_inner_space_or_constraints_from(self.available_space);
self.cell_layouts[cell_index].inside_layout_input = inner;
self.cell_layouts[cell_index].box_cells = cell_box;
- // The first pass laid this cell out at its automatic block size; lay it out again at the
- // percentage-resolved size, which it keeps.
- self.layout_inside_cell(run, cell_index, RootSizingDirectives::default());
+ // The resolved size supplies the percentage basis for descendants. Keep the content's automatic size
+ // for alignment within the rows, as in the first pass.
+ self.layout_inside_cell(
+ run,
+ cell_index,
+ RootSizingDirectives {
+ content_block_size_after_run: geometry::ContentBlockSizeAfterRun::Automatic,
+ ..RootSizingDirectives::default()
+ },
+ );
self.cells[cell_index].baseline = self.cell_baseline(cell_index);
if !self.rows[cell.row_index].is_collapsed {
let border_size = cell_box.border_box_block_size(collapsed);
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-tables/table-cell-overflow-auto-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-tables/table-cell-overflow-auto-ref.html
new file mode 100644
index 00000000000..695ba5341ed
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-tables/table-cell-overflow-auto-ref.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<div style="width: 100px; height: 100px; border: solid;">
+ <div style="max-width: 100px; height: 100px; background: green; overflow-x: scroll;">
+ <div style="width: 120px; height: 50px; background: hotpink;"></div>
+ </div>
+</div>
+<br>
+<div style="width: 100px; height: 100px; border: solid;">
+ <div style="display: table-cell; max-width: 100px; height: 100px; background: green; overflow-x: scroll; vertical-align: middle;">
+ <div style="width: 120px; height: 50px; background: hotpink;"></div>
+ </div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-tables/table-cell-overflow-auto.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-tables/table-cell-overflow-auto.html
new file mode 100644
index 00000000000..e5ec5c874ba
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-tables/table-cell-overflow-auto.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<link rel="help" href="https://drafts.csswg.org/css-tables-3/#row-layout">
+<link rel="match" href="../../../../expected/wpt-import/css/css-tables/table-cell-overflow-auto-ref.html">
+<meta name="assert" content="horizontal scrollbars show up on table-cells with overflowing content and overflow:auto, vertical-align: middle/top" />
+<div style="width: 100px; height: 100px; border: solid;">
+ <div style="display: table-cell; max-width: 100px; height: 100px; background: green; overflow-x: auto; vertical-align: top;">
+ <div style="width: 120px; height: 50px; background: hotpink;"></div>
+ </div>
+</div>
+<br>
+<div style="width: 100px; height: 100px; border: solid;">
+ <div style="display: table-cell; max-width: 100px; height: 100px; background: green; overflow-x: auto; vertical-align: middle;">
+ <div style="width: 120px; height: 50px; background: hotpink;"></div>
+ </div>
+</div>
diff --git a/Tests/LibWeb/Text/expected/css/table-percentage-cell-content-alignment.txt b/Tests/LibWeb/Text/expected/css/table-percentage-cell-content-alignment.txt
new file mode 100644
index 00000000000..2d926abdf50
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/table-percentage-cell-content-alignment.txt
@@ -0,0 +1,14 @@
+auto/top: cell fills host=true, image aligned=true, horizontally centered=true
+auto/middle: cell fills host=true, image aligned=true, horizontally centered=true
+auto/bottom: cell fills host=true, image aligned=true, horizontally centered=true
+100%/top: cell fills host=true, image aligned=true, horizontally centered=true
+100%/middle: cell fills host=true, image aligned=true, horizontally centered=true
+100%/bottom: cell fills host=true, image aligned=true, horizontally centered=true
+50%/top: cell fills host=true, image aligned=true, horizontally centered=true
+50%/middle: cell fills host=true, image aligned=true, horizontally centered=true
+50%/bottom: cell fills host=true, image aligned=true, horizontally centered=true
+100px/top: cell fills host=true, image aligned=true, horizontally centered=true
+100px/middle: cell fills host=true, image aligned=true, horizontally centered=true
+100px/bottom: cell fills host=true, image aligned=true, horizontally centered=true
+percentage child: height=true, centered=true
+positioned cell: image centered=true, absolute child=true
diff --git a/Tests/LibWeb/Text/input/css/table-percentage-cell-content-alignment.html b/Tests/LibWeb/Text/input/css/table-percentage-cell-content-alignment.html
new file mode 100644
index 00000000000..75b80dcbdba
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/table-percentage-cell-content-alignment.html
@@ -0,0 +1,46 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ .host { position: fixed; inset: 0 0 86px; }
+ .table { display: table; width: 100%; height: 100%; }
+ .cell { display: table-cell; width: 100%; }
+ .image { display: block; width: 64px; height: 64px; margin-inline: auto; }
+</style>
+<script>
+ promiseTest(async () => {
+ const host = document.createElement("div");
+ host.className = "host";
+ host.innerHTML = `<div class="table"><div class="cell"><img class="image" src="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='64' height='64'><rect width='64' height='64' fill='green'/></svg>"></div></div>`;
+ document.body.append(host);
+ const cell = host.querySelector(".cell");
+ const image = host.querySelector("img");
+ await image.decode();
+ for (const height of ["auto", "100%", "50%", "100px"]) {
+ cell.style.height = height;
+ for (const alignment of ["top", "middle", "bottom"]) {
+ cell.style.verticalAlign = alignment;
+ const outer = host.getBoundingClientRect();
+ const c = cell.getBoundingClientRect();
+ const i = image.getBoundingClientRect();
+ const fraction = alignment === "middle" ? 0.5 : alignment === "bottom" ? 1 : 0;
+ println(`${height}/${alignment}: cell fills host=${c.height === outer.height}, image aligned=${i.y - c.y === (c.height - i.height) * fraction}, horizontally centered=${i.x - c.x === (c.width - i.width) / 2}`);
+ }
+ }
+ cell.style.height = "100%";
+ cell.style.verticalAlign = "middle";
+ image.style.height = "50%";
+ const percentageCell = cell.getBoundingClientRect();
+ const percentageImage = image.getBoundingClientRect();
+ println(`percentage child: height=${percentageImage.height === percentageCell.height / 2}, centered=${percentageImage.y - percentageCell.y === percentageCell.height / 4}`);
+
+ image.style.height = "64px";
+ cell.style.position = "relative";
+ const positioned = document.createElement("div");
+ positioned.style.cssText = "position: absolute; top: 50%; left: 0; width: 10px; height: 10px";
+ cell.append(positioned);
+ const positionedCell = cell.getBoundingClientRect();
+ const positionedImage = image.getBoundingClientRect();
+ println(`positioned cell: image centered=${positionedImage.y - positionedCell.y === (positionedCell.height - positionedImage.height) / 2}, absolute child=${positioned.getBoundingClientRect().y - positionedCell.y === positionedCell.height / 2}`);
+ host.remove();
+ });
+</script>