Skip to main content

layout_api/
accessibility.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::sync::LazyLock;
6
7use accesskit::Role;
8use bitflags::bitflags;
9use rustc_hash::{FxHashMap, FxHashSet};
10use style::Atom;
11
12bitflags! {
13    #[derive(Clone, Copy, Default, Debug, Eq, PartialEq)]
14    pub struct AccessibilityDamage: u16 {
15        /// The properties of this node (other than children) have changed.
16        const Node = 1 << 0;
17        /// Children have been added to or removed from this node.
18        const Children = 1 << 1;
19        /// This node and its descendants have bounds damage from layout.
20        const Layout = 1 << 2;
21    }
22}
23malloc_size_of::malloc_size_of_is_0!(AccessibilityDamage);
24
25/// A safe-list of ARIA role attribute values which we map to AccessKit roles.
26///
27/// This is currently just the roles that don't have any [supported] or [required] properties
28/// and also don't require an [accessible name].
29///
30/// [supported]: https://w3c.github.io/aria/#supportedState
31/// [required]: https://w3c.github.io/aria/#requiredState
32/// [accessible name]: https://w3c.github.io/aria/#namefromauthor
33pub static SUPPORTED_ARIA_ROLES: LazyLock<FxHashSet<Atom>> = LazyLock::new(|| {
34    [
35        Atom::from("alert"),
36        Atom::from("article"),
37        Atom::from("banner"),
38        Atom::from("blockquote"),
39        Atom::from("caption"),
40        Atom::from("code"),
41        Atom::from("complementary"),
42        Atom::from("contentinfo"),
43        Atom::from("definition"),
44        Atom::from("deletion"),
45        Atom::from("directory"),
46        Atom::from("document"),
47        Atom::from("emphasis"),
48        Atom::from("feed"),
49        Atom::from("figure"),
50        Atom::from("generic"),
51        Atom::from("heading"),
52        Atom::from("insertion"),
53        Atom::from("link"),
54        Atom::from("list"),
55        Atom::from("log"),
56        Atom::from("main"),
57        Atom::from("math"),
58        Atom::from("navigation"),
59        Atom::from("none"),
60        Atom::from("note"),
61        Atom::from("paragraph"),
62        Atom::from("presentation"),
63        Atom::from("rowgroup"),
64        Atom::from("search"),
65        Atom::from("status"),
66        Atom::from("strong"),
67        Atom::from("term"),
68        Atom::from("time"),
69        Atom::from("timer"),
70    ]
71    .into_iter()
72    .collect()
73});
74
75/// A map from ARIA role name to the corresponding [`Role`] in AccessKit.
76pub static ARIA_ROLE_TO_ACCESSKIT_ROLE: LazyLock<FxHashMap<Atom, Role>> = LazyLock::new(|| {
77    [
78        (Atom::from("alert"), Role::Alert),
79        (Atom::from("article"), Role::Article),
80        (Atom::from("banner"), Role::Banner),
81        (Atom::from("blockquote"), Role::Blockquote),
82        (Atom::from("caption"), Role::Caption),
83        (Atom::from("code"), Role::Code),
84        (Atom::from("complementary"), Role::Complementary),
85        (Atom::from("contentinfo"), Role::ContentInfo),
86        (Atom::from("definition"), Role::Definition),
87        (Atom::from("deletion"), Role::ContentDeletion),
88        (Atom::from("directory"), Role::Unknown),
89        (Atom::from("document"), Role::Document),
90        (Atom::from("emphasis"), Role::Emphasis),
91        (Atom::from("feed"), Role::Feed),
92        (Atom::from("figure"), Role::Figure),
93        (Atom::from("generic"), Role::Section),
94        (Atom::from("heading"), Role::Heading),
95        (Atom::from("insertion"), Role::ContentInsertion),
96        (Atom::from("link"), Role::Link),
97        (Atom::from("list"), Role::List),
98        (Atom::from("log"), Role::Log),
99        (Atom::from("main"), Role::Main),
100        (Atom::from("math"), Role::Math),
101        (Atom::from("navigation"), Role::Navigation),
102        (Atom::from("none"), Role::GenericContainer),
103        (Atom::from("note"), Role::Note),
104        (Atom::from("paragraph"), Role::Paragraph),
105        (Atom::from("presentation"), Role::GenericContainer),
106        (Atom::from("rowgroup"), Role::RowGroup),
107        (Atom::from("search"), Role::Search),
108        (Atom::from("separator"), Role::Splitter),
109        (Atom::from("status"), Role::Status),
110        (Atom::from("strong"), Role::Strong),
111        // (Atom::from("subscript"), Role::Subscript), // no corresponding accesskit role.
112        // (Atom::from("superscript"), Role::Superscript), // no corresponding accesskit role.
113        (Atom::from("term"), Role::Term),
114        (Atom::from("time"), Role::Time),
115        (Atom::from("timer"), Role::Timer),
116    ]
117    .into_iter()
118    .collect()
119});
120
121/// The inverse of the map above, i.e. a map from supported AccessKit role to the corresponding ARIA
122/// role.
123///
124/// This is created by inverting the map above to ensure consistency, then for roles where multiple
125/// ARIA roles map to the same AccessKit role an explicit entry is added.
126pub static ACCESSKIT_ROLE_TO_ARIA_ROLE: LazyLock<FxHashMap<Role, Atom>> = LazyLock::new(|| {
127    let mut accesskit_to_aria: FxHashMap<Role, Atom> = ARIA_ROLE_TO_ACCESSKIT_ROLE
128        .iter()
129        .map(|(aria_role, accesskit_role)| (*accesskit_role, aria_role.clone()))
130        .collect();
131    // Note: AccessKit doesn't expose nodes with the `GenericContainer` role by default, so it acts
132    // more like ARIA's `none`/`presentation` role.
133    accesskit_to_aria.insert(Role::GenericContainer, Atom::from("none"));
134    accesskit_to_aria
135});