The headless browser for AI agents and web scraping
fix(js): activate summary clicks to toggle details
lisa03148fe6d99d4e43
diff --git a/crates/obscura-cdp/src/domains/input.rs b/crates/obscura-cdp/src/domains/input.rsindex 115be7ce..21fe6c9a 100644--- a/crates/obscura-cdp/src/domains/input.rs+++ b/crates/obscura-cdp/src/domains/input.rs@@ -303,6 +303,7 @@ pub async fn handle(var ctl = globalThis.__obscura_labeledControl(labelHost);\if (ctl && ctl !== clickTarget && globalThis.__obscura_activateLabel(labelHost, ctl, true)) {{ return; }}\}}\+ if (globalThis.__obscura_activateSummary(clickTarget)) return;\var link = clickTarget.closest ? clickTarget.closest('a[href]') : null;\if (!link && tag === 'A' && clickTarget.getAttribute('href')) link = clickTarget;\var submitButton = tag === 'BUTTON' ? clickTarget : (clickTarget.closest ? clickTarget.closest('button') : null);\diff --git a/crates/obscura-cdp/tests/input_mouse_label_activation.rs b/crates/obscura-cdp/tests/input_mouse_label_activation.rsindex 99604169..54029cab 100644--- a/crates/obscura-cdp/tests/input_mouse_label_activation.rs+++ b/crates/obscura-cdp/tests/input_mouse_label_activation.rs@@ -17,13 +17,14 @@ async fn serve_fixture() -> String {// regardless of inline-wrapper geometry.let body = r#"<!doctype html><html><head><style>html, body { margin: 0; font: 16px monospace }- label { display: block; width: 200px; height: 40px }+ label, summary { display: block; width: 200px; height: 40px }</style></head><body><label id="explicit" for="boxa">a</label><input id="boxa" type="checkbox"><label id="implicit">b <input id="boxb" type="checkbox"></label><label id="deep" for="boxc"><span><b id="deep-text">c</b></span></label><input id="boxc" type="checkbox"><label id="off" for="boxd">d</label><input id="boxd" type="checkbox" disabled>+ <details id="panel"><summary id="summary">More</summary><p>Content</p></details><script>window.events = [];for (const id of ['boxa','boxb','boxc','boxd']) {@@ -139,6 +140,17 @@ async fn mouse_click_on_a_label_activates_its_control() {);}+#[tokio::test(flavor = "current_thread")]+async fn mouse_click_on_summary_toggles_details() {+ let (mut ctx, sid) = setup().await;+ click_element(&mut ctx, 40, &sid, "#summary").await;+ let open = evaluate(&mut ctx, 50, "document.getElementById('panel').open", &sid).await;+ assert_eq!(open["result"]["value"], true);+ click_element(&mut ctx, 60, &sid, "#summary").await;+ let closed = evaluate(&mut ctx, 70, "document.getElementById('panel').open", &sid).await;+ assert_eq!(closed["result"]["value"], false);+}+#[tokio::test(flavor = "current_thread")]async fn mouse_click_deep_inside_a_label_activates_its_control() {let (mut ctx, sid) = setup().await;diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.jsindex 6fccfe2c..8933d8e4 100644--- a/crates/obscura-js/js/bootstrap.js+++ b/crates/obscura-js/js/bootstrap.js@@ -3488,6 +3488,33 @@ const _forwardingLabels = new WeakSet();// __obscura_markTrusted already does for any event.const _TRUSTED_ACTIVATION = Symbol('obscura.trustedActivation');+const _pendingDetailsToggles = new WeakMap();+globalThis.__obscura_activateSummary = function(target) {+ const summary = target.tagName === 'SUMMARY' ? target : target.closest?.('summary');+ if (!summary || summary.parentElement?.tagName !== 'DETAILS') return false;+ const interactive = target.closest?.(_INTERACTIVE);+ if (interactive && summary.contains(interactive)) return false;+ const details = summary.parentElement;+ let firstSummary = details.firstElementChild;+ while (firstSummary && firstSummary.tagName !== 'SUMMARY') firstSummary = firstSummary.nextElementSibling;+ if (firstSummary !== summary) return false;+ const oldState = details.open ? 'open' : 'closed';+ const newState = details.open ? 'closed' : 'open';+ details.open = !details.open;+ const pending = _pendingDetailsToggles.get(details);+ if (pending) {+ pending.newState = newState;+ } else {+ const states = {oldState, newState};+ _pendingDetailsToggles.set(details, states);+ setTimeout(() => {+ _pendingDetailsToggles.delete(details);+ details.dispatchEvent(new ToggleEvent('toggle', states));+ }, 0);+ }+ return true;+};+// Only these elements can be actually disabled. A `disabled` attribute on// anything else, which component libraries do put on plain <div>s, has no// effect on event dispatch.@@ -4579,6 +4606,7 @@ class Element extends Node {return;}}+ if (globalThis.__obscura_activateSummary(this)) return;if (!cancelled) {const link = this.tagName === 'A' ? this : (this.closest ? this.closest('a[href]') : null);if (link) {diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex 1ca6135c..6ab0f3ab 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -18725,6 +18725,49 @@ mod tests {assert_eq!(result, serde_json::json!("yes"));}+ #[tokio::test(flavor = "current_thread")]+ async fn test_summary_activation_toggles_details() {+ let mut rt = setup_runtime(+ r#"<details id="panel"><summary id="first"><span id="text">More</span><button id="nested">Button</button></summary>+ <summary id="second">Other</summary><p>Content</p></details>+ <details id="cancel"><summary id="blocked">Blocked</summary></details>"#,+ );+ let result = rt+ .evaluate(+ r#"+ const panel = document.getElementById('panel');+ const cancel = document.getElementById('cancel');+ globalThis.summaryToggles = [];+ panel.addEventListener('toggle', event => summaryToggles.push([event.oldState, event.newState]));+ document.getElementById('text').click();+ const opened = panel.open;+ document.getElementById('second').click();+ const stillOpen = panel.open;+ document.getElementById('first').click();+ const closed = panel.open;+ document.getElementById('nested').click();+ const afterNested = panel.open;+ document.getElementById('blocked').addEventListener('click', event => event.preventDefault());+ document.getElementById('blocked').click();+ return [opened, stillOpen, closed, afterNested, cancel.open, summaryToggles.length];+ "#,+ )+ .unwrap();+ assert_eq!(result, serde_json::json!([true, true, false, false, false, 0]));+ rt.run_event_loop_bounded(100).await.unwrap();+ assert_eq!(+ rt.evaluate("summaryToggles").unwrap(),+ serde_json::json!([["closed", "closed"]])+ );+ rt.evaluate("document.getElementById('first').click()")+ .unwrap();+ rt.run_event_loop_bounded(100).await.unwrap();+ assert_eq!(+ rt.evaluate("summaryToggles").unwrap(),+ serde_json::json!([["closed", "closed"], ["closed", "open"]])+ );+ }+#[test]fn test_label_click_activates_its_labeled_control() {let mut rt = setup_runtime(