DOMStringMap.cpp 8.4 KB

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