Skip to content

Truly independent web browser

LibWeb: Make a table’s wrapper as wide as the table

sideshowbarker1b1a1ccd6baf
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index 7713e297784..76d2acd2f0c 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -2203,15 +2203,15 @@ impl<'pass> BlockFormattingContext<'pass> {
replaced_inline_size
} else if facts.is_table_wrapper() {
// NB: An auto-width table shrinks to fit the space next to floats. A table with a specified width does not,
- // so clamp it against the containing block instead. That way a table which is too wide for the space
- // next to floats reports its real size and gets placed below the floats.
+ // so its percentages resolve against the containing block instead. Either way, the wrapper reports the
+ // real size of the table, so that a table too wide for the space next to floats is placed below them.
let table_has_specified_inline_size = !self.style(sizing.table_box_inside_wrapper(node)).width().is_auto();
let (inline_size, table_inline_layout) = sizing.compute_table_box_inline_size_inside_wrapper(
node,
remaining_available_space,
constraints,
table_has_specified_inline_size.then_some(available_inline_size),
- formatting_context::TableWrapperInlineSizeMode::ClampToAvailableInlineSize,
+ formatting_context::TableWrapperInlineSizeMode::ShrinkToAvailableInlineSize,
);
if node != self.run.box_ {
self.keep_table_inline_layout_of(node, table_inline_layout);
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index e55cba2973f..a0f60ee23fc 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -98,7 +98,11 @@ pub(super) enum SizeDimension {
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum TableWrapperInlineSizeMode {
- ClampToAvailableInlineSize,
+ /// An auto-width table shrinks to fit the available inline size, but never below its minimum inline size, and the
+ /// wrapper is as wide as the table box, even when that's wider than the available inline size: A table that
+ /// doesn't fit there reports its real size, so that its captions are laid out as wide as it is, and a table too
+ /// wide for the space next to floats is placed below them.
+ ShrinkToAvailableInlineSize,
UseTableUsedInlineSizeIfNotAuto,
/// The wrapper is as wide as the table box, even when that's wider than the available inline size, as for an
/// absolutely positioned table.
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index f97c4e0841d..7d3cc6ba26b 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -4115,8 +4115,16 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
table_wrapper_containing_block_inline_size.unwrap_or_else(|| available_space.inline_size.to_px_or_zero());
// If 'margin-left', or 'margin-right' are computed as 'auto', their used value is '0'.
- let margin_left = style.margin_left().to_px(containing_block_inline_size);
- let margin_right = style.margin_right().to_px(containing_block_inline_size);
+ // Percentage margins resolve against the containing block, even where the space the wrapper has is only what
+ // the floats next to it leave, as placing the wrapper resolves them.
+ let margin_percentage_basis = match table_wrapper_inline_size_mode {
+ formatting_context::TableWrapperInlineSizeMode::ShrinkToAvailableInlineSize => table_wrapper_constraints
+ .percentage_basis_inline_size
+ .unwrap_or(containing_block_inline_size),
+ _ => containing_block_inline_size,
+ };
+ let margin_left = style.margin_left().to_px(margin_percentage_basis);
+ let margin_right = style.margin_right().to_px(margin_percentage_basis);
// table-wrapper can't have borders or paddings but it might have margin taken from table-root.
let available_inline_size = containing_block_inline_size - margin_left - margin_right;
@@ -4139,7 +4147,23 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
table_cells,
table_formatting_context::AutoTableInlineSize::ShrinksToFitAvailableInlineSize,
);
- let table_available = table_cells.available_inner_space_or_constraints_from(available_space);
+ let mut table_available = table_cells.available_inner_space_or_constraints_from(available_space);
+ // The table box of an auto-width table fills its containing block before sizing, which may be wider
+ // than the space the wrapper has (next to floats, e.g.); the table shrinks to fit that space instead.
+ if table_wrapper_inline_size_mode
+ == formatting_context::TableWrapperInlineSizeMode::ShrinkToAvailableInlineSize
+ && self.style(table_run.box_).width().is_auto()
+ && let (AvailableSize::Definite(_), AvailableSize::Definite(table_inline_size)) =
+ (available_space.inline_size, table_available.inline_size)
+ {
+ let offsets = table_cells.border_left
+ + table_cells.padding_left
+ + table_cells.padding_right
+ + table_cells.border_right;
+ table_available.inline_size = AvailableSize::definite(
+ table_inline_size.min((available_inline_size - offsets).max(CssPixels::default())),
+ );
+ }
table.run_until_inline_size_calculation(
LayoutInput::new(
table_available,
@@ -4152,11 +4176,13 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
let table_used = table.table_box_cells();
let table_used_inline_size =
table_used.border_box_inline_size(table_used.uses_collapsing_borders_model());
- let inline_size = if table_wrapper_inline_size_mode
- == formatting_context::TableWrapperInlineSizeMode::UseTableUsedInlineSize
- || (table_wrapper_inline_size_mode
- == formatting_context::TableWrapperInlineSizeMode::UseTableUsedInlineSizeIfNotAuto
- && !self.style(table_run.box_).width().is_auto())
+ let inline_size = if matches!(
+ table_wrapper_inline_size_mode,
+ formatting_context::TableWrapperInlineSizeMode::UseTableUsedInlineSize
+ | formatting_context::TableWrapperInlineSizeMode::ShrinkToAvailableInlineSize
+ ) || (table_wrapper_inline_size_mode
+ == formatting_context::TableWrapperInlineSizeMode::UseTableUsedInlineSizeIfNotAuto
+ && !self.style(table_run.box_).width().is_auto())
{
table_used_inline_size
} else if matches!(available_space.inline_size, AvailableSize::Definite(_)) {
diff --git a/Tests/LibWeb/Layout/expected/display-table-inline-children.txt b/Tests/LibWeb/Layout/expected/display-table-inline-children.txt
index 4f23842fb66..c5a6fb7507a 100644
--- a/Tests/LibWeb/Layout/expected/display-table-inline-children.txt
+++ b/Tests/LibWeb/Layout/expected/display-table-inline-children.txt
@@ -1,7 +1,7 @@
Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline overflow: [0,0 1208x619]
BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 619 0+0+0] [BFC] children: not-inline
BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 603 0+0+8] children: not-inline
- TableWrapper <(anonymous)> at [8,8] [0+0+0 1200 0+0+0] [0+0+0 603 0+0+0] [BFC] children: not-inline
+ TableWrapper <(anonymous)> at [8,8] [0+0+0 1200 0+0+-416] [0+0+0 603 0+0+0] [BFC] children: not-inline
Box <div.aligncenter.block-image> at [8,8] table-box [0+0+0 1200 0+0+0] [0+0+0 603 0+0+0] [TFC] children: not-inline
Box <(anonymous)> at [8,8] table-row [0+0+0 1200 0+0+0] [0+0+0 603 0+0+0] children: not-inline
BlockContainer <(anonymous)> at [8,8] table-cell [0+0+0 1200 0+0+0] [0+0+0 603 0+0+0] [BFC] children: inline
diff --git a/Tests/LibWeb/Text/expected/table/caption-as-wide-as-table.txt b/Tests/LibWeb/Text/expected/table/caption-as-wide-as-table.txt
new file mode 100644
index 00000000000..bf139eadd59
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/table/caption-as-wide-as-table.txt
@@ -0,0 +1,12 @@
+content: table x 1 w 475, caption x 1 w 475 h 20
+fixed: table x 1 w 600, caption x 1 w 600 h 20
+bottom: table x 1 w 600, caption x 1 w 600 h 20
+margin-auto: table x 1 w 600, caption x 1 w 600 h 20
+narrow: table x 1 w 100, caption x 1 w 100 h 20
+caption-margins: table x 1 w 600, caption x 21 w 560 h 20
+caption-width-pct: table x 1 w 600, caption x 1 w 300 h 20
+float: table x 1 w 600, caption x 1 w 600 h 20
+flex-item: table x 1 w 600, caption x 1 w 600 h 20
+long-caption: table x 1 w 600, caption x 1 w 600 h 20
+inline-table: table x 1 w 600, caption x 1 w 600 h 20
+rtl: table x -299 w 600, caption x -299 w 600 h 20
diff --git a/Tests/LibWeb/Text/expected/table/table-too-wide-for-space-next-to-floats.txt b/Tests/LibWeb/Text/expected/table/table-too-wide-for-space-next-to-floats.txt
new file mode 100644
index 00000000000..cfa7614d6b3
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/table/table-too-wide-for-space-next-to-floats.txt
@@ -0,0 +1,13 @@
+too-wide: x 0 y 100 w 485
+fits: x 0 y 0 w 194
+wraps-to-fit: x 0 y 0 w 400
+wider-than-line: x 0 y 100 w 679
+left-float-too-wide: x 0 y 100 w 485
+fixed-450: x 0 y 100 w 450
+pct-80: x 0 y 100 w 480
+two-floats-stepped: x 0 y 100 w 456
+caption: x 0 y 100 w 485
+rtl-too-wide: x 115 y 100 w 485
+pct-margin-left-wraps: x 60 y 0 w 340
+pct-margin-right-wraps: x 0 y 0 w 400
+left-float-pct-margin-wraps: x 200 y 0 w 400
diff --git a/Tests/LibWeb/Text/input/table/caption-as-wide-as-table.html b/Tests/LibWeb/Text/input/table/caption-as-wide-as-table.html
new file mode 100644
index 00000000000..ba637e739f8
--- /dev/null
+++ b/Tests/LibWeb/Text/input/table/caption-as-wide-as-table.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<style>
+ body { font: 16px/20px monospace; }
+ .scroll { width: 300px; overflow: auto; border: 1px solid; margin-bottom: 8px; }
+ td { white-space: nowrap; border: 1px solid; padding: 0; }
+ caption { background: #cde; text-align: center; }
+</style>
+<div class="scroll"><table id="content"><caption>Caption</caption><tr><td>A long cell that makes the table wider than its container</td></tr></table></div>
+<div class="scroll"><table id="fixed" style="width: 600px"><caption>Caption</caption><tr><td>x</td></tr></table></div>
+<div class="scroll"><table id="bottom" style="width: 600px; caption-side: bottom"><caption>Caption</caption><tr><td>x</td></tr></table></div>
+<div class="scroll"><table id="margin-auto" style="width: 600px; margin: 0 auto"><caption>Caption</caption><tr><td>x</td></tr></table></div>
+<div class="scroll"><table id="narrow" style="width: 100px"><caption>Caption</caption><tr><td>x</td></tr></table></div>
+<div class="scroll"><table id="caption-margins" style="width: 600px"><caption style="margin: 0 20px">Caption</caption><tr><td>x</td></tr></table></div>
+<div class="scroll"><table id="caption-width-pct" style="width: 600px"><caption style="width: 50%">Caption</caption><tr><td>x</td></tr></table></div>
+<div class="scroll"><table id="float" style="width: 600px; float: left"><caption>Caption</caption><tr><td>x</td></tr></table></div>
+<div class="scroll" style="display: flex"><table id="flex-item" style="width: 600px; flex-shrink: 0"><caption>Caption</caption><tr><td>x</td></tr></table></div>
+<div class="scroll"><table id="long-caption" style="width: 600px"><caption>word word word word word word word word word word word word</caption><tr><td>x</td></tr></table></div>
+<div class="scroll"><table id="inline-table" style="width: 600px; display: inline-table"><caption>Caption</caption><tr><td>x</td></tr></table></div>
+<div class="scroll" style="direction: rtl"><table id="rtl" style="width: 600px"><caption>Caption</caption><tr><td>x</td></tr></table></div>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const lines = [];
+ for (const t of document.querySelectorAll("table")) {
+ const tr = t.getBoundingClientRect(), cr = t.caption.getBoundingClientRect(), sr = t.parentElement.getBoundingClientRect();
+ lines.push(`${t.id}: table x ${Math.round(tr.left - sr.left)} w ${Math.round(tr.width)}, caption x ${Math.round(cr.left - sr.left)} w ${Math.round(cr.width)} h ${Math.round(cr.height)}`);
+ }
+ for (const line of lines) println(line);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/table/table-too-wide-for-space-next-to-floats.html b/Tests/LibWeb/Text/input/table/table-too-wide-for-space-next-to-floats.html
new file mode 100644
index 00000000000..ddddc9f315a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/table/table-too-wide-for-space-next-to-floats.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<style>
+ body { margin: 0; font: 16px/20px monospace; }
+ .c { width: 600px; outline: 1px solid; margin-bottom: 10px; overflow: hidden; }
+ .f { float: right; width: 200px; height: 100px; background: #ccc; }
+ .fl { float: left; width: 200px; height: 100px; background: #ccc; }
+ td { white-space: nowrap; padding: 0; }
+ table { border-spacing: 0; }
+</style>
+<div class="c"><div class="f"></div><table id="too-wide"><tr><td>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</td></tr></table></div>
+<div class="c"><div class="f"></div><table id="fits"><tr><td>xxxxxxxxxxxxxxxxxxxx</td></tr></table></div>
+<div class="c"><div class="f"></div><table id="wraps-to-fit"><tr><td style="white-space: normal">xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx</td></tr></table></div>
+<div class="c"><div class="f"></div><table id="wider-than-line"><tr><td>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</td></tr></table></div>
+<div class="c"><div class="fl"></div><table id="left-float-too-wide"><tr><td>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</td></tr></table></div>
+<div class="c"><div class="f"></div><table id="fixed-450" style="width: 450px"><tr><td>x</td></tr></table></div>
+<div class="c"><div class="f"></div><table id="pct-80" style="width: 80%"><tr><td>x</td></tr></table></div>
+<div class="c"><div class="f"></div><div class="f" style="height: 50px; width: 100px; clear: right"></div><table id="two-floats-stepped"><tr><td>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</td></tr></table></div>
+<div class="c"><div class="f"></div><table id="caption" style="caption-side: top"><caption>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</caption><tr><td>x</td></tr></table></div>
+<div class="c" style="direction: rtl"><div class="f" style="float: left"></div><table id="rtl-too-wide"><tr><td>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</td></tr></table></div>
+<div class="c"><div class="f"></div><table id="pct-margin-left-wraps" style="margin-left: 10%"><tr><td style="white-space: normal">xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx</td></tr></table></div>
+<div class="c"><div class="f"></div><table id="pct-margin-right-wraps" style="margin-right: 10%"><tr><td style="white-space: normal">xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx</td></tr></table></div>
+<div class="c"><div class="fl"></div><table id="left-float-pct-margin-wraps" style="margin-left: 10%"><tr><td style="white-space: normal">xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx</td></tr></table></div>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const lines = [];
+ for (const t of document.querySelectorAll("table")) {
+ const r = t.getBoundingClientRect(), c = t.parentElement.getBoundingClientRect();
+ lines.push(`${t.id}: x ${Math.round(r.left - c.left)} y ${Math.round(r.top - c.top)} w ${Math.round(r.width)}`);
+ }
+ for (const line of lines) println(line);
+ });
+</script>