Skip to content

The headless browser for AI agents and web scraping

fix(obscura-cdp): Backspace deletes a whole surrogate pair, not one code unit (#1005)

Andrey Mnatsakanovec62004559a3
diff --git a/crates/obscura-cdp/src/domains/input.rs b/crates/obscura-cdp/src/domains/input.rs
index bdb96526..be9060cb 100644
--- a/crates/obscura-cdp/src/domains/input.rs
+++ b/crates/obscura-cdp/src/domains/input.rs
@@ -55,8 +55,16 @@ const BACKSPACE_JS: &str = "(function() {\
if (!t || (t.localName !== 'input' && t.localName !== 'textarea')) return;\
var v = t.value || '';\
var s = t.selectionStart, e = t.selectionEnd;\
+ function backCount(str, p) {\
+ if (p >= 2) {\
+ var low = str.charCodeAt(p - 1), high = str.charCodeAt(p - 2);\
+ if (low >= 0xDC00 && low <= 0xDFFF && high >= 0xD800 && high <= 0xDBFF) return 2;\
+ }\
+ return 1;\
+ }\
if (s == null) {\
- globalThis.__obscura_setFieldValue(t, 'value', v.slice(0, -1));\
+ var n = backCount(v, v.length);\
+ globalThis.__obscura_setFieldValue(t, 'value', v.slice(0, v.length - n));\
} else {\
s = Math.max(0, Math.min(s, v.length));\
e = (e == null) ? s : Math.max(0, Math.min(e, v.length));\
@@ -65,8 +73,9 @@ const BACKSPACE_JS: &str = "(function() {\
globalThis.__obscura_setFieldValue(t, 'value', v.slice(0, lo) + v.slice(hi));\
t.setSelectionRange(lo, lo);\
} else if (s > 0) {\
- globalThis.__obscura_setFieldValue(t, 'value', v.slice(0, s - 1) + v.slice(s));\
- t.setSelectionRange(s - 1, s - 1);\
+ var d = backCount(v, s);\
+ globalThis.__obscura_setFieldValue(t, 'value', v.slice(0, s - d) + v.slice(s));\
+ t.setSelectionRange(s - d, s - d);\
}\
}\
t.dispatchEvent(globalThis.__obscura_markTrusted(new Event('input', {bubbles:true})));\
diff --git a/crates/obscura-cdp/tests/backspace_surrogate.rs b/crates/obscura-cdp/tests/backspace_surrogate.rs
new file mode 100644
index 00000000..911e10ed
--- /dev/null
+++ b/crates/obscura-cdp/tests/backspace_surrogate.rs
@@ -0,0 +1,74 @@
+//! #1005 — Backspace must delete a whole supplementary character (surrogate
+//! pair), not a single UTF-16 code unit that would leave a lone surrogate.
+
+use obscura_cdp::dispatch::{dispatch, CdpContext};
+use obscura_cdp::types::CdpRequest;
+use serde_json::{json, Value};
+
+async fn cdp(ctx: &mut CdpContext, method: &str, params: Value) -> Value {
+ let response = dispatch(
+ &CdpRequest {
+ id: 1,
+ method: method.to_string(),
+ params,
+ session_id: Some("ta".to_string()),
+ },
+ ctx,
+ )
+ .await;
+ assert!(response.error.is_none(), "{method}: {:?}", response.error);
+ response.result.unwrap_or_else(|| json!({}))
+}
+
+async fn evaluate(ctx: &mut CdpContext, expression: &str) -> Value {
+ let result = cdp(
+ ctx,
+ "Runtime.evaluate",
+ json!({"expression": expression, "returnByValue": true}),
+ )
+ .await;
+ assert!(result.get("exceptionDetails").is_none(), "{result}");
+ result["result"]["value"].clone()
+}
+
+async fn setup() -> CdpContext {
+ let mut ctx = CdpContext::new();
+ let page_id = ctx.create_page();
+ ctx.sessions.insert("ta".to_string(), page_id);
+ cdp(
+ &mut ctx,
+ "Page.navigate",
+ json!({"url": "data:text/html,<textarea id='field'></textarea>", "waitUntil": "load"}),
+ )
+ .await;
+ evaluate(&mut ctx, "field.focus();").await;
+ ctx
+}
+
+#[tokio::test(flavor = "current_thread")]
+async fn backspace_deletes_a_whole_surrogate_pair() {
+ let mut ctx = setup().await;
+ // "a<emoji>b" is JS length 4 (emoji = 2 code units); caret at 3 is between
+ // the emoji and 'b'.
+ evaluate(
+ &mut ctx,
+ "field.value = 'a\\u{1F600}b'; field.setSelectionRange(3, 3);",
+ )
+ .await;
+ cdp(
+ &mut ctx,
+ "Input.dispatchKeyEvent",
+ json!({"type": "keyDown", "key": "Backspace", "code": "Backspace"}),
+ )
+ .await;
+ assert_eq!(
+ evaluate(&mut ctx, "field.value").await,
+ json!("ab"),
+ "Backspace must remove the whole emoji, not split the surrogate pair"
+ );
+ assert_eq!(
+ evaluate(&mut ctx, "field.selectionStart").await.as_f64(),
+ Some(1.0),
+ "caret must move to before the deleted character"
+ );
+}