Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ smol_str = "0.3"
bitflags = "2.8.0"
bytemuck = "1"
rayon = "1"
test-that = "0.5.2"
thread_local = "1"

[profile.profile]
Expand Down
31 changes: 31 additions & 0 deletions packages/blitz-dom/src/accessibility.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
use crate::{BaseDocument, ElementData, Node as BlitzDomNode, local_name};
use accesskit::{Node as AccessKitNode, NodeId, Role, Tree, TreeId, TreeUpdate};
use style::properties::longhands::visibility;

impl BaseDocument {
pub fn build_accessibility_tree(&self) -> TreeUpdate {
let mut nodes = std::collections::HashMap::new();
let mut window = AccessKitNode::new(Role::Window);
let mut hidden_nodes = std::collections::HashSet::new();

self.visit(|node_id, node| {
if node.is_hidden_from_accessibility_tree()
|| node
.parent
.map(|p| hidden_nodes.contains(&p))
.unwrap_or(false)
{
hidden_nodes.insert(node_id);
return;
}
let parent = node
.parent
.and_then(|parent_id| nodes.get_mut(&parent_id))
Expand Down Expand Up @@ -55,6 +66,11 @@ impl BaseDocument {

builder.set_role(role);
builder.set_html_tag(name);

// https://www.w3.org/TR/wai-aria-1.2/#tree_exclusion
if element_data.attr(local_name!("aria-hidden")) == Some("true") {
builder.set_hidden();
}
} else if node.is_text_node() {
builder.set_role(Role::TextRun);
builder.set_value(node.text_content());
Expand All @@ -67,6 +83,21 @@ impl BaseDocument {
}
}

impl BlitzDomNode {
// https://www.w3.org/TR/wai-aria-1.2/#tree_exclusion
fn is_hidden_from_accessibility_tree(&self) -> bool {
self.try_stylo_element_data()
.as_ref()
.and_then(|s| s.get())
.map(|s| {
s.styles.is_display_none()
|| s.styles.primary().clone_visibility()
== visibility::computed_value::T::Hidden
})
.unwrap_or(false)
}
}

fn role_from_name(name: &str) -> Option<Role> {
match name {
"alert" => Some(Role::Alert),
Expand Down
1 change: 1 addition & 0 deletions tests/blitz-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ accesskit = { workspace = true }
markup5ever = { workspace = true }
keyboard-types = { workspace = true }
taffy = { workspace = true }
test-that = { workspace = true }
usvg = { workspace = true }

[lib]
Expand Down
145 changes: 145 additions & 0 deletions tests/blitz-tests/tests/accessibility_hidden.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
use accesskit::{Node as AccessKitNode, Role};
use blitz_dom::DocumentConfig;
use blitz_html::{HtmlDocument, HtmlProvider};
use blitz_traits::shell::{ColorScheme, Viewport};
use std::sync::Arc;
use test_that::prelude::*;

#[test]
fn includes_ordinary_div_as_node() -> TestResult<()> {
let mut document =
HtmlDocument::from_html("<html><div></div></html>", default_document_config());
document.resolve(0.0);

let tree_update = document.build_accessibility_tree();

verify_that!(
tree_update.nodes,
contains((
anything(),
matches_pattern!(AccessKitNode {
role(): eq(Role::GenericContainer),
is_hidden(): eq(false),
})
))
)
}

#[test]
fn excludes_div_with_hidden_attribute() -> TestResult<()> {
let mut document =
HtmlDocument::from_html("<html><div hidden></div></html>", default_document_config());
document.resolve(0.0);

let tree_update = document.build_accessibility_tree();

verify_that!(
tree_update.nodes,
not(contains((
anything(),
matches_pattern!(AccessKitNode {
role(): eq(Role::GenericContainer)
})
)))
)
}

#[test]
fn excludes_div_with_display_none() -> TestResult<()> {
let mut document = HtmlDocument::from_html(
r#"<html><div style="display: none;"></div></html>"#,
default_document_config(),
);
document.resolve(0.0);

let tree_update = document.build_accessibility_tree();

verify_that!(
tree_update.nodes,
not(contains((
anything(),
matches_pattern!(AccessKitNode {
role(): eq(Role::GenericContainer)
})
)))
)
}

#[test]
fn excludes_div_with_visibility_hidden() -> TestResult<()> {
let mut document = HtmlDocument::from_html(
r#"<html><div style="visibility: hidden;"></div></html>"#,
default_document_config(),
);
document.resolve(0.0);

let tree_update = document.build_accessibility_tree();

verify_that!(
tree_update.nodes,
not(contains((
anything(),
matches_pattern!(AccessKitNode {
role(): eq(Role::GenericContainer)
})
)))
)
}

#[test]
fn sets_hidden_flag_on_element_with_aria_hidden_attribute() -> TestResult<()> {
let mut document = HtmlDocument::from_html(
r#"<html><div aria-hidden="true"></div></html>"#,
default_document_config(),
);
document.resolve(0.0);

let tree_update = document.build_accessibility_tree();

verify_that!(
tree_update.nodes,
contains((
anything(),
matches_pattern!(AccessKitNode {
role(): eq(Role::GenericContainer),
is_hidden(): eq(true),
})
))
)
}

#[test]
fn excludes_child_element_of_hidden_element() -> TestResult<()> {
let mut document = HtmlDocument::from_html(
r#"<html>
<head/>
<body>
<div hidden="true">
<button/>
</div>
</body>
</html>"#,
default_document_config(),
);
document.resolve(0.0);

let tree_update = document.build_accessibility_tree();

verify_that!(
tree_update.nodes,
not(contains((
anything(),
matches_pattern!(AccessKitNode {
role(): eq(Role::Button),
})
)))
)
}

fn default_document_config() -> DocumentConfig {
DocumentConfig {
viewport: Some(Viewport::new(800, 600, 1.0, ColorScheme::Light)),
html_parser_provider: Some(Arc::new(HtmlProvider) as _),
..Default::default()
}
}
4 changes: 2 additions & 2 deletions tests/blitz-tests/tests/accessibility_roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ fn a_semantic_page_has_no_unknown_elements() {
<footer>End</footer>
</body></html>"##;

// <html>, <head> and <body> have no roles of their own. Everything else in
// <html> and <body> have no roles of their own. Everything else in
// this document should map to something an assistive technology can use.
assert_eq!(unknown_tags(html), vec!["body", "head", "html"]);
assert_eq!(unknown_tags(html), vec!["body", "html"]);
}
Loading