Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(table): preserve fixed percentage intrinsic maxima

ldm06faf46282148
diff --git a/moli-layout/src/style.rs b/moli-layout/src/style.rs
index 7e933ccc5..4cc8ab0d6 100644
--- a/moli-layout/src/style.rs
+++ b/moli-layout/src/style.rs
@@ -1465,10 +1465,21 @@ impl ResolvedLayoutStyle {
self.list_marker_position
}
- pub(crate) fn table_layout_is_fixed(&self) -> bool {
- self.computed.as_ref().is_some_and(|computed| {
+ /// Whether this table uses the fixed table-layout algorithm after the
+ /// preferred inline-size eligibility rule is applied.
+ ///
+ /// `table-layout: fixed` is not sufficient on its own. CSS Tables routes
+ /// an automatic or max-content-sized table through automatic layout;
+ /// Blink exposes this combined decision as `IsFixedTableLayout()`.
+ pub(crate) fn uses_fixed_table_layout(&self) -> bool {
+ let authored_fixed = self.computed.as_ref().is_some_and(|computed| {
computed.clone_table_layout() == style::computed_values::table_layout::T::Fixed
- })
+ });
+ if !authored_fixed {
+ return false;
+ }
+ let preferred_inline_size = self.writing_mode().to_logical(self.taffy.size).inline_size;
+ !preferred_inline_size.is_auto() && !preferred_inline_size.is_max_content()
}
pub(crate) fn table_border_is_collapsed(&self) -> bool {
diff --git a/moli-layout/src/table.rs b/moli-layout/src/table.rs
index 8746b4a7d..1b60fb200 100644
--- a/moli-layout/src/table.rs
+++ b/moli-layout/src/table.rs
@@ -31,9 +31,10 @@ mod columns;
pub(crate) use collapsed_borders::CollapsedTableBorders;
use collapsed_borders::{prepare_collapsed_table_borders, set_collapsed_border_geometry};
use columns::{
- AutomaticTableSizingTarget, TableCellInlineConstraint, TableCellSpanConstraint,
- TableColumnConstraint, TableLayoutMode, apply_cell_constraints, compute_grid_inline_min_max,
- distribute_auto_columns, distribute_fixed_columns, fixed_grid_min_inline_size,
+ AutomaticTableSizingTarget, TABLE_MAX_INLINE_SIZE, 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)]
@@ -137,6 +138,37 @@ struct TableContext {
writing_mode: WritingMode,
}
+/// Parent-facing min/max-content sizes of the complete table wrapper.
+///
+/// Column constraints produce GRID_MIN/GRID_MAX. CSS Tables adds one wrapper
+/// rule after that calculation: a percentage-dependent fixed table has an
+/// effectively unbounded max-content contribution. Keeping the wrapper result
+/// separate prevents that rule from contaminating final column distribution.
+#[derive(Clone, Copy, Debug, PartialEq)]
+struct TableIntrinsicInlineSizes {
+ min_content: f32,
+ max_content: f32,
+}
+
+impl TableIntrinsicInlineSizes {
+ fn from_grid(
+ grid: columns::TableGridInlineMinMax,
+ layout_mode: TableLayoutMode,
+ preferred_inline_size: Dimension,
+ ) -> Self {
+ let max_content =
+ if layout_mode.is_fixed() && preferred_inline_size.may_have_percentage_dependence() {
+ TABLE_MAX_INLINE_SIZE
+ } else {
+ grid.max
+ };
+ Self {
+ min_content: grid.min,
+ max_content: max_content.max(grid.min),
+ }
+ }
+}
+
pub(crate) fn prepare_table_layout_trees<N>(world: &mut LayoutWorld<N>)
where
N: Copy + Debug + Eq + Hash,
@@ -319,7 +351,7 @@ where
let mut columns = Vec::new();
let mut max_columns = 0usize;
let mut column_tracks = Vec::new();
- let layout_mode = if root_style.table_layout_is_fixed() {
+ let layout_mode = if root_style.uses_fixed_table_layout() {
TableLayoutMode::Fixed
} else {
TableLayoutMode::Automatic
@@ -463,10 +495,16 @@ impl TableContext {
undistributable_space,
self.layout_mode,
);
+ let preferred_inline_size = self.writing_mode.to_logical(self.style.size).inline_size;
+ let intrinsic_inline_sizes = TableIntrinsicInlineSizes::from_grid(
+ grid_min_max,
+ self.layout_mode,
+ preferred_inline_size,
+ );
let used_inline_size = self.resolve_used_inline_size(
inputs,
- grid_min_max.min,
- grid_min_max.max,
+ grid_min_max,
+ intrinsic_inline_sizes,
inline_insets,
);
let assignable_inline_size = (used_inline_size - undistributable_space).max(0.0);
@@ -517,16 +555,22 @@ impl TableContext {
fn resolve_used_inline_size(
&self,
inputs: LayoutInput,
- grid_min: f32,
- grid_max: f32,
+ grid: columns::TableGridInlineMinMax,
+ intrinsic: TableIntrinsicInlineSizes,
inline_insets: f32,
) -> f32 {
let space = inputs.constraint_space(self.writing_mode);
+ let (min_content, max_content) =
+ if space.sizing_purpose == SizingPurpose::IntrinsicContribution {
+ (intrinsic.min_content, intrinsic.max_content)
+ } else {
+ (grid.min, grid.max)
+ };
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,
+ AvailableSpace::Definite(value) => min_content.max(value.max(0.0).min(max_content)),
+ AvailableSpace::MinContent => min_content,
+ AvailableSpace::MaxContent => max_content,
};
let logical_size = self.writing_mode.to_logical(self.style.size);
let logical_min_size = self.writing_mode.to_logical(self.style.min_size);
@@ -539,16 +583,16 @@ impl TableContext {
};
let resolve_dimension = |dimension: Dimension| {
if dimension.is_min_content() {
- Some(grid_min)
+ Some(min_content)
} else if dimension.is_max_content() {
- Some(grid_max)
+ Some(max_content)
} 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),
+ AvailableSpace::MinContent => Some(min_content),
+ AvailableSpace::MaxContent => Some(max_content),
}
} else {
dimension
@@ -574,7 +618,7 @@ impl TableContext {
.or(preferred)
.unwrap_or_else(fit_content);
if !self.layout_mode.is_fixed() {
- used = used.max(grid_min);
+ used = used.max(min_content);
}
if let Some(max_size) = max_size {
used = used.min(max_size);
@@ -1446,3 +1490,46 @@ where
self.context.detailed = Some(detailed_grid_info);
}
}
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn percentage_dependent_fixed_table_has_unbounded_parent_max_content_size() {
+ let grid = columns::TableGridInlineMinMax { min: 4.0, max: 4.0 };
+
+ assert_eq!(
+ TableIntrinsicInlineSizes::from_grid(
+ grid,
+ TableLayoutMode::Fixed,
+ Dimension::percent(1.0),
+ ),
+ TableIntrinsicInlineSizes {
+ min_content: 4.0,
+ max_content: TABLE_MAX_INLINE_SIZE,
+ },
+ );
+ assert_eq!(
+ TableIntrinsicInlineSizes::from_grid(
+ grid,
+ TableLayoutMode::Automatic,
+ Dimension::percent(1.0),
+ ),
+ TableIntrinsicInlineSizes {
+ min_content: 4.0,
+ max_content: 4.0,
+ },
+ );
+ assert_eq!(
+ TableIntrinsicInlineSizes::from_grid(
+ grid,
+ TableLayoutMode::Fixed,
+ Dimension::length(40.0),
+ ),
+ TableIntrinsicInlineSizes {
+ min_content: 4.0,
+ max_content: 4.0,
+ },
+ );
+ }
+}
diff --git a/moli-layout/src/table/columns.rs b/moli-layout/src/table/columns.rs
index ab35f0709..f00010b5a 100644
--- a/moli-layout/src/table/columns.rs
+++ b/moli-layout/src/table/columns.rs
@@ -1,9 +1,18 @@
mod auto;
pub(super) use auto::{
- AutomaticTableSizingTarget, compute_grid_inline_min_max, distribute_auto_columns,
+ AutomaticTableSizingTarget, TableGridInlineMinMax, compute_grid_inline_min_max,
+ distribute_auto_columns,
};
+/// Finite stand-in for an unbounded CSS table inline-size contribution.
+///
+/// Blink uses the same one-million CSS pixel ceiling for percentage-dependent
+/// fixed tables and for percentage column systems that have no finite maximum.
+/// Keeping the ceiling beside the shared table constraint types ensures those
+/// two parent-facing cases cannot drift apart.
+pub(super) const TABLE_MAX_INLINE_SIZE: f32 = 1_000_000.0;
+
/// Cell rows consulted while collecting authored table column constraints.
///
/// CSS fixed layout is defined by columns and the first visual row. Automatic
diff --git a/moli-layout/src/table/columns/auto.rs b/moli-layout/src/table/columns/auto.rs
index 980193f55..2979739cf 100644
--- a/moli-layout/src/table/columns/auto.rs
+++ b/moli-layout/src/table/columns/auto.rs
@@ -1,8 +1,8 @@
-use super::{TableCellSpanConstraint, TableColumnConstraint, TableLayoutMode};
+use super::{
+ TABLE_MAX_INLINE_SIZE, 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
diff --git a/moli-renderer-v8/src/runtime/phase_one/mod.rs b/moli-renderer-v8/src/runtime/phase_one/mod.rs
index fa1253ad3..4ad3a1908 100644
--- a/moli-renderer-v8/src/runtime/phase_one/mod.rs
+++ b/moli-renderer-v8/src/runtime/phase_one/mod.rs
@@ -1038,6 +1038,88 @@ td{padding:0;border:0;height:10px}
}));
}
+ #[test]
+ fn fixed_percentage_table_exports_its_parent_facing_max_content_size() {
+ 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}html{scrollbar-width:none}
+.case{width:100px}.wrapper{display:table}.cell{display:table-cell}
+.flex{display:flex;height:10px}.flex table{height:5px}
+#percent-flex{background:rgb(201,11,11)}#percent-table{background:rgb(201,12,12)}
+#calc-flex{background:rgb(202,11,11)}#calc-table{background:rgb(202,12,12)}
+#length-flex{background:rgb(203,11,11)}#length-table{background:rgb(203,12,12)}
+#auto-flex{background:rgb(204,11,11)}#auto-table{background:rgb(204,12,12)}
+</style></head><body>
+<div class=case><div class=wrapper><div class=cell><div id=percent-flex class=flex><table id=percent-table style="table-layout:fixed;width:100%"></table></div></div></div></div>
+<div class=case><div class=wrapper><div class=cell><div id=calc-flex class=flex><table id=calc-table style="table-layout:fixed;width:calc(40px + 0%)"></table></div></div></div></div>
+<div class=case><div class=wrapper><div class=cell><div id=length-flex class=flex><table id=length-table style="table-layout:fixed;width:40px"></table></div></div></div></div>
+<div class=case><div class=wrapper><div class=cell><div id=auto-flex class=flex><table id=auto-table style="table-layout:auto;width:100%"></table></div></div></div></div>
+</body></html>"#,
+ )
+ .await;
+
+ // Chromium gives a percentage-dependent fixed table an
+ // effectively unbounded max-content contribution. The wrapper
+ // therefore fills the 100px opportunity, while final layout still
+ // resolves the authored width normally. Definite and automatic
+ // controls must retain their ordinary finite contributions.
+ for (color, expected_width) in [
+ (rgb(201, 11, 11), 100.0),
+ (rgb(201, 12, 12), 100.0),
+ (rgb(202, 11, 11), 100.0),
+ (rgb(202, 12, 12), 40.0),
+ (rgb(203, 11, 11), 40.0),
+ (rgb(203, 12, 12), 40.0),
+ (rgb(204, 11, 11), 4.0),
+ (rgb(204, 12, 12), 4.0),
+ ] {
+ let rect = solid_paint_rect(&snapshot, color);
+ assert!(
+ (rect.width - expected_width).abs() <= 0.01,
+ "expected width {expected_width}, got {rect:?}",
+ );
+ }
+ }));
+ }
+
+ #[test]
+ fn table_layout_fixed_with_auto_width_uses_automatic_column_measurement() {
+ 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}html{scrollbar-width:none}
+table{table-layout:fixed;width:auto;border-spacing:0;background:rgb(205,11,11)}
+td{padding:0;border:0;height:5px;background:rgb(205,12,12)}
+.wide{width:80px;height:5px;background:rgb(205,13,13)}
+</style></head><body><table><tr><td style="width:10px"></td></tr><tr><td><div class=wide></div></td></tr></table></body></html>"#,
+ )
+ .await;
+
+ // `table-layout: fixed` selects the fixed algorithm only when the
+ // table has an eligible non-auto preferred width. Chromium keeps
+ // the computed property value but measures this table with the
+ // automatic algorithm, so the second row contributes 80px.
+ for color in [rgb(205, 11, 11), rgb(205, 12, 12), rgb(205, 13, 13)] {
+ let rect = solid_paint_rect(&snapshot, color);
+ assert!(
+ (rect.width - 80.0).abs() <= 0.01,
+ "auto-width table should measure every row: {rect:?}",
+ );
+ }
+ }));
+ }
+
#[test]
fn automatic_table_layout_collects_authored_widths_after_a_colspan_header() {
let runtime = tokio::runtime::Builder::new_current_thread()