Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(layout): resolve atomic inline bidi levels through Parley
ldm0d8e7c3772309
diff --git a/Cargo.lock b/Cargo.lockindex c7afbc610..44c3e76c0 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1395,8 +1395,7 @@ dependencies = [[[package]]name = "fontique"version = "0.11.1"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "6688bc1294fe7117d788937b6c53480169b29c566954af490830d4c09da9516a"+source = "git+https://github.com/lexmount/parley?rev=43ab1b7470f0e71ff8cd397a58974c658eb33652#43ab1b7470f0e71ff8cd397a58974c658eb33652"dependencies = ["hashbrown 0.17.1","linebender_resource_handle",@@ -3994,14 +3993,12 @@ dependencies = [[[package]]name = "parlance"version = "0.1.0"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "4b6937eda350acc1a5d05872c3cbf99fe78619c269096e2be3d4a350058639d5"+source = "git+https://github.com/lexmount/parley?rev=43ab1b7470f0e71ff8cd397a58974c658eb33652#43ab1b7470f0e71ff8cd397a58974c658eb33652"[[package]]name = "parley"version = "0.11.1"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "22d2ff88bd3f7d68d1d9b09c7e6209f9a8e8c05088295140a2bcf2e9b17038c5"+source = "git+https://github.com/lexmount/parley?rev=43ab1b7470f0e71ff8cd397a58974c658eb33652#43ab1b7470f0e71ff8cd397a58974c658eb33652"dependencies = ["fontique","harfrust",@@ -4018,8 +4015,7 @@ dependencies = [[[package]]name = "parley_data"version = "0.11.1"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "1567535334d6ba2d3cde19221ba9a7bd0fabb3cbd99046ddfb10ae061cfcc889"+source = "git+https://github.com/lexmount/parley?rev=43ab1b7470f0e71ff8cd397a58974c658eb33652#43ab1b7470f0e71ff8cd397a58974c658eb33652"dependencies = ["icu_properties",]diff --git a/Cargo.toml b/Cargo.tomlindex f2de4738f..a0c7fa6ae 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -98,6 +98,8 @@ exclude = ["vendor/xml5ever-0.39.0"]license = "MIT OR Apache-2.0"[patch.crates-io]+parley = { git = "https://github.com/lexmount/parley", rev = "43ab1b7470f0e71ff8cd397a58974c658eb33652" }+fontique = { git = "https://github.com/lexmount/parley", rev = "43ab1b7470f0e71ff8cd397a58974c658eb33652" }xml5ever = { path = "vendor/xml5ever-0.39.0" }curl = { git = "https://github.com/lexmount/curl-rust", rev = "a0ea59f5ca1b1e4bc8627a28c29463ac96aeb758" }curl-sys = { git = "https://github.com/lexmount/curl-rust", rev = "a0ea59f5ca1b1e4bc8627a28c29463ac96aeb758" }diff --git a/moli-layout/src/inline.rs b/moli-layout/src/inline.rsindex 3111327d6..58502af75 100644--- a/moli-layout/src/inline.rs+++ b/moli-layout/src/inline.rs@@ -14,7 +14,9 @@ use std::{ops::Range,};-use parley::{BreakReason, InlineBox, InlineBoxKind, Layout, PositionedLayoutItem, TextStyle};+use parley::{+ BreakReason, InlineBox, InlineBoxBidi, InlineBoxKind, Layout, PositionedLayoutItem, TextStyle,+};use taffy::{MaybeResolve as _, Point, Size};use crate::{@@ -110,6 +112,18 @@ pub(crate) enum InlineObjectRole {EndEdge,}+impl InlineObjectRole {+ fn parley_bidi(self) -> InlineBoxBidi {+ match self {+ Self::Atomic => InlineBoxBidi::Neutral,+ // CSS tag boundaries do not add bidi characters. Leading edges+ // follow the next participant, closing edges the preceding one.+ Self::StartEdge => InlineBoxBidi::InheritNext,+ Self::EndEdge | Self::Float | Self::OutOfFlow => InlineBoxBidi::InheritPrevious,+ }+ }+}+#[derive(Clone, Debug)]pub(crate) struct InlineObject {pub(crate) box_id: LayoutBoxId,@@ -2022,14 +2036,17 @@ impl InlineBuildInput {}}let object_anchors = self.projected_object_anchors();- for (object_id, (_, _, kind)) in self.objects.iter().enumerate() {- builder.push_inline_box(InlineBox {- id: u64::try_from(object_id).expect("one IFC exceeded the u64 object limit"),- kind: *kind,- index: object_anchors[object_id],- width: 0.0,- height: 0.0,- });+ for (object_id, (_, object, kind)) in self.objects.iter().enumerate() {+ builder.push_inline_box_with_bidi(+ InlineBox {+ id: u64::try_from(object_id).expect("one IFC exceeded the u64 object limit"),+ kind: *kind,+ index: object_anchors[object_id],+ width: 0.0,+ height: 0.0,+ },+ object.role.parley_bidi(),+ );}let layout = builder.build(&self.text);let font_metrics = styles@@ -2949,14 +2966,17 @@ mod tests {let style = builder.push_style(TextStyle::default());builder.push_style_run(style, ..);let anchors = input.projected_object_anchors();- for (id, (_, _, kind)) in input.objects.iter().enumerate() {- builder.push_inline_box(InlineBox {- id: id as u64,- kind: *kind,- index: anchors[id],- width: 1.0,- height: 20.0,- });+ for (id, (_, object, kind)) in input.objects.iter().enumerate() {+ builder.push_inline_box_with_bidi(+ InlineBox {+ id: id as u64,+ kind: *kind,+ index: anchors[id],+ width: 1.0,+ height: 20.0,+ },+ object.role.parley_bidi(),+ );}let mut layout = builder.build(&input.text);layout.break_all_lines(None);diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/inline_layout.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/inline_layout.rsindex 3762877b1..97c0fd36c 100644--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/inline_layout.rs+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/inline_layout.rs@@ -1,5 +1,119 @@use super::*;+// Relative geometry measured in Chromium 145.0.7632.116 in all three modes.+#[test]+fn atomic_inline_boxes_resolve_bidi_contexts_without_expanding_sibling_fragments() {+ for doctype in [+ "",+ r#"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">"#,+ "<!doctype html>",+ ] {+ for context in [+ "normal",+ "embed",+ "bidi-override",+ "isolate",+ "isolate-override",+ "plaintext",+ "dir",+ ] {+ for direction in ["ltr", "rtl"] {+ for break_kind in ["br", "pre", "single"] {+ for inside in [false, true] {+ for tail_space in [false, true] {+ if tail_space && break_kind != "pre" {+ continue;+ }+ let white_space = if break_kind == "pre" {+ "white-space:pre"+ } else {+ ""+ };+ let attributes = if context == "dir" {+ format!(r#"dir={direction} style="{white_space}""#)+ } else {+ format!(+ r#"style="unicode-bidi:{context};direction:{direction};{white_space}""#+ )+ };+ let prefix = match break_kind {+ "br" => "x<br>",+ "pre" => "x\n",+ _ => "",+ };+ let atom = "<i id=atom></i>";+ let markup = format!(+ r#"{doctype}<style>body{{margin:0}}#line{{font:20px/30px monospace;width:300px}}#atom{{display:inline-block;width:10px;height:20px}}</style><div id=line><span {attributes}>{prefix}<span id=word>x</span>{}{}</span>{}</div>"#,+ if tail_space { " " } else { "" },+ if inside { atom } else { "" },+ if inside { "" } else { atom },+ );+ let mut vm =+ new_parsed_test_vm("https://atomic-inline-bidi.test/", &markup);+ let query = r#"JSON.stringify((()=>{+ const line=document.getElementById('line').getBoundingClientRect();+ const word=document.getElementById('word'),range=document.createRange();+ range.selectNodeContents(word);+ const text=range.getBoundingClientRect(),box=word.getBoundingClientRect();+ const atom=document.getElementById('atom').getBoundingClientRect();+ return [line.height,text.left-line.left,text.right-line.left,+ atom.left-line.left,atom.width,box.left-line.left,box.width,+ word.getClientRects().length,range.getClientRects().length,word.textContent.length];+ })())"#;+ let first = vm.eval(query).unwrap();+ let geometry = serde_json::from_str::<[f64; 10]>(&first).unwrap();+ let text_width = geometry[2] - geometry[1];+ let rtl_context =+ direction == "rtl" && !matches!(context, "normal" | "plaintext");+ let expected_text_left = if rtl_context {+ (if inside { 10.0 } else { 0.0 })+ + if tail_space { text_width } else { 0.0 }+ } else {+ 0.0+ };+ let expected_atom_left = if inside && rtl_context {+ 0.0+ } else {+ expected_text_left+ + text_width+ + if tail_space && !rtl_context {+ text_width+ } else {+ 0.0+ }+ };+ let case = format!(+ "{doctype} / {context} / {direction} / {break_kind} / inside={inside} / tail={tail_space}"+ );+ for (actual, expected) in [+ (+ geometry[0],+ if break_kind == "single" { 30.0 } else { 60.0 },+ ),+ (geometry[1], expected_text_left),+ (geometry[3], expected_atom_left),+ (geometry[4], 10.0),+ (geometry[5], geometry[1]),+ (geometry[6], text_width),+ (geometry[7], 1.0),+ (geometry[8], 1.0),+ (geometry[9], 1.0),+ ] {+ assert!(+ (actual - expected).abs() < 0.02,+ "{case}: {first}, expected {expected}, got {actual}"+ );+ }+ publish_layout_for_test(&mut vm);+ assert_eq!(vm.eval(query).unwrap(), first, "publication: {case}");+ }+ }+ }+ }+ }+ }+}+#[test]fn quirks_block_line_height_preserves_text_and_atomic_alignment() {for (doctype, quirks) in [