1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 22:17:44 +00:00

LibWeb: Replace ARIA role static FlyStrings with an enum

This replaces the FlyStrings for ARIA roles that were constructed in
a [[gnu::constructor]] with a single enum. I came across this as the
DOM inspector was crashing due to a null FlyString for an ARIA role.

After fixing that, I was confused as to why these roles were not an
enum. Looking at the spec there's a fixed list of roles and switching
from references to static strings to an enum was pretty much an
exercise in find and replace :).

No functional changes (outside of fixing the mentioned crash).
This commit is contained in:
MacDue 2023-01-29 19:12:00 +01:00 committed by Linus Groh
parent f23eba1ba8
commit 890b4d7980
59 changed files with 344 additions and 327 deletions

View file

@ -5,13 +5,13 @@
*/
#include <LibWeb/DOM/ARIAMixin.h>
#include <LibWeb/DOM/ARIARoleNames.h>
#include <LibWeb/DOM/ARIARoles.h>
#include <LibWeb/Infra/CharacterTypes.h>
namespace Web::DOM {
// https://www.w3.org/TR/wai-aria-1.2/#introroles
DeprecatedFlyString ARIAMixin::role_or_default() const
Optional<ARIARoles::Role> ARIAMixin::role_or_default() const
{
// 1. Use the rules of the host language to detect that an element has a role attribute and to identify the attribute value string for it.
auto role_string = role();
@ -20,10 +20,13 @@ DeprecatedFlyString ARIAMixin::role_or_default() const
auto role_list = role_string.split_view(Infra::is_ascii_whitespace);
// 3. Compare the substrings to all the names of the non-abstract WAI-ARIA roles. Case-sensitivity of the comparison inherits from the case-sensitivity of the host language.
for (auto const& role : role_list) {
for (auto const& role_name : role_list) {
// 4. Use the first such substring in textual order that matches the name of a non-abstract WAI-ARIA role.
if (ARIARoleNames::is_non_abstract_aria_role(role))
return role;
auto role = ARIARoles::from_string(role_name);
if (!role.has_value())
continue;
if (ARIARoles::is_non_abstract_aria_role(*role))
return *role;
}
// https://www.w3.org/TR/wai-aria-1.2/#document-handling_author-errors_roles

View file

@ -7,6 +7,7 @@
#pragma once
#include <AK/DeprecatedFlyString.h>
#include <LibWeb/DOM/ARIARoles.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
namespace Web::DOM {
@ -132,9 +133,9 @@ public:
virtual WebIDL::ExceptionOr<void> set_aria_value_text(DeprecatedString const&) = 0;
// https://www.w3.org/TR/html-aria/#docconformance
virtual DeprecatedFlyString default_role() const { return {}; };
virtual Optional<ARIARoles::Role> default_role() const { return {}; };
DeprecatedFlyString role_or_default() const;
Optional<ARIARoles::Role> role_or_default() const;
// https://www.w3.org/TR/wai-aria-1.2/#tree_exclusion
virtual bool exclude_from_accessibility_tree() const = 0;

View file

@ -1,167 +0,0 @@
/*
* Copyright (c) 2022, Jonah Shafran <jonahshafran@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/DOM/ARIARoleNames.h>
namespace Web::DOM::ARIARoleNames {
#define __ENUMERATE_ARIA_ROLE(name) DeprecatedFlyString name;
ENUMERATE_ARIA_ROLES
#undef __ENUMERATE_ARIA_ROLE
[[gnu::constructor]] static void initialize()
{
static bool s_initialized = false;
if (s_initialized)
return;
#define __ENUMERATE_ARIA_ROLE(name) name = #name;
ENUMERATE_ARIA_ROLES
#undef __ENUMERATE_ARIA_ROLE
// Special case for C++ keyword
switch_ = "switch";
s_initialized = true;
}
// https://www.w3.org/TR/wai-aria-1.2/#abstract_roles
bool is_abstract_aria_role(DeprecatedFlyString const& role)
{
return role.is_one_of(
ARIARoleNames::command,
ARIARoleNames::composite,
ARIARoleNames::input,
ARIARoleNames::landmark,
ARIARoleNames::range,
ARIARoleNames::roletype,
ARIARoleNames::section,
ARIARoleNames::sectionhead,
ARIARoleNames::select,
ARIARoleNames::structure,
ARIARoleNames::widget,
ARIARoleNames::window);
}
// https://www.w3.org/TR/wai-aria-1.2/#widget_roles
bool is_widget_aria_role(DeprecatedFlyString const& role)
{
return role.to_lowercase().is_one_of(
ARIARoleNames::button,
ARIARoleNames::checkbox,
ARIARoleNames::gridcell,
ARIARoleNames::link,
ARIARoleNames::menuitem,
ARIARoleNames::menuitemcheckbox,
ARIARoleNames::option,
ARIARoleNames::progressbar,
ARIARoleNames::radio,
ARIARoleNames::scrollbar,
ARIARoleNames::searchbox,
ARIARoleNames::separator, // TODO: Only when focusable
ARIARoleNames::slider,
ARIARoleNames::spinbutton,
ARIARoleNames::switch_,
ARIARoleNames::tab,
ARIARoleNames::tabpanel,
ARIARoleNames::textbox,
ARIARoleNames::treeitem,
ARIARoleNames::combobox,
ARIARoleNames::grid,
ARIARoleNames::listbox,
ARIARoleNames::menu,
ARIARoleNames::menubar,
ARIARoleNames::radiogroup,
ARIARoleNames::tablist,
ARIARoleNames::tree,
ARIARoleNames::treegrid);
}
// https://www.w3.org/TR/wai-aria-1.2/#document_structure_roles
bool is_document_structure_aria_role(DeprecatedFlyString const& role)
{
return role.to_lowercase().is_one_of(
ARIARoleNames::application,
ARIARoleNames::article,
ARIARoleNames::blockquote,
ARIARoleNames::caption,
ARIARoleNames::cell,
ARIARoleNames::columnheader,
ARIARoleNames::definition,
ARIARoleNames::deletion,
ARIARoleNames::directory,
ARIARoleNames::document,
ARIARoleNames::emphasis,
ARIARoleNames::feed,
ARIARoleNames::figure,
ARIARoleNames::generic,
ARIARoleNames::group,
ARIARoleNames::heading,
ARIARoleNames::img,
ARIARoleNames::insertion,
ARIARoleNames::list,
ARIARoleNames::listitem,
ARIARoleNames::math,
ARIARoleNames::meter,
ARIARoleNames::none,
ARIARoleNames::note,
ARIARoleNames::paragraph,
ARIARoleNames::presentation,
ARIARoleNames::row,
ARIARoleNames::rowgroup,
ARIARoleNames::rowheader,
ARIARoleNames::separator, // TODO: Only when not focusable
ARIARoleNames::strong,
ARIARoleNames::subscript,
ARIARoleNames::table,
ARIARoleNames::term,
ARIARoleNames::time,
ARIARoleNames::toolbar,
ARIARoleNames::tooltip);
}
// https://www.w3.org/TR/wai-aria-1.2/#landmark_roles
bool is_landmark_aria_role(DeprecatedFlyString const& role)
{
return role.to_lowercase().is_one_of(
ARIARoleNames::banner,
ARIARoleNames::complementary,
ARIARoleNames::contentinfo,
ARIARoleNames::form,
ARIARoleNames::main,
ARIARoleNames::navigation,
ARIARoleNames::region,
ARIARoleNames::search);
}
// https://www.w3.org/TR/wai-aria-1.2/#live_region_roles
bool is_live_region_aria_role(DeprecatedFlyString const& role)
{
return role.to_lowercase().is_one_of(
ARIARoleNames::alert,
ARIARoleNames::log,
ARIARoleNames::marquee,
ARIARoleNames::status,
ARIARoleNames::timer);
}
// https://www.w3.org/TR/wai-aria-1.2/#window_roles
bool is_windows_aria_role(DeprecatedFlyString const& role)
{
return role.to_lowercase().is_one_of(
ARIARoleNames::alertdialog,
ARIARoleNames::dialog);
}
bool is_non_abstract_aria_role(DeprecatedFlyString const& role)
{
return is_widget_aria_role(role)
|| is_document_structure_aria_role(role)
|| is_landmark_aria_role(role)
|| is_live_region_aria_role(role)
|| is_windows_aria_role(role);
}
}

View file

@ -0,0 +1,172 @@
/*
* Copyright (c) 2022, Jonah Shafran <jonahshafran@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/GenericShorthands.h>
#include <LibWeb/DOM/ARIARoles.h>
namespace Web::DOM::ARIARoles {
StringView role_name(Role role)
{
switch (role) {
#define __ENUMERATE_ARIA_ROLE(name) \
case Role::name: \
return #name##sv;
ENUMERATE_ARIA_ROLES
#undef __ENUMERATE_ARIA_ROLE
default:
VERIFY_NOT_REACHED();
}
}
Optional<Role> from_string(StringView role_name)
{
#define __ENUMERATE_ARIA_ROLE(name) \
if (role_name.equals_ignoring_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_aria_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_aria_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_aria_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_aria_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_aria_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_aria_role(Role role)
{
return first_is_one_of(role,
Role::alertdialog,
Role::dialog);
}
bool is_non_abstract_aria_role(Role role)
{
return is_widget_aria_role(role)
|| is_document_structure_aria_role(role)
|| is_landmark_aria_role(role)
|| is_live_region_aria_role(role)
|| is_windows_aria_role(role);
}
}

View file

@ -8,7 +8,7 @@
#include <AK/DeprecatedFlyString.h>
namespace Web::DOM::ARIARoleNames {
namespace Web::DOM::ARIARoles {
#define ENUMERATE_ARIA_ROLES \
__ENUMERATE_ARIA_ROLE(alert) \
@ -106,17 +106,22 @@ namespace Web::DOM::ARIARoleNames {
__ENUMERATE_ARIA_ROLE(widget) \
__ENUMERATE_ARIA_ROLE(window)
#define __ENUMERATE_ARIA_ROLE(name) extern DeprecatedFlyString name;
ENUMERATE_ARIA_ROLES
enum class Role {
#define __ENUMERATE_ARIA_ROLE(name) name,
ENUMERATE_ARIA_ROLES
#undef __ENUMERATE_ARIA_ROLE
};
bool is_abstract_aria_role(DeprecatedFlyString const&);
bool is_widget_aria_role(DeprecatedFlyString const&);
bool is_document_structure_aria_role(DeprecatedFlyString const&);
bool is_landmark_aria_role(DeprecatedFlyString const&);
bool is_live_region_aria_role(DeprecatedFlyString const&);
bool is_windows_aria_role(DeprecatedFlyString const&);
StringView role_name(Role);
Optional<Role> from_string(StringView role_name);
bool is_non_abstract_aria_role(DeprecatedFlyString const&);
bool is_abstract_aria_role(Role);
bool is_widget_aria_role(Role);
bool is_document_structure_aria_role(Role);
bool is_landmark_aria_role(Role);
bool is_live_region_aria_role(Role);
bool is_windows_aria_role(Role);
bool is_non_abstract_aria_role(Role);
}

View file

@ -35,10 +35,10 @@ void AccessibilityTreeNode::serialize_tree_as_json(JsonObjectSerializer<StringBu
MUST(object.add("type"sv, "element"sv));
auto role = element->role_or_default();
bool has_role = !role.is_null() && !role.is_empty() && !ARIARoleNames::is_abstract_aria_role(role);
bool has_role = role.has_value() && !ARIARoles::is_abstract_aria_role(*role);
if (has_role)
MUST(object.add("role"sv, role.view()));
MUST(object.add("role"sv, ARIARoles::role_name(*role)));
else
MUST(object.add("role"sv, ""sv));
} else {

View file

@ -1282,8 +1282,8 @@ bool Element::exclude_from_accessibility_tree() const
// Elements with none or presentation as the first role in the role attribute. However, their exclusion is conditional. In addition, the element's descendants and text content are generally included. These exceptions and conditions are documented in the presentation (role) section.
// FIXME: Handle exceptions to excluding presentation role
auto role = role_or_default().to_lowercase();
if (role == ARIARoleNames::none || role == ARIARoleNames::presentation)
auto role = role_or_default();
if (role == ARIARoles::Role::none || role == ARIARoles::Role::presentation)
return true;
// TODO: If not already excluded from the accessibility tree per the above rules, user agents SHOULD NOT include the following elements in the accessibility tree:
@ -1322,7 +1322,7 @@ bool Element::include_in_accessibility_tree() const
// Elements that have an explicit role or a global WAI-ARIA attribute and do not have aria-hidden set to true. (See Excluding Elements in the Accessibility Tree for additional guidance on aria-hidden.)
// NOTE: The spec says only explicit roles count, but playing around in other browsers, this does not seem to be true in practice (for example button elements are always exposed with their implicit role if none is set)
// This issue https://github.com/w3c/aria/issues/1851 seeks clarification on this point
if ((!role_or_default().is_empty() || has_global_aria_attribute()) && aria_hidden() != "true")
if ((role_or_default().has_value() || has_global_aria_attribute()) && aria_hidden() != "true")
return true;
// TODO: Elements that are not hidden and have an ID that is referenced by another element via a WAI-ARIA property.

View file

@ -12,7 +12,7 @@
#include <AK/RefPtr.h>
#include <AK/TypeCasts.h>
#include <AK/Vector.h>
#include <LibWeb/DOM/ARIARoleNames.h>
#include <LibWeb/DOM/ARIARoles.h>
#include <LibWeb/DOM/AccessibilityTreeNode.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/DOMParsing/XMLSerializer.h>