Skip to content

armgit / moli

Public Imported from GitHub
0

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

Fix cyclic calc min-width contributions

ldm0db979d2307c2
diff --git a/moli-layout/src/taffy_tree.rs b/moli-layout/src/taffy_tree.rs
index d97c980e7..fcb1d37c1 100644
--- a/moli-layout/src/taffy_tree.rs
+++ b/moli-layout/src/taffy_tree.rs
@@ -32,6 +32,12 @@ where
N: Copy + Debug + Eq + Hash,
{
for layout_box in &mut world.boxes {
+ let is_replaced = layout_box
+ .element_semantics
+ .as_ref()
+ .is_some_and(crate::LayoutElementSemantics::is_replaced);
+ layout_box.block_intrinsic_style =
+ block_intrinsic_style(&layout_box.style.taffy, is_replaced);
layout_box.cache.clear();
layout_box.unrounded_layout = Layout::with_order(0);
layout_box.final_layout = Layout::with_order(0);
@@ -43,6 +49,7 @@ where
world.viewport_layout.children.clear();
world.viewport_layout.cache.clear();
+ world.block_child_intrinsic_zero_min_width = false;
world.viewport_layout.unrounded_layout = Layout::with_order(0);
world.viewport_layout.final_layout = Layout::with_order(0);
world.viewport_layout.style = Style {
@@ -1170,7 +1177,7 @@ where
};
if self.is_viewport_taffy_node(node_id) {
return compute_cached_layout(self, node_id, inputs, |world, node_id, inputs| {
- compute_block_layout(world, node_id, inputs, None)
+ world.compute_block_layout_with_cyclic_min_width(node_id, inputs, None)
});
}
if self.should_hide(node_id, inputs) {
@@ -1235,7 +1242,14 @@ where
}
fn get_block_child_style(&self, child_node_id: NodeId) -> Self::BlockItemStyle<'_> {
- self.get_core_container_style(child_node_id)
+ let id = LayoutBoxId::from_taffy(child_node_id);
+ let child = &self.boxes[id.index()];
+ if self.block_child_intrinsic_zero_min_width {
+ if let Some(style) = child.block_intrinsic_style.as_ref() {
+ return style;
+ }
+ }
+ &child.style.taffy
}
fn compute_block_child_layout(
@@ -1322,6 +1336,44 @@ impl<N> LayoutWorld<N>
where
N: Copy + Debug + Eq + Hash,
{
+ fn compute_block_layout_with_cyclic_min_width(
+ &mut self,
+ node_id: NodeId,
+ inputs: LayoutInput,
+ block_context: Option<&mut BlockContext<'_>>,
+ ) -> LayoutOutput {
+ let width_is_definite = {
+ let style = self.get_core_container_style(node_id);
+ let resolved_preferred = style
+ .size
+ .width
+ .maybe_resolve(inputs.parent_size.width, resolve_stylo_calc_value);
+ let resolved_min = style
+ .min_size
+ .width
+ .maybe_resolve(inputs.parent_size.width, resolve_stylo_calc_value);
+ let resolved_max = style
+ .max_size
+ .width
+ .maybe_resolve(inputs.parent_size.width, resolve_stylo_calc_value);
+ let min_max_determine_width =
+ matches!((resolved_min, resolved_max), (Some(min), Some(max)) if max <= min);
+ inputs
+ .known_dimensions
+ .maybe_apply_aspect_ratio(style.aspect_ratio)
+ .width
+ .or(resolved_preferred)
+ .is_some()
+ || min_max_determine_width
+ };
+ let previous = self.block_child_intrinsic_zero_min_width;
+ self.block_child_intrinsic_zero_min_width =
+ inputs.sizing_mode == SizingMode::InherentSize && !width_is_definite;
+ let output = compute_block_layout(self, node_id, inputs, block_context);
+ self.block_child_intrinsic_zero_min_width = previous;
+ output
+ }
+
fn should_hide(&self, node_id: NodeId, inputs: LayoutInput) -> bool {
inputs.run_mode == RunMode::PerformHiddenLayout
|| self.boxes[LayoutBoxId::from_taffy(node_id).index()]
@@ -1371,7 +1423,7 @@ where
return compute_table_layout(self, id, inputs);
}
if display.is_table() {
- return compute_block_layout(self, node_id, inputs, block_context);
+ return self.compute_block_layout_with_cyclic_min_width(node_id, inputs, block_context);
}
match kind {
@@ -1403,7 +1455,7 @@ where
| LayoutBoxKind::AnonymousTableRowGroup
| LayoutBoxKind::AnonymousTableRow
| LayoutBoxKind::AnonymousTableCell => {
- compute_block_layout(self, node_id, inputs, block_context)
+ self.compute_block_layout_with_cyclic_min_width(node_id, inputs, block_context)
}
LayoutBoxKind::PrincipalInline
| LayoutBoxKind::InlineContinuation
@@ -2188,6 +2240,24 @@ fn inline_percentage_basis(available: AvailableSpace) -> Option<f32> {
Some(available.into_option().unwrap_or(0.0))
}
+fn block_intrinsic_style(style: &Style<Atom>, is_replaced: bool) -> Option<Style<Atom>> {
+ if is_replaced || !style.min_size.width.into_raw().is_calc() {
+ return None;
+ }
+
+ // When this box's containing block depends on its contribution, CSS
+ // Sizing resolves a cyclic percentage in min-width against zero. Retain a
+ // separate projection for that probe so final layout still uses the
+ // original expression against the now-known containing-block width.
+ let width = style
+ .min_size
+ .width
+ .maybe_resolve(Some(0.0), resolve_stylo_calc_value)?;
+ let mut projected = style.clone();
+ projected.min_size.width = Dimension::length(width);
+ Some(projected)
+}
+
#[cfg(test)]
mod tests {
use super::inline_percentage_basis;
diff --git a/moli-layout/src/world.rs b/moli-layout/src/world.rs
index af4b92138..47cd0b725 100644
--- a/moli-layout/src/world.rs
+++ b/moli-layout/src/world.rs
@@ -211,6 +211,9 @@ pub struct LayoutBox<N> {
/// Static position emitted by the shared IFC's out-of-flow placeholder.
pub(crate) inline_static_position: Option<InlineStaticPosition>,
pub(crate) style: ResolvedLayoutStyle,
+ /// Optional style projection used only while this box contributes to an
+ /// intrinsically sized block containing block.
+ pub(crate) block_intrinsic_style: Option<Style<Atom>>,
pub(crate) text: Option<Arc<str>>,
pub(crate) text_selection: Option<crate::LayoutTextSelection>,
/// Shared text/inline layout owned by this formatting-context root.
@@ -347,6 +350,7 @@ where
pub(crate) display_contents_mapping: HashMap<N, Vec<LayoutBoxId>>,
pub(crate) root: LayoutBoxId,
pub(crate) viewport_layout: ViewportLayoutState,
+ pub(crate) block_child_intrinsic_zero_min_width: bool,
}
impl<N> LayoutWorld<N>
@@ -360,6 +364,7 @@ where
display_contents_mapping: HashMap::new(),
root: LayoutBoxId::from_index(0),
viewport_layout: ViewportLayoutState::default(),
+ block_child_intrinsic_zero_min_width: false,
}
}
@@ -679,6 +684,7 @@ where
positioned_containing_block: None,
inline_static_position: None,
style,
+ block_intrinsic_style: None,
text,
text_selection: None,
inline_layout: None,
diff --git a/moli-renderer-v8/src/runtime/phase_one/mod.rs b/moli-renderer-v8/src/runtime/phase_one/mod.rs
index 9b76dc7fb..c901dfa0d 100644
--- a/moli-renderer-v8/src/runtime/phase_one/mod.rs
+++ b/moli-renderer-v8/src/runtime/phase_one/mod.rs
@@ -734,6 +734,57 @@ html, body { display: block; margin: 0; padding: 0 }
}));
}
+ #[test]
+ fn layout_renderer_preserves_calc_min_width_in_float_intrinsic_contribution() {
+ 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 mut page_vm = parse_phase_one_html_into_page_vm_for_test(
+ r#"<!doctype html><html><head><style>
+html,body{margin:0;padding:0}
+#host{width:0}
+#outer{float:left;height:30px;background:rgb(101,102,103)}
+#inner{width:40px;min-width:calc(160px + 0%);height:20px;background:rgb(111,112,113)}
+#definite{clear:both;width:200px;height:20px}
+#definite-child{width:40px;min-width:calc(20px + 50%);height:20px;background:rgb(121,122,123)}
+</style></head><body><div id=host><div id=outer><div id=inner></div></div></div>
+<div id=definite><div id=definite-child></div></div></body></html>"#,
+ )
+ .await;
+ page_vm.vm_mut().sync_live_document_style_sources();
+
+ let snapshot = page_vm
+ .vm_mut()
+ .screenshot_layout_snapshot(moli_layout::PaintViewport::new(800, 600, 1.0))
+ .expect("float intrinsic layout should succeed")
+ .expect("fixture should have a document element");
+ assert_paint_rect(
+ solid_paint_rect(
+ &snapshot,
+ moli_layout::PaintColor::new(101.0 / 255.0, 102.0 / 255.0, 103.0 / 255.0, 1.0),
+ ),
+ moli_layout::PaintRect::new(0.0, 0.0, 160.0, 30.0),
+ );
+ assert_paint_rect(
+ solid_paint_rect(
+ &snapshot,
+ moli_layout::PaintColor::new(111.0 / 255.0, 112.0 / 255.0, 113.0 / 255.0, 1.0),
+ ),
+ moli_layout::PaintRect::new(0.0, 0.0, 160.0, 20.0),
+ );
+ assert_paint_rect(
+ solid_paint_rect(
+ &snapshot,
+ moli_layout::PaintColor::new(121.0 / 255.0, 122.0 / 255.0, 123.0 / 255.0, 1.0),
+ ),
+ moli_layout::PaintRect::new(0.0, 30.0, 120.0, 20.0),
+ );
+ }));
+ }
+
#[test]
fn layout_renderer_projects_current_blitz_taffy_parity_from_stylo() {
let runtime = tokio::runtime::Builder::new_current_thread()