ARIAMixin.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 maybe_role_string = role();
  15. if (!maybe_role_string.has_value())
  16. return default_role();
  17. // 2. Separate the attribute value string for that attribute into a sequence of whitespace-free substrings by separating on whitespace.
  18. auto role_string = maybe_role_string.value();
  19. auto role_list = role_string.bytes_as_string_view().split_view_if(Infra::is_ascii_whitespace);
  20. // 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.
  21. for (auto const& role_name : role_list) {
  22. // 4. Use the first such substring in textual order that matches the name of a non-abstract WAI-ARIA role.
  23. auto role = role_from_string(role_name);
  24. if (!role.has_value())
  25. continue;
  26. if (is_non_abstract_role(*role))
  27. return *role;
  28. }
  29. // https://www.w3.org/TR/wai-aria-1.2/#document-handling_author-errors_roles
  30. // 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.
  31. // https://www.w3.org/TR/wai-aria-1.2/#implicit_semantics
  32. return default_role();
  33. }
  34. // https://www.w3.org/TR/wai-aria-1.2/#global_states
  35. bool ARIAMixin::has_global_aria_attribute() const
  36. {
  37. return aria_atomic().has_value()
  38. || aria_busy().has_value()
  39. || aria_controls().has_value()
  40. || aria_current().has_value()
  41. || aria_described_by().has_value()
  42. || aria_details().has_value()
  43. || aria_disabled().has_value()
  44. || aria_drop_effect().has_value()
  45. || aria_error_message().has_value()
  46. || aria_flow_to().has_value()
  47. || aria_grabbed().has_value()
  48. || aria_has_popup().has_value()
  49. || aria_hidden().has_value()
  50. || aria_invalid().has_value()
  51. || aria_key_shortcuts().has_value()
  52. || aria_label().has_value()
  53. || aria_labelled_by().has_value()
  54. || aria_live().has_value()
  55. || aria_owns().has_value()
  56. || aria_relevant().has_value()
  57. || aria_role_description().has_value();
  58. }
  59. Optional<String> ARIAMixin::parse_id_reference(Optional<String> const& id_reference) const
  60. {
  61. if (!id_reference.has_value())
  62. return {};
  63. if (id_reference_exists(id_reference.value()))
  64. return id_reference.value();
  65. return {};
  66. }
  67. Vector<String> ARIAMixin::parse_id_reference_list(Optional<String> const& id_list) const
  68. {
  69. Vector<String> result;
  70. if (!id_list.has_value())
  71. return result;
  72. auto id_references = id_list->bytes_as_string_view().split_view_if(Infra::is_ascii_whitespace);
  73. for (auto const id_reference_view : id_references) {
  74. auto id_reference = MUST(String::from_utf8(id_reference_view));
  75. if (id_reference_exists(id_reference))
  76. result.append(id_reference);
  77. }
  78. return result;
  79. }
  80. }