mirror of
https://github.com/lexmount/moli.git
synced 2026-11-18 16:08:37 +00:00
fix(ax): derive link URLs from computed roles
Share Chromium's link-role classification between AX nodes, accessible names, URL selection and semantic text output, including the four document reference roles. Select addresses by computed role and native URL capability, preserving image resource URLs without treating ARIA image links as navigation URLs. Handle image buttons, empty href and SVG href/xlink:href consistently. Cover the reported scripted image link and role overrides with a real HTML fixture that checks both AX JSON and exact semantic text lines. The same 20 cases match Chromium 156.
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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<Url> {
|
||||
// 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<Url> {
|
||||
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())
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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]"
|
||||
);
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Value> =
|
||||
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::<Vec<_>>();
|
||||
let target = target.map(|target| origin.join(target)).transpose()?;
|
||||
assert_eq!(
|
||||
urls,
|
||||
target
|
||||
.as_ref()
|
||||
.map(|url| url.as_str())
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>(),
|
||||
"{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(
|
||||
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>AX link URLs</title>
|
||||
<base href="/assets/">
|
||||
</head>
|
||||
<body>
|
||||
<a href="guide#top">Docs</a>
|
||||
<a role="doc-noteref" href="#note">Footnote</a>
|
||||
<a role="doc-backlink" href="../article#back">Backlink</a>
|
||||
<a role="doc-biblioref" href="https://references.test/book">Bibliography</a>
|
||||
<a role="doc-glossref" href="/glossary#term">Glossary</a>
|
||||
<a role="button" href="/ignored">Anchor button</a>
|
||||
<a role="image" aria-label="Anchor image" href="/ignored">Artwork</a>
|
||||
<a href="">Empty href</a>
|
||||
<img role="link" alt="Help" tabindex="0" src="/help.svg" onclick="location.href='/docs'">
|
||||
<img role="link" alt="Image href" tabindex="0" href="/ignored" src="/help.svg">
|
||||
<img role="doc-noteref" alt="Image footnote" src="/help.svg">
|
||||
<img alt="Photo" src="photo.svg">
|
||||
<img role="button" alt="Picture button" src="photo.svg">
|
||||
<img role="doc-cover" alt="Cover" src="cover.svg">
|
||||
<img role="graphics-symbol" alt="Symbol" src="symbol.svg">
|
||||
<input type="image" alt="Submit" src="submit.svg">
|
||||
<input type="image" role="link" alt="Input link" src="submit.svg">
|
||||
<span role="link" tabindex="0" href="/ignored" onclick="location.href='/docs'">Script only</span>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="60">
|
||||
<a href="vector#top" aria-label="SVG link"><text x="0" y="20">Vector</text></a>
|
||||
<a xlink:href="legacy#top" aria-label="SVG legacy link"><text x="0" y="40">Legacy</text></a>
|
||||
</svg>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user