Skip to content

The headless browser for AI agents and web scraping

Merge remote-tracking branch 'github/main' into integrate/pr-1185-main-20261004

SGavrl8ee455968912
diff --cc crates/obscura-cdp/src/domains/input.rs
index 59fb8fb0,02df7015..d74383ab
--- a/crates/obscura-cdp/src/domains/input.rs
+++ b/crates/obscura-cdp/src/domains/input.rs
@@@ -430,12 -439,10 +439,12 @@@ pub async fn handle
code = js_str(code),
virtual_key_code = virtual_key_code,
);
- page.evaluate(&js);
+ if page.evaluate(&js).as_bool() == Some(false) {
+ return Ok(json!({}));
+ }
if !text.is_empty() && text != "\r" && text != "\n" {
- page.evaluate(&insert_text_js(text));
+ page.evaluate(&insert_text_js(text, false));
}
if key == "Enter" {
diff --cc crates/obscura-cdp/tests/textarea_enter_selection.rs
index 4b48bebb,1bb6bd86..572086e8
--- a/crates/obscura-cdp/tests/textarea_enter_selection.rs
+++ b/crates/obscura-cdp/tests/textarea_enter_selection.rs
@@@ -109,34 -109,77 +109,109 @@@ async fn typing_continues_after_the_ins
assert_eq!(result["end"], 3);
}
+#[tokio::test(flavor = "current_thread")]
+async fn canceled_keydown_preserves_fields_and_delivers_keyup_without_editing_defaults() {
+ let mut ctx = setup().await;
+ for tag in ["input", "textarea"] {
+ evaluate(&mut ctx, &format!(
+ "document.body.innerHTML = '<{tag} id=field></{tag}>'; field.focus();\
+ field.addEventListener('keydown', event => event.preventDefault());\
+ for (const type of ['keydown', 'keypress', 'beforeinput', 'input', 'keyup'])\
+ field.addEventListener(type, event => changes.push(event.type));"
+ )).await;
+ for event_type in ["keyDown", "rawKeyDown"] {
+ for (key, text, virtual_key) in [
+ ("Z", "Z", 90), ("Backspace", "", 8),
+ ("Delete", "", 46), ("Enter", "\r", 13),
+ ] {
+ evaluate(&mut ctx, "field.value = 'abCD'; field.setSelectionRange(1, 3); changes.length = 0;").await;
+ cdp(&mut ctx, "Input.dispatchKeyEvent", json!({
+ "type": event_type, "key": key, "text": text,
+ "windowsVirtualKeyCode": virtual_key,
+ })).await;
+ cdp(&mut ctx, "Input.dispatchKeyEvent", json!({
+ "type": "keyUp", "key": key, "windowsVirtualKeyCode": virtual_key,
+ })).await;
+ assert_eq!(state(&mut ctx).await, json!({
+ "value": "abCD", "start": 1, "end": 3,
+ "changes": ["keydown", "keyup"],
+ }), "{tag} {event_type} {key}");
+ }
+ }
+ }
+}
++
+ #[tokio::test(flavor = "current_thread")]
+ async fn text_insertion_dispatches_cancelable_beforeinput_and_trusted_input_events() {
+ let mut ctx = setup().await;
+ for tag in ["input", "textarea"] {
+ for (method, params) in [
+ ("Input.insertText", json!({"text": "Z"})),
+ ("Input.dispatchKeyEvent", json!({"type": "keyDown", "key": "Z", "text": "Z"})),
+ ("Input.dispatchKeyEvent", json!({"type": "char", "text": "Z"})),
+ ] {
+ evaluate(&mut ctx, &format!(r#"(function() {{
+ document.body.innerHTML = '<{tag} id="field"></{tag}>';
+ var field = document.getElementById('field');
+ field.value = 'abCD'; field.focus(); field.setSelectionRange(1, 3);
+ globalThis.editEvents = [];
+ for (var type of ['beforeinput', 'input']) field.addEventListener(type, function(e) {{
+ editEvents.push([e.type, e instanceof InputEvent, e.data, e.inputType,
+ e.isTrusted, e.bubbles, e.composed, e.cancelable, field.value]);
+ }});
+ }})()"#)).await;
+ cdp(&mut ctx, method, params).await;
+ assert_eq!(evaluate(&mut ctx, "document.getElementById('field').value").await, "aZD");
+ assert_eq!(evaluate(&mut ctx, "editEvents").await, json!([
+ ["beforeinput", true, "Z", "insertText", true, true, true, true, "abCD"],
+ ["input", true, "Z", "insertText", true, true, true, false, "aZD"]
+ ]), "{tag}: {method}");
+ }
+ }
+ }
+
+ #[tokio::test(flavor = "current_thread")]
+ async fn canceled_or_readonly_text_insertion_preserves_value_and_selection() {
+ let mut ctx = setup().await;
+ for tag in ["input", "textarea"] {
+ for guard in [
+ "field.addEventListener('beforeinput', function(e) { e.preventDefault(); });",
+ "field.readOnly = true;",
+ "field.setAttribute('readonly', '');",
+ ] {
+ evaluate(&mut ctx, &format!(r#"(function() {{
+ document.body.innerHTML = '<{tag} id="field"></{tag}>';
+ var field = document.getElementById('field');
+ field.value = 'abCD'; field.focus(); field.setSelectionRange(1, 3);
+ globalThis.editEvents = [];
+ for (var type of ['beforeinput', 'input']) field.addEventListener(type, function(e) {{
+ editEvents.push(e.type);
+ }});
+ {guard}
+ }})()"#)).await;
+ cdp(&mut ctx, "Input.insertText", json!({"text": "Z"})).await;
+ assert_eq!(evaluate(&mut ctx, "(function() { var field = document.getElementById('field'); return [field.value, field.selectionStart, field.selectionEnd, editEvents]; })()").await,
+ json!(["abCD", 1, 3, ["beforeinput"]]), "{tag}: {guard}");
+ }
+ }
+ }
+
+ #[tokio::test(flavor = "current_thread")]
+ async fn text_insertion_reads_value_and_selection_after_beforeinput_handlers() {
+ let mut ctx = setup().await;
+ evaluate(&mut ctx, r#"field.addEventListener('beforeinput', function() {
+ field.value = 'xy'; field.setSelectionRange(0, 1);
+ });"#).await;
+ for (method, params, caret) in [
+ ("Input.insertText", json!({"text": "Z"}), 2),
+ ("Input.dispatchKeyEvent", json!({"type": "keyDown", "key": "Z", "text": "Z"}), 1),
+ ("Input.dispatchKeyEvent", json!({"type": "char", "text": "Z"}), 1),
+ ] {
+ evaluate(&mut ctx, "field.value = 'abCD'; field.setSelectionRange(1, 3);").await;
+ cdp(&mut ctx, method, params).await;
+ let result = state(&mut ctx).await;
+ assert_eq!(result["value"], "Zy");
+ assert_eq!(result["start"], caret, "{method}");
+ assert_eq!(result["end"], caret, "{method}");
+ }
+ }