Skip to content

The headless browser for AI agents and web scraping

fix(js): implement HTMLInputElement.indeterminate

Nguyen Thanh Dat94f8d0197c99
diff --git a/crates/obscura-cdp/src/domains/input.rs b/crates/obscura-cdp/src/domains/input.rs
index f2d5bd13..06b2db24 100644
--- a/crates/obscura-cdp/src/domains/input.rs
+++ b/crates/obscura-cdp/src/domains/input.rs
@@ -160,6 +160,7 @@ pub async fn handle(
if (globalThis.__obscura_isDisabled(clickTarget)) return;\
var checkable = tag === 'INPUT' && (type === 'checkbox' || type === 'radio');\
var oldChecked = checkable ? !!clickTarget.checked : false;\
+ var oldIndeterminate = checkable ? !!clickTarget.indeterminate : false;\
var radioStates = null;\
if (checkable && type === 'radio') {{\
var radioName = clickTarget.getAttribute('name') || '';\
@@ -176,13 +177,14 @@ pub async fn handle(
clickTarget.checked = true;\
}} else if (checkable) {{\
clickTarget.checked = !oldChecked;\
+ clickTarget.indeterminate = false;\
}}\
var click = globalThis.__obscura_markTrusted(new MouseEvent('click', {{bubbles:true,cancelable:true,view:globalThis,clientX:{x},clientY:{y},button:0,buttons:0,detail:{click_count},altKey:{alt_key},ctrlKey:{ctrl_key},metaKey:{meta_key},shiftKey:{shift_key}}}));\
var cancelled = !clickTarget.dispatchEvent(click);\
if (cancelled) {{\
if (radioStates) {{\
for (var rr = 0; rr < radioStates.length; rr++) radioStates[rr][0].checked = radioStates[rr][1];\
- }} else if (checkable) clickTarget.checked = oldChecked;\
+ }} else if (checkable) {{ clickTarget.checked = oldChecked; clickTarget.indeterminate = oldIndeterminate; }}\
return;\
}}\
if (checkable && clickTarget.checked !== oldChecked) {{\
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index 66eda4ea..4aff32b3 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -699,9 +699,11 @@ function _fp(key) { return _getFp()[key]; }
globalThis._eventRegistry = globalThis._eventRegistry || {};
globalThis._formValues = globalThis._formValues || {};
globalThis._formChecked = globalThis._formChecked || {};
+globalThis._formIndeterminate = globalThis._formIndeterminate || {};
const _eventRegistry = globalThis._eventRegistry;
const _formValues = globalThis._formValues;
const _formChecked = globalThis._formChecked;
+const _formIndeterminate = globalThis._formIndeterminate;
const _domParse = (cmd, a1, a2) => { try { return JSON.parse(_dom(cmd, a1, a2)); } catch { return null; } };
// HTML "ASCII whitespace": U+0009 TAB, U+000A LF, U+000C FF, U+000D CR, U+0020 SPACE.
@@ -3556,9 +3558,10 @@ class Element extends Node {
if (_isActuallyDisabled(this) && _tag !== 'LABEL') {
return;
}
- let _oldChecked = false, _radioStates = null;
+ let _oldChecked = false, _oldIndeterminate = false, _radioStates = null;
if (_checkable) {
_oldChecked = !!this.checked;
+ _oldIndeterminate = !!this.indeterminate;
if (_type === 'radio') {
const _name = this.getAttribute('name') || '';
if (_name) {
@@ -3574,7 +3577,12 @@ class Element extends Node {
}
this.checked = true;
} else {
+ // Legacy-pre-activation behaviour (HTML spec): a checkbox toggles its
+ // checkedness *and* drops indeterminateness. Clearing it here, not on
+ // `change`, is what lets the cancelled-activation path put the old
+ // flag back instead of leaving it stuck off.
this.checked = !_oldChecked;
+ this.indeterminate = false;
}
}
const _clickEvent = new MouseEvent("click", {bubbles: true, cancelable: true});
@@ -3582,7 +3590,7 @@ class Element extends Node {
const cancelled = !this.dispatchEvent(_clickEvent);
if (cancelled) {
if (_radioStates) { for (let i = 0; i < _radioStates.length; i++) _radioStates[i][0].checked = _radioStates[i][1]; }
- else if (_checkable) this.checked = _oldChecked;
+ else if (_checkable) { this.checked = _oldChecked; this.indeterminate = _oldIndeterminate; }
return;
}
if (_checkable && this.checked !== _oldChecked) {
@@ -3929,6 +3937,13 @@ class Element extends Node {
return this.hasAttribute("checked");
}
set checked(v) { _formChecked[this._nid] = !!v; }
+ // `indeterminate` is IDL-only: it has no content attribute to reflect, so
+ // the property itself must exist on the prototype for `'indeterminate' in
+ // el` to be true on a freshly created element. It is node-keyed like
+ // `checked` because element wrappers are rebuilt on each lookup, so a
+ // per-instance field would not survive getElementById returning a new one.
+ get indeterminate() { return _formIndeterminate[this._nid] === true; }
+ set indeterminate(v) { _formIndeterminate[this._nid] = !!v; }
get selected() {
if (this._selected !== undefined) return this._selected;
return this.hasAttribute("selected");
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index 7114a5bd..7be42e3a 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -13221,6 +13221,48 @@ mod tests {
assert_eq!(result, serde_json::json!([false, false, false, true]));
}
+ #[test]
+ fn test_checkbox_indeterminate_is_idl_only_and_cleared_by_activation() {
+ // `indeterminate` has no content attribute, so it exists only if the
+ // prototype defines it -- `'indeterminate' in el` is the check that
+ // fails when it is missing. Activation clears it as well as toggling
+ // checkedness (HTML legacy-pre-activation behaviour), and a cancelled
+ // click puts both back, so a script-set flag is never left stuck.
+ let mut rt = setup_runtime(
+ r#"<input type="checkbox" id="fresh">
+ <input type="checkbox" id="click">
+ <input type="checkbox" id="cancel">"#,
+ );
+ let result = rt
+ .evaluate(
+ r#"
+ const fresh = document.getElementById('fresh');
+ const present = 'indeterminate' in fresh;
+ const initial = fresh.indeterminate;
+ fresh.indeterminate = true;
+ const roundTrip = fresh.indeterminate;
+
+ const clicked = document.getElementById('click');
+ clicked.indeterminate = true;
+ clicked.click();
+
+ const cancelled = document.getElementById('cancel');
+ cancelled.indeterminate = true;
+ cancelled.addEventListener('click', e => e.preventDefault());
+ cancelled.click();
+
+ return [present, initial, roundTrip,
+ clicked.checked, clicked.indeterminate,
+ cancelled.checked, cancelled.indeterminate];
+ "#,
+ )
+ .unwrap();
+ assert_eq!(
+ result,
+ serde_json::json!([true, false, true, true, false, false, true])
+ );
+ }
+
#[test]
fn test_disabled_only_applies_to_disableable_elements() {
// A `disabled` attribute is meaningless on anything that cannot be