DOMStringMap.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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() = default;
  15. // https://html.spec.whatwg.org/multipage/dom.html#concept-domstringmap-pairs
  16. Vector<DOMStringMap::NameValuePair> DOMStringMap::get_name_value_pairs() const
  17. {
  18. // 1. Let list be an empty list of name-value pairs.
  19. Vector<NameValuePair> list;
  20. // 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,
  21. // 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
  22. // is the attribute's value.
  23. m_associated_element->for_each_attribute([&](auto& name, auto& value) {
  24. if (!name.starts_with("data-"))
  25. return;
  26. auto name_after_starting_data = name.view().substring_view(5);
  27. for (auto character : name_after_starting_data) {
  28. if (is_ascii_upper_alpha(character))
  29. return;
  30. }
  31. // 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
  32. // that followed it by the same character converted to ASCII uppercase.
  33. StringBuilder builder;
  34. for (size_t character_index = 0; character_index < name_after_starting_data.length(); ++character_index) {
  35. auto current_character = name_after_starting_data[character_index];
  36. if (character_index + 1 < name_after_starting_data.length() && current_character == '-') {
  37. auto next_character = name_after_starting_data[character_index + 1];
  38. if (is_ascii_lower_alpha(next_character)) {
  39. builder.append(to_ascii_uppercase(next_character));
  40. // Skip the next character
  41. ++character_index;
  42. return;
  43. }
  44. }
  45. builder.append(current_character);
  46. }
  47. list.append({ builder.to_string(), value });
  48. });
  49. // 4. Return list.
  50. return list;
  51. }
  52. // https://html.spec.whatwg.org/multipage/dom.html#concept-domstringmap-pairs
  53. // NOTE: There isn't a direct link to this, so the link is to one of the algorithms above it.
  54. Vector<String> DOMStringMap::supported_property_names() const
  55. {
  56. // 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.
  57. Vector<String> names;
  58. auto name_value_pairs = get_name_value_pairs();
  59. for (auto& name_value_pair : name_value_pairs) {
  60. names.append(name_value_pair.name);
  61. }
  62. return names;
  63. }
  64. // https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-nameditem
  65. String DOMStringMap::determine_value_of_named_property(String const& name) const
  66. {
  67. // 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
  68. // DOMStringMap's name-value pairs.
  69. auto name_value_pairs = get_name_value_pairs();
  70. auto optional_value = name_value_pairs.first_matching([&name](NameValuePair& name_value_pair) {
  71. return name_value_pair.name == name;
  72. });
  73. // NOTE: determine_value_of_named_property is only called if `name` is in supported_property_names.
  74. VERIFY(optional_value.has_value());
  75. return optional_value->value;
  76. }
  77. // https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-setitem
  78. DOM::ExceptionOr<void> DOMStringMap::set_value_of_new_named_property(String const& name, String const& value)
  79. {
  80. AK::StringBuilder builder;
  81. // 3. Insert the string data- at the front of name.
  82. // NOTE: This is done out of order because StringBuilder doesn't have prepend.
  83. builder.append("data-");
  84. for (size_t character_index = 0; character_index < name.length(); ++character_index) {
  85. // 1. If name contains a U+002D HYPHEN-MINUS character (-) followed by an ASCII lower alpha, then throw a "SyntaxError" DOMException.
  86. auto current_character = name[character_index];
  87. if (current_character == '-' && character_index + 1 < name.length()) {
  88. auto next_character = name[character_index + 1];
  89. if (is_ascii_lower_alpha(next_character))
  90. return DOM::SyntaxError::create("Name cannot contain a '-' followed by a lowercase character.");
  91. }
  92. // 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.
  93. if (is_ascii_upper_alpha(current_character)) {
  94. builder.append('-');
  95. builder.append(to_ascii_lowercase(current_character));
  96. continue;
  97. }
  98. builder.append(current_character);
  99. }
  100. auto data_name = builder.to_string();
  101. // FIXME: 4. If name does not match the XML Name production, throw an "InvalidCharacterError" DOMException.
  102. // 5. Set an attribute value for the DOMStringMap's associated element using name and value.
  103. m_associated_element->set_attribute(data_name, value);
  104. return {};
  105. }
  106. // https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-setitem
  107. DOM::ExceptionOr<void> DOMStringMap::set_value_of_existing_named_property(String const& name, String const& value)
  108. {
  109. return set_value_of_new_named_property(name, value);
  110. }
  111. // https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-removeitem
  112. bool DOMStringMap::delete_existing_named_property(String const& name)
  113. {
  114. AK::StringBuilder builder;
  115. // 2. Insert the string data- at the front of name.
  116. // NOTE: This is done out of order because StringBuilder doesn't have prepend.
  117. builder.append("data-");
  118. for (auto character : name) {
  119. // 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.
  120. if (is_ascii_upper_alpha(character)) {
  121. builder.append('-');
  122. builder.append(to_ascii_lowercase(character));
  123. continue;
  124. }
  125. builder.append(character);
  126. }
  127. // Remove an attribute by name given name and the DOMStringMap's associated element.
  128. auto data_name = builder.to_string();
  129. m_associated_element->remove_attribute(data_name);
  130. // The spec doesn't have the step. This indicates that the deletion was successful.
  131. return true;
  132. }
  133. }