Skip to content

armgit / moli

Public Imported from GitHub
0

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

Fix flex max-content text wrapping precision

ldm05f1dc8201e69
diff --git a/moli-layout/src/taffy_tree.rs b/moli-layout/src/taffy_tree.rs
index da7c1a1ad..b07159220 100644
--- a/moli-layout/src/taffy_tree.rs
+++ b/moli-layout/src/taffy_tree.rs
@@ -1808,9 +1808,24 @@ where
}
.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
+ // slightly different order. Preserve the max-content boundary plus a
+ // small floating-point margin when those widths differ only by noise.
+ // Otherwise a one-line flex item can immediately rewrap during final
+ // layout. Keep genuinely constrained widths unchanged so normal
+ // wrapping is unaffected.
+ let intrinsic_max_width = content_widths.max + float_max_width;
+ let intrinsic_tolerance = width.abs().max(1.0) * f32::EPSILON * 8.0;
+ let line_break_width = if intrinsic_max_width <= width + intrinsic_tolerance {
+ width.max(intrinsic_max_width + intrinsic_tolerance)
+ } else {
+ width
+ };
let max_advance = match available_space.width {
AvailableSpace::MaxContent => None,
- AvailableSpace::MinContent | AvailableSpace::Definite(_) => Some(width),
+ AvailableSpace::MinContent | AvailableSpace::Definite(_) => Some(line_break_width),
};
let has_inline_float = context
.objects
diff --git a/moli-renderer-v8/src/runtime/page_vm/tests/rendering_update.rs b/moli-renderer-v8/src/runtime/page_vm/tests/rendering_update.rs
index 6dfeeb05b..376030cbe 100644
--- a/moli-renderer-v8/src/runtime/page_vm/tests/rendering_update.rs
+++ b/moli-renderer-v8/src/runtime/page_vm/tests/rendering_update.rs
@@ -2334,6 +2334,63 @@ getComputedStyle(document.getElementById('inline-gradient')).getPropertyValue('-
.expect("background-clip:text screenshot fixture should run");
}
+#[tokio::test(flavor = "current_thread")]
+async fn rounded_flex_max_content_width_does_not_rewrap_its_text() {
+ run_page_vm_async_test(async move {
+ let loader =
+ crate::network::ResourceRequestClient::new(&FetchConfig::default()).expect("loader");
+ loader.set_optional_resource_fetch_mask(
+ crate::protocol_types::OptionalResourceFetchMask::FONT,
+ );
+ let mut page_vm = test_page_vm_with_loader_and_document_url(
+ &loader,
+ Vec::new(),
+ Url::parse("https://example.com/flex-intrinsic-rounding.html")?,
+ );
+ let encoded_font = include_bytes!(concat!(
+ env!("CARGO_MANIFEST_DIR"),
+ "/../moli-layout/tests/fixtures/moli-ahem.woff2"
+ ));
+ let encoded = base64::engine::general_purpose::STANDARD.encode(encoded_font);
+ page_vm.vm_mut().eval(&format!(
+ r#"
+document.head.innerHTML = `<style>
+@font-face {{ font-family:MoliAhem; src:url(data:font/woff2;base64,{encoded}) format('woff2') }}
+* {{ box-sizing:border-box }}
+html,body {{ margin:0 }}
+.row {{ display:flex; justify-content:center; gap:8px; padding-top:20px }}
+.item {{ display:inline-flex; align-items:center; gap:8px; padding:10px 14px; border:1px solid; border-radius:24px }}
+.icon {{ width:20px; height:20px; flex:0 0 auto }}
+.text,#constrained {{ font-family:MoliAhem; font-size:9px }}
+#constrained {{ width:80px }}
+</style>`;
+document.body.innerHTML = `<div class=row><div class=item id=ask><i class=icon></i><span class=text id=ask-text>Ask about files</span></div></div><div id=constrained>Ask about files</div>`;
+'installed'
+"#
+ ))?;
+ page_vm
+ .vm_mut()
+ .prime_document_lifecycle_processing_and_record_stylesheet_network_results();
+
+ let geometry = page_vm.vm_mut().eval(
+ r#"JSON.stringify(Object.fromEntries(['ask','ask-text','constrained'].map(id=>{const r=document.getElementById(id).getBoundingClientRect();return [id,[r.width,r.height]]})))"#,
+ )?;
+ let geometry: serde_json::Value = serde_json::from_str(&geometry)?;
+ // Chromium keeps the auto-sized label on one 11px line, leaving the
+ // icon and padding to establish the 42px pill height.
+ assert_eq!(geometry["ask"], serde_json::json!([139, 42]));
+ assert_eq!(geometry["ask-text"], serde_json::json!([81, 11]));
+ assert_eq!(
+ geometry["constrained"],
+ serde_json::json!([80, 22]),
+ "a genuinely narrower content box must still wrap; geometry={geometry}"
+ );
+ Ok::<_, anyhow::Error>(())
+ })
+ .await
+ .expect("rounded flex max-content fixture should run");
+}
+
#[tokio::test(flavor = "current_thread")]
async fn stylesheet_lifecycle_registers_only_the_current_documents_data_web_fonts() {
run_page_vm_async_test(async move {