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 preserved newline inline-block baselines
ldm07c4006670b2c
diff --git a/moli-layout/src/builder.rs b/moli-layout/src/builder.rsindex 67c6c0b46..70a621bf3 100644--- a/moli-layout/src/builder.rs+++ b/moli-layout/src/builder.rs@@ -16,6 +16,7 @@ use crate::{LayoutAnonymousReason, LayoutBoxId, LayoutBoxKind, LayoutCapabilityDiagnostic, LayoutDisplay,LayoutElementCategory, LayoutElementSemantics, LayoutError, LayoutPseudo, LayoutSource,LayoutSourceKind, LayoutStyleResolver, LayoutWorld, ResolvedLayoutStyle,+ style::InlineWhiteSpaceCollapse,};use style::values::generics::image::GenericImage;@@ -588,13 +589,13 @@ where}return Ok(children.into_iter()- .filter(|id| !self.is_whitespace_text(world, *id))+ .filter(|id| !self.is_ignorable_whitespace_text(world, *id)).collect());}if !has_inline {return Ok(children.into_iter()- .filter(|id| !self.is_whitespace_text(world, *id))+ .filter(|id| !self.is_ignorable_whitespace_text(world, *id)).collect());}@@ -697,7 +698,10 @@ whereif run.is_empty() {return Ok(());}- if run.iter().all(|id| self.is_whitespace_text(world, *id)) {+ if run+ .iter()+ .all(|id| self.is_ignorable_whitespace_text(world, *id))+ {run.clear();return Ok(());}@@ -756,7 +760,7 @@ where// whitespace in the same anonymous table wrapper. A real text run// still terminates that wrapper. This mirrors LayoutObject's// anonymous-table reuse after whitespace suppression.- if self.is_whitespace_text(world, child)+ if self.is_ignorable_whitespace_text(world, child)&& output.last().copied().is_some_and(|last| {world.box_by_id(last).is_some_and(|layout_box| {layout_box.kind == LayoutBoxKind::AnonymousTableWrapper@@ -768,7 +772,7 @@ wherelet next_table_part = children[index + 1..].iter().copied()- .find(|next| !self.is_whitespace_text(world, *next))+ .find(|next| !self.is_ignorable_whitespace_text(world, *next)).map(|next| self.table_role(world, next)).transpose()?.flatten()@@ -827,7 +831,7 @@ where) -> Result<Vec<LayoutBoxId>, LayoutError> {let mut output = Vec::new();for child in children {- if self.is_whitespace_text(world, child) {+ if self.is_ignorable_whitespace_text(world, child) {continue;}self.insert_table_child(world, owner, parent_style, parent_role, &mut output, child)?;@@ -1058,7 +1062,7 @@ whereworld: &LayoutWorld<S::NodeId>,id: LayoutBoxId,) -> bool {- self.is_inline_in_flow(world, id) && !self.is_whitespace_text(world, id)+ self.is_inline_in_flow(world, id) && !self.is_ignorable_whitespace_text(world, id)}fn is_block_in_flow(&self, world: &LayoutWorld<S::NodeId>, id: LayoutBoxId) -> bool {@@ -1069,16 +1073,19 @@ where})}- fn is_whitespace_text(&self, world: &LayoutWorld<S::NodeId>, id: LayoutBoxId) -> bool {+ fn is_ignorable_whitespace_text(+ &self,+ world: &LayoutWorld<S::NodeId>,+ id: LayoutBoxId,+ ) -> bool {world.box_by_id(id).is_some_and(|layout_box| {layout_box.kind.is_text()&& !layout_box.capability_diagnostics.contains(&LayoutCapabilityDiagnostic::GeneratedContentUnsupported)- && layout_box- .text- .as_deref()- .is_none_or(|text| text.trim().is_empty())+ && layout_box.text.as_deref().is_none_or(|text| {+ whitespace_text_is_ignorable(text, layout_box.style.white_space_collapse())+ })})}@@ -1171,6 +1178,25 @@ where}}+fn whitespace_text_is_ignorable(text: &str, mode: InlineWhiteSpaceCollapse) -> bool {+ if text.is_empty() {+ return true;+ }+ if !text+ .chars()+ .all(|character| matches!(character, ' ' | '\t' | '\n' | '\r' | '\u{000C}'))+ {+ return false;+ }+ match mode {+ InlineWhiteSpaceCollapse::Collapse => true,+ InlineWhiteSpaceCollapse::PreserveBreaks => !text+ .chars()+ .any(|character| matches!(character, '\n' | '\r' | '\u{000C}')),+ InlineWhiteSpaceCollapse::Preserve | InlineWhiteSpaceCollapse::BreakSpaces => false,+ }+}+fn principal_kind(semantics: &LayoutElementSemantics,style: &ResolvedLayoutStyle,@@ -1266,3 +1292,36 @@ fn push_diagnostic(diagnostics.push(diagnostic);}}++#[cfg(test)]+mod tests {+ use super::{InlineWhiteSpaceCollapse, whitespace_text_is_ignorable};++ #[test]+ fn only_collapsible_whitespace_is_ignorable_during_box_construction() {+ assert!(whitespace_text_is_ignorable(+ " \t\n",+ InlineWhiteSpaceCollapse::Collapse,+ ));+ assert!(whitespace_text_is_ignorable(+ " \t",+ InlineWhiteSpaceCollapse::PreserveBreaks,+ ));+ assert!(!whitespace_text_is_ignorable(+ "\n",+ InlineWhiteSpaceCollapse::PreserveBreaks,+ ));+ assert!(!whitespace_text_is_ignorable(+ "\n",+ InlineWhiteSpaceCollapse::Preserve,+ ));+ assert!(!whitespace_text_is_ignorable(+ " ",+ InlineWhiteSpaceCollapse::BreakSpaces,+ ));+ assert!(!whitespace_text_is_ignorable(+ "\u{00a0}",+ InlineWhiteSpaceCollapse::Collapse,+ ));+ }+}diff --git a/moli-layout/src/inline.rs b/moli-layout/src/inline.rsindex e7b65a361..887fb2bab 100644--- a/moli-layout/src/inline.rs+++ b/moli-layout/src/inline.rs@@ -9,7 +9,7 @@use std::{collections::BTreeMap, fmt::Debug, hash::Hash, ops::Range};-use parley::{InlineBox, InlineBoxKind, Layout, PositionedLayoutItem, TextStyle};+use parley::{BreakReason, InlineBox, InlineBoxKind, Layout, PositionedLayoutItem, TextStyle};use taffy::{MaybeResolve as _, Point, Size};use crate::{@@ -625,7 +625,10 @@ pub(crate) fn build_inline_line_placements(}}).collect::<Vec<_>>();- let phantom = !geometries.iter().any(|geometry| geometry.creates_line);+ let phantom = css_line_is_phantom(+ line.break_reason(),+ geometries.iter().any(|geometry| geometry.creates_line),+ );// Every non-phantom CSS line starts with the IFC owner's zero-width font strut.// Reconstruct it explicitly whenever this sidecar owns in-flow// vertical geometry. Float and out-of-flow placeholders do not create@@ -860,6 +863,13 @@ struct InlineItemVerticalGeometry {glyph_key: Option<(usize, usize)>,}+/// CSS line boxes ending in a preserved newline exist even when they contain+/// no paintable item. Parley's explicit break reason covers both preserved+/// segment breaks and the normalized `<br>` control.+fn css_line_is_phantom(break_reason: BreakReason, has_in_flow_content: bool) -> bool {+ !has_in_flow_content && break_reason != BreakReason::Explicit+}+fn non_edge_vertical_offset(vertical_align: InlineVerticalAlign,parent_baseline: f32,@@ -1759,6 +1769,40 @@ mod tests {);}+ #[test]+ fn explicit_break_prevents_an_otherwise_empty_line_from_being_phantom() {+ assert!(!css_line_is_phantom(BreakReason::Explicit, false));+ assert!(css_line_is_phantom(BreakReason::None, false));+ assert!(!css_line_is_phantom(BreakReason::None, true));+ }++ #[test]+ fn parley_forced_break_and_optional_editor_tail_map_to_css_phantom_lines() {+ let text = "\n";+ let mut font_context = parley::FontContext::new();+ let mut layout_context = parley::LayoutContext::<TextBrush>::new();+ let mut builder = layout_context.style_run_builder(&mut font_context, text, 1.0, true);+ let style = builder.push_style(TextStyle::default());+ builder.push_style_run(style, ..);+ let mut layout = builder.build(text);+ layout.break_all_lines(None);++ let mut lines = layout.lines();+ let forced_break_line = lines.next().expect("preserved newline must create a line");+ assert_eq!(forced_break_line.break_reason(), BreakReason::Explicit);+ assert!(!css_line_is_phantom(+ forced_break_line.break_reason(),+ false,+ ));+ for editor_tail in lines {+ let break_reason = editor_tail.break_reason();+ assert!(+ css_line_is_phantom(break_reason, false),+ "Parley editor tail must not become an extra CSS line box: {break_reason:?}"+ );+ }+ }+fn normalize(chunks: &[(LayoutBoxId, &str)],mode: InlineWhiteSpaceCollapse,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.rsindex 5604a6a83..2d94c8ac4 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@@ -3246,6 +3246,16 @@ html,body{{margin:0;padding:0}}r#"#wrap-root{width:40px;word-break:break-all}#wrap{padding:0 1px;border-left:1px solid;border-right:1px solid;background:rgb(91,92,93);color:rgb(101,102,103)}"#,"<div id=wrap-root class=fixed><span id=wrap>ABCDE</span></div>",)?;+ let preserved_break_baseline = render(+ &mut page_vm,+ r#"+#baseline-wrapper{width:200px;font-size:0;line-height:0;background:rgb(111,112,113)}+#baseline-text,#baseline-break{display:inline-block;width:100px;height:200px}+#baseline-text{background:rgb(121,122,123)}+#baseline-break{white-space:pre;background:rgb(131,132,133)}+"#,+ "<div id=baseline-wrapper><div id=baseline-text>text</div><div id=baseline-break>\n</div></div>",+ )?;let rgb = |red: u8, green: u8, blue: u8| {moli_layout::PaintColor::new(f32::from(red) / 255.0,@@ -3484,6 +3494,16 @@ html,body{{margin:0;padding:0}}(12.0, 36.0),],);+ assert_rects(+ "zero-sized text inline-block baseline",+ &solid_rects(&preserved_break_baseline, rgb(121, 122, 123)),+ &[(0.0, 0.0, 100.0, 200.0)],+ );+ assert_rects(+ "preserved-break inline-block baseline",+ &solid_rects(&preserved_break_baseline, rgb(131, 132, 133)),+ &[(100.0, 0.0, 100.0, 200.0)],+ );assert_eq!(page_vm.vm_mut().document_web_font_counts_for_test(),(3, 3, 3),