Skip to content

The headless browser for AI agents and web scraping

fix(cdp): correct accessible names and visibility

Railly Hugof81c2961100a
diff --git a/crates/obscura-cdp/src/domains/accessibility.rs b/crates/obscura-cdp/src/domains/accessibility.rs
index 55bd4ddc..807382c0 100644
--- a/crates/obscura-cdp/src/domains/accessibility.rs
+++ b/crates/obscura-cdp/src/domains/accessibility.rs
@@ -1,4 +1,7 @@
+use std::collections::{HashMap, HashSet};
+
use obscura_dom::{DomTree, NodeData, NodeId};
+use obscura_js::runtime::AccessibilityStyle;
use serde_json::{json, Value};
use crate::dispatch::CdpContext;
@@ -35,8 +38,16 @@ pub async fn handle(
let page = ctx
.get_session_page(session_id)
.ok_or("No page")?;
+ #[cfg(feature = "render")]
+ let styles = page
+ .js
+ .as_ref()
+ .map(|js| js.accessibility_styles())
+ .unwrap_or_default();
+ #[cfg(not(feature = "render"))]
+ let styles = HashMap::new();
let nodes = page
- .with_dom(|dom| build_ax_nodes(dom))
+ .with_dom(|dom| build_ax_nodes_with_styles(dom, &styles))
.unwrap_or_default();
Ok(json!({ "nodes": nodes }))
}
@@ -45,7 +56,15 @@ pub async fn handle(
}
/// Walk the full DOM tree and produce CDP Accessibility AXNode array.
+#[cfg(test)]
fn build_ax_nodes(dom: &DomTree) -> Vec<Value> {
+ build_ax_nodes_with_styles(dom, &HashMap::new())
+}
+
+fn build_ax_nodes_with_styles(
+ dom: &DomTree,
+ styles: &HashMap<NodeId, AccessibilityStyle>,
+) -> Vec<Value> {
let mut nodes: Vec<Value> = Vec::new();
let mut id_counter: u32 = 0;
// Map DOM NodeId → AX string id, populated only for nodes actually in the AX tree
@@ -56,6 +75,7 @@ fn build_ax_nodes(dom: &DomTree) -> Vec<Value> {
// Collect all DOM nodes in tree order (root + descendants)
let mut all_dom_ids: Vec<NodeId> = vec![document];
all_dom_ids.extend(dom.descendants(document));
+ let context = NameContext::new(dom, &all_dom_ids, styles);
// First pass: assign AX IDs only to nodes that will produce an AX node
let mut eligible: Vec<NodeId> = Vec::new();
@@ -63,7 +83,7 @@ fn build_ax_nodes(dom: &DomTree) -> Vec<Value> {
// Quick check without full build_ax_node (just role check to avoid borrow issues)
if let Some(node) = dom.get_node(*dom_id) {
let role = map_role(&node.data);
- if !role.is_empty() {
+ if !role.is_empty() && !context.is_hidden(*dom_id) {
id_counter += 1;
dom_to_ax.insert(dom_id.raw(), id_counter.to_string());
eligible.push(*dom_id);
@@ -73,11 +93,25 @@ fn build_ax_nodes(dom: &DomTree) -> Vec<Value> {
// Second pass: build AXNode for eligible nodes
for dom_id in &eligible {
- if let Some(ax) = build_ax_node(dom, *dom_id, &dom_to_ax) {
+ if let Some(ax) = build_ax_node(dom, *dom_id, &dom_to_ax, &context) {
nodes.push(ax);
}
}
+ let mut children: HashMap<String, Vec<Value>> = HashMap::new();
+ for node in &nodes {
+ if let Some(parent) = node["parentId"].as_str() {
+ children
+ .entry(parent.to_string())
+ .or_default()
+ .push(node["nodeId"].clone());
+ }
+ }
+ for node in &mut nodes {
+ if let Some(ids) = children.remove(node["nodeId"].as_str().unwrap()) {
+ node["childIds"] = json!(ids);
+ }
+ }
nodes
}
@@ -85,6 +119,7 @@ fn build_ax_node(
dom: &DomTree,
node_id: NodeId,
dom_to_ax: &std::collections::HashMap<u32, String>,
+ context: &NameContext,
) -> Option<Value> {
let node = dom.get_node(node_id)?;
let ax_id = dom_to_ax.get(&node_id.raw())?.clone();
@@ -95,16 +130,10 @@ fn build_ax_node(
return None;
}
- let name = compute_name(dom, &node);
+ let name = compute_name(dom, &node, context);
let value = compute_value(dom, &node);
let properties = compute_properties(dom, &node);
- let child_ids: Vec<String> = dom
- .children(node_id)
- .iter()
- .filter_map(|child_id| dom_to_ax.get(&child_id.raw()).cloned())
- .collect();
-
// Resolve parentId — walk DOM ancestors until we find one in the AX tree
let parent_id: Option<String> = {
let mut current = node_id;
@@ -144,9 +173,6 @@ fn build_ax_node(
if !properties.is_empty() {
ax_node.as_object_mut().unwrap().insert("properties".into(), json!(properties));
}
- if !child_ids.is_empty() {
- ax_node.as_object_mut().unwrap().insert("childIds".into(), json!(child_ids));
- }
ax_node.as_object_mut().unwrap().insert("backendDOMNodeId".into(), json!(node_id.raw()));
Some(ax_node)
@@ -269,79 +295,299 @@ fn map_role(data: &NodeData) -> &'static str {
}
}
-/// Compute the accessible name for a node.
-fn compute_name(dom: &DomTree, node: &obscura_dom::Node) -> Option<String> {
- if let NodeData::Element { attrs, .. } = &node.data {
- // aria-label takes highest priority
- if let Some(label) = attrs
- .iter()
- .find(|a| a.name.local.as_ref() == "aria-label")
- .map(|a| a.value.clone())
- {
- return Some(label);
- }
+#[derive(Clone, Copy, Default)]
+struct Visibility {
+ subtree_hidden: bool,
+ visibility_hidden: bool,
+}
- // aria-labelledby
- if let Some(labelledby) = attrs
- .iter()
- .find(|a| a.name.local.as_ref() == "aria-labelledby")
- {
- let ids: Vec<&str> = labelledby.value.split_whitespace().collect();
- let mut name = String::new();
- for id_str in ids {
- if let Some(ref_id) = dom.get_element_by_id(id_str) {
- name.push_str(&dom.text_content(ref_id));
- name.push(' ');
- }
+struct NameContext {
+ visibility: HashMap<NodeId, Visibility>,
+ elements_by_id: HashMap<String, NodeId>,
+ labels: HashMap<NodeId, Vec<NodeId>>,
+}
+
+impl NameContext {
+ fn new(dom: &DomTree, ids: &[NodeId], styles: &HashMap<NodeId, AccessibilityStyle>) -> Self {
+ let mut context = Self {
+ visibility: HashMap::new(),
+ elements_by_id: HashMap::new(),
+ labels: HashMap::new(),
+ };
+ for id in ids {
+ let Some(node) = dom.get_node(*id) else {
+ continue;
+ };
+ let parent = node
+ .parent
+ .and_then(|id| context.visibility.get(&id))
+ .copied()
+ .unwrap_or_default();
+ let style = styles.get(id).copied().unwrap_or_default();
+ let hidden = node.get_attribute("hidden").is_some()
+ || node
+ .get_attribute("aria-hidden")
+ .is_some_and(|value| value.eq_ignore_ascii_case("true"))
+ || matches!(
+ tag(&node),
+ "head" | "script" | "style" | "template" | "noscript"
+ )
+ || (tag(&node) == "input" && input_type(&node) == "hidden");
+ context.visibility.insert(
+ *id,
+ Visibility {
+ subtree_hidden: parent.subtree_hidden || hidden || style.display_none,
+ visibility_hidden: style.visibility_hidden.unwrap_or(parent.visibility_hidden),
+ },
+ );
+ if let Some(value) = node.get_attribute("id").filter(|value| !value.is_empty()) {
+ context
+ .elements_by_id
+ .entry(value.to_string())
+ .or_insert(*id);
+ }
+ }
+ for id in ids {
+ let Some(node) = dom.get_node(*id) else {
+ continue;
+ };
+ if tag(&node) != "label" {
+ continue;
}
- let trimmed = name.trim().to_string();
- if !trimmed.is_empty() {
- return Some(trimmed);
+ let control = if let Some(target) = node.get_attribute("for") {
+ context
+ .elements_by_id
+ .get(target)
+ .copied()
+ .filter(|id| dom.get_node(*id).is_some_and(|node| is_labelable(&node)))
+ } else {
+ dom.descendants(*id)
+ .into_iter()
+ .find(|id| dom.get_node(*id).is_some_and(|node| is_labelable(&node)))
+ };
+ if let Some(control) = control {
+ context.labels.entry(control).or_default().push(*id);
}
}
+ context
+ }
- // alt attribute for images
- if let Some(alt) = attrs
- .iter()
- .find(|a| a.name.local.as_ref() == "alt")
- .map(|a| a.value.clone())
- {
- if !alt.is_empty() {
- return Some(alt);
+ fn is_hidden(&self, id: NodeId) -> bool {
+ self.visibility
+ .get(&id)
+ .is_some_and(|state| state.subtree_hidden || state.visibility_hidden)
+ }
+}
+
+fn tag(node: &obscura_dom::Node) -> &str {
+ node.as_element()
+ .map(|name| name.local.as_ref())
+ .unwrap_or("")
+}
+
+fn input_type(node: &obscura_dom::Node) -> String {
+ node.get_attribute("type")
+ .unwrap_or("text")
+ .to_ascii_lowercase()
+}
+
+fn is_labelable(node: &obscura_dom::Node) -> bool {
+ matches!(
+ tag(node),
+ "button" | "meter" | "output" | "progress" | "select" | "textarea"
+ ) || (tag(node) == "input" && input_type(node) != "hidden")
+}
+
+fn is_nested_control(node: &obscura_dom::Node) -> bool {
+ matches!(
+ tag(node),
+ "input" | "textarea" | "select" | "button" | "summary" | "iframe"
+ ) || contenteditable_keyword(node) == Some(true)
+ || matches!(
+ map_role(&node.data),
+ "button"
+ | "textbox"
+ | "searchbox"
+ | "checkbox"
+ | "radio"
+ | "listbox"
+ | "combobox"
+ | "slider"
+ | "spinbutton"
+ | "menuitem"
+ | "tab"
+ )
+}
+
+fn flat_string(value: &str) -> String {
+ value.split_whitespace().collect::<Vec<_>>().join(" ")
+}
+
+fn nonempty(value: &str) -> Option<String> {
+ let value = flat_string(value);
+ (!value.is_empty()).then_some(value)
+}
+
+fn text_alternative(node: &obscura_dom::Node) -> Option<String> {
+ if let Some(label) = node.get_attribute("aria-label").and_then(nonempty) {
+ return Some(label);
+ }
+ if tag(node) == "img" || (tag(node) == "input" && input_type(node) == "image") {
+ return node.get_attribute("alt").map(flat_string);
+ }
+ if tag(node) == "input" {
+ match input_type(node).as_str() {
+ "button" => return node.get_attribute("value").and_then(nonempty),
+ "submit" => {
+ return Some(
+ node.get_attribute("value")
+ .map(flat_string)
+ .unwrap_or_else(|| "Submit".into()),
+ )
}
+ "reset" => {
+ return Some(
+ node.get_attribute("value")
+ .map(flat_string)
+ .unwrap_or_else(|| "Reset".into()),
+ )
+ }
+ _ => {}
}
+ }
+ None
+}
- // title attribute
- if let Some(title) = attrs
- .iter()
- .find(|a| a.name.local.as_ref() == "title")
- .map(|a| a.value.clone())
+fn content_name(
+ dom: &DomTree,
+ root: NodeId,
+ context: &NameContext,
+ include_hidden: bool,
+) -> String {
+ let mut text = String::new();
+ let mut pending = vec![(root, false)];
+ while let Some((id, end_block)) = pending.pop() {
+ if end_block {
+ text.push(' ');
+ continue;
+ }
+ let Some(node) = dom.get_node(id) else {
+ continue;
+ };
+ if !include_hidden
+ && context
+ .visibility
+ .get(&id)
+ .is_some_and(|state| state.subtree_hidden)
{
- if !title.is_empty() {
- return Some(title);
+ continue;
+ }
+ if matches!(tag(&node), "script" | "style" | "template" | "noscript") {
+ continue;
+ }
+ if id != root && is_nested_control(&node) {
+ continue;
+ }
+ let hidden = !include_hidden && context.is_hidden(id);
+ if let NodeData::Text { contents } = &node.data {
+ if !hidden {
+ text.push_str(contents);
}
+ continue;
}
-
- // placeholder
- if let Some(placeholder) = attrs
- .iter()
- .find(|a| a.name.local.as_ref() == "placeholder")
- .map(|a| a.value.clone())
- {
- if !placeholder.is_empty() {
- return Some(placeholder);
+ if !hidden {
+ if let Some(alternative) = text_alternative(&node) {
+ text.push(' ');
+ text.push_str(&alternative);
+ text.push(' ');
+ continue;
}
}
+ let block = matches!(
+ tag(&node),
+ "br" | "p" | "div" | "li" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6"
+ );
+ if block {
+ text.push(' ');
+ pending.push((id, true));
+ }
+ pending.extend(dom.children(id).into_iter().rev().map(|id| (id, false)));
}
+ flat_string(&text)
+}
- // For text nodes, the name is the text content
+/// Compute the accessible name for a node.
+fn compute_name(dom: &DomTree, node: &obscura_dom::Node, context: &NameContext) -> Option<String> {
if let NodeData::Text { contents } = &node.data {
- let trimmed = contents.trim().to_string();
- if !trimmed.is_empty() {
- return Some(trimmed);
+ return nonempty(contents);
+ }
+ if let Some(labelledby) = node.get_attribute("aria-labelledby") {
+ let mut seen = HashSet::new();
+ let references: Vec<_> = labelledby
+ .split_whitespace()
+ .filter_map(|id| context.elements_by_id.get(id).copied())
+ .filter(|id| seen.insert(*id))
+ .collect();
+ if !references.is_empty() {
+ let parts: Vec<_> = references
+ .into_iter()
+ .map(|id| {
+ if let Some(alternative) =
+ dom.get_node(id).and_then(|node| text_alternative(&node))
+ {
+ return alternative;
+ }
+ let text = content_name(dom, id, context, context.is_hidden(id));
+ if !text.is_empty() {
+ return text;
+ }
+ dom.get_node(id)
+ .and_then(|node| node.get_attribute("title").and_then(nonempty))
+ .unwrap_or_default()
+ })
+ .collect();
+ return Some(flat_string(&parts.join(" ")));
}
}
-
+ if let Some(label) = node.get_attribute("aria-label").and_then(nonempty) {
+ return Some(label);
+ }
+ if let Some(labels) = context.labels.get(&node.id) {
+ let parts: Vec<_> = labels
+ .iter()
+ .map(|id| content_name(dom, *id, context, context.is_hidden(*id)))
+ .collect();
+ if let Some(name) = nonempty(&parts.join(" ")) {
+ return Some(name);
+ }
+ }
+ if let Some(alternative) = text_alternative(node) {
+ return Some(alternative);
+ }
+ if matches!(
+ map_role(&node.data),
+ "button"
+ | "link"
+ | "heading"
+ | "checkbox"
+ | "radio"
+ | "menuitem"
+ | "tab"
+ | "cell"
+ | "row"
+ | "LabelText"
+ | "StaticText"
+ ) {
+ if let Some(name) = nonempty(&content_name(dom, node.id, context, false)) {
+ return Some(name);
+ }
+ }
+ if let Some(title) = node.get_attribute("title").and_then(nonempty) {
+ return Some(title);
+ }
+ if matches!(tag(node), "input" | "textarea") {
+ return node.get_attribute("placeholder").and_then(nonempty);
+ }
None
}
@@ -496,6 +742,174 @@ mod tests {
);
}
+ fn assert_name(dom: &DomTree, id: &str, expected: Option<&str>) {
+ let nodes = build_ax_nodes(dom);
+ let node = ax_node_for(&nodes, dom.get_element_by_id(id).unwrap());
+ assert_eq!(
+ node.get("name").and_then(|name| name["value"].as_str()),
+ expected,
+ "#{id}"
+ );
+ }
+
+ #[test]
+ fn names_from_content_preserve_inline_words_and_normalize_whitespace() {
+ let dom = parse_html(
+ r#"<button id="button" title="tooltip"> Save <b>changes</b> now </button>
+ <a id="link" href="/">Read <span>the <em>docs</em></span></a>
+ <h2 id="heading">Access<b>ibility</b> test</h2>
+ <div id="generic">Do not duplicate all descendant text</div>"#,
+ );
+ assert_name(&dom, "button", Some("Save changes now"));
+ assert_name(&dom, "link", Some("Read the docs"));
+ assert_name(&dom, "heading", Some("Accessibility test"));
+ assert_name(&dom, "generic", None);
+ }
+
+ #[test]
+ fn html_labels_follow_control_association_and_document_order() {
+ let dom = parse_html(
+ r#"<label for="email"> Work <b>email</b> </label>
+ <input id="email" placeholder="fallback"><label for="email">address</label>
+ <label>Accept <span>terms</span><input id="accept" type="checkbox"></label>
+ <label for="email">Elsewhere<input id="wrong"></label>
+ <label>First<input id="first"><input id="second"></label>
+ <label for="missing">No association<input id="missing-for"></label>"#,
+ );
+ assert_name(&dom, "email", Some("Work email address Elsewhere"));
+ assert_name(&dom, "accept", Some("Accept terms"));
+ assert_name(&dom, "wrong", None);
+ assert_name(&dom, "first", Some("First"));
+ assert_name(&dom, "second", None);
+ assert_name(&dom, "missing-for", None);
+ }
+
+ #[test]
+ fn aria_labelledby_precedes_aria_label_and_native_names() {
+ let dom = parse_html(
+ r#"<span id="one">First</span><span id="two">Second</span>
+ <label for="target">Native</label>
+ <button id="target" aria-labelledby="two missing one two" aria-label="ARIA">Content</button>
+ <button id="invalid" aria-labelledby="missing" aria-label=" ARIA fallback ">Content</button>
+ <button id="blank" aria-label=" ">Content</button>
+ <span id="empty"></span><button id="empty-ref" aria-labelledby="empty" aria-label="fallback">Content</button>
+ <button id="self" aria-labelledby="self one" aria-label="Self">Content</button>
+ <span id="cycle-a" aria-labelledby="cycle-b">A</span><span id="cycle-b" aria-labelledby="cycle-a">B</span>
+ <button id="cycle" aria-labelledby="cycle-a">Content</button>"#,
+ );
+ assert_name(&dom, "target", Some("Second First"));
+ assert_name(&dom, "invalid", Some("ARIA fallback"));
+ assert_name(&dom, "blank", Some("Content"));
+ assert_name(&dom, "empty-ref", Some(""));
+ assert_name(&dom, "self", Some("Self First"));
+ assert_name(&dom, "cycle", Some("A"));
+ }
+
+ #[test]
+ fn hidden_references_name_controls_without_exposing_hidden_nodes() {
+ let dom = parse_html(
+ r#"<span id="hidden" hidden>Hidden <span hidden>reference</span></span>
+ <span id="visible">Visible <span hidden>noise</span></span>
+ <button id="referenced" aria-labelledby="hidden visible">Content</button>
+ <label hidden for="native">Hidden native</label><input id="native">
+ <button id="content">Shown<span aria-hidden="true"> noise</span><script>noise</script></button>"#,
+ );
+ assert_name(&dom, "referenced", Some("Hidden reference Visible"));
+ assert_name(&dom, "native", Some("Hidden native"));
+ assert_name(&dom, "content", Some("Shown"));
+ let nodes = build_ax_nodes(&dom);
+ let hidden = dom.get_element_by_id("hidden").unwrap();
+ assert!(!nodes
+ .iter()
+ .any(|node| node["backendDOMNodeId"] == hidden.raw()));
+ }
+
+ #[test]
+ fn aria_references_preserve_link_text() {
+ let dom = parse_html(
+ r#"<span id="label">Read <a href="/terms">terms</a><a href="/secret" hidden> secret</a></span>
+ <button id="native-link" aria-labelledby="label" aria-label="Fallback">Continue</button>
+ <span id="role-label">Review <span role="link">privacy</span></span>
+ <button id="role-link" aria-labelledby="role-label">Continue</button>
+ <span id="hidden-label" hidden>Read <a href="/terms" hidden>hidden terms</a></span>
+ <button id="hidden-link" aria-labelledby="hidden-label">Continue</button>"#,
+ );
+ assert_name(&dom, "native-link", Some("Read terms"));
+ assert_name(&dom, "role-link", Some("Review privacy"));
+ assert_name(&dom, "hidden-link", Some("Read hidden terms"));
+ }
+
+ #[test]
+ fn native_labels_preserve_link_text() {
+ let dom = parse_html(
+ r#"<label for="explicit">Read <a href="/terms">terms</a> and <span role="link">privacy</span></label>
+ <input id="explicit">
+ <label>Accept <a href="/terms">terms</a> and <span role="link">privacy</span><input id="implicit" type="checkbox"></label>
+ <label for="hidden" hidden>Read <a href="/terms" hidden>hidden terms</a></label><input id="hidden">"#,
+ );
+ assert_name(&dom, "explicit", Some("Read terms and privacy"));
+ assert_name(&dom, "implicit", Some("Accept terms and privacy"));
+ assert_name(&dom, "hidden", Some("Read hidden terms"));
+ }
+
+ #[test]
+ fn nested_buttons_remain_excluded_from_content_names() {
+ let dom = parse_html(
+ r#"<button id="outer">Outer</button><button id="inner" role="link">Nested action</button>"#,
+ );
+ let outer = dom.get_element_by_id("outer").unwrap();
+ let inner = dom.get_element_by_id("inner").unwrap();
+ dom.append_child(outer, inner);
+ assert_eq!(dom.get_node(inner).unwrap().parent, Some(outer));
+ assert_name(&dom, "outer", Some("Outer"));
+ assert_name(&dom, "inner", Some("Nested action"));
+ }
+
+ #[test]
+ fn nested_controls_do_not_pollute_labels_or_content_names() {
+ let dom = parse_html(
+ r#"<label for="field">Account <a href="/">Help</a><select><option>Noise</option></select><textarea>Noise</textarea><span role="textbox">Noise</span><span contenteditable="true">Noise</span><a href="/" role="button">Noise</a><button role="link">Noise</button></label>
+ <input id="field"><div role="button" id="button">Open <span role="button">Nested action</span><img alt="folder"></div>"#,
+ );
+ assert_name(&dom, "field", Some("Account Help"));
+ assert_name(&dom, "button", Some("Open folder"));
+ }
+
+ #[test]
+ fn html_and_aria_hidden_subtrees_are_absent() {
+ let dom = parse_html(
+ r#"<div hidden="false"><button id="html">Hidden</button></div>
+ <div aria-hidden="TrUe"><button id="aria" aria-hidden="false">Hidden</button></div>
+ <input id="input" type="hidden"><button id="visible" aria-hidden="false">Visible</button>"#,
+ );
+ let nodes = build_ax_nodes(&dom);
+ for id in ["html", "aria", "input"] {
+ let id = dom.get_element_by_id(id).unwrap();
+ assert!(!nodes
+ .iter()
+ .any(|node| node["backendDOMNodeId"] == id.raw()));
+ }
+ assert_name(&dom, "visible", Some("Visible"));
+ }
+
+ #[test]
+ fn native_alternatives_and_aria_override_content() {
+ let dom = parse_html(
+ r#"<button id="aria" aria-label="Named">Content</button>
+ <button id="image"><img alt="Download"></button>
+ <img id="decorative" alt="" title="Not a name">
+ <input id="submit" type="submit" value="Send">
+ <input id="password" type="password" value="secret" placeholder="Password">
+ <label for="select">Choice</label><select id="select"><option>Not the name</option></select>"#,
+ );
+ assert_name(&dom, "aria", Some("Named"));
+ assert_name(&dom, "image", Some("Download"));
+ assert_name(&dom, "decorative", Some(""));
+ assert_name(&dom, "submit", Some("Send"));
+ assert_name(&dom, "password", Some("Password"));
+ assert_name(&dom, "select", Some("Choice"));
+ }
+
#[test]
fn content_editing_hosts_expose_ax_values_without_duplicating_descendants() {
let dom = parse_html(
diff --git a/crates/obscura-cdp/tests/accessibility_names.rs b/crates/obscura-cdp/tests/accessibility_names.rs
new file mode 100644
index 00000000..f4a7a210
--- /dev/null
+++ b/crates/obscura-cdp/tests/accessibility_names.rs
@@ -0,0 +1,169 @@
+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("ax-session".to_string()),
+ },
+ ctx,
+ )
+ .await;
+ assert!(response.error.is_none(), "{method}: {:?}", response.error);
+ response.result.unwrap()
+}
+
+async fn setup(html: &str) -> CdpContext {
+ let mut ctx = CdpContext::new();
+ let page = ctx.create_page();
+ ctx.sessions.insert("ax-session".to_string(), page);
+ cdp(&mut ctx, "Page.navigate", json!({"url": "about:blank"})).await;
+ cdp(&mut ctx, "Runtime.evaluate", json!({
+ "expression": format!("document.body.innerHTML = {}", serde_json::to_string(html).unwrap()),
+ "returnByValue": true
+ })).await;
+ ctx
+}
+
+#[tokio::test(flavor = "current_thread")]
+async fn accessibility_names_use_live_dom_and_backend_ids() {
+ let mut ctx = setup(
+ r#"<label for="field">Email</label><input id="field">
+ <button id="save">Save <b>changes</b></button><a href="/">Read docs</a>"#,
+ )
+ .await;
+ let tree = cdp(&mut ctx, "Accessibility.getFullAXTree", json!({})).await;
+ let nodes = tree["nodes"].as_array().unwrap();
+ for (role, name) in [
+ ("textbox", "Email"),
+ ("button", "Save changes"),
+ ("link", "Read docs"),
+ ] {
+ assert!(
+ nodes
+ .iter()
+ .any(|node| node["role"]["value"] == role && node["name"]["value"] == name),
+ "{tree}"
+ );
+ }
+ let button = nodes
+ .iter()
+ .find(|node| node["role"]["value"] == "button")
+ .unwrap();
+ let resolved = cdp(
+ &mut ctx,
+ "DOM.resolveNode",
+ json!({"backendNodeId": button["backendDOMNodeId"]}),
+ )
+ .await;
+ assert!(resolved["object"]["objectId"].is_string(), "{resolved}");
+ cdp(
+ &mut ctx,
+ "Runtime.evaluate",
+ json!({"expression": "document.getElementById('save').textContent = 'Updated'"}),
+ )
+ .await;
+ let tree = cdp(&mut ctx, "Accessibility.getFullAXTree", json!({})).await;
+ assert!(tree["nodes"]
+ .as_array()
+ .unwrap()
+ .iter()
+ .any(|node| node["role"]["value"] == "button" && node["name"]["value"] == "Updated"));
+}
+
+#[tokio::test(flavor = "current_thread")]
+async fn accessibility_labels_preserve_link_text() {
+ let mut ctx = setup(
+ r#"<span id="label">Read <a href="/terms">terms</a></span>
+ <button aria-labelledby="label" aria-label="Fallback">Continue</button>
+ <label for="field">Accept <a href="/terms">terms</a> and <span role="link">privacy</span><select><option>Noise</option></select><span role="button">Nested action</span></label>
+ <input id="field">"#,
+ )
+ .await;
+ let tree = cdp(&mut ctx, "Accessibility.getFullAXTree", json!({})).await;
+ let nodes = tree["nodes"].as_array().unwrap();
+ for (role, name) in [
+ ("button", "Read terms"),
+ ("textbox", "Accept terms and privacy"),
+ ] {
+ assert!(
+ nodes
+ .iter()
+ .any(|node| node["role"]["value"] == role && node["name"]["value"] == name),
+ "{tree}"
+ );
+ }
+}
+
+#[cfg(feature = "render")]
+#[tokio::test(flavor = "current_thread")]
+async fn accessibility_names_and_visibility_use_the_live_cascade() {
+ let mut ctx = setup(r#"<style>
+ .gone { display: none !important }
+ .invisible { visibility: hidden }
+ .restored { visibility: visible }
+ </style>
+ <div class="gone" style="display:block"><button>Display hidden</button></div>
+ <div class="invisible">Hidden text<button>Inherited hidden</button><button class="restored">Restored</button></div>
+ <button style="opacity:0">Transparent</button>
+ <button style="position:absolute;left:-10000px">Offscreen</button>
+ <span id="label" class="gone">Hidden <span hidden>reference</span></span>
+ <button aria-labelledby="label">Fallback</button>
+ <label class="gone" for="field">Hidden native <span hidden>label</span></label><input id="field">
+ <button id="dynamic">Before<span class="gone"> noise</span></button>"#).await;
+ let tree = cdp(&mut ctx, "Accessibility.getFullAXTree", json!({})).await;
+ let nodes = tree["nodes"].as_array().unwrap();
+ let names: Vec<_> = nodes
+ .iter()
+ .filter(|node| node["role"]["value"] == "button")
+ .map(|node| node["name"]["value"].as_str().unwrap_or(""))
+ .collect();
+ assert_eq!(
+ names,
+ [
+ "Restored",
+ "Transparent",
+ "Offscreen",
+ "Hidden reference",
+ "Before"
+ ]
+ );
+ assert!(nodes
+ .iter()
+ .any(|node| node["role"]["value"] == "textbox"
+ && node["name"]["value"] == "Hidden native label"));
+ for node in nodes {
+ if let Some(parent) = node["parentId"].as_str() {
+ let parent = nodes.iter().find(|node| node["nodeId"] == parent).unwrap();
+ assert!(
+ parent["childIds"]
+ .as_array()
+ .unwrap()
+ .contains(&node["nodeId"]),
+ "{tree}"
+ );
+ }
+ if let Some(children) = node["childIds"].as_array() {
+ for child in children {
+ let child = nodes.iter().find(|node| node["nodeId"] == *child).unwrap();
+ assert_eq!(child["parentId"], node["nodeId"]);
+ }
+ }
+ }
+ cdp(
+ &mut ctx,
+ "Runtime.evaluate",
+ json!({"expression": "document.getElementById('dynamic').className = 'gone'"}),
+ )
+ .await;
+ let tree = cdp(&mut ctx, "Accessibility.getFullAXTree", json!({})).await;
+ assert!(!tree["nodes"]
+ .as_array()
+ .unwrap()
+ .iter()
+ .any(|node| node["name"]["value"] == "Before"));
+}
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index d9d7f2b9..19afbaa0 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -141,6 +141,12 @@ fn with_sync_render_loading_disabled<R>(
}
}
+#[derive(Clone, Copy, Debug, Default)]
+pub struct AccessibilityStyle {
+ pub display_none: bool,
+ pub visibility_hidden: Option<bool>,
+}
+
#[derive(Debug, Clone)]
pub struct RemoteObjectInfo {
/// True when this object is a value that was thrown or that a promise
@@ -3831,6 +3837,29 @@ impl ObscuraJsRuntime {
state.dom.as_ref().map(f)
}
+ #[cfg(feature = "render")]
+ pub fn accessibility_styles(&self) -> HashMap<NodeId, AccessibilityStyle> {
+ let mut state = self.state.borrow_mut();
+ crate::ops::sample_live_document_animations(&mut state);
+ let Some(prepared) = crate::ops::ensure_prepared_render(&mut state) else {
+ return HashMap::new();
+ };
+ prepared
+ .layout()
+ .styles
+ .iter()
+ .map(|(id, style)| {
+ (
+ *id,
+ AccessibilityStyle {
+ display_none: style.display == obscura_render::Display::None,
+ visibility_hidden: style.visibility_hidden,
+ },
+ )
+ })
+ .collect()
+ }
+
/// Absolute URLs the page requested via fetch()/XHR, in request order
/// (issue #301). Backs `--dump assets`.
pub fn fetched_urls(&self) -> Vec<String> {