diff --git a/moli-dom/src/accessibility/ax_node.rs b/moli-dom/src/accessibility/ax_node.rs index 78a64b697eb..6086c809fe4 100644 --- a/moli-dom/src/accessibility/ax_node.rs +++ b/moli-dom/src/accessibility/ax_node.rs @@ -58,6 +58,11 @@ struct AccessibilityNodeRareData { } impl AccessibilityNode { + /// Whether the computed AX role is a link, including document references. + pub fn is_link(&self) -> bool { + super::ax_roles::is_link(self.role) + } + /// An ordinary AX node includes an empty properties array even when it /// needs no rare allocation. Synthetic nodes can omit it via set_rare_data. pub fn new(node_id: AccessibilityNodeId, role: &'static str) -> Self { @@ -238,6 +243,35 @@ mod tests { NonZeroU32::new(value).expect("nonzero fixture ID") } + #[test] + fn is_link_includes_document_reference_roles() { + for role in [ + "link", + "doc-backlink", + "doc-biblioref", + "doc-glossref", + "doc-noteref", + ] { + assert!( + AccessibilityNode::new(AccessibilityNodeId::Dom(id(1)), role).is_link(), + "{role}" + ); + } + for role in [ + "button", + "image", + "doc-cover", + "doc-footnote", + "doc-biblioentry", + "generic", + ] { + assert!( + !AccessibilityNode::new(AccessibilityNodeId::Dom(id(1)), role).is_link(), + "{role}" + ); + } + } + #[test] #[cfg(target_pointer_width = "64")] fn common_ax_node_layout_stays_compact() { diff --git a/moli-dom/src/accessibility/ax_properties.rs b/moli-dom/src/accessibility/ax_properties.rs index dfb57fe154d..d103dc19d32 100644 --- a/moli-dom/src/accessibility/ax_properties.rs +++ b/moli-dom/src/accessibility/ax_properties.rs @@ -9,7 +9,7 @@ use url::Url; use super::ax_dom::ax_child_ids; use super::ax_projection::{AxTreeProjection, AxUnavailable}; -use super::ax_roles::{ax_role, heading_level}; +use super::ax_roles::{ax_role, heading_level, is_image, is_link}; const MAX_AX_NAME_VISITED_OBJECTS: usize = 100; @@ -434,30 +434,32 @@ fn ax_is_text_control(element: &Element) -> bool { } fn ax_name_comes_from_contents(document: &NativeDom, node: &Node, element: &Element) -> bool { - if ax_role(node) == "row" { + let role = ax_role(node); + if role == "row" { return ax_row_name_comes_from_contents(document, node); } - matches!( - ax_role(node), - "button" - | "cell" - | "checkbox" - | "columnheader" - | "gridcell" - | "heading" - | "link" - | "menuitem" - | "menuitemcheckbox" - | "menuitemradio" - | "option" - | "radio" - | "rowheader" - | "switch" - | "tab" - | "tooltip" - | "treeitem" - ) || matches!(element.local_name(), "caption" | "figcaption" | "legend") + is_link(role) + || matches!( + role, + "button" + | "cell" + | "checkbox" + | "columnheader" + | "gridcell" + | "heading" + | "menuitem" + | "menuitemcheckbox" + | "menuitemradio" + | "option" + | "radio" + | "rowheader" + | "switch" + | "tab" + | "tooltip" + | "treeitem" + ) + || matches!(element.local_name(), "caption" | "figcaption" | "legend") } fn ax_row_name_comes_from_contents(document: &NativeDom, node: &Node) -> bool { @@ -590,7 +592,8 @@ pub(super) fn ax_properties(document: &NativeDom, node_id: NodeId, node: &Node) properties.push(ax_bool_property("focused", "booleanOrUndefined", true)); } NodeData::Element(element) => { - if ax_role(node) == "status" { + let role = ax_role(node); + if role == "status" { let live = element .attribute("aria-live") .map(str::trim) @@ -629,20 +632,11 @@ pub(super) fn ax_properties(document: &NativeDom, node_id: NodeId, node: &Node) use super::ax_roles::listitem_level; properties.push(ax_integer_property("level", listitem_level(document, node))); } - if element.is_html_element("a") - && let Some(url) = element - .attribute("href") - .and_then(|href| ax_resolve_element_url(document, href)) - { + if let Some(url) = ax_element_url(document, element, role) { properties.push(ax_string_property("url", url.as_str())); - properties.push(ax_bool_property("focusable", "booleanOrUndefined", true)); } - if element.is_html_element("img") - && let Some(url) = element - .attribute("src") - .and_then(|src| ax_resolve_element_url(document, src)) - { - properties.push(ax_string_property("url", url.as_str())); + if element.is_html_element("a") && element.has_attribute("href") { + properties.push(ax_bool_property("focusable", "booleanOrUndefined", true)); } if element.is_html_element("button") { let disabled = ax_control_is_disabled(document, node_id, element); @@ -975,11 +969,42 @@ pub(super) fn ax_integer_property(name: &str, value: usize) -> Value { }) } +fn ax_element_url(document: &NativeDom, element: &Element, role: &str) -> Option { + // Follow Blink's AXNodeObject::Url: the computed role selects the kind of + // URL, and the native element must provide that kind of address. + if is_link(role) { + let href = if element.is_html_element("a") + || element.is_html_element("area") + || element.is_html_element("link") + || (element.namespace() == "http://www.w3.org/1998/Math/MathML" + && element.local_name() == "a") + { + element.attribute("href") + } else if element.is_svg_element("a") { + element + .attribute("href") + .or_else(|| element.attribute_ns("http://www.w3.org/1999/xlink", "href")) + } else { + None + }; + return href.and_then(|href| ax_resolve_element_url(document, href)); + } + if is_image(role) && element.is_html_element("img") { + return element + .attribute("src") + .filter(|src| !src.trim().is_empty()) + .and_then(|src| ax_resolve_element_url(document, src)); + } + if role == "button" && element.is_html_input() && element.input_type() == InputType::Image { + return element + .attribute("src") + .and_then(|src| ax_resolve_element_url(document, src)); + } + None +} + fn ax_resolve_element_url(document: &NativeDom, value: &str) -> Option { let value = value.trim(); - if value.is_empty() { - return None; - } let base = document.document().map(|doc| doc.base_url().clone()); base.as_ref() .and_then(|base| Url::options().base_url(Some(base)).parse(value).ok()) diff --git a/moli-dom/src/accessibility/ax_roles.rs b/moli-dom/src/accessibility/ax_roles.rs index f4e22928ceb..ed1c7955442 100644 --- a/moli-dom/src/accessibility/ax_roles.rs +++ b/moli-dom/src/accessibility/ax_roles.rs @@ -3,6 +3,19 @@ use crate::{ native::{Element, NativeDom, Node}, }; +/// Chromium's ui::IsLink classification for computed AX roles. +pub(super) fn is_link(role: &str) -> bool { + matches!( + role, + "link" | "doc-backlink" | "doc-biblioref" | "doc-glossref" | "doc-noteref" + ) +} + +/// Blink's AXObject::IsImage excludes canvas from ui::IsImage. +pub(super) fn is_image(role: &str) -> bool { + matches!(role, "image" | "doc-cover" | "graphics-symbol" | "SvgRoot") +} + pub(super) fn ax_role(node: &Node) -> &'static str { match node.kind() { NodeData::Document(_) => "RootWebArea", diff --git a/moli/src/fetch_dump/semantic.rs b/moli/src/fetch_dump/semantic.rs index 837f14c91fd..05ce97aab88 100644 --- a/moli/src/fetch_dump/semantic.rs +++ b/moli/src/fetch_dump/semantic.rs @@ -101,7 +101,7 @@ fn render_payloads_text(payloads: &[AccessibilityNode]) -> String { out.push_str(" = "); out.push_str(value); } - if node.role == "link" + if node.is_link() && let Some(url) = node .properties() .into_iter() @@ -238,22 +238,32 @@ mod tests { #[test] fn text_shows_the_url_of_links_only() { - let url = || { - Some(vec![ - serde_json::json!({"name":"url", "value":{"type":"string", "value":"https://example.com/a"}}), - ]) - }; - let mut link = node(2, "link", Some(1), &[]); - link.name = Some("A".to_owned()); - link.set_rare_data(None, None, url()); - let mut image = node(3, "image", Some(1), &[]); - image.set_rare_data(None, None, url()); - let text = render_payloads_text(&[node(1, "RootWebArea", None, &[2, 3]), link, image]); - assert_eq!( - text, - "- RootWebArea [backendNodeId=1]\n \ - - link: A → https://example.com/a [backendNodeId=2]\n \ + for role in [ + "link", + "doc-backlink", + "doc-biblioref", + "doc-glossref", + "doc-noteref", + ] { + let url = || { + Some(vec![ + serde_json::json!({"name":"url", "value":{"type":"string", "value":"https://example.com/a"}}), + ]) + }; + let mut link = node(2, role, Some(1), &[]); + link.name = Some("A".to_owned()); + link.set_rare_data(None, None, url()); + let mut image = node(3, "image", Some(1), &[]); + image.set_rare_data(None, None, url()); + let text = render_payloads_text(&[node(1, "RootWebArea", None, &[2, 3]), link, image]); + assert_eq!( + text, + format!( + "- RootWebArea [backendNodeId=1]\n \ + - {role}: A → https://example.com/a [backendNodeId=2]\n \ - image [backendNodeId=3]" - ); + ) + ); + } } } diff --git a/moli/src/fetch_dump/tests.rs b/moli/src/fetch_dump/tests.rs index 9bbe95e8872..ab0b5861cd2 100644 --- a/moli/src/fetch_dump/tests.rs +++ b/moli/src/fetch_dump/tests.rs @@ -350,6 +350,84 @@ async fn semantic_dump_preserves_protocol_json_bytes_and_text_fields() -> Result Ok(()) } +#[tokio::test] +async fn semantic_link_urls_follow_computed_roles() -> Result<()> { + let (_browser, mut page, http_server) = + load_page(include_str!("../../tests/fixtures/semantic-link-urls.html")).await?; + let nodes: Vec = + serde_json::from_str(&super::semantic::render_json(&mut page, false).await?)?; + let text = super::semantic::render_text(&mut page, false).await?; + let origin = page.final_url().clone(); + for (name, role, target, show_target) in [ + ("Docs", "link", Some("/assets/guide#top"), true), + ("Footnote", "doc-noteref", Some("/assets/#note"), true), + ("Backlink", "doc-backlink", Some("/article#back"), true), + ( + "Bibliography", + "doc-biblioref", + Some("https://references.test/book"), + true, + ), + ("Glossary", "doc-glossref", Some("/glossary#term"), true), + ("Anchor button", "button", None, false), + ("Anchor image", "image", None, false), + ("Empty href", "link", Some("/assets/"), true), + ("Help", "link", None, false), + ("Image href", "link", None, false), + ("Image footnote", "doc-noteref", None, false), + ("Photo", "image", Some("/assets/photo.svg"), false), + ("Picture button", "button", None, false), + ("Cover", "doc-cover", Some("/assets/cover.svg"), false), + ( + "Symbol", + "graphics-symbol", + Some("/assets/symbol.svg"), + false, + ), + ("Submit", "button", Some("/assets/submit.svg"), false), + ("Input link", "link", None, false), + ("Script only", "link", None, false), + ("SVG link", "link", Some("/assets/vector#top"), true), + ("SVG legacy link", "link", Some("/assets/legacy#top"), true), + ] { + let node = nodes + .iter() + .find(|node| node["name"]["value"] == name && node["role"]["value"] == role) + .unwrap_or_else(|| panic!("missing {role}: {name}: {nodes:?}")); + let properties = node["properties"].as_array().expect("AX properties"); + let urls = properties + .iter() + .filter(|property| property["name"] == "url") + .map(|property| property["value"]["value"].as_str().expect("AX URL")) + .collect::>(); + let target = target.map(|target| origin.join(target)).transpose()?; + assert_eq!( + urls, + target + .as_ref() + .map(|url| url.as_str()) + .into_iter() + .collect::>(), + "{role}: {name} URL" + ); + let arrow = if show_target { + format!(" → {}", target.as_ref().expect("link target")) + } else { + String::new() + }; + let expected = format!( + "- {role}: {name}{arrow} [backendNodeId={}]", + node["backendDOMNodeId"] + ); + assert!( + text.lines().any(|line| line.trim_start() == expected), + "missing {expected:?}: {text}" + ); + } + http_server.abort(); + Ok(()) +} + #[tokio::test] async fn render_semantic_tree_dumps_include_child_frames_only_when_requested() -> Result<()> { let (_browser, mut page, http_server) = load_page( diff --git a/moli/tests/fixtures/semantic-link-urls.html b/moli/tests/fixtures/semantic-link-urls.html new file mode 100644 index 00000000000..c325777609f --- /dev/null +++ b/moli/tests/fixtures/semantic-link-urls.html @@ -0,0 +1,31 @@ + + + + AX link URLs + + + + Docs + Footnote + Backlink + Bibliography + Glossary + Anchor button + Artwork + Empty href + Help + Image href + Image footnote + Photo + Picture button + Cover + Symbol + + + Script only + + Vector + Legacy + + +