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(editing): preserve surrogate pairs in text control actions
ldm0b89f09f78121
diff --git a/moli-protocol/src/domains/input/tests.rs b/moli-protocol/src/domains/input/tests.rsindex dfe5d6f6e..a4a4d4348 100644--- a/moli-protocol/src/domains/input/tests.rs+++ b/moli-protocol/src/domains/input/tests.rs@@ -2424,19 +2424,93 @@ async fn dispatch_key_event_text_controls_preserve_surrogate_pairs() {"<html><body><input id='field'><textarea id='bio'></textarea></body></html>",).await;+let mut command_id = 5000;for control in ["field", "bio"] {- for (key, caret, modifiers, expected) in [- ("Backspace", 3, 0, r#"["AB",1,1]"#),- ("Delete", 1, 0, r#"["AB",1,1]"#),- ("ArrowLeft", 3, 0, r#"["AπB",1,1]"#),- ("ArrowRight", 1, 0, r#"["AπB",3,3]"#),- ("ArrowLeft", 3, 8, r#"["AπB",1,3]"#),- ("ArrowRight", 1, 8, r#"["AπB",1,3]"#),+ for (initial, key, start, end, modifiers, value, next_start, next_end) in [+ ("AπB", "Backspace", 3, 3, 0, "AB", 1, 1),+ ("AπB", "Delete", 1, 1, 0, "AB", 1, 1),+ ("AπB", "ArrowLeft", 3, 3, 0, "AπB", 1, 1),+ ("AπB", "ArrowRight", 1, 1, 0, "AπB", 3, 3),+ ("AπB", "ArrowLeft", 3, 3, 8, "AπB", 1, 3),+ ("AπB", "ArrowRight", 1, 1, 8, "AπB", 1, 3),+ ("AπB", "Backspace", 2, 2, 0, "AB", 1, 1),+ ("AπB", "Delete", 2, 2, 0, "Aπ", 3, 3),+ ("AπB", "ArrowLeft", 2, 2, 0, "AπB", 1, 1),+ ("AπB", "ArrowRight", 2, 2, 0, "AπB", 4, 4),+ ("AπB", "Backspace", 0, 0, 0, "AπB", 0, 0),+ ("AπB", "Delete", 4, 4, 0, "AπB", 4, 4),+ ("AπB", "ArrowLeft", 2, 2, 8, "AπB", 1, 3),+ ("AπB", "ArrowRight", 2, 2, 8, "AπB", 3, 4),+ ("AπB", "Backspace", 1, 2, 0, "AB", 1, 1),+ ("AπB", "Delete", 1, 2, 0, "AB", 1, 1),+ ("AπB", "Backspace", 2, 3, 0, "AB", 1, 1),+ ("AπB", "Delete", 2, 3, 0, "Aπ", 3, 3),+ ("AπB", "Backspace", 1, 3, 0, "AB", 1, 1),+ ("AπB", "Delete", 1, 3, 0, "AB", 1, 1),+ ("AπB", "Backspace", 0, 2, 0, "B", 0, 0),+ ("AπB", "Delete", 0, 2, 0, "B", 0, 0),+ ("AπB", "Backspace", 2, 4, 0, "Aπ", 3, 3),+ ("AπB", "Delete", 2, 4, 0, "Aπ", 3, 3),+ ("AπB", "ArrowLeft", 1, 2, 0, "AπB", 1, 1),+ ("AπB", "ArrowRight", 1, 2, 0, "AπB", 3, 3),+ ("AπB", "ArrowLeft", 2, 3, 0, "AπB", 1, 1),+ ("AπB", "ArrowRight", 2, 3, 0, "AπB", 4, 4),+ ("", "Backspace", 0, 0, 0, "", 0, 0),+ ("", "Delete", 0, 0, 0, "", 0, 0),+ ("", "ArrowLeft", 0, 0, 0, "", 0, 0),+ ("", "ArrowRight", 0, 0, 0, "", 0, 0),+ ("", "ArrowLeft", 0, 0, 8, "", 0, 0),+ ("", "ArrowRight", 0, 0, 8, "", 0, 0),+ ("", "Home", 0, 0, 0, "", 0, 0),+ ("", "End", 0, 0, 0, "", 0, 0),+ ("", "Home", 0, 0, 8, "", 0, 0),+ ("", "End", 0, 0, 8, "", 0, 0),+ ("π", "Backspace", 1, 1, 0, "", 0, 0),+ ("π", "Delete", 1, 1, 0, "π", 1, 1),+ ("π", "ArrowLeft", 1, 1, 0, "π", 0, 0),+ ("π", "ArrowRight", 1, 1, 0, "π", 1, 1),+ ("π", "ArrowLeft", 1, 1, 8, "π", 0, 2),+ ("π", "ArrowRight", 1, 1, 8, "π", 1, 1),+ ("π", "Home", 1, 1, 0, "π", 0, 0),+ ("π", "End", 1, 1, 0, "π", 2, 2),+ ("π", "Home", 1, 1, 8, "π", 0, 2),+ ("π", "End", 1, 1, 8, "π", 2, 2),+ ("Aπ", "Backspace", 2, 2, 0, "A", 1, 1),+ ("Aπ", "Delete", 2, 2, 0, "Aπ", 2, 2),+ ("Aπ", "ArrowLeft", 2, 2, 0, "Aπ", 1, 1),+ ("Aπ", "ArrowRight", 2, 2, 0, "Aπ", 2, 2),+ ("Aπ", "ArrowLeft", 2, 2, 8, "Aπ", 1, 3),+ ("Aπ", "ArrowRight", 2, 2, 8, "Aπ", 2, 2),+ ("Aπ", "Home", 2, 2, 0, "Aπ", 0, 0),+ ("Aπ", "End", 2, 2, 0, "Aπ", 3, 3),+ ("Aπ", "Home", 2, 2, 8, "Aπ", 0, 3),+ ("Aπ", "End", 2, 2, 8, "Aπ", 3, 3),+ ("ππ", "Backspace", 3, 3, 0, "π", 2, 2),+ ("ππ", "Delete", 3, 3, 0, "ππ", 3, 3),+ ("ππ", "ArrowLeft", 3, 3, 0, "ππ", 2, 2),+ ("ππ", "ArrowRight", 3, 3, 0, "ππ", 3, 3),+ ("ππ", "ArrowLeft", 3, 3, 8, "ππ", 2, 4),+ ("ππ", "ArrowRight", 3, 3, 8, "ππ", 3, 3),+ ("ππ", "Home", 3, 3, 0, "ππ", 0, 0),+ ("ππ", "End", 3, 3, 0, "ππ", 4, 4),+ ("ππ", "Home", 3, 3, 8, "ππ", 0, 4),+ ("ππ", "End", 3, 3, 8, "ππ", 4, 4),] {- evaluate_string(&mut ctx, &format!(- "window.__utf16Control = document.getElementById('{control}'); __utf16Control.value = 'AπB'; __utf16Control.focus(); __utf16Control.setSelectionRange({caret}, {caret}); 'ready'"- )).await;+ assert_eq!(+ evaluate_string(+ &mut ctx,+ &format!(+ "window.__utf16Control = document.getElementById('{control}'); \+ __utf16Control.value = '{initial}'; __utf16Control.focus(); \+ __utf16Control.setSelectionRange({start}, {end}); \+ JSON.stringify([__utf16Control.selectionStart, __utf16Control.selectionEnd])"+ )+ )+ .await,+ json!([start, end]).to_string(),+ "selection APIs must retain UTF-16 code unit offsets"+ );ctx.process_async(json!({"id": command_id,"method": "Input.dispatchKeyEvent",@@ -2445,9 +2519,15 @@ async fn dispatch_key_event_text_controls_preserve_surrogate_pairs() {.await;ctx.expect_result(command_id, json!({}), None);command_id += 1;- assert_eq!(evaluate_string(&mut ctx,- "JSON.stringify([__utf16Control.value, __utf16Control.selectionStart, __utf16Control.selectionEnd])"- ).await, expected, "{control} {key} modifiers={modifiers}");+ assert_eq!(+ evaluate_string(+ &mut ctx,+ "JSON.stringify([__utf16Control.value, __utf16Control.selectionStart, __utf16Control.selectionEnd])"+ )+ .await,+ json!([value, next_start, next_end]).to_string(),+ "{control} {key} value={initial:?} modifiers={modifiers} with selection [{start}, {end}]"+ );}}}diff --git a/moli-renderer-v8/src/native_bridge/document/lifecycle.rs b/moli-renderer-v8/src/native_bridge/document/lifecycle.rsindex 70755cc2c..a69d83ef1 100644--- a/moli-renderer-v8/src/native_bridge/document/lifecycle.rs+++ b/moli-renderer-v8/src/native_bridge/document/lifecycle.rs@@ -18,7 +18,8 @@ use crate::{document_runtime::DomHandle,util::{call_object_method, node_wrapper_from_handle, utf16_next_scalar_boundary,- utf16_previous_scalar_boundary, utf16_replace_units_range_lossy, utf16_units, v8str,+ utf16_previous_scalar_boundary, utf16_replace_units_range_lossy,+ utf16_scalar_boundary_at_or_after, utf16_units, v8str,},webidl,};@@ -751,25 +752,22 @@ fn exec_command_delete_text_control(}}).unwrap_or((value_len, value_len));+ // Selection APIs expose code units, but native editing first resolves both+ // endpoints to caret positions that cannot split a surrogate pair.+ let start = utf16_scalar_boundary_at_or_after(&value_units, start as usize) as u32;+ let end = utf16_scalar_boundary_at_or_after(&value_units, end as usize) as u32;let (from, to) = if start != end {(start, end)+ } else if command == "forwarddelete" {+ (+ start,+ utf16_next_scalar_boundary(&value_units, start as usize) as u32,+ )} else {- let caret = if start == 0 {- 0- } else {- utf16_next_scalar_boundary(&value_units, start as usize - 1) as u32- };- if command == "forwarddelete" {- (- caret,- utf16_next_scalar_boundary(&value_units, caret as usize) as u32,- )- } else {- (- utf16_previous_scalar_boundary(&value_units, caret as usize) as u32,- caret,- )- }+ (+ utf16_previous_scalar_boundary(&value_units, start as usize) as u32,+ start,+ )};if from == to {return Some(true);diff --git a/moli-renderer-v8/src/script_vm/input_dispatch.rs b/moli-renderer-v8/src/script_vm/input_dispatch.rsindex 4bba5748a..77e6e7b20 100644--- a/moli-renderer-v8/src/script_vm/input_dispatch.rs+++ b/moli-renderer-v8/src/script_vm/input_dispatch.rs@@ -42,7 +42,7 @@ use crate::runtime::{};use crate::util::{node_wrapper_from_handle, utf16_len, utf16_next_scalar_boundary,- utf16_previous_scalar_boundary, utf16_units,+ utf16_previous_scalar_boundary, utf16_scalar_boundary_at_or_after, utf16_units,};fn related_target_value<'s>(@@ -2061,25 +2061,20 @@ impl ScriptVm {if target.is_text_control && matches!(key_lower.as_str(), "backspace" | "delete") {let value_units = utf16_units(&text_control_value(runtime, handle));let (start, end) = current_selection_range(runtime, handle);+ let start = utf16_scalar_boundary_at_or_after(&value_units, start as usize) as u32;+ let end = utf16_scalar_boundary_at_or_after(&value_units, end as usize) as u32;let (from, to) = if start != end {(start, end)+ } else if key_lower == "backspace" {+ (+ utf16_previous_scalar_boundary(&value_units, start as usize) as u32,+ start,+ )} else {- let caret = if start == 0 {- 0- } else {- utf16_next_scalar_boundary(&value_units, start as usize - 1) as u32- };- if key_lower == "backspace" {- (- utf16_previous_scalar_boundary(&value_units, caret as usize) as u32,- caret,- )- } else {- (- caret,- utf16_next_scalar_boundary(&value_units, caret as usize) as u32,- )- }+ (+ start,+ utf16_next_scalar_boundary(&value_units, start as usize) as u32,+ )};if from == to {return Ok(input_dispatch_outcome(true));@@ -2136,6 +2131,8 @@ impl ScriptVm {let value_units = utf16_units(&text_control_value(runtime, handle));let value_len = value_units.len() as u32;let (start, end, direction) = current_selection_state(runtime, handle);+ let start = utf16_scalar_boundary_at_or_after(&value_units, start as usize) as u32;+ let end = utf16_scalar_boundary_at_or_after(&value_units, end as usize) as u32;if shift {let (anchor, focus) = match direction.as_str() {"backward" => (end, start),@@ -2153,6 +2150,14 @@ impl ScriptVm {"end" => value_len,_ => focus,};+ if next_focus == focus+ && matches!(+ key_lower.as_str(),+ "arrowleft" | "left" | "arrowright" | "right"+ )+ {+ return Ok(input_dispatch_outcome(true));+ }let (next_start, next_end, next_direction) = if next_focus < anchor {(next_focus, anchor, "backward")} else if next_focus > anchor {@@ -2189,6 +2194,15 @@ impl ScriptVm {"end" => value_len,_ => end,};+ if start == end+ && caret == start+ && matches!(+ key_lower.as_str(),+ "arrowleft" | "left" | "arrowright" | "right"+ )+ {+ return Ok(input_dispatch_outcome(true));+ }let _ = text_control_set_selection_range_internal(scope,runtime_ptr,diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/text_controls.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/text_controls.rsindex 6c26b8d26..74d6135cc 100644--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/text_controls.rs+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/text_controls.rs@@ -1198,34 +1198,62 @@ fn exec_command_insert_text_enforces_maxlength_in_utf16_units() {#[test]fn exec_command_text_control_deletion_preserves_surrogate_pairs() {- let mut vm = new_storage_test_vm("https://exec-command-utf16-deletion.test/");- let result = vm- .eval(- r#"-(() => {+ let mut vm = new_storage_test_vm("https://exec-command-surrogate-pairs.test/");++ for tag in ["input", "textarea"] {+ for (initial, command, start, end, value, caret) in [+ ("AπB", "delete", 3, 3, "AB", 1),+ ("AπB", "forwardDelete", 1, 1, "AB", 1),+ ("AπB", "delete", 2, 2, "AB", 1),+ ("AπB", "forwardDelete", 2, 2, "Aπ", 3),+ ("AπB", "delete", 0, 0, "AπB", 0),+ ("AπB", "forwardDelete", 4, 4, "AπB", 4),+ ("AπB", "delete", 1, 2, "AB", 1),+ ("AπB", "forwardDelete", 1, 2, "AB", 1),+ ("AπB", "delete", 2, 3, "AB", 1),+ ("AπB", "forwardDelete", 2, 3, "Aπ", 3),+ ("AπB", "delete", 1, 3, "AB", 1),+ ("AπB", "forwardDelete", 1, 3, "AB", 1),+ ("AπB", "delete", 0, 2, "B", 0),+ ("AπB", "forwardDelete", 0, 2, "B", 0),+ ("AπB", "delete", 2, 4, "Aπ", 3),+ ("AπB", "forwardDelete", 2, 4, "Aπ", 3),+ ("", "delete", 0, 0, "", 0),+ ("", "forwardDelete", 0, 0, "", 0),+ ("π", "delete", 1, 1, "", 0),+ ("π", "forwardDelete", 1, 1, "π", 1),+ ("Aπ", "delete", 2, 2, "A", 1),+ ("Aπ", "forwardDelete", 2, 2, "Aπ", 2),+ ("ππ", "delete", 3, 3, "π", 2),+ ("ππ", "forwardDelete", 3, 3, "ππ", 3),+ ] {+ let result = vm+ .eval(&format!(+ r#"+(() => {{const root = document.documentElement || document.appendChild(document.createElement('html'));const body = document.body || root.appendChild(document.createElement('body'));- return JSON.stringify(['input', 'textarea'].flatMap(tag => {- return [['delete', 3], ['forwardDelete', 1], ['delete', 2]].map(([command, caret]) => {- const control = document.createElement(tag);- body.append(control);- control.value = 'AπB';- control.focus();- control.setSelectionRange(caret, caret);- const returned = document.execCommand(command);- const result = [returned, control.value, control.selectionStart, control.selectionEnd];- control.remove();- return result;- });- }));-})()-"#,- )- .expect("text control deletion should preserve complete surrogate pairs");- assert_eq!(- result,- r#"[[true,"AB",1,1],[true,"AB",1,1],[true,"AB",1,1],[true,"AB",1,1],[true,"AB",1,1],[true,"AB",1,1]]"#- );+ const control = document.createElement('{tag}');+ body.append(control);+ control.value = '{initial}';+ control.focus();+ control.setSelectionRange({start}, {end});+ const before = [control.selectionStart, control.selectionEnd];+ const returned = document.execCommand('{command}');+ const result = [...before, returned, control.value, control.selectionStart, control.selectionEnd];+ control.remove();+ return JSON.stringify(result);+}})()+"#+ ))+ .expect("execCommand deletion should evaluate");+ assert_eq!(+ serde_json::from_str::<serde_json::Value>(&result).unwrap(),+ serde_json::json!([start, end, true, value, caret, caret]),+ "{tag} {command} value={initial:?} with selection [{start}, {end}]"+ );+ }+ }}#[test]diff --git a/moli-renderer-v8/src/util.rs b/moli-renderer-v8/src/util.rsindex 1732fed7a..8d5d8e05d 100644--- a/moli-renderer-v8/src/util.rs+++ b/moli-renderer-v8/src/util.rs@@ -604,6 +604,19 @@ pub(crate) fn utf16_replace_units_range_lossy(string_from_utf16_units_lossy(&next)}+pub(crate) fn utf16_scalar_boundary_at_or_after(units: &[u16], offset: usize) -> usize {+ let offset = offset.min(units.len());+ if offset > 0+ && offset < units.len()+ && (0xD800..=0xDBFF).contains(&units[offset - 1])+ && (0xDC00..=0xDFFF).contains(&units[offset])+ {+ offset + 1+ } else {+ offset+ }+}+pub(crate) fn utf16_previous_scalar_boundary(units: &[u16], offset: usize) -> usize {let mut previous = offset.min(units.len()).saturating_sub(1);if previous > 0