Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(layout): implement automatic table column sizing

ldm0c1204434f77c
diff --git a/Cargo.lock b/Cargo.lock
index ee4b0646b..49542b4c7 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4812,7 +4812,7 @@ dependencies = [
[[package]]
name = "taffy"
version = "0.13.0"
-source = "git+https://github.com/ldm0/taffy?rev=847d1ce20c03b0378eafa530fa43f1e92d9292ea#847d1ce20c03b0378eafa530fa43f1e92d9292ea"
+source = "git+https://github.com/ldm0/taffy?rev=71bc149ce5358406cdabba66da853ceb11ac74ee#71bc149ce5358406cdabba66da853ceb11ac74ee"
dependencies = [
"arrayvec",
"serde",
diff --git a/Cargo.toml b/Cargo.toml
index e22c761ee..1d33a2dc0 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -98,7 +98,7 @@ stylo_atoms = { git = "https://github.com/ldm0/stylo", rev = "32c3de3f2aab3340c5
stylo_dom = { git = "https://github.com/ldm0/stylo", rev = "32c3de3f2aab3340c51f03f2a0878d1e4eed7fd6" }
stylo_static_prefs = { git = "https://github.com/ldm0/stylo", rev = "32c3de3f2aab3340c51f03f2a0878d1e4eed7fd6" }
stylo_traits = { git = "https://github.com/ldm0/stylo", rev = "32c3de3f2aab3340c51f03f2a0878d1e4eed7fd6" }
-taffy = { git = "https://github.com/ldm0/taffy", rev = "847d1ce20c03b0378eafa530fa43f1e92d9292ea" }
+taffy = { git = "https://github.com/ldm0/taffy", rev = "71bc149ce5358406cdabba66da853ceb11ac74ee" }
[workspace.lints.clippy]
disallowed_methods = "deny"
diff --git a/moli-layout/src/replaced.rs b/moli-layout/src/replaced.rs
index 578c703fb..6ae35ddb9 100644
--- a/moli-layout/src/replaced.rs
+++ b/moli-layout/src/replaced.rs
@@ -209,7 +209,11 @@ fn apply_aspect_ratio_to_content_size(
padding_border: Size<f32>,
) -> Size<Option<f32>> {
size.maybe_add(padding_border)
- .maybe_apply_resolved_aspect_ratio(aspect_ratio, padding_border)
+ .maybe_apply_aspect_ratio_with_box_sizing(
+ aspect_ratio,
+ BoxSizing::BorderBox,
+ padding_border,
+ )
.maybe_sub(padding_border)
.maybe_max(Size::ZERO)
}
diff --git a/moli-layout/src/style.rs b/moli-layout/src/style.rs
index 832de8fa1..7e933ccc5 100644
--- a/moli-layout/src/style.rs
+++ b/moli-layout/src/style.rs
@@ -131,11 +131,6 @@ pub(crate) enum InlineDirection {
}
/// Physical block-flow direction retained at the Stylo/Taffy boundary.
-///
-/// Taffy currently has no writing-mode input, so its numeric algorithms run
-/// in horizontal-tb coordinates. Keeping the full authored mode here lets the
-/// browser adapter physicalize the result without confusing `vertical-lr`
-/// and `vertical-rl`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) enum LayoutWritingMode {
#[default]
@@ -157,6 +152,15 @@ impl LayoutWritingMode {
matches!(self, Self::HorizontalTb)
}
+ /// Project the inherited Stylo value into Taffy's logical constraint space.
+ pub(crate) const fn to_taffy(self) -> taffy::WritingMode {
+ match self {
+ Self::HorizontalTb => taffy::WritingMode::HorizontalTb,
+ Self::VerticalRl => taffy::WritingMode::VerticalRl,
+ Self::VerticalLr => taffy::WritingMode::VerticalLr,
+ }
+ }
+
/// Physical edge used as block-start by vertical normal flow.
pub(crate) const fn vertical_block_start_is_right(self) -> Option<bool> {
match self {
@@ -1935,6 +1939,11 @@ impl ResolvedLayoutStyle {
self.writing_mode.vertical_block_start_is_right()
}
+ /// Returns the inherited writing mode used by the numeric layout tree.
+ pub(crate) const fn writing_mode(&self) -> taffy::WritingMode {
+ self.writing_mode.to_taffy()
+ }
+
pub(crate) fn text_leaf_from(parent: &Self) -> Self {
Self {
computed: parent.computed.clone(),
diff --git a/moli-layout/src/table.rs b/moli-layout/src/table.rs
index 02939f7b5..b92569885 100644
--- a/moli-layout/src/table.rs
+++ b/moli-layout/src/table.rs
@@ -13,10 +13,11 @@ use std::{fmt::Debug, hash::Hash};
use style::Atom;
use taffy::{
- AvailableSpace, DetailedGridInfo, Dimension, Display, GridAutoFlow, Layout,
- LayoutGridContainer, LayoutInput, LayoutOutput, LayoutPartialTree, Line, MaybeMath,
- MaybeResolve, NodeId, Point, Rect, ResolveOrZero, RunMode, Size, SizingMode, SizingPurpose,
- Style, TraversePartialTree, TraverseTree, compute_grid_layout, style_helpers,
+ AutoSizeBehavior, AvailableSpace, CacheTree, DetailedGridInfo, Dimension, Display,
+ GridAutoFlow, IntrinsicSizeResult, Layout, LayoutGridContainer, LayoutInput, LayoutOutput,
+ LayoutPartialTree, Line, LogicalSize, MaybeResolve, NodeId, Point, Rect, RequestedAxis,
+ ResolveOrZero, RunMode, Size, SizingMode, SizingPurpose, Style, TraversePartialTree,
+ TraverseTree, WritingMode, compute_grid_layout, style_helpers,
};
use crate::{LayoutBoxId, LayoutBoxKind, LayoutWorld, style::resolve_stylo_calc_value};
@@ -27,8 +28,9 @@ mod columns;
pub(crate) use collapsed_borders::CollapsedTableBorders;
use collapsed_borders::{prepare_collapsed_table_borders, set_collapsed_border_geometry};
use columns::{
- TableCellInlineConstraint, TableCellSpanConstraint, TableColumnConstraint,
- distribute_fixed_cell_spans, distribute_fixed_columns, fixed_grid_min_inline_size,
+ AutomaticTableSizingTarget, TableCellInlineConstraint, TableCellSpanConstraint,
+ TableColumnConstraint, TableLayoutMode, apply_cell_constraints, compute_grid_inline_min_max,
+ distribute_auto_columns, distribute_fixed_columns, fixed_grid_min_inline_size,
};
#[derive(Clone)]
@@ -127,8 +129,9 @@ struct TableContext {
collapsed_borders: bool,
column_count: usize,
column_constraints: Vec<TableColumnConstraint>,
- fixed_layout: bool,
+ layout_mode: TableLayoutMode,
inline_border_spacing: f32,
+ writing_mode: WritingMode,
}
pub(crate) fn prepare_table_layout_trees<N>(world: &mut LayoutWorld<N>)
@@ -175,7 +178,8 @@ fn apply_parent_facing_table_inline_constraints<N>(world: &mut LayoutWorld<N>, r
where
N: Copy + Debug + Eq + Hash,
{
- let context = build_table_context(world, root);
+ let mut context = build_table_context(world, root);
+ context.collect_authored_fixed_cell_constraints(world);
let Some(min_border_box_size) = context.fixed_grid_min_border_box_size() else {
return;
};
@@ -234,16 +238,18 @@ where
N: Copy + Debug + Eq + Hash,
{
let mut context = build_table_context(world, root);
- context.resolve_column_tracks(inputs);
+ context.collect_cell_inline_constraints(world);
+ let grid_inputs = context.resolve_column_tracks(inputs);
let mut output = {
let mut wrapper = TableTreeWrapper {
world,
context: &mut context,
};
- compute_grid_layout(&mut wrapper, NodeId::from(0usize), inputs)
+ compute_grid_layout(&mut wrapper, NodeId::from(0usize), grid_inputs)
};
if inputs.run_mode == RunMode::PerformLayout {
+ let caption_parent_writing_mode = world.boxes[root.index()].style.writing_mode();
let top_captions = context
.captions
.iter()
@@ -256,13 +262,20 @@ where
.copied()
.filter(|caption| world.boxes[caption.index()].style.caption_is_bottom())
.collect::<Vec<_>>();
- let top_height = layout_captions(world, &top_captions, output.size.width, 0.0);
+ let top_height = layout_captions(
+ world,
+ &top_captions,
+ output.size.width,
+ 0.0,
+ caption_parent_writing_mode,
+ );
shift_grid_children(world, &context.cells, top_height);
let bottom_height = layout_captions(
world,
&bottom_captions,
output.size.width,
top_height + output.size.height,
+ caption_parent_writing_mode,
);
apply_structural_layout(world, root, &context, top_height, output.size);
if let Some(first_baseline) = &mut output.first_baselines.y {
@@ -303,8 +316,12 @@ where
let mut columns = Vec::new();
let mut max_columns = 0usize;
let mut column_tracks = Vec::new();
- let mut cell_span_constraints = Vec::new();
- let fixed_layout = root_style.table_layout_is_fixed();
+ let layout_mode = if root_style.table_layout_is_fixed() {
+ TableLayoutMode::Fixed
+ } else {
+ TableLayoutMode::Automatic
+ };
+ let writing_mode = root_style.writing_mode();
for column in grouped_children.columns.iter().copied() {
collect_columns(world, column, None, &mut columns, &mut column_tracks);
}
@@ -312,6 +329,8 @@ where
collect_rows(world, section, None, &mut rows, &mut cells);
}
place_table_cells(&mut cells, &rows, &mut max_columns);
+ max_columns = max_columns.max(column_tracks.len()).max(1);
+ column_tracks.resize(max_columns, TableColumnConstraint::auto());
for cell in &mut cells {
cell.style.grid_column = Line {
start: style_helpers::line((cell.column + 1).min(i16::MAX as usize) as i16),
@@ -321,44 +340,12 @@ where
start: style_helpers::line((cell.row + 1).min(i16::MAX as usize) as i16),
end: style_helpers::span(cell.row_span as u16),
};
- if cell.row == 0 {
- let inline_constraint = table_cell_inline_constraint(&cell.style, fixed_layout);
- if cell.column_span == 1 {
- if cell.column >= column_tracks.len() {
- column_tracks.resize(cell.column + 1, TableColumnConstraint::auto());
- }
- column_tracks[cell.column].encompass_first_row_cell(inline_constraint);
- } else if fixed_layout {
- cell_span_constraints.push(TableCellSpanConstraint {
- start_column: cell.column,
- span: cell.column_span,
- cell: inline_constraint,
- });
- }
- }
- cell.style.size.width = Dimension::auto();
- // CSS table-cell `height` is a minimum contribution, while the used
- // border box still fills its row or rowspan. A definite grid-item
- // height would leave a rowspan cell at one-row height instead.
- if cell.style.min_size.height == Dimension::auto() {
- cell.style.min_size.height = cell.style.size.height;
- }
- cell.style.size.height = Dimension::auto();
+ clear_table_cell_inline_sizing(&mut cell.style, writing_mode);
+ normalize_table_cell_block_sizing(&mut cell.style, writing_mode);
}
- max_columns = max_columns.max(column_tracks.len()).max(1);
- column_tracks.resize(max_columns, TableColumnConstraint::auto());
- if fixed_layout {
- distribute_fixed_cell_spans(
- &mut column_tracks,
- &mut cell_span_constraints,
- spacing.width,
- );
- }
- style.grid_template_columns = column_tracks
- .iter()
- .copied()
- .map(|track| track.intrinsic_grid_track().into())
- .collect();
+ let placeholder_track: taffy::TrackSizingFunction = style_helpers::auto();
+ style.grid_template_columns =
+ std::iter::repeat_n(placeholder_track.into(), max_columns).collect();
style.grid_template_rows = if rows.is_empty() {
vec![style_helpers::auto()]
} else {
@@ -389,35 +376,72 @@ where
collapsed_borders: collapsed,
column_count: max_columns,
column_constraints: column_tracks,
- fixed_layout,
+ layout_mode,
inline_border_spacing: spacing.width,
+ writing_mode,
}
}
impl TableContext {
- /// Resolve table column semantics before invoking the numeric Grid backend.
- /// Grid receives final lengths for a definite fixed-layout table and never
- /// participates in the table free-space distribution algorithm.
- fn resolve_column_tracks(&mut self, inputs: LayoutInput) {
- let Some(assignable_inline_size) = self.fixed_assignable_inline_size(inputs) else {
+ /// Gather the fixed-layout first-row widths needed while exposing the
+ /// table's parent-facing minimum before an intrinsic measurement pass can
+ /// borrow the layout world mutably.
+ fn collect_authored_fixed_cell_constraints<N>(&mut self, world: &LayoutWorld<N>)
+ where
+ N: Copy + Debug + Eq + Hash,
+ {
+ if !self.layout_mode.is_fixed() {
return;
- };
- self.style.grid_template_columns =
- distribute_fixed_columns(assignable_inline_size, &self.column_constraints)
- .into_iter()
- .map(|size| {
- let track: taffy::TrackSizingFunction = style_helpers::length(size);
- track.into()
- })
- .collect();
+ }
+ let mut cell_constraints = vec![None; self.column_count];
+ let mut cell_spans = Vec::new();
+ for cell in self.cells.iter().filter(|cell| cell.row == 0) {
+ let constraint = authored_table_cell_inline_constraint(
+ &world.boxes[cell.id.index()].style.taffy,
+ self.writing_mode,
+ self.layout_mode,
+ );
+ collect_cell_constraint(cell, constraint, &mut cell_constraints, &mut cell_spans);
+ }
+ apply_cell_constraints(
+ &mut self.column_constraints,
+ &cell_constraints,
+ &mut cell_spans,
+ self.inline_border_spacing,
+ self.layout_mode,
+ );
}
- fn fixed_assignable_inline_size(&self, inputs: LayoutInput) -> Option<f32> {
- if !self.fixed_layout {
- return None;
+ /// Gather cell measures after the table tree is complete. Fixed layout
+ /// consumes the first visual row; automatic layout consumes every row.
+ fn collect_cell_inline_constraints<N>(&mut self, world: &mut LayoutWorld<N>)
+ where
+ N: Copy + Debug + Eq + Hash,
+ {
+ let mut cell_constraints = vec![None; self.column_count];
+ let mut cell_spans = Vec::new();
+ for cell in &self.cells {
+ if self.layout_mode.is_fixed() && cell.row != 0 {
+ continue;
+ }
+ let constraint =
+ table_cell_inline_constraint(world, cell.id, self.writing_mode, self.layout_mode);
+ collect_cell_constraint(cell, constraint, &mut cell_constraints, &mut cell_spans);
}
+ apply_cell_constraints(
+ &mut self.column_constraints,
+ &cell_constraints,
+ &mut cell_spans,
+ self.inline_border_spacing,
+ self.layout_mode,
+ );
+ }
- let percentage_basis = inputs.parent_size.width;
+ /// Resolve the CSS table's used border-box inline size, synchronize it
+ /// with column constraints, and hand only final lengths to Grid.
+ fn resolve_column_tracks(&mut self, inputs: LayoutInput) -> LayoutInput {
+ let space = inputs.constraint_space(self.writing_mode);
+ let percentage_basis = space.margin_padding_percentage_basis();
let padding = self
.style
.padding
@@ -426,55 +450,140 @@ impl TableContext {
.style
.border
.resolve_or_zero(percentage_basis, resolve_stylo_calc_value);
- let inline_insets = padding.left + padding.right + border.left + border.right;
- let to_border_box = |size: Option<f32>| {
- size.map(|size| {
- if self.style.box_sizing == taffy::BoxSizing::ContentBox {
- size + inline_insets
- } else {
- size.max(inline_insets)
- }
+ let inline_insets = physical_inline_sum(self.writing_mode, padding)
+ + physical_inline_sum(self.writing_mode, border);
+ let internal_spacing =
+ self.inline_border_spacing.max(0.0) * self.column_count.saturating_sub(1) as f32;
+ let undistributable_space = inline_insets + internal_spacing;
+ let grid_min_max = compute_grid_inline_min_max(
+ &self.column_constraints,
+ undistributable_space,
+ self.layout_mode,
+ );
+ let used_inline_size = self.resolve_used_inline_size(
+ inputs,
+ grid_min_max.min,
+ grid_min_max.max,
+ inline_insets,
+ );
+ let assignable_inline_size = (used_inline_size - undistributable_space).max(0.0);
+ let column_sizes = if self.layout_mode.is_fixed() {
+ distribute_fixed_columns(assignable_inline_size, &self.column_constraints)
+ } else {
+ distribute_auto_columns(
+ assignable_inline_size,
+ &self.column_constraints,
+ AutomaticTableSizingTarget::Constrained,
+ )
+ };
+ self.style.grid_template_columns = column_sizes
+ .into_iter()
+ .map(|size| {
+ let track: taffy::TrackSizingFunction = style_helpers::length(size);
+ track.into()
})
+ .collect();
+
+ let numeric_inline_size = if self.style.box_sizing == taffy::BoxSizing::ContentBox {
+ (used_inline_size - inline_insets).max(0.0)
+ } else {
+ used_inline_size
};
- let (preferred, min_size, max_size) = if inputs.sizing_mode == SizingMode::InherentSize {
- (
- to_border_box(
- self.style
- .size
- .width
- .maybe_resolve(percentage_basis, resolve_stylo_calc_value),
- ),
- to_border_box(
- self.style
- .min_size
- .width
- .maybe_resolve(percentage_basis, resolve_stylo_calc_value),
- ),
- to_border_box(
- self.style
- .max_size
- .width
- .maybe_resolve(percentage_basis, resolve_stylo_calc_value),
- ),
- )
+ set_physical_inline_dimension(
+ self.writing_mode,
+ &mut self.style.size,
+ style_helpers::length(numeric_inline_size),
+ );
+ set_physical_inline_dimension(
+ self.writing_mode,
+ &mut self.style.min_size,
+ Dimension::auto(),
+ );
+ set_physical_inline_dimension(
+ self.writing_mode,
+ &mut self.style.max_size,
+ Dimension::auto(),
+ );
+
+ let mut grid_space = space;
+ grid_space.known_size.inline_size = Some(used_inline_size);
+ grid_space.definite_size.inline_size = Some(used_inline_size);
+ grid_space.into_layout_input()
+ }
+
+ fn resolve_used_inline_size(
+ &self,
+ inputs: LayoutInput,
+ grid_min: f32,
+ grid_max: f32,
+ inline_insets: f32,
+ ) -> f32 {
+ let space = inputs.constraint_space(self.writing_mode);
+ let available = space.available_size.inline_size;
+ let fit_content = || match available {
+ AvailableSpace::Definite(value) => grid_min.max(value.max(0.0).min(grid_max)),
+ AvailableSpace::MinContent => grid_min,
+ AvailableSpace::MaxContent => grid_max,
+ };
+ let logical_size = self.writing_mode.to_logical(self.style.size);
+ let logical_min_size = self.writing_mode.to_logical(self.style.min_size);
+ let logical_max_size = self.writing_mode.to_logical(self.style.max_size);
+ let percentage_basis = space.percentage_resolution_size.inline_size;
+ let box_sizing_adjustment = if self.style.box_sizing == taffy::BoxSizing::ContentBox {
+ inline_insets
} else {
- (None, None, None)
+ 0.0
+ };
+ let resolve_dimension = |dimension: Dimension| {
+ if dimension.is_min_content() {
+ Some(grid_min)
+ } else if dimension.is_max_content() {
+ Some(grid_max)
+ } else if dimension.is_fit_content() {
+ Some(fit_content())
+ } else if dimension.is_stretch() {
+ match available {
+ AvailableSpace::Definite(value) => Some(value.max(0.0)),
+ AvailableSpace::MinContent => Some(grid_min),
+ AvailableSpace::MaxContent => Some(grid_max),
+ }
+ } else {
+ dimension
+ .maybe_resolve(percentage_basis, resolve_stylo_calc_value)
+ .map(|size| size + box_sizing_adjustment)
+ }
};
- let synthesized_border_box_size = preferred
- .maybe_clamp(min_size, max_size)
- .maybe_max(Some(inline_insets));
- let border_box_size = inputs
- .known_dimensions
- .width
- .or(synthesized_border_box_size)?;
- let internal_spacing =
- self.inline_border_spacing.max(0.0) * self.column_count.saturating_sub(1) as f32;
- Some((border_box_size - inline_insets - internal_spacing).max(0.0))
+ let authored_sizes_apply = inputs.sizing_mode == SizingMode::InherentSize;
+ let preferred = authored_sizes_apply
+ .then(|| resolve_dimension(logical_size.inline_size))
+ .flatten();
+ let min_size = authored_sizes_apply
+ .then(|| resolve_dimension(logical_min_size.inline_size))
+ .flatten();
+ let max_size = authored_sizes_apply
+ .then(|| resolve_dimension(logical_max_size.inline_size))
+ .flatten();
+
+ let mut used = space
+ .known_size
+ .inline_size
+ .or(preferred)
+ .unwrap_or_else(fit_content);
+ if !self.layout_mode.is_fixed() {
+ used = used.max(grid_min);
+ }
+ if let Some(max_size) = max_size {
+ used = used.min(max_size);
+ }
+ if let Some(min_size) = min_size {
+ used = used.max(min_size);
+ }
+ used.max(inline_insets)
}
fn fixed_grid_min_border_box_size(&self) -> Option<f32> {
- if !self.fixed_layout {
+ if !self.layout_mode.is_fixed() {
return None;
}
@@ -636,6 +745,30 @@ fn place_table_cells(cells: &mut [TableCell], rows: &[TableRow], max_columns: &m
}
}
+fn collect_cell_constraint(
+ cell: &TableCell,
+ constraint: TableCellInlineConstraint,
+ cell_constraints: &mut [Option<TableCellInlineConstraint>],
+ cell_spans: &mut Vec<TableCellSpanConstraint>,
+) {
+ if cell.column_span == 1 {
+ let Some(slot) = cell_constraints.get_mut(cell.column) else {
+ return;
+ };
+ if let Some(existing) = slot {
+ existing.encompass(constraint);
+ } else {
+ *slot = Some(constraint);
+ }
+ } else {
+ cell_spans.push(TableCellSpanConstraint {
+ start_column: cell.column,
+ span: cell.column_span,
+ cell: constraint,
+ });
+ }
+}
+
fn table_data<N>(world: &LayoutWorld<N>, id: LayoutBoxId) -> crate::LayoutTableData
where
N: Copy + Debug + Eq + Hash,
@@ -669,34 +802,216 @@ fn minimum_dimension_track(dimension: Dimension) -> taffy::TrackSizingFunction {
}
}
-fn table_cell_inline_constraint(style: &Style<Atom>, fixed: bool) -> TableCellInlineConstraint {
- match style.size.width.tag() {
- taffy::CompactLength::LENGTH_TAG => {
- let padding = style
- .padding
- .resolve_or_zero(None, resolve_stylo_calc_value);
- let border = style.border.resolve_or_zero(None, resolve_stylo_calc_value);
- let border_padding = padding.left + padding.right + border.left + border.right;
- let outer_width = if style.box_sizing == taffy::BoxSizing::ContentBox {
- style.size.width.value() + border_padding
- } else {
- style.size.width.value().max(border_padding)
- };
- TableCellInlineConstraint::length(outer_width)
+fn authored_table_cell_inline_constraint(
+ style: &Style<Atom>,
+ table_writing_mode: WritingMode,
+ mode: TableLayoutMode,
+) -> TableCellInlineConstraint {
+ let padding = style
+ .padding
+ .resolve_or_zero(None, resolve_stylo_calc_value);
+ let border = style.border.resolve_or_zero(None, resolve_stylo_calc_value);
+ let inline_insets = physical_inline_sum(table_writing_mode, padding)
+ + physical_inline_sum(table_writing_mode, border);
+ let logical_size = table_writing_mode.to_logical(style.size);
+ let preferred = outer_fixed_size(logical_size.inline_size, inline_insets, style.box_sizing);
+ let percent = (logical_size.inline_size.tag() == taffy::CompactLength::PERCENT_TAG)
+ .then(|| logical_size.inline_size.value().max(0.0));
+ let percent_border_padding =
+ if mode.is_fixed() && percent.is_some() && style.box_sizing == taffy::BoxSizing::ContentBox
+ {
+ inline_insets
+ } else {
+ 0.0
+ };
+ TableCellInlineConstraint {
+ min_inline_size: 0.0,
+ max_inline_size: preferred.unwrap_or(percent_border_padding),
+ percent,
+ percent_border_padding,
+ is_constrained: preferred.is_some(),
+ }
+}
+
+fn table_cell_inline_constraint<N>(
+ world: &mut LayoutWorld<N>,
+ cell: LayoutBoxId,
+ table_writing_mode: WritingMode,
+ mode: TableLayoutMode,
+) -> TableCellInlineConstraint
+where
+ N: Copy + Debug + Eq + Hash,
+{
+ let style = world.boxes[cell.index()].style.taffy.clone();
+ let padding = style
+ .padding
+ .resolve_or_zero(None, resolve_stylo_calc_value);
+ let border = style.border.resolve_or_zero(None, resolve_stylo_calc_value);
+ let inline_insets = physical_inline_sum(table_writing_mode, padding)
+ + physical_inline_sum(table_writing_mode, border);
+ let logical_size = table_writing_mode.to_logical(style.size);
+ let logical_min_size = table_writing_mode.to_logical(style.min_size);
+ let logical_max_size = table_writing_mode.to_logical(style.max_size);
+ let preferred = outer_fixed_size(logical_size.inline_size, inline_insets, style.box_sizing);
+ let css_min = outer_fixed_size(
+ logical_min_size.inline_size,
+ inline_insets,
+ style.box_sizing,
+ );
+ let css_max = outer_fixed_size(
+ logical_max_size.inline_size,
+ inline_insets,
+ style.box_sizing,
+ );
+ let percent = (logical_size.inline_size.tag() == taffy::CompactLength::PERCENT_TAG)
+ .then(|| logical_size.inline_size.value().max(0.0));
+
+ let (content_min, content_max) = if mode.is_fixed() {
+ let max = if preferred.is_none() {
+ measure_table_cell_intrinsic_inline_size(
+ world,
+ cell,
+ table_writing_mode,
+ AvailableSpace::MaxContent,
+ )
+ } else {
+ 0.0
+ };
+ (0.0, max)
+ } else {
+ (
+ measure_table_cell_intrinsic_inline_size(
+ world,
+ cell,
+ table_writing_mode,
+ AvailableSpace::MinContent,
+ ),
+ measure_table_cell_intrinsic_inline_size(
+ world,
+ cell,
+ table_writing_mode,
+ AvailableSpace::MaxContent,
+ ),
+ )
+ };
+
+ let mut min_inline_size = if mode.is_fixed() {
+ 0.0
+ } else {
+ content_min.max(css_min.unwrap_or(0.0))
+ };
+ let mut content_max = preferred.unwrap_or(content_max);
+ if let Some(css_max) = css_max {
+ content_max = content_max.min(css_max);
+ min_inline_size = min_inline_size.min(css_max);
+ }
+ let max_inline_size = min_inline_size.max(content_max);
+ let percent_border_padding =
+ if mode.is_fixed() && percent.is_some() && style.box_sizing == taffy::BoxSizing::ContentBox
+ {
+ inline_insets
+ } else {
+ 0.0
+ };
+
+ TableCellInlineConstraint {
+ min_inline_size,
+ max_inline_size,
+ percent,
+ percent_border_padding,
+ is_constrained: preferred.is_some(),
+ }
+}
+
+fn measure_table_cell_intrinsic_inline_size<N>(
+ world: &mut LayoutWorld<N>,
+ cell: LayoutBoxId,
+ table_writing_mode: WritingMode,
+ available_inline_size: AvailableSpace,
+) -> f32
+where
+ N: Copy + Debug + Eq + Hash,
+{
+ let available_space = table_writing_mode.to_physical(LogicalSize {
+ inline_size: available_inline_size,
+ block_size: AvailableSpace::MaxContent,
+ });
+ let intrinsic_inputs = LayoutInput {
+ known_dimensions: Size::NONE,
+ definite_dimensions: Size::NONE,
+ parent_size: Size::NONE,
+ parent_writing_mode: table_writing_mode,
+ available_space,
+ sizing_mode: SizingMode::ContentSize,
+ sizing_purpose: SizingPurpose::IntrinsicContribution,
+ run_mode: RunMode::ComputeSize,
+ axis: RequestedAxis::from(table_writing_mode.inline_axis()),
+ block_auto_behavior: AutoSizeBehavior::FitContent,
+ vertical_margins_are_collapsible: Line::FALSE,
+ };
+ table_writing_mode
+ .to_logical(
+ world
+ .compute_child_size(cell.to_taffy(), intrinsic_inputs)
+ .size,
+ )
+ .inline_size
+ .max(0.0)
+}
+
+fn outer_fixed_size(
+ dimension: Dimension,
+ inline_insets: f32,
+ box_sizing: taffy::BoxSizing,
+) -> Option<f32> {
+ (dimension.tag() == taffy::CompactLength::LENGTH_TAG).then(|| {
+ if box_sizing == taffy::BoxSizing::ContentBox {
+ dimension.value().max(0.0) + inline_insets
+ } else {
+ dimension.value().max(0.0).max(inline_insets)
}
- taffy::CompactLength::PERCENT_TAG if fixed => {
- let border_padding = if style.box_sizing == taffy::BoxSizing::ContentBox {
- let padding = style
- .padding
- .resolve_or_zero(None, resolve_stylo_calc_value);
- let border = style.border.resolve_or_zero(None, resolve_stylo_calc_value);
- padding.left + padding.right + border.left + border.right
- } else {
- 0.0
- };
- TableCellInlineConstraint::percent(style.size.width.value(), border_padding)
+ })
+}
+
+fn physical_inline_sum(writing_mode: WritingMode, rect: Rect<f32>) -> f32 {
+ if writing_mode.is_horizontal() {
+ rect.left + rect.right
+ } else {
+ rect.top + rect.bottom
+ }
+}
+
+fn clear_table_cell_inline_sizing(style: &mut Style<Atom>, writing_mode: WritingMode) {
+ set_physical_inline_dimension(writing_mode, &mut style.size, Dimension::auto());
+ set_physical_inline_dimension(writing_mode, &mut style.min_size, Dimension::auto());
+ set_physical_inline_dimension(writing_mode, &mut style.max_size, Dimension::auto());
+}
+
+fn normalize_table_cell_block_sizing(style: &mut Style<Atom>, writing_mode: WritingMode) {
+ let size = writing_mode.to_logical(style.size).block_size;
+ let min_size = writing_mode.to_logical(style.min_size).block_size;
+ if writing_mode.is_horizontal() {
+ if min_size.is_auto() {
+ style.min_size.height = size;
}
- _ => TableCellInlineConstraint::auto(),
+ style.size.height = Dimension::auto();
+ } else {
+ if min_size.is_auto() {
+ style.min_size.width = size;
+ }
+ style.size.width = Dimension::auto();
+ }
+}
+
+fn set_physical_inline_dimension(
+ writing_mode: WritingMode,
+ size: &mut Size<Dimension>,
+ value: Dimension,
+) {
+ if writing_mode.is_horizontal() {
+ size.width = value;
+ } else {
+ size.height = value;
}
}
@@ -705,6 +1020,7 @@ fn layout_captions<N>(
captions: &[LayoutBoxId],
width: f32,
mut y: f32,
+ parent_writing_mode: WritingMode,
) -> f32
where
N: Copy + Debug + Eq + Hash,
@@ -729,6 +1045,7 @@ where
width: Some(width),
height: None,
},
+ parent_writing_mode,
available_space: Size {
width: AvailableSpace::Definite(width),
height: AvailableSpace::MaxContent,
@@ -737,6 +1054,7 @@ where
sizing_purpose: SizingPurpose::Layout,
run_mode: RunMode::PerformLayout,
axis: taffy::RequestedAxis::Both,
+ block_auto_behavior: AutoSizeBehavior::FitContent,
vertical_margins_are_collapsible: Line::FALSE,
};
let output = world.compute_child_layout(caption.to_taffy(), inputs);
@@ -998,6 +1316,32 @@ where
context: &'a mut TableContext,
}
+impl<N> TableTreeWrapper<'_, N>
+where
+ N: Copy + Debug + Eq + Hash,
+{
+ /// Execute one Grid child query with the pass-local table-cell style.
+ /// Taffy's cache keys layout inputs rather than style identity, so clear
+ /// both sides of the swap to keep authored intrinsic measurements from
+ /// aliasing final Grid layout.
+ fn with_grid_cell_style<R>(
+ &mut self,
+ cell_index: usize,
+ operation: impl FnOnce(&mut LayoutWorld<N>, LayoutBoxId) -> R,
+ ) -> R {
+ let cell = self.context.cells[cell_index].id;
+ self.world.cache_clear(cell.to_taffy());
+ let authored_style = std::mem::replace(
+ &mut self.world.boxes[cell.index()].style.taffy,
+ self.context.cells[cell_index].style.clone(),
+ );
+ let result = operation(self.world, cell);
+ self.world.boxes[cell.index()].style.taffy = authored_style;
+ self.world.cache_clear(cell.to_taffy());
+ result
+ }
+}
+
impl<N> TraversePartialTree for TableTreeWrapper<'_, N>
where
N: Copy + Debug + Eq + Hash,
@@ -1047,19 +1391,19 @@ where
fn compute_child_layout(&mut self, node_id: NodeId, inputs: LayoutInput) -> LayoutOutput {
let cell_index = usize::from(node_id);
- let cell = self.context.cells[cell_index].id;
// The virtual table grid owns the used grid-item style: margins are
- // zero, column sizing has already consumed the applicable first-row
- // width, and cell height is a minimum contribution. Measuring the
- // subtree through the original box style would reintroduce widths from
- // later rows and make auto tracks overflow the table content box.
- let original = std::mem::replace(
- &mut self.world.boxes[cell.index()].style.taffy,
- self.context.cells[cell_index].style.clone(),
- );
- let output = self.world.compute_child_layout(cell.to_taffy(), inputs);
- self.world.boxes[cell.index()].style.taffy = original;
- output
+ // zero, column sizing has consumed every applicable inline constraint,
+ // and cell block size is a minimum contribution.
+ self.with_grid_cell_style(cell_index, |world, cell| {
+ world.compute_child_layout(cell.to_taffy(), inputs)
+ })
+ }
+
+ fn compute_child_size(&mut self, node_id: NodeId, inputs: LayoutInput) -> IntrinsicSizeResult {
+ let cell_index = usize::from(node_id);
+ self.with_grid_cell_style(cell_index, |world, cell| {
+ world.compute_child_size(cell.to_taffy(), inputs)
+ })
}
}
diff --git a/moli-layout/src/table/columns.rs b/moli-layout/src/table/columns.rs
index 55e6fdbf7..ab35f0709 100644
--- a/moli-layout/src/table/columns.rs
+++ b/moli-layout/src/table/columns.rs
@@ -1,4 +1,25 @@
-use taffy::{TrackSizingFunction, style_helpers};
+mod auto;
+
+pub(super) use auto::{
+ AutomaticTableSizingTarget, compute_grid_inline_min_max, distribute_auto_columns,
+};
+
+/// Cell rows consulted while collecting authored table column constraints.
+///
+/// CSS fixed layout is defined by columns and the first visual row. Automatic
+/// layout instead combines cells from every row. Keeping that distinction as
+/// a type prevents a first-row shortcut from leaking into automatic tables.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub(super) enum TableLayoutMode {
+ Fixed,
+ Automatic,
+}
+
+impl TableLayoutMode {
+ pub(super) const fn is_fixed(self) -> bool {
+ matches!(self, Self::Fixed)
+ }
+}
/// A width constraint collected by the CSS table formatting context.
///
@@ -37,6 +58,7 @@ pub(super) struct TableCellInlineConstraint {
}
impl TableCellInlineConstraint {
+ #[cfg(test)]
pub(super) const fn auto() -> Self {
Self {
min_inline_size: 0.0,
@@ -47,6 +69,7 @@ impl TableCellInlineConstraint {
}
}
+ #[cfg(test)]
pub(super) fn length(value: f32) -> Self {
Self {
max_inline_size: value.max(0.0),
@@ -55,6 +78,7 @@ impl TableCellInlineConstraint {
}
}
+ #[cfg(test)]
pub(super) fn percent(ratio: f32, border_padding: f32) -> Self {
Self {
max_inline_size: border_padding.max(0.0),
@@ -63,6 +87,26 @@ impl TableCellInlineConstraint {
..Self::auto()
}
}
+
+ /// Merge another single-column cell before applying the aggregate to its
+ /// column. This follows Blink's `CellInlineConstraint::Encompass`: a
+ /// declared width constrains the column maximum, while an unconstrained
+ /// cell can still raise its intrinsic floor.
+ pub(super) fn encompass(&mut self, other: Self) {
+ self.min_inline_size = self.min_inline_size.max(other.min_inline_size);
+ self.max_inline_size = if self.is_constrained == other.is_constrained {
+ self.max_inline_size.max(other.max_inline_size)
+ } else if self.is_constrained {
+ self.max_inline_size.max(other.min_inline_size)
+ } else {
+ self.min_inline_size.max(other.max_inline_size)
+ };
+ self.is_constrained |= other.is_constrained;
+ if other.percent > self.percent {
+ self.percent = other.percent;
+ self.percent_border_padding = other.percent_border_padding;
+ }
+ }
}
/// A cell constraint that covers more than one column.
@@ -109,10 +153,16 @@ impl TableColumnConstraint {
}
}
- /// Apply a single-column first-row cell while preserving the precedence of
- /// an authored `<col>`/`<colgroup>` measure.
- pub(super) fn encompass_first_row_cell(&mut self, cell: TableCellInlineConstraint) {
- if self.is_constrained {
+ /// Apply an aggregated single-column cell constraint.
+ ///
+ /// Authored `<col>`/`<colgroup>` measures win in fixed layout. Automatic
+ /// layout combines them with cell measures from every row.
+ pub(super) fn encompass_cell(
+ &mut self,
+ cell: TableCellInlineConstraint,
+ mode: TableLayoutMode,
+ ) {
+ if self.is_constrained && mode.is_fixed() {
return;
}
@@ -121,11 +171,20 @@ impl TableColumnConstraint {
.unwrap_or(cell.min_inline_size)
.max(cell.min_inline_size),
);
- self.max_inline_size = Some(
- self.max_inline_size
- .unwrap_or(0.0)
- .max(cell.max_inline_size),
- );
+ self.max_inline_size = Some(match self.max_inline_size {
+ Some(max_inline_size) if self.is_constrained => {
+ max_inline_size.max(if cell.is_constrained {
+ cell.max_inline_size
+ } else {
+ cell.min_inline_size
+ })
+ }
+ Some(max_inline_size) => max_inline_size.max(cell.max_inline_size),
+ None => cell.max_inline_size,
+ });
+ if let Some(max_inline_size) = &mut self.max_inline_size {
+ *max_inline_size = (*max_inline_size).max(self.min_inline_size.unwrap_or(0.0));
+ }
if cell.percent > self.percent {
self.percent = cell.percent;
self.percent_border_padding = cell.percent_border_padding;
@@ -133,20 +192,7 @@ impl TableColumnConstraint {
self.is_constrained |= cell.is_constrained;
}
- /// Track used while the table has no definite assignable inline size, or
- /// by automatic table layout. It is deliberately never used to perform
- /// fixed-table free-space distribution.
- pub(super) fn intrinsic_grid_track(self) -> TrackSizingFunction {
- if let Some(percent) = self.percent {
- style_helpers::percent(percent)
- } else if self.is_constrained {
- style_helpers::length(self.max_inline_size.unwrap_or(0.0))
- } else {
- style_helpers::auto()
- }
- }
-
- fn resolved_percent(self, assignable_inline_size: f32) -> Option<f32> {
+ pub(super) fn resolved_percent(self, assignable_inline_size: f32) -> Option<f32> {
self.percent.map(|ratio| {
self.min_inline_size
.unwrap_or(0.0)
@@ -154,7 +200,7 @@ impl TableColumnConstraint {
})
}
- fn fixed_inline_size(self) -> Option<f32> {
+ pub(super) fn fixed_inline_size(self) -> Option<f32> {
(self.is_constrained && self.percent.is_none())
.then_some(self.max_inline_size.unwrap_or(0.0))
}
@@ -175,6 +221,54 @@ impl TableColumnConstraint {
min_inline_size.max(self.percent_border_padding)
}
}
+
+ pub(super) fn min_inline_size_or_zero(self) -> f32 {
+ self.min_inline_size.unwrap_or(0.0)
+ }
+}
+
+/// Merge cell measures into column measures, resolve spanning cells in
+/// shortest-span order, and normalize percentage constraints.
+pub(super) fn apply_cell_constraints(
+ column_constraints: &mut Vec<TableColumnConstraint>,
+ cell_constraints: &[Option<TableCellInlineConstraint>],
+ cell_spans: &mut [TableCellSpanConstraint],
+ inline_border_spacing: f32,
+ mode: TableLayoutMode,
+) {
+ if column_constraints.len() < cell_constraints.len() {
+ column_constraints.resize(cell_constraints.len(), TableColumnConstraint::auto());
+ }
+ for (column, cell) in column_constraints.iter_mut().zip(cell_constraints) {
+ if let Some(cell) = cell {
+ column.encompass_cell(*cell, mode);
+ }
+ }
+
+ if mode.is_fixed() {
+ distribute_fixed_cell_spans(column_constraints, cell_spans, inline_border_spacing);
+ } else {
+ auto::distribute_auto_cell_spans(column_constraints, cell_spans, inline_border_spacing);
+ }
+
+ let mut total_percent = 0.0f32;
+ for column in column_constraints.iter_mut() {
+ if let Some(percent) = &mut column.percent {
+ if !mode.is_fixed() {
+ *percent = (*percent).min((1.0 - total_percent).max(0.0));
+ }
+ total_percent += *percent;
+ }
+ let max_inline_size = column.max_inline_size.get_or_insert(0.0);
+ *max_inline_size = (*max_inline_size).max(column.min_inline_size.unwrap_or(0.0));
+ }
+ if mode.is_fixed() && total_percent > 1.0 {
+ for column in column_constraints {
+ if let Some(percent) = &mut column.percent {
+ *percent /= total_percent;
+ }
+ }
+ }
}
/// Minimum column extent used by fixed table layout before the authored table
@@ -603,4 +697,54 @@ mod tests {
100.0,
);
}
+
+ #[test]
+ fn automatic_percentages_are_clipped_to_the_remaining_grid_fraction() {
+ let mut columns = vec![
+ TableColumnConstraint::percent(0.7, 0.0),
+ TableColumnConstraint::percent(0.6, 0.0),
+ TableColumnConstraint::percent(0.2, 0.0),
+ ];
+ let mut spans = [];
+
+ apply_cell_constraints(
+ &mut columns,
+ &[],
+ &mut spans,
+ 0.0,
+ TableLayoutMode::Automatic,
+ );
+
+ assert_eq!(columns[0].percent, Some(0.7));
+ assert!((columns[1].percent.unwrap_or(0.0) - 0.3).abs() < 0.001);
+ assert_eq!(columns[2].percent, Some(0.0));
+ }
+
+ #[test]
+ fn automatic_cells_preserve_constrained_and_intrinsic_column_roles() {
+ let mut columns = vec![TableColumnConstraint::auto(); 2];
+ let cells = [
+ Some(TableCellInlineConstraint::length(75.0)),
+ Some(TableCellInlineConstraint {
+ min_inline_size: 20.0,
+ max_inline_size: 50.0,
+ ..TableCellInlineConstraint::auto()
+ }),
+ ];
+ let mut spans = [];
+
+ apply_cell_constraints(
+ &mut columns,
+ &cells,
+ &mut spans,
+ 0.0,
+ TableLayoutMode::Automatic,
+ );
+
+ assert!(columns[0].is_constrained);
+ assert_eq!(columns[0].max_inline_size, Some(75.0));
+ assert!(!columns[1].is_constrained);
+ assert_eq!(columns[1].min_inline_size, Some(20.0));
+ assert_eq!(columns[1].max_inline_size, Some(50.0));
+ }
}
diff --git a/moli-layout/src/table/columns/auto.rs b/moli-layout/src/table/columns/auto.rs
new file mode 100644
index 000000000..980193f55
--- /dev/null
+++ b/moli-layout/src/table/columns/auto.rs
@@ -0,0 +1,954 @@
+use super::{TableCellSpanConstraint, TableColumnConstraint, TableLayoutMode};
+use crate::LAYOUT_SUBPIXELS_PER_CSS_PIXEL;
+
+const TABLE_MAX_INLINE_SIZE: f32 = 1_000_000.0;
+
+/// Compare sizes at Blink's 26.6 `LayoutUnit` boundary.
+///
+/// Table distribution still uses floats internally, but the exact-maximum
+/// branch is observable only after the shared layout quantization. Comparing
+/// the corresponding fixed-point values avoids both addition-order noise and
+/// a table-specific magic epsilon.
+fn same_layout_unit(left: f32, right: f32) -> bool {
+ let raw = |value: f32| {
+ (f64::from(value.max(0.0)) * f64::from(LAYOUT_SUBPIXELS_PER_CSS_PIXEL)).trunc() as i64
+ };
+ raw(left) == raw(right)
+}
+
+/// Whether excess inline size came from a definite table constraint or from
+/// an intrinsic colspan contribution.
+///
+/// The distinction only matters above the maximum guess: a definite table may
+/// grow constrained columns, while an unconstrained intrinsic contribution
+/// must leave their authored maximum intact.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub(in crate::table) enum AutomaticTableSizingTarget {
+ Constrained,
+ Intrinsic,
+}
+
+impl AutomaticTableSizingTarget {
+ const fn grows_constrained_columns(self) -> bool {
+ matches!(self, Self::Constrained)
+ }
+}
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+enum AutomaticColumnClass {
+ Percentage,
+ Constrained,
+ Automatic,
+}
+
+impl AutomaticColumnClass {
+ fn classify(column: TableColumnConstraint) -> Self {
+ if column.percent.is_some() {
+ Self::Percentage
+ } else if column.is_constrained {
+ Self::Constrained
+ } else {
+ Self::Automatic
+ }
+ }
+}
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+enum DistributionPhase {
+ Minimum,
+ Percentage,
+ Specified,
+ Maximum,
+ AboveMaximum,
+}
+
+/// The four guesses from CSS Tables 3's width-distribution algorithm.
+/// Named fields make phase boundaries explicit and prevent an array index
+/// from silently coupling two different guesses.
+#[derive(Clone, Copy, Debug, Default, PartialEq)]
+struct AutomaticColumnGuesses {
+ minimum: f32,
+ percentage: f32,
+ specified: f32,
+ maximum: f32,
+}
+
+impl AutomaticColumnGuesses {
+ fn collect(target_inline_size: f32, constraints: &[TableColumnConstraint]) -> Self {
+ let mut guesses = Self::default();
+ for column in constraints.iter().copied() {
+ let min = column_min(column);
+ let max = column_max(column);
+ guesses.minimum += min;
+ match AutomaticColumnClass::classify(column) {
+ AutomaticColumnClass::Percentage => {
+ let percentage = resolved_percentage_size(column, target_inline_size);
+ guesses.percentage += percentage;
+ guesses.specified += percentage;
+ guesses.maximum += percentage;
+ }
+ AutomaticColumnClass::Constrained => {
+ guesses.percentage += min;
+ guesses.specified += max;
+ guesses.maximum += max;
+ }
+ AutomaticColumnClass::Automatic => {
+ guesses.percentage += min;
+ guesses.specified += min;
+ guesses.maximum += max;
+ }
+ }
+ }
+ guesses
+ }
+
+ fn phase_for(self, target_inline_size: f32) -> DistributionPhase {
+ if target_inline_size <= self.minimum {
+ DistributionPhase::Minimum
+ } else if target_inline_size <= self.percentage {
+ DistributionPhase::Percentage
+ } else if target_inline_size <= self.specified {
+ DistributionPhase::Specified
+ } else if target_inline_size <= self.maximum
+ || same_layout_unit(target_inline_size, self.maximum)
+ {
+ DistributionPhase::Maximum
+ } else {
+ DistributionPhase::AboveMaximum
+ }
+ }
+}
+
+#[derive(Clone, Copy, Debug)]
+enum GrowthRule {
+ PercentageTowardResolved { target_inline_size: f32 },
+ ConstrainedTowardMaximum,
+ AutomaticTowardMaximum,
+ AutomaticAboveMaximum,
+ ConstrainedAboveMaximum,
+ PercentageAboveMaximum,
+}
+
+impl GrowthRule {
+ fn receives(self, column: TableColumnConstraint) -> bool {
+ let class = AutomaticColumnClass::classify(column);
+ match self {
+ Self::PercentageTowardResolved { .. } | Self::PercentageAboveMaximum => {
+ class == AutomaticColumnClass::Percentage
+ }
+ Self::ConstrainedTowardMaximum | Self::ConstrainedAboveMaximum => {
+ class == AutomaticColumnClass::Constrained
+ }
+ Self::AutomaticTowardMaximum | Self::AutomaticAboveMaximum => {
+ class == AutomaticColumnClass::Automatic
+ }
+ }
+ }
+
+ fn weight(self, column: TableColumnConstraint) -> f32 {
+ match self {
+ Self::PercentageTowardResolved { target_inline_size } => {
+ resolved_percentage_size(column, target_inline_size) - column_min(column)
+ }
+ Self::ConstrainedTowardMaximum | Self::AutomaticTowardMaximum => {
+ column_max(column) - column_min(column)
+ }
+ Self::AutomaticAboveMaximum | Self::ConstrainedAboveMaximum => column_max(column),
+ Self::PercentageAboveMaximum => column.percent.unwrap_or(0.0),
+ }
+ .max(0.0)
+ }
+}
+
+fn column_min(column: TableColumnConstraint) -> f32 {
+ column.min_inline_size_or_zero().max(0.0)
+}
+
+fn column_max(column: TableColumnConstraint) -> f32 {
+ column
+ .max_inline_size
+ .unwrap_or(0.0)
+ .max(column_min(column))
+}
+
+fn resolved_percentage_size(column: TableColumnConstraint, target_inline_size: f32) -> f32 {
+ column
+ .resolved_percent(target_inline_size)
+ .unwrap_or_else(|| column_min(column))
+}
+
+/// Intrinsic border-box limits for a table grid.
+#[derive(Clone, Copy, Debug, Default, PartialEq)]
+pub(in crate::table) struct TableGridInlineMinMax {
+ pub(in crate::table) min: f32,
+ pub(in crate::table) max: f32,
+}
+
+/// Compute CSS Tables GRID_MIN/GRID_MAX, including decorations and spacing
+/// that cannot be assigned to an individual column.
+pub(in crate::table) fn compute_grid_inline_min_max(
+ constraints: &[TableColumnConstraint],
+ undistributable_space: f32,
+ layout_mode: TableLayoutMode,
+) -> TableGridInlineMinMax {
+ let mut min = 0.0f32;
+ let mut max = 0.0f32;
+ let mut percent_max_estimate = 0.0f32;
+ let mut non_percent_max_sum = 0.0f32;
+ let mut percent_sum = 0.0f32;
+
+ for column in constraints.iter().copied() {
+ let column_max = column
+ .max_inline_size
+ .unwrap_or(0.0)
+ .max(column.min_inline_size_or_zero());
+ if layout_mode.is_fixed() && column.fixed_inline_size().is_some() {
+ min += column_max;
+ } else {
+ min += column.min_inline_size_or_zero();
+ }
+ max += column_max;
+
+ if let Some(percent) = column.percent {
+ percent_sum += percent;
+ if percent > 0.0 && column_max > 0.0 {
+ percent_max_estimate = percent_max_estimate
+ .max((column_max - column.percent_border_padding).max(0.0) / percent);
+ }
+ } else {
+ non_percent_max_sum += column_max;
+ }
+ }
+
+ percent_sum = percent_sum.clamp(0.0, 1.0);
+ if percent_sum > 0.0 {
+ if non_percent_max_sum > 0.0 {
+ let size_from_percent_and_fixed = if percent_sum >= 1.0 {
+ TABLE_MAX_INLINE_SIZE
+ } else {
+ non_percent_max_sum / (1.0 - percent_sum)
+ };
+ max = max.max(size_from_percent_and_fixed);
+ }
+ max = max.max(percent_max_estimate);
+ }
+
+ let undistributable_space = undistributable_space.max(0.0);
+ min += undistributable_space;
+ max = max.max(min - undistributable_space) + undistributable_space;
+ TableGridInlineMinMax { min, max }
+}
+
+/// Distribute an automatic table's assignable inline size using the CSS
+/// Tables minimum, percentage, specified and maximum guesses.
+pub(in crate::table) fn distribute_auto_columns(
+ target_inline_size: f32,
+ constraints: &[TableColumnConstraint],
+ sizing_target: AutomaticTableSizingTarget,
+) -> Vec<f32> {
+ if constraints.is_empty() {
+ return Vec::new();
+ }
+
+ let guesses = AutomaticColumnGuesses::collect(target_inline_size, constraints);
+ let target = target_inline_size.max(guesses.minimum).max(0.0);
+ let phase = guesses.phase_for(target);
+ let mut sizes = constraints
+ .iter()
+ .copied()
+ .map(column_min)
+ .collect::<Vec<_>>();
+
+ let fills_target = match phase {
+ DistributionPhase::Minimum => true,
+ DistributionPhase::Percentage => {
+ distribute_growth(
+ &mut sizes,
+ constraints,
+ target - guesses.minimum,
+ GrowthRule::PercentageTowardResolved {
+ target_inline_size: target,
+ },
+ );
+ true
+ }
+ DistributionPhase::Specified => {
+ set_percentage_columns(&mut sizes, constraints, target);
+ distribute_growth(
+ &mut sizes,
+ constraints,
+ target - guesses.percentage,
+ GrowthRule::ConstrainedTowardMaximum,
+ );
+ true
+ }
+ DistributionPhase::Maximum => {
+ let exact_match = same_layout_unit(target, guesses.maximum);
+ for (size, column) in sizes.iter_mut().zip(constraints.iter().copied()) {
+ *size = match AutomaticColumnClass::classify(column) {
+ AutomaticColumnClass::Percentage => resolved_percentage_size(column, target),
+ AutomaticColumnClass::Constrained => column_max(column),
+ AutomaticColumnClass::Automatic if exact_match => column_max(column),
+ AutomaticColumnClass::Automatic => column_min(column),
+ };
+ }
+ if !exact_match {
+ distribute_growth(
+ &mut sizes,
+ constraints,
+ target - guesses.specified,
+ GrowthRule::AutomaticTowardMaximum,
+ );
+ }
+ true
+ }
+ DistributionPhase::AboveMaximum => {
+ for (size, column) in sizes.iter_mut().zip(constraints.iter().copied()) {
+ *size = match AutomaticColumnClass::classify(column) {
+ AutomaticColumnClass::Percentage => resolved_percentage_size(column, target),
+ AutomaticColumnClass::Constrained | AutomaticColumnClass::Automatic => {
+ column_max(column)
+ }
+ };
+ }
+ let growth_rule = above_maximum_growth_rule(constraints, sizing_target);
+ if let Some(growth_rule) = growth_rule {
+ distribute_growth(
+ &mut sizes,
+ constraints,
+ target - guesses.maximum,
+ growth_rule,
+ );
+ true
+ } else {
+ // An intrinsic colspan maximum does not enlarge columns that
+ // are all explicitly constrained.
+ false
+ }
+ }
+ };
+
+ if fills_target {
+ absorb_remainder(&mut sizes, target);
+ }
+ sizes
+}
+
+fn distribute_growth(
+ sizes: &mut [f32],
+ constraints: &[TableColumnConstraint],
+ distributable: f32,
+ rule: GrowthRule,
+) {
+ let recipient_count = constraints
+ .iter()
+ .copied()
+ .filter(|column| rule.receives(*column))
+ .count();
+ if recipient_count == 0 || distributable <= 0.0 {
+ return;
+ }
+ let total_weight = constraints
+ .iter()
+ .copied()
+ .filter(|column| rule.receives(*column))
+ .map(|column| rule.weight(column))
+ .sum::<f32>();
+ let mut remaining = distributable;
+ let mut last = None;
+ for (index, column) in constraints.iter().copied().enumerate() {
+ if !rule.receives(column) {
+ continue;
+ }
+ last = Some(index);
+ let delta = if total_weight > 0.0 {
+ distributable * rule.weight(column) / total_weight
+ } else {
+ distributable / recipient_count as f32
+ };
+ sizes[index] += delta;
+ remaining -= delta;
+ }
+ if let Some(index) = last {
+ sizes[index] = (sizes[index] + remaining).max(0.0);
+ }
+}
+
+fn set_percentage_columns(
+ sizes: &mut [f32],
+ constraints: &[TableColumnConstraint],
+ target_inline_size: f32,
+) {
+ for (size, column) in sizes.iter_mut().zip(constraints.iter().copied()) {
+ if AutomaticColumnClass::classify(column) == AutomaticColumnClass::Percentage {
+ *size = resolved_percentage_size(column, target_inline_size);
+ }
+ }
+}
+
+fn above_maximum_growth_rule(
+ constraints: &[TableColumnConstraint],
+ sizing_target: AutomaticTableSizingTarget,
+) -> Option<GrowthRule> {
+ let has_class = |class| {
+ constraints
+ .iter()
+ .copied()
+ .any(|column| AutomaticColumnClass::classify(column) == class)
+ };
+
+ if has_class(AutomaticColumnClass::Automatic) {
+ Some(GrowthRule::AutomaticAboveMaximum)
+ } else if sizing_target.grows_constrained_columns()
+ && has_class(AutomaticColumnClass::Constrained)
+ {
+ Some(GrowthRule::ConstrainedAboveMaximum)
+ } else if has_class(AutomaticColumnClass::Percentage) {
+ Some(GrowthRule::PercentageAboveMaximum)
+ } else {
+ None
+ }
+}
+
+fn absorb_remainder(sizes: &mut [f32], target: f32) {
+ let assigned = sizes.iter().sum::<f32>();
+ if let Some(last) = sizes.last_mut() {
+ *last = (*last + target - assigned).max(0.0);
+ }
+}
+
+/// Project a wide automatic-layout cell onto the columns it spans. Existing
+/// column measures form the lower guesses; the cell's min/max constraints are
+/// distributed with the same allocator used for the final table width.
+pub(super) fn distribute_auto_cell_spans(
+ columns: &mut [TableColumnConstraint],
+ cell_spans: &mut [TableCellSpanConstraint],
+ inline_border_spacing: f32,
+) {
+ cell_spans.sort_by_key(|constraint| (constraint.span, constraint.start_column));
+
+ for constraint in cell_spans {
+ let Some(available_columns) = columns.get_mut(constraint.start_column..) else {
+ continue;
+ };
+ let effective_span = constraint.span.min(available_columns.len());
+ if effective_span == 0 {
+ continue;
+ }
+ let column_span = &mut available_columns[..effective_span];
+ let inner_spacing =
+ inline_border_spacing.max(0.0) * effective_span.saturating_sub(1) as f32;
+ let cell_min = (constraint.cell.min_inline_size - inner_spacing).max(0.0);
+ let cell_max = (constraint.cell.max_inline_size - inner_spacing).max(0.0);
+
+ if let Some(cell_percent) = constraint.cell.percent {
+ let columns_percent = column_span
+ .iter()
+ .filter_map(|column| column.percent)
+ .sum::<f32>();
+ let surplus = cell_percent - columns_percent;
+ let non_percent_count = column_span
+ .iter()
+ .filter(|column| column.percent.is_none())
+ .count();
+ if surplus > 0.0 && non_percent_count > 0 {
+ let total_max = column_span
+ .iter()
+ .filter(|column| column.percent.is_none())
+ .map(|column| column.max_inline_size.unwrap_or(0.0))
+ .sum::<f32>();
+ for column in column_span
+ .iter_mut()
+ .filter(|column| column.percent.is_none())
+ {
+ let share = if total_max > 0.0 {
+ surplus * column.max_inline_size.unwrap_or(0.0) / total_max
+ } else {
+ surplus / non_percent_count as f32
+ };
+ column.percent = Some(share);
+ }
+ }
+ }
+
+ let min_sizes = distribute_auto_columns(
+ cell_min,
+ column_span,
+ AutomaticTableSizingTarget::Constrained,
+ );
+ for (column, size) in column_span.iter_mut().zip(min_sizes) {
+ column.min_inline_size = Some(column.min_inline_size_or_zero().max(size));
+ }
+ let max_sizing_target = if constraint.cell.is_constrained {
+ AutomaticTableSizingTarget::Constrained
+ } else {
+ AutomaticTableSizingTarget::Intrinsic
+ };
+ let max_sizes = distribute_auto_columns(cell_max, column_span, max_sizing_target);
+ for (column, size) in column_span.iter_mut().zip(max_sizes) {
+ column.max_inline_size = Some(
+ column
+ .max_inline_size
+ .unwrap_or(0.0)
+ .max(column.min_inline_size_or_zero())
+ .max(size),
+ );
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::table::columns::TableCellInlineConstraint;
+
+ const CONSTRAINED: AutomaticTableSizingTarget = AutomaticTableSizingTarget::Constrained;
+ const INTRINSIC: AutomaticTableSizingTarget = AutomaticTableSizingTarget::Intrinsic;
+
+ fn assert_sizes(actual_sizes: &[f32], expected_sizes: &[f32]) {
+ assert_eq!(actual_sizes.len(), expected_sizes.len());
+ for (index, (actual, expected)) in actual_sizes.iter().zip(expected_sizes).enumerate() {
+ assert!(
+ (actual - expected).abs() < 0.001,
+ "column {index}: expected {expected}, got {actual}; all={actual_sizes:?}",
+ );
+ }
+ }
+
+ fn minimum_sizes(columns: &[TableColumnConstraint]) -> Vec<f32> {
+ columns
+ .iter()
+ .map(|column| column.min_inline_size_or_zero())
+ .collect()
+ }
+
+ fn maximum_sizes(columns: &[TableColumnConstraint]) -> Vec<f32> {
+ columns
+ .iter()
+ .map(|column| column.max_inline_size.unwrap_or(0.0))
+ .collect()
+ }
+
+ fn automatic(min: f32, max: f32) -> TableColumnConstraint {
+ TableColumnConstraint {
+ min_inline_size: Some(min),
+ max_inline_size: Some(max),
+ ..TableColumnConstraint::auto()
+ }
+ }
+
+ fn constrained(min: f32, max: f32) -> TableColumnConstraint {
+ TableColumnConstraint {
+ is_constrained: true,
+ ..automatic(min, max)
+ }
+ }
+
+ fn percentage(min: f32, max: f32, ratio: f32, border_padding: f32) -> TableColumnConstraint {
+ TableColumnConstraint {
+ percent: Some(ratio),
+ percent_border_padding: border_padding,
+ ..automatic(min, max)
+ }
+ }
+
+ #[test]
+ fn phase_selection_includes_each_guess_boundary() {
+ let guesses = AutomaticColumnGuesses {
+ minimum: 30.0,
+ percentage: 70.0,
+ specified: 110.0,
+ maximum: 170.0,
+ };
+ let layout_unit = 1.0 / LAYOUT_SUBPIXELS_PER_CSS_PIXEL;
+
+ for (target, expected) in [
+ (10.0, DistributionPhase::Minimum),
+ (30.0, DistributionPhase::Minimum),
+ (50.0, DistributionPhase::Percentage),
+ (70.0, DistributionPhase::Percentage),
+ (90.0, DistributionPhase::Specified),
+ (110.0, DistributionPhase::Specified),
+ (140.0, DistributionPhase::Maximum),
+ (170.0, DistributionPhase::Maximum),
+ (170.0 + layout_unit / 2.0, DistributionPhase::Maximum),
+ (170.0 + layout_unit, DistributionPhase::AboveMaximum),
+ (171.0, DistributionPhase::AboveMaximum),
+ ] {
+ assert_eq!(guesses.phase_for(target), expected, "target={target}");
+ }
+ }
+
+ #[test]
+ fn minimum_guess_clamps_targets_below_the_intrinsic_floor() {
+ let constraints = [automatic(30.0, 60.0), constrained(20.0, 40.0)];
+
+ assert_sizes(
+ &distribute_auto_columns(10.0, &constraints, CONSTRAINED),
+ &[30.0, 20.0],
+ );
+ assert_sizes(
+ &distribute_auto_columns(-100.0, &constraints, CONSTRAINED),
+ &[30.0, 20.0],
+ );
+ assert!(distribute_auto_columns(100.0, &[], CONSTRAINED).is_empty());
+ }
+
+ #[test]
+ fn percentage_guess_grows_only_percentage_columns() {
+ let constraints = [percentage(10.0, 10.0, 0.8, 0.0), automatic(20.0, 60.0)];
+
+ assert_sizes(
+ &distribute_auto_columns(50.0, &constraints, CONSTRAINED),
+ &[30.0, 20.0],
+ );
+ assert_sizes(
+ &distribute_auto_columns(100.0, &constraints, CONSTRAINED),
+ &[80.0, 20.0],
+ );
+ }
+
+ #[test]
+ fn specified_guess_grows_only_constrained_columns() {
+ let constraints = [
+ percentage(10.0, 10.0, 0.25, 0.0),
+ constrained(20.0, 60.0),
+ automatic(30.0, 90.0),
+ ];
+
+ assert_sizes(
+ &distribute_auto_columns(100.0, &constraints, CONSTRAINED),
+ &[25.0, 45.0, 30.0],
+ );
+ assert_sizes(
+ &distribute_auto_columns(120.0, &constraints, CONSTRAINED),
+ &[30.0, 60.0, 30.0],
+ );
+ }
+
+ #[test]
+ fn maximum_guess_grows_only_automatic_columns() {
+ let constraints = [
+ percentage(10.0, 10.0, 0.25, 0.0),
+ constrained(20.0, 60.0),
+ automatic(30.0, 90.0),
+ ];
+
+ assert_sizes(
+ &distribute_auto_columns(150.0, &constraints, CONSTRAINED),
+ &[37.5, 60.0, 52.5],
+ );
+ assert_sizes(
+ &distribute_auto_columns(200.0, &constraints, CONSTRAINED),
+ &[50.0, 60.0, 90.0],
+ );
+ }
+
+ #[test]
+ fn exact_maximum_preserves_each_automatic_columns_max_content_size() {
+ // Mirrors Blink's DistributeColspanAutoExactMaxSize regression: when
+ // the target is exactly the maximum guess, redistributing the tracks
+ // can move a rounding remainder into one column and wrap content.
+ let maxima = [0.09375, 22.109_375, 33.781_25, 2_000.343_8];
+ let constraints = [
+ automatic(0.0, maxima[0]),
+ automatic(3.328_125, maxima[1]),
+ automatic(3.328_125, maxima[2]),
+ automatic(0.0, maxima[3]),
+ ];
+ let target = maxima.iter().sum::<f32>();
+
+ assert_eq!(
+ distribute_auto_columns(target, &constraints, CONSTRAINED),
+ maxima
+ );
+ }
+
+ #[test]
+ fn mixed_columns_walk_specified_maximum_and_above_maximum_phases() {
+ let constraints = [
+ automatic(20.0, 100.0),
+ constrained(10.0, 50.0),
+ percentage(30.0, 30.0, 0.25, 0.0),
+ ];
+
+ assert_sizes(
+ &distribute_auto_columns(90.0, &constraints, CONSTRAINED),
+ &[20.0, 40.0, 30.0],
+ );
+ assert_sizes(
+ &distribute_auto_columns(150.0, &constraints, CONSTRAINED),
+ &[62.5, 50.0, 37.5],
+ );
+ assert_sizes(
+ &distribute_auto_columns(400.0, &constraints, CONSTRAINED),
+ &[250.0, 50.0, 100.0],
+ );
+ }
+
+ #[test]
+ fn specified_guess_shrinks_conflicting_authored_widths_proportionally() {
+ let constraints = [constrained(0.0, 75.0), constrained(0.0, 80.0)];
+
+ assert_sizes(
+ &distribute_auto_columns(100.0, &constraints, CONSTRAINED),
+ &[48.387_096, 51.612_904],
+ );
+ }
+
+ #[test]
+ fn above_maximum_prefers_automatic_columns_and_weights_their_maxima() {
+ let constraints = [
+ percentage(10.0, 10.0, 0.25, 0.0),
+ constrained(20.0, 60.0),
+ automatic(10.0, 30.0),
+ automatic(20.0, 90.0),
+ ];
+
+ assert_sizes(
+ &distribute_auto_columns(400.0, &constraints, CONSTRAINED),
+ &[100.0, 60.0, 60.0, 180.0],
+ );
+ }
+
+ #[test]
+ fn above_maximum_distinguishes_definite_tables_from_intrinsic_colspans() {
+ let constraints = [constrained(20.0, 40.0), constrained(10.0, 60.0)];
+
+ assert_sizes(
+ &distribute_auto_columns(200.0, &constraints, CONSTRAINED),
+ &[80.0, 120.0],
+ );
+ assert_sizes(
+ &distribute_auto_columns(200.0, &constraints, INTRINSIC),
+ &[40.0, 60.0],
+ );
+ }
+
+ #[test]
+ fn above_maximum_falls_back_to_percentage_then_equal_weight_distribution() {
+ let percentages = [
+ percentage(10.0, 10.0, 0.25, 0.0),
+ percentage(10.0, 10.0, 0.25, 0.0),
+ ];
+ assert_sizes(
+ &distribute_auto_columns(200.0, &percentages, INTRINSIC),
+ &[100.0, 100.0],
+ );
+
+ let zero_weight_automatic = [automatic(0.0, 0.0), automatic(0.0, 0.0)];
+ assert_sizes(
+ &distribute_auto_columns(100.0, &zero_weight_automatic, CONSTRAINED),
+ &[50.0, 50.0],
+ );
+ }
+
+ #[test]
+ fn grid_min_max_accounts_for_percentages_fixed_columns_and_spacing() {
+ let constraints = [
+ automatic(20.0, 80.0),
+ constrained(10.0, 40.0),
+ percentage(30.0, 30.0, 0.25, 5.0),
+ ];
+
+ assert_eq!(
+ compute_grid_inline_min_max(&constraints, 12.0, TableLayoutMode::Automatic),
+ TableGridInlineMinMax {
+ min: 72.0,
+ max: 172.0,
+ },
+ );
+ assert_eq!(
+ compute_grid_inline_min_max(&constraints, 12.0, TableLayoutMode::Fixed),
+ TableGridInlineMinMax {
+ min: 102.0,
+ max: 172.0,
+ },
+ );
+ }
+
+ #[test]
+ fn grid_max_caps_unbounded_full_percentage_estimates() {
+ let constraints = [automatic(0.0, 50.0), percentage(0.0, 0.0, 1.0, 0.0)];
+
+ assert_eq!(
+ compute_grid_inline_min_max(&constraints, 5.0, TableLayoutMode::Automatic),
+ TableGridInlineMinMax {
+ min: 5.0,
+ max: TABLE_MAX_INLINE_SIZE + 5.0,
+ },
+ );
+ }
+
+ #[test]
+ fn automatic_colspan_distributes_percentage_and_intrinsic_surplus_exactly() {
+ let mut columns = [percentage(20.0, 40.0, 0.2, 0.0), automatic(10.0, 60.0)];
+ let mut spans = [TableCellSpanConstraint {
+ start_column: 0,
+ span: 2,
+ cell: TableCellInlineConstraint {
+ min_inline_size: 100.0,
+ max_inline_size: 200.0,
+ percent: Some(0.5),
+ percent_border_padding: 0.0,
+ is_constrained: false,
+ },
+ }];
+
+ distribute_auto_cell_spans(&mut columns, &mut spans, 0.0);
+
+ assert_eq!(columns[0].percent, Some(0.2));
+ assert_eq!(columns[1].percent, Some(0.3));
+ assert_sizes(&minimum_sizes(&columns), &[40.0, 60.0]);
+ assert_sizes(&maximum_sizes(&columns), &[80.0, 120.0]);
+ }
+
+ #[test]
+ fn automatic_colspan_weights_percentage_surplus_by_existing_maxima() {
+ // Chromium distributes the unclaimed 30% in a 10:20 ratio while the
+ // existing percentage column retains its authored 30%.
+ let mut columns = [
+ automatic(0.0, 10.0),
+ automatic(0.0, 20.0),
+ percentage(0.0, 10.0, 0.3, 0.0),
+ ];
+ let mut spans = [TableCellSpanConstraint {
+ start_column: 0,
+ span: 3,
+ cell: TableCellInlineConstraint {
+ percent: Some(0.6),
+ ..TableCellInlineConstraint::auto()
+ },
+ }];
+
+ distribute_auto_cell_spans(&mut columns, &mut spans, 0.0);
+
+ for (column, expected) in columns.iter().zip([0.1, 0.2, 0.3]) {
+ assert!((column.percent.unwrap_or(0.0) - expected).abs() < 0.001);
+ }
+ }
+
+ #[test]
+ fn automatic_colspan_weights_intrinsic_minimum_by_existing_maxima() {
+ // This is the intrinsic-size companion to the percentage case above
+ // and matches Blink's 10:10:20 -> 25:25:50 regression.
+ let mut columns = [
+ automatic(0.0, 10.0),
+ automatic(0.0, 10.0),
+ automatic(0.0, 20.0),
+ ];
+ let mut spans = [TableCellSpanConstraint {
+ start_column: 0,
+ span: 3,
+ cell: TableCellInlineConstraint {
+ min_inline_size: 100.0,
+ max_inline_size: 100.0,
+ ..TableCellInlineConstraint::auto()
+ },
+ }];
+
+ distribute_auto_cell_spans(&mut columns, &mut spans, 0.0);
+
+ assert_sizes(&minimum_sizes(&columns), &[25.0, 25.0, 50.0]);
+ assert_sizes(&maximum_sizes(&columns), &[25.0, 25.0, 50.0]);
+ }
+
+ #[test]
+ fn automatic_colspan_excludes_inner_spacing_from_column_constraints() {
+ let mut columns = [automatic(0.0, 0.0), automatic(0.0, 0.0)];
+ let mut spans = [TableCellSpanConstraint {
+ start_column: 0,
+ span: 2,
+ cell: TableCellInlineConstraint {
+ min_inline_size: 110.0,
+ max_inline_size: 210.0,
+ percent: None,
+ percent_border_padding: 0.0,
+ is_constrained: false,
+ },
+ }];
+
+ distribute_auto_cell_spans(&mut columns, &mut spans, 10.0);
+
+ assert_sizes(&minimum_sizes(&columns), &[50.0, 50.0]);
+ assert_sizes(&maximum_sizes(&columns), &[100.0, 100.0]);
+ }
+
+ #[test]
+ fn automatic_colspan_constrainedness_controls_fixed_column_growth() {
+ let span = |is_constrained| TableCellSpanConstraint {
+ start_column: 0,
+ span: 2,
+ cell: TableCellInlineConstraint {
+ min_inline_size: 0.0,
+ max_inline_size: 200.0,
+ percent: None,
+ percent_border_padding: 0.0,
+ is_constrained,
+ },
+ };
+
+ let mut intrinsic_columns = [constrained(0.0, 40.0), constrained(0.0, 60.0)];
+ distribute_auto_cell_spans(&mut intrinsic_columns, &mut [span(false)], 0.0);
+ assert_sizes(&maximum_sizes(&intrinsic_columns), &[40.0, 60.0]);
+
+ let mut constrained_columns = [constrained(0.0, 40.0), constrained(0.0, 60.0)];
+ distribute_auto_cell_spans(&mut constrained_columns, &mut [span(true)], 0.0);
+ assert_sizes(&maximum_sizes(&constrained_columns), &[80.0, 120.0]);
+ }
+
+ #[test]
+ fn automatic_colspan_divides_percentage_evenly_when_columns_have_no_maxima() {
+ let mut columns = [TableColumnConstraint::auto(); 2];
+ let mut spans = [TableCellSpanConstraint {
+ start_column: 0,
+ span: 2,
+ cell: TableCellInlineConstraint {
+ min_inline_size: 0.0,
+ max_inline_size: 0.0,
+ percent: Some(0.6),
+ percent_border_padding: 0.0,
+ is_constrained: false,
+ },
+ }];
+
+ distribute_auto_cell_spans(&mut columns, &mut spans, 0.0);
+
+ assert_eq!(columns[0].percent, Some(0.3));
+ assert_eq!(columns[1].percent, Some(0.3));
+ }
+
+ #[test]
+ fn automatic_colspan_clips_to_remaining_columns_and_ignores_missing_starts() {
+ let mut columns = [
+ automatic(0.0, 10.0),
+ automatic(0.0, 10.0),
+ automatic(0.0, 10.0),
+ ];
+ let mut spans = [
+ TableCellSpanConstraint {
+ start_column: 99,
+ span: 2,
+ cell: TableCellInlineConstraint {
+ min_inline_size: 500.0,
+ max_inline_size: 500.0,
+ ..TableCellInlineConstraint::auto()
+ },
+ },
+ TableCellSpanConstraint {
+ start_column: 1,
+ span: 99,
+ cell: TableCellInlineConstraint {
+ min_inline_size: 80.0,
+ max_inline_size: 80.0,
+ ..TableCellInlineConstraint::auto()
+ },
+ },
+ ];
+
+ distribute_auto_cell_spans(&mut columns, &mut spans, 0.0);
+
+ assert_sizes(&minimum_sizes(&columns), &[0.0, 40.0, 40.0]);
+ assert_sizes(&maximum_sizes(&columns), &[10.0, 40.0, 40.0]);
+ }
+}
diff --git a/moli-layout/src/taffy_tree.rs b/moli-layout/src/taffy_tree.rs
index 551ba0ab2..5cb17de3b 100644
--- a/moli-layout/src/taffy_tree.rs
+++ b/moli-layout/src/taffy_tree.rs
@@ -3,14 +3,14 @@ use std::{fmt::Debug, hash::Hash};
use parley::{AlignmentOptions, PositionedLayoutItem, YieldData};
use style::Atom;
use taffy::{
- AlignContent, AlignContentKeyword, AlignmentSafety, AvailableSpace, BlockContext,
- BlockFormattingContext, BoxSizing, CacheTree, Clear, DetailedGridInfo, Dimension, Display,
- FloatDirection, Layout, LayoutBlockContainer, LayoutFlexboxContainer, LayoutGridContainer,
- LayoutInput, LayoutOutput, LayoutPartialTree, LeafLayoutContext, Line, MaybeMath, MaybeResolve,
- NodeId, Point, ResolveOrZero, RoundTree, RunMode, Size, SizingMode, SizingPurpose, Style,
- TraversePartialTree, TraverseTree, compute_block_layout, compute_cached_layout,
- compute_flexbox_layout, compute_grid_layout, compute_hidden_layout,
- compute_leaf_layout_with_context, compute_root_layout, round_layout,
+ AbsoluteAxis, AlignContent, AlignContentKeyword, AlignmentSafety, AutoSizeBehavior,
+ AvailableSpace, BlockContext, BlockFormattingContext, BoxSizing, CacheTree, Clear,
+ DetailedGridInfo, Dimension, Display, FloatDirection, Layout, LayoutBlockContainer,
+ LayoutFlexboxContainer, LayoutGridContainer, LayoutInput, LayoutOutput, LayoutPartialTree,
+ LeafLayoutContext, Line, MaybeMath, MaybeResolve, NodeId, Point, ResolveOrZero, RoundTree,
+ RunMode, Size, SizingMode, SizingPurpose, Style, TraversePartialTree, TraverseTree,
+ compute_block_layout, compute_cached_layout, compute_flexbox_layout, compute_grid_layout,
+ compute_hidden_layout, compute_leaf_layout_with_context, compute_root_layout, round_layout,
};
use crate::{
@@ -580,6 +580,7 @@ where
continue;
};
let item_layout = world.boxes[item.index()].unrounded_layout;
+ let parent_writing_mode = world.boxes[item.index()].style.writing_mode();
let parent_width = (item_layout.size.width
- item_layout.border.left
- item_layout.border.right
@@ -593,6 +594,7 @@ where
width: Some(parent_width),
height: None,
},
+ parent_writing_mode,
available_space: Size {
width: AvailableSpace::MaxContent,
height: AvailableSpace::MaxContent,
@@ -601,6 +603,7 @@ where
sizing_purpose: SizingPurpose::Layout,
run_mode: RunMode::PerformLayout,
axis: taffy::RequestedAxis::Both,
+ block_auto_behavior: AutoSizeBehavior::FitContent,
vertical_margins_are_collapsible: Line::FALSE,
};
let output = world.compute_child_layout(marker.to_taffy(), inputs);
@@ -665,6 +668,7 @@ where
continue;
};
let control_layout = world.boxes[control.index()].unrounded_layout;
+ let parent_writing_mode = world.boxes[control.index()].style.writing_mode();
let content_width = (control_layout.size.width
- control_layout.border.left
- control_layout.border.right
@@ -679,6 +683,7 @@ where
width: Some(content_width),
height: None,
},
+ parent_writing_mode,
available_space: Size {
width: AvailableSpace::Definite(content_width),
height: AvailableSpace::MaxContent,
@@ -687,6 +692,7 @@ where
sizing_purpose: SizingPurpose::Layout,
run_mode: RunMode::PerformLayout,
axis: taffy::RequestedAxis::Both,
+ block_auto_behavior: AutoSizeBehavior::FitContent,
vertical_margins_are_collapsible: Line::FALSE,
};
let output = world.compute_child_layout(content.to_taffy(), inputs);
@@ -850,6 +856,7 @@ struct PositionedContainingArea {
origin: Point<f32>,
size: Size<f32>,
direction: taffy::Direction,
+ writing_mode: taffy::WritingMode,
requires_inline_layout: bool,
}
@@ -986,6 +993,7 @@ where
height: viewport.css_height as f32,
},
direction: world.boxes[world.root.index()].style.taffy.direction,
+ writing_mode: world.boxes[world.root.index()].style.writing_mode(),
requires_inline_layout: false,
};
};
@@ -1007,6 +1015,7 @@ where
height: rect.height,
},
direction: containing_box.style.taffy.direction,
+ writing_mode: containing_box.style.writing_mode(),
requires_inline_layout: true,
};
}
@@ -1024,6 +1033,7 @@ where
},
size: padding_box_size,
direction: containing_box.style.taffy.direction,
+ writing_mode: containing_box.style.writing_mode(),
requires_inline_layout: containing_box.inline_formatting_context,
}
}
@@ -1115,6 +1125,7 @@ fn layout_inline_absolute_child<N>(
) where
N: Copy + Debug + Eq + Hash,
{
+ let child_writing_mode = world.boxes[child.index()].style.writing_mode();
let style = world.boxes[child.index()].style.taffy.clone();
if style.display == Display::None || style.position != taffy::Position::Absolute {
return;
@@ -1122,16 +1133,17 @@ fn layout_inline_absolute_child<N>(
let area_width = area.size.width;
let area_height = area.size.height;
+ let percentage_basis = area.writing_mode.to_logical(area.size).inline_size;
let aspect_ratio = style.aspect_ratio;
let margin = style
.margin
- .map(|value| value.maybe_resolve(area_width, resolve_stylo_calc_value));
+ .map(|value| value.maybe_resolve(percentage_basis, resolve_stylo_calc_value));
let padding = style
.padding
- .resolve_or_zero(Some(area_width), resolve_stylo_calc_value);
+ .resolve_or_zero(Some(percentage_basis), resolve_stylo_calc_value);
let border = style
.border
- .resolve_or_zero(Some(area_width), resolve_stylo_calc_value);
+ .resolve_or_zero(Some(percentage_basis), resolve_stylo_calc_value);
let padding_border_sum = (padding + border).sum_axes();
let box_sizing_adjustment = if style.box_sizing == BoxSizing::ContentBox {
padding_border_sum
@@ -1154,6 +1166,15 @@ fn layout_inline_absolute_child<N>(
.inset
.bottom
.maybe_resolve(area_height, resolve_stylo_calc_value);
+ let block_auto_behavior = match child_writing_mode.block_axis() {
+ AbsoluteAxis::Horizontal if left.is_some() && right.is_some() => {
+ AutoSizeBehavior::StretchExplicit
+ }
+ AbsoluteAxis::Vertical if top.is_some() && bottom.is_some() => {
+ AutoSizeBehavior::StretchExplicit
+ }
+ _ => AutoSizeBehavior::FitContent,
+ };
let style_size = style
.size
.maybe_resolve(area.size, resolve_stylo_calc_value)
@@ -1215,11 +1236,13 @@ fn layout_inline_absolute_child<N>(
known_dimensions,
definite_dimensions: known_dimensions,
parent_size: area.size.map(Some),
+ parent_writing_mode: area.writing_mode,
available_space,
sizing_mode: SizingMode::ContentSize,
sizing_purpose: SizingPurpose::IntrinsicContribution,
run_mode: RunMode::ComputeSize,
axis: taffy::RequestedAxis::Horizontal,
+ block_auto_behavior,
vertical_margins_are_collapsible: Line::FALSE,
},
available_width,
@@ -1235,11 +1258,13 @@ fn layout_inline_absolute_child<N>(
known_dimensions,
definite_dimensions: known_dimensions,
parent_size: area.size.map(Some),
+ parent_writing_mode: area.writing_mode,
available_space,
sizing_mode: SizingMode::ContentSize,
sizing_purpose: SizingPurpose::Layout,
run_mode: RunMode::ComputeSize,
axis: taffy::RequestedAxis::Both,
+ block_auto_behavior,
vertical_margins_are_collapsible: Line::FALSE,
},
)
@@ -1253,11 +1278,13 @@ fn layout_inline_absolute_child<N>(
known_dimensions: final_size.map(Some),
definite_dimensions: known_dimensions,
parent_size: area.size.map(Some),
+ parent_writing_mode: area.writing_mode,
available_space,
sizing_mode: SizingMode::ContentSize,
sizing_purpose: SizingPurpose::Layout,
run_mode: RunMode::PerformLayout,
axis: taffy::RequestedAxis::Both,
+ block_auto_behavior,
vertical_margins_are_collapsible: Line::FALSE,
},
);
@@ -1406,6 +1433,16 @@ where
}
}
+ fn get_writing_mode(&self, node_id: NodeId) -> taffy::WritingMode {
+ if self.is_viewport_taffy_node(node_id) {
+ taffy::WritingMode::HorizontalTb
+ } else {
+ self.boxes[LayoutBoxId::from_taffy(node_id).index()]
+ .style
+ .writing_mode()
+ }
+ }
+
fn get_scrollbar_insets(&self, node_id: NodeId) -> taffy::Rect<f32> {
if self.is_viewport_taffy_node(node_id) {
// The synthetic viewport expresses its gutters as non-painted
@@ -1780,6 +1817,7 @@ where
let text = layout_box.text.clone();
let font_size = layout_box.style.font_size();
let line_height = layout_box.style.line_height();
+ let writing_mode = layout_box.style.writing_mode();
let replaced_context = layout_box.replaced_context;
let resolved_aspect_ratio = layout_box.resolved_aspect_ratio();
@@ -1801,19 +1839,11 @@ where
inputs.sizing_mode,
inputs.axis,
);
- return LayoutOutput {
- size,
- content_size: size,
- first_baselines: Point::NONE,
- last_baselines: Point::NONE,
- top_margin: taffy::tree::CollapsibleMarginSet::ZERO,
- bottom_margin: taffy::tree::CollapsibleMarginSet::ZERO,
- margins_can_collapse_through: false,
- };
+ return LayoutOutput::from_sizes(size, size);
}
let scrollbar_insets = self.get_scrollbar_insets(id.to_taffy());
- let context = LeafLayoutContext::new(resolved_aspect_ratio, scrollbar_insets);
+ let context = LeafLayoutContext::new(writing_mode, resolved_aspect_ratio, scrollbar_insets);
compute_leaf_layout_with_context(
inputs,
&style,
@@ -1846,6 +1876,7 @@ where
) -> LayoutOutput {
let scrollbar_insets = self.get_scrollbar_insets(id.to_taffy());
let style = self.boxes[id.index()].style.taffy.clone();
+ let writing_mode = self.boxes[id.index()].style.writing_mode();
let resolved_aspect_ratio = self.boxes[id.index()].resolved_aspect_ratio();
let is_floated = box_is_effectively_floated(self, id);
// Both Taffy's block-float parent and Moli's IFC float parent
@@ -1883,7 +1914,7 @@ where
}
let mut content_widths = std::mem::take(&mut inline_context.content_widths);
let mut measurement: Option<InlineMeasurement> = None;
- let context = LeafLayoutContext::new(resolved_aspect_ratio, scrollbar_insets);
+ let context = LeafLayoutContext::new(writing_mode, resolved_aspect_ratio, scrollbar_insets);
let mut output = compute_leaf_layout_with_context(
leaf_inputs,
&style,
@@ -2024,6 +2055,7 @@ where
// output and retains the expensive shaping allocations.
reset_inline_layout_for_probe(layout);
+ let parent_writing_mode = self.boxes[owner.index()].style.writing_mode();
let child_inputs = LayoutInput {
run_mode: inputs.run_mode,
sizing_mode: SizingMode::InherentSize,
@@ -2032,7 +2064,9 @@ where
known_dimensions: Size::NONE,
definite_dimensions: Size::NONE,
parent_size: available_space.into_options(),
+ parent_writing_mode,
available_space,
+ block_auto_behavior: AutoSizeBehavior::FitContent,
vertical_margins_are_collapsible: Line::FALSE,
};
// A float's max-content contribution is measured independently from
diff --git a/moli-layout/tests/phase4_layout_contract.rs b/moli-layout/tests/phase4_layout_contract.rs
index 9a0d84664..ddc410b46 100644
--- a/moli-layout/tests/phase4_layout_contract.rs
+++ b/moli-layout/tests/phase4_layout_contract.rs
@@ -490,13 +490,16 @@ fn table_sections_use_visual_header_body_footer_order() {
.insert(13, sized(LayoutDisplay::TableCell, 25.0, 10.0, header_b));
let snapshot = render(&source, &mut styles, 200, 100);
+ // Chromium combines all three rows into 75px/80px specified guesses,
+ // distributes the table's 100px used width to 48.375px/51.625px, then
+ // snaps the shared paint edge to 48px at device scale 1.
for (color, expected) in [
- (header_a, (0.0, 0.0, 75.0)),
- (header_b, (75.0, 0.0, 25.0)),
- (body_a, (0.0, 10.0, 75.0)),
- (body_b, (75.0, 10.0, 25.0)),
- (footer_a, (0.0, 20.0, 75.0)),
- (footer_b, (75.0, 20.0, 25.0)),
+ (header_a, (0.0, 0.0, 48.0)),
+ (header_b, (48.0, 0.0, 52.0)),
+ (body_a, (0.0, 10.0, 48.0)),
+ (body_b, (48.0, 10.0, 52.0)),
+ (footer_a, (0.0, 20.0, 48.0)),
+ (footer_b, (48.0, 20.0, 52.0)),
] {
let actual = rect(&snapshot, color);
assert_close(actual.x, expected.0);
diff --git a/moli-renderer-v8/src/runtime/phase_one/mod.rs b/moli-renderer-v8/src/runtime/phase_one/mod.rs
index 4a4b408fb..fdb6ba4b4 100644
--- a/moli-renderer-v8/src/runtime/phase_one/mod.rs
+++ b/moli-renderer-v8/src/runtime/phase_one/mod.rs
@@ -1038,6 +1038,158 @@ td{padding:0;border:0;height:10px}
}));
}
+ #[test]
+ fn automatic_table_layout_collects_authored_widths_after_a_colspan_header() {
+ let runtime = tokio::runtime::Builder::new_current_thread()
+ .enable_all()
+ .build()
+ .expect("current-thread runtime should build");
+
+ runtime.block_on(tokio::task::LocalSet::new().run_until(async move {
+ let snapshot = render_test_snapshot(
+ r#"<!doctype html><html><head><style>
+html,body{margin:0;padding:0}
+table{border-spacing:3px;background:rgb(10,11,12)}
+td{box-sizing:border-box;padding:0;border:0}
+.month{height:4px}
+.day{width:10px;height:10px}
+#first{background:rgb(21,31,41)}#second{background:rgb(22,32,42)}#third{background:rgb(23,33,43)}
+</style></head><body><table>
+<tr><td class=month colspan=3></td></tr>
+<tr><td id=first class=day></td><td id=second class=day></td><td id=third class=day></td></tr>
+</table></body></html>"#,
+ )
+ .await;
+
+ // GitHub's contribution calendar has the same shape: a leading
+ // month-label row spans columns, while the authored 10px day-cell
+ // widths only appear in later rows. Automatic layout must collect
+ // those later constraints; the 3px border spacing stays separate.
+ assert_paint_rect(
+ solid_paint_rect(&snapshot, rgb(10, 11, 12)),
+ moli_layout::PaintRect::new(0.0, 0.0, 42.0, 23.0),
+ );
+ for (color, x) in [
+ (rgb(21, 31, 41), 3.0),
+ (rgb(22, 32, 42), 16.0),
+ (rgb(23, 33, 43), 29.0),
+ ] {
+ assert_paint_rect(
+ solid_paint_rect(&snapshot, color),
+ moli_layout::PaintRect::new(x, 10.0, 10.0, 10.0),
+ );
+ }
+ }));
+ }
+
+ #[test]
+ fn automatic_table_layout_matches_chromium_distribution_phases_and_colspans() {
+ let runtime = tokio::runtime::Builder::new_current_thread()
+ .enable_all()
+ .build()
+ .expect("current-thread runtime should build");
+
+ runtime.block_on(tokio::task::LocalSet::new().run_until(async move {
+ let snapshot = render_test_snapshot(
+ r#"<!doctype html><html><head><style>
+html,body{margin:0;padding:0}
+table{table-layout:auto;border-spacing:0}
+td{box-sizing:border-box;border:0;padding:0;height:10px}
+#mixed{width:400px}#mixed-auto>div{width:90px;height:10px}
+#mixed-percent{background:rgb(31,41,51)}#mixed-fixed{background:rgb(32,42,52)}#mixed-auto{background:rgb(33,43,53)}
+#fixed-only{width:300px}#fixed-small{background:rgb(41,51,61)}#fixed-large{background:rgb(42,52,62)}
+#percent-only{width:200px}#percent-left{background:rgb(51,61,71)}#percent-right{background:rgb(52,62,72)}
+#spans{width:220px;border-spacing:10px 0}#span-left>div{width:40px;height:10px}#span-right>div{width:60px;height:10px}
+#span-left{background:rgb(61,71,81)}#span-right{background:rgb(62,72,82)}
+#percent-span{width:300px}#percent-span-left{background:rgb(71,81,91)}#percent-span-right{background:rgb(72,82,92)}
+</style></head><body>
+<table id=mixed><col style="width:25%"><col style="width:60px"><col><tr><td id=mixed-percent></td><td id=mixed-fixed></td><td id=mixed-auto><div></div></td></tr></table>
+<table id=fixed-only><col style="width:50px"><col style="width:100px"><tr><td id=fixed-small></td><td id=fixed-large></td></tr></table>
+<table id=percent-only><col style="width:25%"><col style="width:25%"><tr><td id=percent-left></td><td id=percent-right></td></tr></table>
+<table id=spans><tr><td colspan=2 style="width:150px"></td></tr><tr><td id=span-left><div></div></td><td id=span-right><div></div></td></tr></table>
+<table id=percent-span><tr><td colspan=2 style="width:60%"></td></tr><tr><td id=percent-span-left></td><td id=percent-span-right></td></tr></table>
+</body></html>"#,
+ )
+ .await;
+
+ // Captured with Chromium 147. The cases exercise, in order:
+ // automatic-column priority above MAX, constrained-column growth,
+ // percentage-only fallback, constrained colspan distribution with
+ // separate border spacing, and percentage colspan projection.
+ for (color, expected) in [
+ (rgb(31, 41, 51), (0.0, 0.0, 100.0)),
+ (rgb(32, 42, 52), (100.0, 0.0, 60.0)),
+ (rgb(33, 43, 53), (160.0, 0.0, 240.0)),
+ (rgb(41, 51, 61), (0.0, 10.0, 100.0)),
+ (rgb(42, 52, 62), (100.0, 10.0, 200.0)),
+ (rgb(51, 61, 71), (0.0, 20.0, 100.0)),
+ (rgb(52, 62, 72), (100.0, 20.0, 100.0)),
+ (rgb(61, 71, 81), (10.0, 40.0, 76.0)),
+ (rgb(62, 72, 82), (96.0, 40.0, 114.0)),
+ (rgb(71, 81, 91), (0.0, 60.0, 150.0)),
+ (rgb(72, 82, 92), (150.0, 60.0, 150.0)),
+ ] {
+ assert_paint_rect(
+ solid_paint_rect(&snapshot, color),
+ moli_layout::PaintRect::new(expected.0, expected.1, expected.2, 10.0),
+ );
+ }
+ }));
+ }
+
+ #[test]
+ fn automatic_table_layout_preserves_chromium_constraint_collection_edges() {
+ let runtime = tokio::runtime::Builder::new_current_thread()
+ .enable_all()
+ .build()
+ .expect("current-thread runtime should build");
+
+ runtime.block_on(tokio::task::LocalSet::new().run_until(async move {
+ let snapshot = render_test_snapshot(
+ r#"<!doctype html><html><head><style>
+html,body{margin:0;padding:0}
+table{table-layout:auto;border-spacing:0}
+td{box-sizing:border-box;border:0;padding:0;height:10px}
+#over{width:300px}#over-a{background:rgb(81,91,101)}#over-b{background:rgb(82,92,102)}
+#group{width:240px}#group-a{background:rgb(91,101,111)}#group-b{background:rgb(92,102,112)}#group-c{background:rgb(93,103,113)}
+#rtl{width:200px;direction:rtl}#rtl-a{background:rgb(101,111,121)}#rtl-b{background:rgb(102,112,122)}
+#padded{width:300px}#padded-a{box-sizing:content-box;width:50%;padding:0 10px;background:rgb(111,121,131)}#padded-b{background:rgb(112,122,132)}
+#conflict{width:300px}#conflict-a{width:120px;background:rgb(121,131,141)}#conflict-b{background:rgb(122,132,142)}
+</style></head><body>
+<table id=over><col style="width:70%"><col style="width:60%"><tr><td id=over-a></td><td id=over-b></td></tr></table>
+<table id=group><colgroup span=2 style="width:40px"></colgroup><col><tr><td id=group-a></td><td id=group-b></td><td id=group-c></td></tr></table>
+<table id=rtl><col style="width:50px"><col><tr><td id=rtl-a></td><td id=rtl-b></td></tr></table>
+<table id=padded><tr><td id=padded-a></td><td id=padded-b></td></tr></table>
+<table id=conflict><col style="width:80px"><col><tr><td id=conflict-a></td><td id=conflict-b></td></tr></table>
+</body></html>"#,
+ )
+ .await;
+
+ // Captured with Chromium 147. These cases cross the DOM/style
+ // collection boundary before exercising percentage clipping,
+ // colgroup expansion, logical RTL placement, table-cell sizing
+ // box semantics, and column/cell constraint precedence.
+ for (color, expected) in [
+ (rgb(81, 91, 101), (0.0, 0.0, 210.0)),
+ (rgb(82, 92, 102), (210.0, 0.0, 90.0)),
+ (rgb(91, 101, 111), (0.0, 10.0, 40.0)),
+ (rgb(92, 102, 112), (40.0, 10.0, 40.0)),
+ (rgb(93, 103, 113), (80.0, 10.0, 160.0)),
+ (rgb(101, 111, 121), (150.0, 20.0, 50.0)),
+ (rgb(102, 112, 122), (0.0, 20.0, 150.0)),
+ (rgb(111, 121, 131), (0.0, 30.0, 150.0)),
+ (rgb(112, 122, 132), (150.0, 30.0, 150.0)),
+ (rgb(121, 131, 141), (0.0, 40.0, 120.0)),
+ (rgb(122, 132, 142), (120.0, 40.0, 180.0)),
+ ] {
+ assert_paint_rect(
+ solid_paint_rect(&snapshot, color),
+ moli_layout::PaintRect::new(expected.0, expected.1, expected.2, 10.0),
+ );
+ }
+ }));
+ }
+
#[test]
fn fixed_table_layout_distributes_first_row_colspan_constraints() {
let runtime = tokio::runtime::Builder::new_current_thread()