Revert "Apply laf/bidi-engine-launch's changes to element.rs and interaction.rs ahead of the merge"

Restores element.rs and interaction.rs to the merge in 8f0eb3f4; the reverted commit landed on top of it by mistake.

Co-authored-by: vercel-v[bot] <353008837+vercel-v[bot]@users.noreply.github.com>
This commit is contained in:
Lahfir
2026-11-24 11:44:27 -08:00
committed by GitHub
co-authored by vercel-v[bot]
parent 2c4c75f0aa
commit 9a1fa1ca07
2 changed files with 139 additions and 75 deletions
+15 -46
View File
@@ -1734,16 +1734,21 @@ pub(crate) fn decide_check_plan<T>(
CheckPlan::Pointer(target)
}
/// Evaluates `CHECK_CONTROL_FN_JS`, click geometry, and check-mode hit-testing on Chrome and
/// returns the explicit `CheckPlan` for `toggle`.
pub(crate) async fn plan_chrome_check(
/// Formats the error when `check` or `uncheck` targets a non-checkable element.
pub(crate) fn not_checkable_error(target: &str) -> String {
format!(
"Element '{}' is not a checkbox, radio button, or checkable control",
target
)
}
pub async fn is_element_checked(
client: &CdpClient,
session_id: &str,
ref_map: &RefMap,
selector_or_ref: &str,
iframe_sessions: &HashMap<String, String>,
want_checked: bool,
) -> Result<CheckPlan<(f64, f64, String)>, String> {
) -> Result<bool, String> {
let (object_id, effective_session_id) = resolve_element_object_id(
client,
session_id,
@@ -1753,28 +1758,6 @@ pub(crate) async fn plan_chrome_check(
)
.await?;
let js = format!(
r#"async function() {{
const ctrl = ({CHECK_CONTROL_FN_JS})(this);
if (!ctrl) return null;
const isNative = !!(ctrl.tagName && ctrl.tagName.toUpperCase() === 'INPUT' && (ctrl.type === 'checkbox' || ctrl.type === 'radio'));
const checked = isNative ? !!ctrl.checked : ctrl.getAttribute('aria-checked') === 'true';
const r = ctrl.getClientRects && ctrl.getClientRects()[0];
const s = getComputedStyle(ctrl);
const clickable = !!(r && r.width > 0 && r.height > 0 && s.display !== 'none' && s.visibility !== 'hidden' && parseFloat(s.opacity) > 0 && s.pointerEvents !== 'none');
if (!clickable) {{
return {{ checked: checked, native: isNative, clickable: false, blocker: null, x: null, y: null }};
}}
const clickPoint = {CLICK_POINT_JS};
const doc = ctrl.ownerDocument || document;
const point = await clickPoint(ctrl, doc);
const blockerAt = {BLOCKER_AT_JS};
const topDoc = (point.view && point.view.document) || doc;
const blocker = blockerAt(doc, ctrl, point.lx, point.ly, true) || blockerAt(topDoc, ctrl, point.x, point.y, true);
return {{ checked: checked, native: isNative, clickable: true, blocker: blocker, x: point.x, y: point.y }};
}}"#
);
let result: EvaluateResult = client
.send_command_typed(
"Runtime.callFunctionOn",
@@ -1820,25 +1803,11 @@ pub(crate) async fn plan_chrome_check(
)
.await?;
Ok(result.result.value.and_then(|v| v.as_bool()))
}
pub async fn is_element_checked(
client: &CdpClient,
session_id: &str,
ref_map: &RefMap,
selector_or_ref: &str,
iframe_sessions: &HashMap<String, String>,
) -> Result<bool, String> {
Ok(element_checked_state(
client,
session_id,
ref_map,
selector_or_ref,
iframe_sessions,
)
.await?
.unwrap_or(false))
Ok(result
.result
.value
.and_then(|v| v.as_bool())
.unwrap_or(false))
}
pub(crate) const ELEMENT_INNER_TEXT_JS: &str = "function() { return this.innerText || ''; }";
+124 -29
View File
@@ -157,6 +157,17 @@ pub async fn hover(
Ok((x + offset.0, y + offset.1))
}
pub(crate) const FOCUS_JS: &str = "function() { this.focus(); }";
/// Resolves `this`'s href against the document base, or null for a non-link.
pub(crate) const CLICK_HREF_JS: &str = "function() { var h = this.getAttribute('href'); if (!h) return null; try { return new URL(h, document.baseURI).toString(); } catch(e) { return null; } }";
pub(crate) const CLEAR_INPUT_JS: &str = r#"function() {
this.select && this.select();
this.value = '';
this.dispatchEvent(new Event('input', { bubbles: true }));
}"#;
/// Stable codes for refusals before acting. An error message that starts with
/// `<code>: ` surfaces the code in the JSON envelope.
pub const FORM_ERROR_CODES: &[&str] = &[
@@ -1305,6 +1316,117 @@ pub async fn scroll(
Ok(())
}
pub(crate) const SELECT_OPTION_JS: &str = r#"async function(vals) {
const normalize = (value) => String(value ?? '')
.replace(/[\u200B\u200C\u200D\u2060\uFEFF]/g, '')
.replace(/\s+/g, ' ')
.trim();
if (!(this instanceof HTMLSelectElement)) {
const role = this.getAttribute('role');
if (role !== 'combobox' && role !== 'listbox') {
return { error: 'select target <' + this.tagName.toLowerCase() + '> is not a <select> or a role=combobox/listbox element' };
}
const realClick = (el) => {
const r = el.getBoundingClientRect();
const o = { bubbles: true, cancelable: true, composed: true,
clientX: r.left + r.width / 2, clientY: r.top + r.height / 2,
button: 0, pointerId: 1, isPrimary: true };
el.dispatchEvent(new PointerEvent('pointerdown', o));
el.dispatchEvent(new MouseEvent('mousedown', o));
el.dispatchEvent(new PointerEvent('pointerup', o));
el.dispatchEvent(new MouseEvent('mouseup', o));
el.dispatchEvent(new MouseEvent('click', o));
};
if (role === 'combobox' && this.getAttribute('aria-expanded') !== 'true') {
realClick(this);
}
const collect = () => {
let scope = role === 'listbox' ? this : null;
if (!scope) {
const controls = this.getAttribute('aria-controls');
if (controls) {
scope = document.getElementById(controls);
if (!scope) return [];
}
}
const nodes = Array.from((scope || document).querySelectorAll('[role=option]'));
return nodes.filter((o) => o.getClientRects().length > 0);
};
let options = collect();
for (let i = 0; i < 40 && options.length === 0; i++) {
await new Promise((r) => setTimeout(r, 50));
options = collect();
}
if (options.length === 0) {
return { error: 'combobox opened but no visible role=option elements were found' };
}
const nameOf = (o) => {
const labels = (o.getAttribute('aria-labelledby') || '').trim();
if (labels) {
const text = labels.split(/\s+/)
.map((id) => { const el = document.getElementById(id); return el ? el.textContent : ''; })
.join(' ');
if (normalize(text)) return normalize(text);
}
return normalize(o.getAttribute('aria-label')) || normalize(o.textContent);
};
const valueOf = (o) => normalize(o.getAttribute('data-value')) || normalize(o.getAttribute('value'));
const wanted = new Set();
for (const value of vals) {
const want = normalize(value);
const matches = options.filter((o) => nameOf(o) === want || (valueOf(o) && valueOf(o) === want));
if (matches.length === 0) {
const available = options.map((o) => '"' + nameOf(o) + '"').join(', ');
return { error: 'No option matched ' + JSON.stringify(vals) + '. Available options: ' + available };
}
if (matches.length > 1) {
return { error: 'Multiple options matched ' + JSON.stringify(value) };
}
if (matches[0].getAttribute('aria-disabled') === 'true') {
return { error: 'Option "' + nameOf(matches[0]) + '" is disabled' };
}
wanted.add(matches[0]);
}
const multi = role === 'listbox' && this.getAttribute('aria-multiselectable') === 'true';
for (const opt of wanted) {
if (multi && opt.getAttribute('aria-selected') === 'true') continue;
opt.scrollIntoView({ block: 'nearest' });
realClick(opt);
}
return { matched: wanted.size };
}
const options = Array.from(this.options);
const wanted = new Set();
for (const value of vals) {
let matches = options.filter((opt) =>
value === opt.value ||
value === opt.label.trim() ||
value === opt.textContent.trim()
);
if (matches.length === 0) {
const normalizedValue = normalize(value);
matches = options.filter((opt) =>
normalize(opt.label) === normalizedValue
);
if (matches.length > 1) {
return { error: 'Multiple options matched ' + JSON.stringify(value) + ' after whitespace normalization' };
}
}
if (matches.length === 0) {
const available = options.map(o => o.value + ' ("' + normalize(o.label) + '")').join(', ');
return { error: 'No option matched ' + JSON.stringify(vals) + '. Available options: ' + available };
}
for (const opt of matches) wanted.add(opt);
}
if (wanted.size === 0) {
const available = options.map(o => o.value + ' ("' + normalize(o.label) + '")').join(', ');
return { error: 'No option matched ' + JSON.stringify(vals) + '. Available options: ' + available };
}
for (const opt of options) opt.selected = wanted.has(opt);
this.dispatchEvent(new Event('change', { bubbles: true }));
return { matched: wanted.size };
}"#;
/// A refused `select`: the message is fixed text; the options the page offered
/// travel separately as error data.
pub struct SelectError {
@@ -1484,34 +1606,8 @@ pub async fn select_option(
for (const opt of options) opt.selected = wanted.has(opt);
this.dispatchEvent(new Event('change', { bubbles: true }));
return { matched: wanted.size };
}"#;
pub async fn select_option(
client: &CdpClient,
session_id: &str,
ref_map: &RefMap,
selector_or_ref: &str,
values: &[String],
iframe_sessions: &HashMap<String, String>,
) -> Result<(), String> {
let (object_id, effective_session_id) = resolve_element_object_id(
client,
session_id,
ref_map,
selector_or_ref,
iframe_sessions,
)
.await?;
// Matching nothing must be an error, not a silent success: an agent that
// selects a misspelled option otherwise sees "Done", and only discovers
// the page state is wrong after more commands. List what was available.
//
// Native <select> keeps its own path. For a div-based ARIA combobox/listbox
// (Radix, Headless UI, ui-sdk) there is no `this.options`, so open the widget
// when it is not already expanded, wait for its role=option items, and click
// the ones whose accessible name or value matches.
let js = SELECT_OPTION_JS.to_string();
}"#
.to_string();
let result = client
.send_command_typed::<_, Value>(
@@ -1749,7 +1845,6 @@ async fn toggle(
ref_map,
selector_or_ref,
iframe_sessions,
want_checked,
)
.await
else {