DOMStringMap.cpp 8.2 KB

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