DOMStringMap.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/CharacterTypes.h>
  7. #include <LibWeb/DOM/Element.h>
  8. #include <LibWeb/HTML/DOMStringMap.h>
  9. namespace Web::HTML {
  10. DOMStringMap::DOMStringMap(DOM::Element& associated_element)
  11. : m_associated_element(associated_element)
  12. {
  13. }
  14. DOMStringMap::~DOMStringMap()
  15. {
  16. }
  17. // https://html.spec.whatwg.org/multipage/dom.html#concept-domstringmap-pairs
  18. Vector<DOMStringMap::NameValuePair> DOMStringMap::get_name_value_pairs() const
  19. {
  20. // 1. Let list be an empty list of name-value pairs.
  21. Vector<NameValuePair> list;
  22. // 2. For each content attribute on the DOMStringMap's associated element whose first five characters are the string "data-" and whose remaining characters (if any) do not include any ASCII upper alphas,
  23. // in the order that those attributes are listed in the element's attribute list, add a name-value pair to list whose name is the attribute's name with the first five characters removed and whose value
  24. // is the attribute's value.
  25. m_associated_element->for_each_attribute([&](auto& name, auto& value) {
  26. if (!name.starts_with("data-"))
  27. return;
  28. auto name_after_starting_data = name.view().substring_view(5);
  29. for (auto character : name_after_starting_data) {
  30. if (is_ascii_upper_alpha(character))
  31. return;
  32. }
  33. // 3. For each name in list, for each U+002D HYPHEN-MINUS character (-) in the name that is followed by an ASCII lower alpha, remove the U+002D HYPHEN-MINUS character (-) and replace the character
  34. // that followed it by the same character converted to ASCII uppercase.
  35. StringBuilder builder;
  36. for (size_t character_index = 0; character_index < name_after_starting_data.length(); ++character_index) {
  37. auto current_character = name_after_starting_data[character_index];
  38. if (character_index + 1 < name_after_starting_data.length() && current_character == '-') {
  39. auto next_character = name_after_starting_data[character_index + 1];
  40. if (is_ascii_lower_alpha(next_character)) {
  41. builder.append(to_ascii_uppercase(next_character));
  42. // Skip the next character
  43. ++character_index;
  44. return;
  45. }
  46. }
  47. builder.append(current_character);
  48. }
  49. list.append({ builder.to_string(), value });
  50. });
  51. // 4. Return list.
  52. return list;
  53. }
  54. // https://html.spec.whatwg.org/multipage/dom.html#concept-domstringmap-pairs
  55. // NOTE: There isn't a direct link to this, so the link is to one of the algorithms above it.
  56. Vector<String> DOMStringMap::supported_property_names() const
  57. {
  58. // The supported property names on a DOMStringMap object at any instant are the names of each pair returned from getting the DOMStringMap's name-value pairs at that instant, in the order returned.
  59. Vector<String> names;
  60. auto name_value_pairs = get_name_value_pairs();
  61. for (auto& name_value_pair : name_value_pairs) {
  62. names.append(name_value_pair.name);
  63. }
  64. return names;
  65. }
  66. // https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-nameditem
  67. String DOMStringMap::determine_value_of_named_property(String const& name) const
  68. {
  69. // To determine the value of a named property name for a DOMStringMap, return the value component of the name-value pair whose name component is name in the list returned from getting the
  70. // DOMStringMap's name-value pairs.
  71. auto name_value_pairs = get_name_value_pairs();
  72. auto optional_value = name_value_pairs.first_matching([&name](NameValuePair& name_value_pair) {
  73. return name_value_pair.name == name;
  74. });
  75. // NOTE: determine_value_of_named_property is only called if `name` is in supported_property_names.
  76. VERIFY(optional_value.has_value());
  77. return optional_value->value;
  78. }
  79. // https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-setitem
  80. DOM::ExceptionOr<void> DOMStringMap::set_value_of_new_named_property(String const& name, String const& value)
  81. {
  82. AK::StringBuilder builder;
  83. // 3. Insert the string data- at the front of name.
  84. // NOTE: This is done out of order because StringBuilder doesn't have prepend.
  85. builder.append("data-");
  86. for (size_t character_index = 0; character_index < name.length(); ++character_index) {
  87. // 1. If name contains a U+002D HYPHEN-MINUS character (-) followed by an ASCII lower alpha, then throw a "SyntaxError" DOMException.
  88. auto current_character = name[character_index];
  89. if (current_character == '-' && character_index + 1 < name.length()) {
  90. auto next_character = name[character_index + 1];
  91. if (is_ascii_lower_alpha(next_character))
  92. return DOM::SyntaxError::create("Name cannot contain a '-' followed by a lowercase character.");
  93. }
  94. // 2. For each ASCII upper alpha in name, insert a U+002D HYPHEN-MINUS character (-) before the character and replace the character with the same character converted to ASCII lowercase.
  95. if (is_ascii_upper_alpha(current_character)) {
  96. builder.append('-');
  97. builder.append(to_ascii_lowercase(current_character));
  98. continue;
  99. }
  100. builder.append(current_character);
  101. }
  102. auto data_name = builder.to_string();
  103. // FIXME: 4. If name does not match the XML Name production, throw an "InvalidCharacterError" DOMException.
  104. // 5. Set an attribute value for the DOMStringMap's associated element using name and value.
  105. m_associated_element->set_attribute(data_name, value);
  106. return {};
  107. }
  108. // https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-setitem
  109. DOM::ExceptionOr<void> DOMStringMap::set_value_of_existing_named_property(String const& name, String const& value)
  110. {
  111. return set_value_of_new_named_property(name, value);
  112. }
  113. // https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-removeitem
  114. bool DOMStringMap::delete_existing_named_property(String const& name)
  115. {
  116. AK::StringBuilder builder;
  117. // 2. Insert the string data- at the front of name.
  118. // NOTE: This is done out of order because StringBuilder doesn't have prepend.
  119. builder.append("data-");
  120. for (auto character : name) {
  121. // 1. For each ASCII upper alpha in name, insert a U+002D HYPHEN-MINUS character (-) before the character and replace the character with the same character converted to ASCII lowercase.
  122. if (is_ascii_upper_alpha(character)) {
  123. builder.append('-');
  124. builder.append(to_ascii_lowercase(character));
  125. continue;
  126. }
  127. builder.append(character);
  128. }
  129. // Remove an attribute by name given name and the DOMStringMap's associated element.
  130. auto data_name = builder.to_string();
  131. m_associated_element->remove_attribute(data_name);
  132. // The spec doesn't have the step. This indicates that the deletion was successful.
  133. return true;
  134. }
  135. }