summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/ARIA/Roles.cpp
blob: 823959c96ec1286f80440500815173d140fc95cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * Copyright (c) 2022, Jonah Shafran <jonahshafran@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/GenericShorthands.h>
#include <LibWeb/ARIA/Roles.h>

namespace Web::ARIA {

StringView role_name(Role role)
{
    // Note: Role::switch_ is mapped to "switch" (due to C++ keyword clash)
    switch (role) {
#define __ENUMERATE_ARIA_ROLE(name)                \
    case Role::name:                               \
        if constexpr (Role::name == Role::switch_) \
            return "switch"sv;                     \
        return #name##sv;
        ENUMERATE_ARIA_ROLES
#undef __ENUMERATE_ARIA_ROLE
    default:
        VERIFY_NOT_REACHED();
    }
}

Optional<Role> role_from_string(StringView role_name)
{
    // Note: "switch" is mapped to Role::switch_ (due to C++ keyword clash)
#define __ENUMERATE_ARIA_ROLE(name)                           \
    if constexpr (Role::name == Role::switch_) {              \
        if (role_name.equals_ignoring_ascii_case("switch"sv)) \
            return Role::switch_;                             \
    } else {                                                  \
        if (role_name.equals_ignoring_ascii_case(#name##sv))  \
            return Role::name;                                \
    }
    ENUMERATE_ARIA_ROLES
#undef __ENUMERATE_ARIA_ROLE
    return {};
}

// https://www.w3.org/TR/wai-aria-1.2/#abstract_roles
bool is_abstract_role(Role role)
{
    return first_is_one_of(role,
        Role::command,
        Role::composite,
        Role::input,
        Role::landmark,
        Role::range,
        Role::roletype,
        Role::section,
        Role::sectionhead,
        Role::select,
        Role::structure,
        Role::widget,
        Role::window);
}

// https://www.w3.org/TR/wai-aria-1.2/#widget_roles
bool is_widget_role(Role role)
{
    return first_is_one_of(role,
        Role::button,
        Role::checkbox,
        Role::gridcell,
        Role::link,
        Role::menuitem,
        Role::menuitemcheckbox,
        Role::option,
        Role::progressbar,
        Role::radio,
        Role::scrollbar,
        Role::searchbox,
        Role::separator, // TODO: Only when focusable
        Role::slider,
        Role::spinbutton,
        Role::switch_,
        Role::tab,
        Role::tabpanel,
        Role::textbox,
        Role::treeitem,
        Role::combobox,
        Role::grid,
        Role::listbox,
        Role::menu,
        Role::menubar,
        Role::radiogroup,
        Role::tablist,
        Role::tree,
        Role::treegrid);
}

// https://www.w3.org/TR/wai-aria-1.2/#document_structure_roles
bool is_document_structure_role(Role role)
{
    return first_is_one_of(role,
        Role::application,
        Role::article,
        Role::blockquote,
        Role::caption,
        Role::cell,
        Role::columnheader,
        Role::definition,
        Role::deletion,
        Role::directory,
        Role::document,
        Role::emphasis,
        Role::feed,
        Role::figure,
        Role::generic,
        Role::group,
        Role::heading,
        Role::img,
        Role::insertion,
        Role::list,
        Role::listitem,
        Role::math,
        Role::meter,
        Role::none,
        Role::note,
        Role::paragraph,
        Role::presentation,
        Role::row,
        Role::rowgroup,
        Role::rowheader,
        Role::separator, // TODO: Only when not focusable
        Role::strong,
        Role::subscript,
        Role::table,
        Role::term,
        Role::time,
        Role::toolbar,
        Role::tooltip);
}

// https://www.w3.org/TR/wai-aria-1.2/#landmark_roles
bool is_landmark_role(Role role)
{
    return first_is_one_of(role,
        Role::banner,
        Role::complementary,
        Role::contentinfo,
        Role::form,
        Role::main,
        Role::navigation,
        Role::region,
        Role::search);
}

// https://www.w3.org/TR/wai-aria-1.2/#live_region_roles
bool is_live_region_role(Role role)
{
    return first_is_one_of(role,
        Role::alert,
        Role::log,
        Role::marquee,
        Role::status,
        Role::timer);
}

// https://www.w3.org/TR/wai-aria-1.2/#window_roles
bool is_windows_role(Role role)
{
    return first_is_one_of(role,
        Role::alertdialog,
        Role::dialog);
}

bool is_non_abstract_role(Role role)
{
    return is_widget_role(role)
        || is_document_structure_role(role)
        || is_landmark_role(role)
        || is_live_region_role(role)
        || is_windows_role(role);
}

// https://www.w3.org/TR/wai-aria-1.2/#namefromcontent
bool allows_name_from_content(Role role)
{
    return first_is_one_of(role,
        Role::button,
        Role::cell,
        Role::checkbox,
        Role::columnheader,
        Role::gridcell,
        Role::heading,
        Role::link,
        Role::menuitem,
        Role::menuitemcheckbox,
        Role::menuitemradio,
        Role::option,
        Role::radio,
        Role::row,
        Role::rowheader,
        Role::sectionhead,
        Role::switch_,
        Role::tab,
        Role::tooltip,
        Role::treeitem);
}

}