LibWeb: Extract ARIA role-attribute checking out from role_or_default()

This change separates the steps for checking the string value of the
ARIA “role” attribute out from the element.role_or_default() function
into a separate function — in order to expose a way to just check if the
ARIA “role” attribute actually has a value, without also then computing
a default role value if no “role” attribute value was found.

Otherwise, without this change, the only available function for
retrieving ARIA role values is the element.role_or_default() function —
which always does the additional step of computing (and returning) a
default role value if no “role” attribute is found.
This commit is contained in:
sideshowbarker 2024-12-06 18:41:38 +09:00 committed by Tim Ledbetter
parent c5966bbdcb
commit 7a4a9cc7bc
Notes: github-actions[bot] 2024-12-06 18:32:48 +00:00
2 changed files with 10 additions and 2 deletions

View file

@ -11,12 +11,12 @@
namespace Web::ARIA {
// https://www.w3.org/TR/wai-aria-1.2/#introroles
Optional<Role> ARIAMixin::role_or_default() const
Optional<Role> ARIAMixin::role_from_role_attribute_value() 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 maybe_role_string = role();
if (!maybe_role_string.has_value())
return default_role();
return OptionalNone {};
// 2. Separate the attribute value string for that attribute into a sequence of whitespace-free substrings by separating on whitespace.
auto role_string = maybe_role_string.value();
@ -35,6 +35,13 @@ Optional<Role> ARIAMixin::role_or_default() const
// https://www.w3.org/TR/wai-aria-1.2/#document-handling_author-errors_roles
// If the role attribute contains no tokens matching the name of a non-abstract WAI-ARIA role, the user agent MUST treat the element as if no role had been provided.
// https://www.w3.org/TR/wai-aria-1.2/#implicit_semantics
return OptionalNone {};
}
Optional<Role> ARIAMixin::role_or_default() const
{
if (auto role = role_from_role_attribute_value(); role.has_value())
return role;
return default_role();
}

View file

@ -28,6 +28,7 @@ public:
// https://www.w3.org/TR/html-aria/#docconformance
virtual Optional<Role> default_role() const { return {}; }
Optional<Role> role_from_role_attribute_value() const;
Optional<Role> role_or_default() const;
// https://www.w3.org/TR/wai-aria-1.2/#tree_exclusion