dom_accessibility_api/
accessible_description.rs

1use web_sys::Element;
2
3use crate::{
4    accessible_name_and_description::{
5        compute_text_alternative, Compute, ComputeTextAlternativeOptions,
6    },
7    util::query_id_refs,
8};
9
10/// Implements <https://w3c.github.io/accname/#mapping_additional_nd_description>.
11pub fn compute_accessible_description(
12    root: &Element,
13    options: ComputeTextAlternativeOptions,
14) -> String {
15    let mut description = query_id_refs(root, "aria-describedby")
16        .iter()
17        .map(|element| {
18            compute_text_alternative(
19                element,
20                ComputeTextAlternativeOptions {
21                    compute: Some(Compute::Description),
22                    ..options.clone()
23                },
24            )
25        })
26        .collect::<Vec<_>>()
27        .join(" ");
28
29    // TODO: Technically we need to make sure that node wasn't used for the accessible name.
30    //       This causes `description_1.0_combobox-focusable-manual` to fail.
31
32    // https://w3c.github.io/aria/#aria-description
33    // Mentions that aria-description should only be calculated if aria-describedby didn't provide a description.
34    if description.is_empty() {
35        if let Some(aria_description) = root.get_attribute("aria-description") {
36            description = aria_description;
37        }
38    }
39
40    // https://www.w3.org/TR/html-aam-1.0/#accessible-name-and-description-computation
41    // Says for so many elements to use the `title` that we assume all elements are considered.
42    if description.is_empty() {
43        if let Some(title) = root.get_attribute("title") {
44            description = title;
45        }
46    }
47
48    description
49}