DOMStringMap.cpp 7.4 KB

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