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): clamp negative widths in intrinsic inline probes

ldm0bdf76173fe43
diff --git a/moli-layout/src/taffy_tree.rs b/moli-layout/src/taffy_tree.rs
index 971a32a02..bc2fa1e03 100644
--- a/moli-layout/src/taffy_tree.rs
+++ b/moli-layout/src/taffy_tree.rs
@@ -2397,8 +2397,13 @@ where
float_max_width = float_max_width.max(left_band + right_band);
}
}
- let width = known_dimensions.width.unwrap_or_else(|| {
- match available_space.width {
+ // Intrinsic block probes can pass a negative known width when margins
+ // exceed the containing block. Clamp both known and measured widths:
+ // the float slots are nonnegative, and Parley requires each line's
+ // available width to be no greater than the paragraph's width.
+ let width = known_dimensions
+ .width
+ .unwrap_or_else(|| match available_space.width {
AvailableSpace::MinContent => content_widths.min.max(float_min_width),
AvailableSpace::MaxContent => content_widths.max + float_max_width,
// Taffy has already resolved and clamped the content-box
@@ -2411,9 +2416,8 @@ where
.min(limit)
.max(content_widths.min.max(float_min_width)),
AvailableSpace::Definite(limit) => limit,
- }
- .max(0.0)
- });
+ })
+ .max(0.0);
// Taffy may feed an intrinsic inline size back through a quantized
// definite flex/grid constraint, while Parley's content-width and
// line-breaking passes can accumulate the same glyph advances in a
@@ -2536,7 +2540,7 @@ where
let alignment_block_size = height.max(alignment_float_height);
InlineMeasurement {
size: Size {
- width: known_dimensions.width.unwrap_or(width),
+ width,
height: known_dimensions.height.unwrap_or(height),
},
alignment_block_size,
diff --git a/moli-layout/tests/phase4_layout_contract.rs b/moli-layout/tests/phase4_layout_contract.rs
index 64db0de42..1eb434722 100644
--- a/moli-layout/tests/phase4_layout_contract.rs
+++ b/moli-layout/tests/phase4_layout_contract.rs
@@ -661,6 +661,75 @@ fn left_float_restricts_inline_slots_and_clear_moves_the_next_block_below_it() {
assert_close(rect(&snapshot, GREEN).y, 40.0);
}
+#[test]
+fn intrinsic_inline_probes_clamp_widths_consumed_by_margins_beside_floats() {
+ // A grid/flex intrinsic probe can give the paragraph a known width of
+ // `container_width - 40px`. Parley's float-aware breaker must still get
+ // a nonnegative paragraph width when the margin consumes all the space.
+ let source = Source(vec![
+ Node::element("root", "div", LayoutElementCategory::Generic, None, vec![1]),
+ Node::element(
+ "container",
+ "div",
+ LayoutElementCategory::Generic,
+ None,
+ vec![2, 3],
+ ),
+ Node::element("float", "div", LayoutElementCategory::Generic, None, vec![]),
+ Node::element(
+ "paragraph",
+ "p",
+ LayoutElementCategory::Generic,
+ None,
+ vec![4],
+ ),
+ Node::text("text", "text"),
+ ]);
+ for display in [LayoutDisplay::Grid, LayoutDisplay::Flex] {
+ for container_width in [0.0, 20.0, 40.0, 100.0] {
+ let mut styles = Styles::default();
+ styles
+ .primary
+ .insert(0, sized(display, 200.0, 100.0, PaintColor::TRANSPARENT));
+ styles.primary.insert(
+ 1,
+ style(LayoutDisplay::Block, PaintColor::TRANSPARENT).tap_taffy(|taffy| {
+ taffy.size.width = Dimension::length(container_width);
+ }),
+ );
+ styles.primary.insert(
+ 2,
+ sized(LayoutDisplay::Block, 10.0, 10.0, BLUE).with_float(Float::Left, Clear::None),
+ );
+ styles.primary.insert(
+ 3,
+ style(LayoutDisplay::Block, PaintColor::TRANSPARENT).tap_taffy(|taffy| {
+ taffy.margin.left = taffy::LengthPercentageAuto::length(40.0);
+ }),
+ );
+
+ let snapshot = render(&source, &mut styles, 200, 100);
+ assert_close(rect(&snapshot, BLUE).x, 0.0);
+ let glyphs = snapshot
+ .fragments
+ .iter()
+ .filter_map(|fragment| match fragment {
+ PaintFragment::GlyphRun(run) => Some(run.glyphs_in_surface()),
+ _ => None,
+ })
+ .flatten()
+ .collect::<Vec<_>>();
+ assert!(!glyphs.is_empty(), "text must survive the intrinsic probe");
+ assert_close(glyphs[0].x, 40.0);
+ assert!(
+ glyphs
+ .iter()
+ .all(|glyph| glyph.x.is_finite() && glyph.y.is_finite())
+ );
+ }
+ }
+}
+
#[test]
fn float_descendant_of_structural_inline_rounds_with_its_ifc_owner() {
let source = Source(vec![