/src/serenity/Userland/Libraries/LibWeb/ARIA/ARIAMixin.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022, Jonah Shafran <jonahshafran@gmail.com> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibWeb/ARIA/ARIAMixin.h> |
8 | | #include <LibWeb/ARIA/Roles.h> |
9 | | #include <LibWeb/Infra/CharacterTypes.h> |
10 | | |
11 | | namespace Web::ARIA { |
12 | | |
13 | | // https://www.w3.org/TR/wai-aria-1.2/#introroles |
14 | | Optional<Role> ARIAMixin::role_or_default() const |
15 | 0 | { |
16 | | // 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. |
17 | 0 | auto maybe_role_string = role(); |
18 | 0 | if (!maybe_role_string.has_value()) |
19 | 0 | return default_role(); |
20 | | |
21 | | // 2. Separate the attribute value string for that attribute into a sequence of whitespace-free substrings by separating on whitespace. |
22 | 0 | auto role_string = maybe_role_string.value(); |
23 | 0 | auto role_list = role_string.bytes_as_string_view().split_view_if(Infra::is_ascii_whitespace); |
24 | | |
25 | | // 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. |
26 | 0 | for (auto const& role_name : role_list) { |
27 | | // 4. Use the first such substring in textual order that matches the name of a non-abstract WAI-ARIA role. |
28 | 0 | auto role = role_from_string(role_name); |
29 | 0 | if (!role.has_value()) |
30 | 0 | continue; |
31 | 0 | if (is_non_abstract_role(*role)) |
32 | 0 | return *role; |
33 | 0 | } |
34 | | |
35 | | // https://www.w3.org/TR/wai-aria-1.2/#document-handling_author-errors_roles |
36 | | // 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. |
37 | | // https://www.w3.org/TR/wai-aria-1.2/#implicit_semantics |
38 | 0 | return default_role(); |
39 | 0 | } |
40 | | |
41 | | // https://www.w3.org/TR/wai-aria-1.2/#global_states |
42 | | bool ARIAMixin::has_global_aria_attribute() const |
43 | 0 | { |
44 | 0 | return aria_atomic().has_value() |
45 | 0 | || aria_braille_label().has_value() |
46 | 0 | || aria_braille_role_description().has_value() |
47 | 0 | || aria_busy().has_value() |
48 | 0 | || aria_controls().has_value() |
49 | 0 | || aria_current().has_value() |
50 | 0 | || aria_described_by().has_value() |
51 | 0 | || aria_description().has_value() |
52 | 0 | || aria_details().has_value() |
53 | 0 | || aria_disabled().has_value() |
54 | 0 | || aria_drop_effect().has_value() |
55 | 0 | || aria_error_message().has_value() |
56 | 0 | || aria_flow_to().has_value() |
57 | 0 | || aria_grabbed().has_value() |
58 | 0 | || aria_has_popup().has_value() |
59 | 0 | || aria_hidden().has_value() |
60 | 0 | || aria_invalid().has_value() |
61 | 0 | || aria_key_shortcuts().has_value() |
62 | 0 | || aria_label().has_value() |
63 | 0 | || aria_labelled_by().has_value() |
64 | 0 | || aria_live().has_value() |
65 | 0 | || aria_owns().has_value() |
66 | 0 | || aria_relevant().has_value() |
67 | 0 | || aria_role_description().has_value(); |
68 | 0 | } |
69 | | |
70 | | Optional<String> ARIAMixin::parse_id_reference(Optional<String> const& id_reference) const |
71 | 0 | { |
72 | 0 | if (!id_reference.has_value()) |
73 | 0 | return {}; |
74 | | |
75 | 0 | if (id_reference_exists(id_reference.value())) |
76 | 0 | return id_reference.value(); |
77 | | |
78 | 0 | return {}; |
79 | 0 | } |
80 | | |
81 | | Vector<String> ARIAMixin::parse_id_reference_list(Optional<String> const& id_list) const |
82 | 0 | { |
83 | 0 | Vector<String> result; |
84 | 0 | if (!id_list.has_value()) |
85 | 0 | return result; |
86 | | |
87 | 0 | auto id_references = id_list->bytes_as_string_view().split_view_if(Infra::is_ascii_whitespace); |
88 | 0 | for (auto const id_reference_view : id_references) { |
89 | 0 | auto id_reference = MUST(String::from_utf8(id_reference_view)); |
90 | 0 | if (id_reference_exists(id_reference)) |
91 | 0 | result.append(id_reference); |
92 | 0 | } |
93 | 0 | return result; |
94 | 0 | } |
95 | | |
96 | | } |