2023-01-29 18:12:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Jonah Shafran <jonahshafran@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/GenericShorthands.h>
|
2023-01-28 22:23:16 +00:00
|
|
|
#include <LibWeb/ARIA/Roles.h>
|
2023-01-29 18:12:00 +00:00
|
|
|
|
2023-01-28 22:23:16 +00:00
|
|
|
namespace Web::ARIA {
|
2023-01-29 18:12:00 +00:00
|
|
|
|
|
|
|
StringView role_name(Role role)
|
|
|
|
{
|
2023-01-29 00:45:08 +00:00
|
|
|
// Note: Role::switch_ is mapped to "switch" (due to C++ keyword clash)
|
2023-01-29 18:12:00 +00:00
|
|
|
switch (role) {
|
2023-01-29 00:45:08 +00:00
|
|
|
#define __ENUMERATE_ARIA_ROLE(name) \
|
|
|
|
case Role::name: \
|
|
|
|
if constexpr (Role::name == Role::switch_) \
|
|
|
|
return "switch"sv; \
|
2023-01-29 18:12:00 +00:00
|
|
|
return #name##sv;
|
|
|
|
ENUMERATE_ARIA_ROLES
|
|
|
|
#undef __ENUMERATE_ARIA_ROLE
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-28 22:23:16 +00:00
|
|
|
Optional<Role> role_from_string(StringView role_name)
|
2023-01-29 18:12:00 +00:00
|
|
|
{
|
2023-01-29 00:45:08 +00:00
|
|
|
// Note: "switch" is mapped to Role::switch_ (due to C++ keyword clash)
|
2023-03-10 07:48:54 +00:00
|
|
|
#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; \
|
2023-01-29 00:45:08 +00:00
|
|
|
}
|
2023-01-29 18:12:00 +00:00
|
|
|
ENUMERATE_ARIA_ROLES
|
|
|
|
#undef __ENUMERATE_ARIA_ROLE
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/wai-aria-1.2/#abstract_roles
|
2023-01-28 22:23:16 +00:00
|
|
|
bool is_abstract_role(Role role)
|
2023-01-29 18:12:00 +00:00
|
|
|
{
|
|
|
|
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
|
2023-01-28 22:23:16 +00:00
|
|
|
bool is_widget_role(Role role)
|
2023-01-29 18:12:00 +00:00
|
|
|
{
|
|
|
|
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
|
2023-01-28 22:23:16 +00:00
|
|
|
bool is_document_structure_role(Role role)
|
2023-01-29 18:12:00 +00:00
|
|
|
{
|
|
|
|
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
|
2023-01-28 22:23:16 +00:00
|
|
|
bool is_landmark_role(Role role)
|
2023-01-29 18:12:00 +00:00
|
|
|
{
|
|
|
|
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
|
2023-01-28 22:23:16 +00:00
|
|
|
bool is_live_region_role(Role role)
|
2023-01-29 18:12:00 +00:00
|
|
|
{
|
|
|
|
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
|
2023-01-28 22:23:16 +00:00
|
|
|
bool is_windows_role(Role role)
|
2023-01-29 18:12:00 +00:00
|
|
|
{
|
|
|
|
return first_is_one_of(role,
|
|
|
|
Role::alertdialog,
|
|
|
|
Role::dialog);
|
|
|
|
}
|
|
|
|
|
2023-01-28 22:23:16 +00:00
|
|
|
bool is_non_abstract_role(Role role)
|
2023-01-29 18:12:00 +00:00
|
|
|
{
|
2023-01-28 22:23:16 +00:00
|
|
|
return is_widget_role(role)
|
|
|
|
|| is_document_structure_role(role)
|
|
|
|
|| is_landmark_role(role)
|
|
|
|
|| is_live_region_role(role)
|
|
|
|
|| is_windows_role(role);
|
2023-01-29 18:12:00 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 17:20:16 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2023-01-29 18:12:00 +00:00
|
|
|
}
|