Skip to content

armgit / moli

Public Imported from GitHub
0

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

Use a global CSS layout-unit grid

ldm0d1f20f66f863
diff --git a/Cargo.lock b/Cargo.lock
index 1f11789ea..a466167ad 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4573,7 +4573,7 @@ dependencies = [
[[package]]
name = "taffy"
version = "0.13.0"
-source = "git+https://github.com/ldm0/taffy?rev=330d8cdd9183f0144d6c32206bcc90a2e506024c#330d8cdd9183f0144d6c32206bcc90a2e506024c"
+source = "git+https://github.com/ldm0/taffy?rev=28923cb1d9164245e10566ef8156fa5cfa9633bb#28923cb1d9164245e10566ef8156fa5cfa9633bb"
dependencies = [
"arrayvec",
"serde",
diff --git a/Cargo.toml b/Cargo.toml
index 6bd6386ca..f77ec5928 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -93,7 +93,7 @@ stylo_atoms = { git = "https://github.com/ldm0/stylo", rev = "74cd014af14d84db1f
stylo_dom = { git = "https://github.com/ldm0/stylo", rev = "74cd014af14d84db1fc8faed85086895012fc687" }
stylo_static_prefs = { git = "https://github.com/ldm0/stylo", rev = "74cd014af14d84db1fc8faed85086895012fc687" }
stylo_traits = { git = "https://github.com/ldm0/stylo", rev = "74cd014af14d84db1fc8faed85086895012fc687" }
-taffy = { git = "https://github.com/ldm0/taffy", rev = "330d8cdd9183f0144d6c32206bcc90a2e506024c" }
+taffy = { git = "https://github.com/ldm0/taffy", rev = "28923cb1d9164245e10566ef8156fa5cfa9633bb" }
[workspace.lints.clippy]
disallowed_methods = "deny"
diff --git a/moli-layout/src/taffy_tree.rs b/moli-layout/src/taffy_tree.rs
index eedac78d3..742018dd1 100644
--- a/moli-layout/src/taffy_tree.rs
+++ b/moli-layout/src/taffy_tree.rs
@@ -9,7 +9,8 @@ use taffy::{
LayoutOutput, LayoutPartialTree, 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, compute_root_layout, round_layout,
+ compute_hidden_layout, compute_leaf_layout, compute_root_layout,
+ round_layout_with_scale_factor,
};
use crate::{
@@ -27,6 +28,9 @@ use crate::{
world::InlineStaticPosition,
};
+// Blink stores box geometry in 1/64 CSS-pixel LayoutUnits.
+const LAYOUT_SUBPIXELS_PER_CSS_PIXEL: f32 = 64.0;
+
pub(crate) fn compute_world_layout<N>(world: &mut LayoutWorld<N>, viewport: PaintViewport)
where
N: Copy + Debug + Eq + Hash,
@@ -78,7 +82,7 @@ where
finish_form_control_contents(world);
finish_outside_list_markers(world);
finish_sticky_positioning(world, viewport);
- round_layout(world, root);
+ round_layout_with_scale_factor(world, root, LAYOUT_SUBPIXELS_PER_CSS_PIXEL);
// Outside markers deliberately are not numeric children of the list item,
// otherwise Taffy would allocate them a normal-flow row. Round each
// detached numeric root explicitly so paint consumes the geometry written
@@ -88,7 +92,7 @@ where
.filter(|id| world.boxes[id.index()].outside_list_marker)
.collect::<Vec<_>>();
for marker in outside_markers {
- round_layout(world, marker.to_taffy());
+ round_layout_with_scale_factor(world, marker.to_taffy(), LAYOUT_SUBPIXELS_PER_CSS_PIXEL);
}
}
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 fdc87b20d..022f4d629 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
@@ -751,9 +751,8 @@ document.body.innerHTML = `<div id=centered></div><div id=definite-parent><div i
)?;
let geometry: serde_json::Value = serde_json::from_str(&geometry)?;
for (id, expected) in [
- // Taffy's final device-pixel rounding maps Chromium's 232.5px
- // computed origin to the 233px paint/geometry edge.
- ("centered", [233.0, 0.0, 975.0, 20.0]),
+ // Chromium retains the half-pixel origin in DOM geometry.
+ ("centered", [232.5, 0.0, 975.0, 20.0]),
("definite-parent", [0.0, 30.0, 100.0, 400.0]),
("definite-child", [0.0, 227.0, 10.0, 10.0]),
("indefinite-parent", [0.0, 500.0, 100.0, 100.0]),
@@ -781,7 +780,7 @@ document.body.innerHTML = `<div id=centered></div><div id=definite-parent><div i
<[u8; 4]>::try_from(&image.rgba[index..index + 4]).expect("RGBA pixel")
};
assert_eq!(pixel(233, 10), [255, 0, 0, 255]);
- assert_eq!(pixel(232, 10), [255, 255, 255, 255]);
+ assert_eq!(pixel(232, 10), [255, 127, 127, 255]);
assert_eq!(pixel(5, 230), [0, 0, 255, 255]);
assert_eq!(pixel(5, 505), [0, 255, 0, 255]);
Ok::<_, anyhow::Error>(())
@@ -2419,9 +2418,9 @@ document.body.innerHTML = `<div id=bar><header id=header><div id=wrapper><span i
let geometry: serde_json::Value = serde_json::from_str(&geometry)?;
for (id, expected) in [
("bar", [0.0, 0.0, 200.0, 60.0]),
- ("header", [6.0, 5.0, 48.0, 51.0]),
- ("wrapper", [6.0, 5.0, 48.0, 51.0]),
- ("atomic", [6.0, 5.0, 48.0, 48.0]),
+ ("header", [6.0, 4.5, 48.0, 51.0]),
+ ("wrapper", [6.0, 4.5, 48.0, 51.0]),
+ ("atomic", [6.0, 4.5, 48.0, 48.0]),
] {
let actual = geometry[id]
.as_array()
@@ -2440,6 +2439,66 @@ document.body.innerHTML = `<div id=bar><header id=header><div id=wrapper><span i
.expect("baseline atomic strut fixture should run");
}
+#[tokio::test(flavor = "current_thread")]
+async fn atomic_inline_location_uses_the_global_layout_unit_grid() {
+ run_page_vm_async_test(async move {
+ let loader =
+ crate::network::ResourceRequestClient::new(&FetchConfig::default()).expect("loader");
+ let mut page_vm = test_page_vm_with_loader_and_document_url(
+ &loader,
+ Vec::new(),
+ Url::parse("https://example.com/atomic-inline-layout-unit.html")?,
+ );
+ page_vm.vm_mut().eval(
+ r#"
+document.head.innerHTML = `<style>
+html,body{margin:0}
+#line{margin:0;white-space:nowrap;font:16px/75px sans-serif}
+#atomic{display:inline-block;position:relative;left:.1px}
+</style>`;
+document.body.innerHTML = `<p id=line>All the <span id=atomic>words</span> after</p>`;
+'installed'
+"#,
+ )?;
+ page_vm
+ .vm_mut()
+ .prime_document_lifecycle_processing_and_record_stylesheet_network_results();
+
+ let geometry = page_vm.vm_mut().eval(
+ r#"JSON.stringify((()=>{const line=document.getElementById('line');const atomic=document.getElementById('atomic');const rect=node=>{const range=document.createRange();range.selectNodeContents(node);const value=range.getBoundingClientRect();return [value.x,value.right]};const box=atomic.getBoundingClientRect();return {preceding:rect(line.firstChild),atomic:[box.x,box.right],text:rect(atomic)}})())"#,
+ )?;
+ let geometry: serde_json::Value = serde_json::from_str(&geometry)?;
+ let number = |group: &str, index: usize| {
+ geometry[group][index]
+ .as_f64()
+ .unwrap_or_else(|| panic!("missing {group}[{index}] in {geometry}"))
+ as f32
+ };
+ let preceding_right = number("preceding", 1);
+ let atomic_x = number("atomic", 0);
+ let text_x = number("text", 0);
+ let geometry_epsilon = 0.0001;
+ let unrounded_atomic_x = preceding_right + 0.1;
+ let expected_atomic_x = (unrounded_atomic_x * 64.0).round() / 64.0;
+
+ assert!(
+ (unrounded_atomic_x * 64.0 - (unrounded_atomic_x * 64.0).round()).abs() > 0.05,
+ "fixture must place the unrounded atomic origin away from the 1/64 grid: {geometry}"
+ );
+ assert!(
+ (atomic_x - expected_atomic_x).abs() <= geometry_epsilon,
+ "the atomic outer placement must use the global 1/64 layout grid; expected {expected_atomic_x}: {geometry}"
+ );
+ assert!(
+ (text_x - atomic_x).abs() <= geometry_epsilon,
+ "the atomic IFC must paint its first glyph at the rounded box origin: {geometry}"
+ );
+ Ok::<_, anyhow::Error>(())
+ })
+ .await
+ .expect("atomic inline layout-unit 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 {
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
index 647cc61d9..a01304b01 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
@@ -4583,7 +4583,7 @@ fn box_metrics_round_fractional_computed_px_values() {
)
.expect("box metrics should round fractional computed px values");
- assert_eq!(result, "80|80|80|31|31|31|80|31");
+ assert_eq!(result, "80|80|80|31|31|31|79.59375|30.59375");
}
#[test]