ARIAMixin.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2022, Jonah Shafran <jonahshafran@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/ARIA/ARIAMixin.h>
  7. #include <LibWeb/ARIA/Roles.h>
  8. #include <LibWeb/Infra/CharacterTypes.h>
  9. namespace Web::ARIA {
  10. // https://www.w3.org/TR/wai-aria-1.2/#introroles
  11. Optional<Role> ARIAMixin::role_or_default() const
  12. {
  13. // 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.
  14. auto role_string = role();
  15. // 2. Separate the attribute value string for that attribute into a sequence of whitespace-free substrings by separating on whitespace.
  16. auto role_list = role_string.split_view(Infra::is_ascii_whitespace);
  17. // 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.
  18. for (auto const& role_name : role_list) {
  19. // 4. Use the first such substring in textual order that matches the name of a non-abstract WAI-ARIA role.
  20. auto role = role_from_string(role_name);
  21. if (!role.has_value())
  22. continue;
  23. if (is_non_abstract_role(*role))
  24. return *role;
  25. }
  26. // https://www.w3.org/TR/wai-aria-1.2/#document-handling_author-errors_roles
  27. // 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.
  28. // https://www.w3.org/TR/wai-aria-1.2/#implicit_semantics
  29. return default_role();
  30. }
  31. // https://www.w3.org/TR/wai-aria-1.2/#global_states
  32. bool ARIAMixin::has_global_aria_attribute() const
  33. {
  34. return !aria_atomic().is_null()
  35. || !aria_busy().is_null()
  36. || !aria_controls().is_null()
  37. || !aria_current().is_null()
  38. || !aria_described_by().is_null()
  39. || !aria_details().is_null()
  40. || !aria_disabled().is_null()
  41. || !aria_drop_effect().is_null()
  42. || !aria_error_message().is_null()
  43. || !aria_flow_to().is_null()
  44. || !aria_grabbed().is_null()
  45. || !aria_has_popup().is_null()
  46. || !aria_hidden().is_null()
  47. || !aria_invalid().is_null()
  48. || !aria_key_shortcuts().is_null()
  49. || !aria_label().is_null()
  50. || !aria_labelled_by().is_null()
  51. || !aria_live().is_null()
  52. || !aria_owns().is_null()
  53. || !aria_relevant().is_null()
  54. || !aria_role_description().is_null();
  55. }
  56. }