DOMTokenList.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/StringBuilder.h>
  8. #include <LibWeb/Bindings/DOMTokenListPrototype.h>
  9. #include <LibWeb/DOM/DOMTokenList.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/Element.h>
  12. #include <LibWeb/HTML/HTMLLinkElement.h>
  13. #include <LibWeb/Infra/CharacterTypes.h>
  14. #include <LibWeb/WebIDL/DOMException.h>
  15. namespace {
  16. // https://infra.spec.whatwg.org/#set-append
  17. inline void append_to_ordered_set(Vector<String>& set, String item)
  18. {
  19. if (!set.contains_slow(item))
  20. set.append(move(item));
  21. }
  22. // https://infra.spec.whatwg.org/#list-remove
  23. inline void remove_from_ordered_set(Vector<String>& set, StringView item)
  24. {
  25. set.remove_first_matching([&](auto const& value) { return value == item; });
  26. }
  27. // https://infra.spec.whatwg.org/#set-replace
  28. inline void replace_in_ordered_set(Vector<String>& set, String const& item, String replacement)
  29. {
  30. auto item_index = set.find_first_index(item);
  31. VERIFY(item_index.has_value());
  32. auto replacement_index = set.find_first_index(replacement);
  33. if (!replacement_index.has_value()) {
  34. set[*item_index] = move(replacement);
  35. return;
  36. }
  37. auto index_to_set = min(*item_index, *replacement_index);
  38. auto index_to_remove = max(*item_index, *replacement_index);
  39. if (index_to_set == index_to_remove)
  40. return;
  41. set[index_to_set] = move(replacement);
  42. set.remove(index_to_remove);
  43. }
  44. }
  45. namespace Web::DOM {
  46. JS_DEFINE_ALLOCATOR(DOMTokenList);
  47. JS::NonnullGCPtr<DOMTokenList> DOMTokenList::create(Element& associated_element, FlyString associated_attribute)
  48. {
  49. auto& realm = associated_element.realm();
  50. return realm.heap().allocate<DOMTokenList>(realm, associated_element, move(associated_attribute));
  51. }
  52. // https://dom.spec.whatwg.org/#ref-for-domtokenlist%E2%91%A0%E2%91%A2
  53. DOMTokenList::DOMTokenList(Element& associated_element, FlyString associated_attribute)
  54. : Bindings::PlatformObject(associated_element.realm())
  55. , m_associated_element(associated_element)
  56. , m_associated_attribute(move(associated_attribute))
  57. {
  58. m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = 1 };
  59. associated_attribute_changed(associated_element.get_attribute_value(m_associated_attribute));
  60. }
  61. void DOMTokenList::initialize(JS::Realm& realm)
  62. {
  63. Base::initialize(realm);
  64. WEB_SET_PROTOTYPE_FOR_INTERFACE(DOMTokenList);
  65. }
  66. void DOMTokenList::visit_edges(Cell::Visitor& visitor)
  67. {
  68. Base::visit_edges(visitor);
  69. visitor.visit(m_associated_element);
  70. }
  71. // https://dom.spec.whatwg.org/#ref-for-domtokenlist%E2%91%A0%E2%91%A1
  72. void DOMTokenList::associated_attribute_changed(StringView value)
  73. {
  74. m_token_set.clear();
  75. if (value.is_empty())
  76. return;
  77. auto split_values = value.split_view_if(Infra::is_ascii_whitespace);
  78. for (auto const& split_value : split_values)
  79. append_to_ordered_set(m_token_set, String::from_utf8(split_value).release_value_but_fixme_should_propagate_errors());
  80. }
  81. // https://dom.spec.whatwg.org/#dom-domtokenlist-item
  82. Optional<String> DOMTokenList::item(size_t index) const
  83. {
  84. // 1. If index is equal to or greater than this’s token set’s size, then return null.
  85. if (index >= m_token_set.size())
  86. return {};
  87. // 2. Return this’s token set[index].
  88. return m_token_set[index];
  89. }
  90. // https://dom.spec.whatwg.org/#dom-domtokenlist-contains
  91. bool DOMTokenList::contains(String const& token)
  92. {
  93. return m_token_set.contains_slow(token);
  94. }
  95. // https://dom.spec.whatwg.org/#dom-domtokenlist-add
  96. WebIDL::ExceptionOr<void> DOMTokenList::add(Vector<String> const& tokens)
  97. {
  98. // 1. For each token in tokens:
  99. for (auto const& token : tokens) {
  100. // a. If token is the empty string, then throw a "SyntaxError" DOMException.
  101. // b. If token contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException.
  102. TRY(validate_token(token));
  103. // 2. For each token in tokens, append token to this’s token set.
  104. append_to_ordered_set(m_token_set, token);
  105. }
  106. // 3. Run the update steps.
  107. run_update_steps();
  108. return {};
  109. }
  110. // https://dom.spec.whatwg.org/#dom-domtokenlist-remove
  111. WebIDL::ExceptionOr<void> DOMTokenList::remove(Vector<String> const& tokens)
  112. {
  113. // 1. For each token in tokens:
  114. for (auto const& token : tokens) {
  115. // a. If token is the empty string, then throw a "SyntaxError" DOMException.
  116. // b. If token contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException.
  117. TRY(validate_token(token));
  118. // 2. For each token in tokens, remove token from this’s token set.
  119. remove_from_ordered_set(m_token_set, token);
  120. }
  121. // 3. Run the update steps.
  122. run_update_steps();
  123. return {};
  124. }
  125. // https://dom.spec.whatwg.org/#dom-domtokenlist-toggle
  126. WebIDL::ExceptionOr<bool> DOMTokenList::toggle(String const& token, Optional<bool> force)
  127. {
  128. // 1. If token is the empty string, then throw a "SyntaxError" DOMException.
  129. // 2. If token contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException.
  130. TRY(validate_token(token));
  131. // 3. If this’s token set[token] exists, then:
  132. if (contains(token)) {
  133. // a. If force is either not given or is false, then remove token from this’s token set, run the update steps and return false.
  134. if (!force.has_value() || !force.value()) {
  135. remove_from_ordered_set(m_token_set, token);
  136. run_update_steps();
  137. return false;
  138. }
  139. // b. Return true.
  140. return true;
  141. }
  142. // 4. Otherwise, if force not given or is true, append token to this’s token set, run the update steps, and return true.
  143. if (!force.has_value() || force.value()) {
  144. append_to_ordered_set(m_token_set, token);
  145. run_update_steps();
  146. return true;
  147. }
  148. // 5. Return false.
  149. return false;
  150. }
  151. // https://dom.spec.whatwg.org/#dom-domtokenlist-replace
  152. WebIDL::ExceptionOr<bool> DOMTokenList::replace(String const& token, String const& new_token)
  153. {
  154. // 1. If either token or newToken is the empty string, then throw a "SyntaxError" DOMException.
  155. TRY(validate_token_not_empty(token));
  156. TRY(validate_token_not_empty(new_token));
  157. // 2. If either token or newToken contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException.
  158. TRY(validate_token_not_whitespace(token));
  159. TRY(validate_token_not_whitespace(new_token));
  160. // 3. If this’s token set does not contain token, then return false.
  161. if (!contains(token))
  162. return false;
  163. // 4. Replace token in this’s token set with newToken.
  164. replace_in_ordered_set(m_token_set, token, new_token);
  165. // 5. Run the update steps.
  166. run_update_steps();
  167. // 6. Return true.
  168. return true;
  169. }
  170. // https://dom.spec.whatwg.org/#dom-domtokenlist-supports
  171. // https://dom.spec.whatwg.org/#concept-domtokenlist-validation
  172. WebIDL::ExceptionOr<bool> DOMTokenList::supports(StringView token)
  173. {
  174. static HashMap<FlyString, Vector<StringView>> supported_tokens_map = {
  175. // NOTE: The supported values for rel were taken from HTMLLinkElement::Relationship
  176. { HTML::AttributeNames::rel, { "alternate"sv, "stylesheet"sv, "preload"sv, "dns-prefetch"sv, "preconnect"sv, "icon"sv } },
  177. };
  178. // 1. If the associated attribute’s local name does not define supported tokens, throw a TypeError.
  179. auto supported_tokens = supported_tokens_map.get(m_associated_attribute);
  180. if (!supported_tokens.has_value())
  181. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Attribute {} does not define any supported tokens", m_associated_attribute)) };
  182. // AD-HOC: Other browsers return false for rel attributes on non-link elements for all attribute values we currently support.
  183. if (m_associated_attribute == HTML::AttributeNames::rel && !is<HTML::HTMLLinkElement>(*m_associated_element))
  184. return false;
  185. // 2. Let lowercase token be a copy of token, in ASCII lowercase.
  186. auto lowercase_token = token.to_lowercase_string();
  187. // 3. If lowercase token is present in supported tokens, return true.
  188. if (supported_tokens->contains_slow(lowercase_token))
  189. return true;
  190. // 4. Return false.
  191. return false;
  192. }
  193. // https://dom.spec.whatwg.org/#concept-ordered-set-serializer
  194. String DOMTokenList::serialize_ordered_set() const
  195. {
  196. StringBuilder builder;
  197. builder.join(' ', m_token_set);
  198. return MUST(builder.to_string());
  199. }
  200. // https://dom.spec.whatwg.org/#dom-domtokenlist-value
  201. String DOMTokenList::value() const
  202. {
  203. return m_associated_element->get_attribute_value(m_associated_attribute);
  204. }
  205. // https://dom.spec.whatwg.org/#ref-for-concept-element-attributes-set-value%E2%91%A2
  206. void DOMTokenList::set_value(String const& value)
  207. {
  208. JS::GCPtr<DOM::Element> associated_element = m_associated_element.ptr();
  209. if (!associated_element)
  210. return;
  211. MUST(associated_element->set_attribute(m_associated_attribute, value));
  212. }
  213. WebIDL::ExceptionOr<void> DOMTokenList::validate_token(StringView token) const
  214. {
  215. TRY(validate_token_not_empty(token));
  216. TRY(validate_token_not_whitespace(token));
  217. return {};
  218. }
  219. WebIDL::ExceptionOr<void> DOMTokenList::validate_token_not_empty(StringView token) const
  220. {
  221. if (token.is_empty())
  222. return WebIDL::SyntaxError::create(realm(), "Non-empty DOM tokens are not allowed"_string);
  223. return {};
  224. }
  225. WebIDL::ExceptionOr<void> DOMTokenList::validate_token_not_whitespace(StringView token) const
  226. {
  227. if (any_of(token, Infra::is_ascii_whitespace))
  228. return WebIDL::InvalidCharacterError::create(realm(), "DOM tokens containing ASCII whitespace are not allowed"_string);
  229. return {};
  230. }
  231. // https://dom.spec.whatwg.org/#concept-dtl-update
  232. void DOMTokenList::run_update_steps()
  233. {
  234. JS::GCPtr<DOM::Element> associated_element = m_associated_element.ptr();
  235. if (!associated_element)
  236. return;
  237. // 1. If the associated element does not have an associated attribute and token set is empty, then return.
  238. if (!associated_element->has_attribute(m_associated_attribute) && m_token_set.is_empty())
  239. return;
  240. // 2. Set an attribute value for the associated element using associated attribute’s local name and the result of running the ordered set serializer for token set.
  241. MUST(associated_element->set_attribute(m_associated_attribute, serialize_ordered_set()));
  242. }
  243. Optional<JS::Value> DOMTokenList::item_value(size_t index) const
  244. {
  245. auto string = item(index);
  246. if (!string.has_value())
  247. return {};
  248. return JS::PrimitiveString::create(vm(), string.release_value());
  249. }
  250. }