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(forms): paint focused text-control selections and carets
ldm0a007ab27933b
diff --git a/moli-layout/src/builder.rs b/moli-layout/src/builder.rsindex 5e5c3c0fc..2215854bc 100644--- a/moli-layout/src/builder.rs+++ b/moli-layout/src/builder.rs@@ -320,6 +320,13 @@ wheremetrics,);let id = world.allocate(box_node);+ if matches!(+ semantics.category,+ crate::LayoutElementCategory::FormControl(_)+ ) {+ world.boxes[id.index()].text_selection =+ self.source.text_selection(source_node);+ }world.map_source(source_node, id);if is_leaf_element(&semantics, kind, &style) {diff --git a/moli-layout/src/form.rs b/moli-layout/src/form.rsindex 808c9cae7..afe8976c9 100644--- a/moli-layout/src/form.rs+++ b/moli-layout/src/form.rs@@ -24,9 +24,11 @@ whereform_control_texts(&world.boxes[id.index()].element_semantics).map(|(texts, placeholder)| (id, texts, placeholder))})- .filter(|(_, texts, _)| !texts.is_empty())+ .filter(|(id, texts, _)| {+ !texts.is_empty() || world.boxes[id.index()].text_selection.is_some()+ }).collect::<Vec<_>>();- for (control, texts, placeholder) in controls {+ for (control, mut texts, placeholder) in controls {let Some(owner) = world.boxes[control.index()].source else {continue;};@@ -57,6 +59,11 @@ where)?};let text_style = ResolvedLayoutStyle::text_leaf_from(&wrapper_style);+ let selection = world.boxes[control.index()].text_selection;+ let caret_color = world.boxes[control.index()].style.caret_color();+ if texts.is_empty() && selection.is_some() {+ texts.push(Arc::from(""));+ }for (row, text) in texts.into_iter().enumerate() {let mut wrapper = LayoutWorld::new_box(None,@@ -71,6 +78,8 @@ whereNone,);wrapper.inline_formatting_context = true;+ wrapper.text_selection = selection;+ wrapper.selection_caret_color = Some(caret_color);let wrapper_id = world.allocate(wrapper);let text_id = world.allocate(LayoutWorld::new_box(None,@@ -84,6 +93,8 @@ wheretext_style.clone(),Some(text),));+ world.boxes[text_id.index()].text_selection = selection;+ world.boxes[text_id.index()].selection_caret_color = Some(caret_color);world.append_synthesized_child(wrapper_id, text_id)?;world.append_synthesized_child(control, wrapper_id)?;}diff --git a/moli-layout/src/inline.rs b/moli-layout/src/inline.rsindex 27f539359..631748879 100644--- a/moli-layout/src/inline.rs+++ b/moli-layout/src/inline.rs@@ -1134,10 +1134,12 @@ fn resolve_inline_lines(for (index, state) in states.iter().enumerate() {state_indices.insert(state.box_id.index(), index);}+ let empty_caret = context.text.is_empty()+ && matches!(context.selection, Some(InlineSelection::Caret { .. }));let phantom = css_line_is_phantom(line.break_reason(),geometries.iter().any(|geometry| geometry.creates_line),- );+ ) && !empty_caret;for state in &mut states {state.parent = state_indices.get(&state.parent_box.index()).copied();state.alignment.anchor = state@@ -1314,6 +1316,9 @@ fn resolve_inline_lines(let bounds = if phantom {InlineVerticalBounds::ZERO+ } else if empty_caret {+ // Empty editors still need their font strut for caret geometry.+ fallback_root_bounds} else if context.uses_quirks_line_height {root_bounds.unwrap_or(InlineVerticalBounds::ZERO)} else {@@ -1960,7 +1965,10 @@ pub(crate) fn prepare_inline_contexts<N>(continue;}let input = collect_inline_input(world, owner);- if input.units.is_empty() && input.objects.is_empty() {+ if input.units.is_empty()+ && input.objects.is_empty()+ && world.boxes[owner.index()].text_selection.is_none()+ {continue;}let parley = services.parley_mut();@@ -2065,7 +2073,19 @@ impl InlineBuildInput {whereN: Copy + Debug + Eq + Hash,{- let selection = project_inline_selection(world, &self.source_map);+ let selection = project_inline_selection(world, &self.source_map).or_else(|| {+ world.boxes[self.root_style.index()]+ .text_selection+ .filter(|selection| selection.is_caret())+ .map(|_| InlineSelection::Caret {+ offset: 0,+ color: world.boxes[self.root_style.index()]+ .selection_caret_color+ .unwrap_or_else(|| {+ world.boxes[self.root_style.index()].style.caret_color()+ }),+ })+ });let mut root_text_style = world.boxes[self.root_style.index()].style.parley_text_style();@@ -2257,8 +2277,17 @@ where};if selection.is_caret() {if caret.is_none() {- caret = caret_output_offset(source_map, entry.box_id, selection.start)- .map(|offset| (offset, world.boxes[entry.box_id.index()].style.text_color()));+ caret =+ caret_output_offset(source_map, entry.box_id, selection.start).map(|offset| {+ (+ offset,+ world.boxes[entry.box_id.index()]+ .selection_caret_color+ .unwrap_or_else(|| {+ world.boxes[entry.box_id.index()].style.caret_color()+ }),+ )+ });}continue;}diff --git a/moli-layout/src/source.rs b/moli-layout/src/source.rsindex 9c94ed2f8..d097a4f2a 100644--- a/moli-layout/src/source.rs+++ b/moli-layout/src/source.rs@@ -465,7 +465,7 @@ pub(crate) struct LayoutCssImageResources {pub(crate) mask: Vec<Option<LayoutImageResource>>,}-/// One document selection projected onto a source text node in UTF-16 units.+/// A document or form-control selection sampled in UTF-16 units.////// Equal endpoints represent a caret. The range is sampled before the/// synchronous one-shot pass and copied into pass-local state; paint never@@ -540,7 +540,7 @@ pub trait LayoutSource {None}- /// Returns the active document selection intersecting one source text node.+ /// Returns the active selection for a source text node or text control.fn text_selection(&self, _node: Self::NodeId) -> Option<LayoutTextSelection> {None}diff --git a/moli-layout/src/style.rs b/moli-layout/src/style.rsindex 616779a9f..f0904f61c 100644--- a/moli-layout/src/style.rs+++ b/moli-layout/src/style.rs@@ -1385,6 +1385,19 @@ impl ResolvedLayoutStyle {self.current_color()}+ pub(crate) fn caret_color(&self) -> PaintColor {+ use style::values::generics::color::ColorOrAuto;+ let Some(computed) = self.computed.as_ref() else {+ return self.current_color();+ };+ match computed.clone_caret_color().0 {+ ColorOrAuto::Color(color) => {+ absolute_paint_color(color.resolve_to_absolute(&computed.clone_color()))+ }+ ColorOrAuto::Auto => self.current_color(),+ }+ }+pub(crate) const fn accepts_pointer_events(&self) -> bool {self.pointer_events}diff --git a/moli-layout/src/taffy_tree.rs b/moli-layout/src/taffy_tree.rsindex dc1a1ca28..d06abaa22 100644--- a/moli-layout/src/taffy_tree.rs+++ b/moli-layout/src/taffy_tree.rs@@ -693,7 +693,7 @@ where.element_semantics.as_ref().is_some_and(crate::LayoutElementSemantics::is_select_listbox);- let inline_padding = if listbox { 2.0 } else { 4.0 };+ let inline_padding = if listbox { 2.0 } else { 0.0 };let content_width = (control_layout.size.width- control_layout.border.left- control_layout.border.rightdiff --git a/moli-layout/src/world.rs b/moli-layout/src/world.rsindex 2b8651402..bac4cbb31 100644--- a/moli-layout/src/world.rs+++ b/moli-layout/src/world.rs@@ -247,6 +247,7 @@ pub struct LayoutBox<N> {pub(crate) style: ResolvedLayoutStyle,pub(crate) text: Option<Arc<str>>,pub(crate) text_selection: Option<crate::LayoutTextSelection>,+ pub(crate) selection_caret_color: Option<crate::PaintColor>,/// Shared text/inline layout owned by this formatting-context root.pub(crate) inline_layout: Option<InlineFormattingContext>,/// Outermost IFC that consumes this box as a flattened or atomic item.@@ -1185,6 +1186,7 @@ wherestyle,text,text_selection: None,+ selection_caret_color: None,inline_layout: None,inline_context_owner: None,inline_flattened: false,diff --git a/moli-renderer-v8/src/layout_renderer/source_view.rs b/moli-renderer-v8/src/layout_renderer/source_view.rsindex d7597e68f..aca71db00 100644--- a/moli-renderer-v8/src/layout_renderer/source_view.rs+++ b/moli-renderer-v8/src/layout_renderer/source_view.rs@@ -181,6 +181,17 @@ impl LayoutSource for NativeLayoutSourceView<'_> {}fn text_selection(&self, node: Self::NodeId) -> Option<LayoutTextSelection> {+ if self.runtime.active_element_handle() == Some(node)+ && crate::native_bridge::element::is_text_control(self.runtime, node)+ {+ let element = self.host().node(node)?.as_element()?;+ if element.attribute("disabled").is_none() {+ return Some(LayoutTextSelection::new(+ element.selection_start() as usize,+ element.selection_end() as usize,+ ));+ }+ }self.text_selections.get(&node).copied()}diff --git a/moli-renderer-v8/src/runtime/page_vm/tests/rendering_update/form_controls.rs b/moli-renderer-v8/src/runtime/page_vm/tests/rendering_update/form_controls.rsindex a8c124f16..b465d94a4 100644--- a/moli-renderer-v8/src/runtime/page_vm/tests/rendering_update/form_controls.rs+++ b/moli-renderer-v8/src/runtime/page_vm/tests/rendering_update/form_controls.rs@@ -1,5 +1,57 @@use super::*;+#[tokio::test(flavor = "current_thread")]+async fn focused_text_controls_paint_utf16_selection_and_empty_carets() {+ run_page_vm_async_test(async move {+ let loader = crate::network::ResourceRequestClient::new(&FetchConfig::default())?;+ let mut page = test_page_vm_with_loader_and_document_url(+ &loader, Vec::new(), Url::parse("https://example.com/text-control-selection.html")?,+ );+ page.vm_mut().eval(r#"+document.head.innerHTML='<style>html,body{margin:0}input,textarea{display:block;width:220px;height:40px;border:0;padding:8px;color:red;caret-color:lime;font:20px/24px sans-serif}::placeholder{color:gray}</style>';+document.body.innerHTML='<input id=plain><input id=placeholder placeholder=Example><textarea id=area></textarea>';+'installed'+"#)?;+ page.vm_mut().sync_live_document_style_sources();+ for id in ["plain", "placeholder", "area"] {+ let mut caret_start = 0.0;+ let mut selected_right = 0.0;+ for (state, script, caret, selected) in [+ ("empty", "el.focus();el.value='';el.setSelectionRange(0,0)", true, false),+ ("before emoji", "el.value='A😀B';el.setSelectionRange(1,1)", true, false),+ ("selected emoji", "el.setSelectionRange(1,3)", false, true),+ ("after emoji", "el.setSelectionRange(3,3)", true, false),+ ("blurred", "el.blur()", false, false),+ ] {+ page.vm_mut().eval(&format!("{{const el=document.getElementById('{id}');{script};}}'updated'"))?;+ let snapshot = page.vm_mut().screenshot_layout_snapshot(+ moli_layout::PaintViewport::new(260, 180, 1.0),+ )?.expect("text control layout");+ let fills = snapshot.fragments.iter().filter_map(|fragment| match fragment {+ moli_layout::PaintFragment::Fill { shape:moli_layout::PaintShape::Rect(rect),brush:moli_layout::PaintBrush::Solid(color),transform } => Some((transform.map_rect(*rect).bounding_rect(),*color)),+ _ => None,+ }).collect::<Vec<_>>();+ let carets = fills.iter().filter(|(rect,color)| rect.width == 1.5 && *color == moli_layout::PaintColor::new(0.0,1.0,0.0,1.0)).collect::<Vec<_>>();+ let selections = fills.iter().filter(|(_,color)| *color == moli_layout::PaintColor::new(180.0/255.0,213.0/255.0,1.0,1.0)).collect::<Vec<_>>();+ assert_eq!(carets.len(),usize::from(caret),"{id}/{state}: active caret");+ assert_eq!(selections.len(),usize::from(selected),"{id}/{state}: active selection");+ if let Some((rect,_))=carets.first() {+ assert!(rect.height>0.0,"{id}/{state}: visible caret");+ if state=="empty" {assert_eq!(rect.x,8.0,"caret starts at the CSS padding edge");}+ if state=="before emoji" {caret_start=rect.x;}+ if state=="after emoji" {assert!((rect.x-selected_right).abs()<0.1,"UTF-16 selection ends at the next caret");}+ }+ if let Some((rect,_))=selections.first() {+ assert!((rect.x-caret_start).abs()<0.1,"selection starts at the previous caret");+ assert!(rect.width>0.0);+ selected_right=rect.right();+ }+ }+ }+ Ok::<_,anyhow::Error>(())+ }).await.expect("text control selection fixture");+}+fn dark_pixels(image: &moli_paint::RasterImage, x: u32, y: u32, width: u32, height: u32) -> usize {(y..y + height).flat_map(|y| (x..x + width).map(move |x| ((y * image.width + x) * 4) as usize))