From 7a4a9cc7bc8dc3d30b95d06fda90e03832e3b721 Mon Sep 17 00:00:00 2001 From: sideshowbarker Date: Fri, 6 Dec 2024 18:41:38 +0900 Subject: [PATCH] LibWeb: Extract ARIA role-attribute checking out from role_or_default() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- Libraries/LibWeb/ARIA/ARIAMixin.cpp | 11 +++++++++-- Libraries/LibWeb/ARIA/ARIAMixin.h | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Libraries/LibWeb/ARIA/ARIAMixin.cpp b/Libraries/LibWeb/ARIA/ARIAMixin.cpp index 836e3a9ce16..ba477beac13 100644 --- a/Libraries/LibWeb/ARIA/ARIAMixin.cpp +++ b/Libraries/LibWeb/ARIA/ARIAMixin.cpp @@ -11,12 +11,12 @@ namespace Web::ARIA { // https://www.w3.org/TR/wai-aria-1.2/#introroles -Optional ARIAMixin::role_or_default() const +Optional 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 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 ARIAMixin::role_or_default() const +{ + if (auto role = role_from_role_attribute_value(); role.has_value()) + return role; return default_role(); } diff --git a/Libraries/LibWeb/ARIA/ARIAMixin.h b/Libraries/LibWeb/ARIA/ARIAMixin.h index 6c78c66d91a..d570c784099 100644 --- a/Libraries/LibWeb/ARIA/ARIAMixin.h +++ b/Libraries/LibWeb/ARIA/ARIAMixin.h @@ -28,6 +28,7 @@ public: // https://www.w3.org/TR/html-aria/#docconformance virtual Optional default_role() const { return {}; } + Optional role_from_role_attribute_value() const; Optional role_or_default() const; // https://www.w3.org/TR/wai-aria-1.2/#tree_exclusion