DOMTokenList.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.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/#ref-for-dfn-supported-property-indices%E2%91%A3
  82. bool DOMTokenList::is_supported_property_index(u32 index) const
  83. {
  84. return index < m_token_set.size();
  85. }
  86. // https://dom.spec.whatwg.org/#dom-domtokenlist-item
  87. Optional<String> DOMTokenList::item(size_t index) const
  88. {
  89. // 1. If index is equal to or greater than this’s token set’s size, then return null.
  90. if (index >= m_token_set.size())
  91. return {};
  92. // 2. Return this’s token set[index].
  93. return m_token_set[index];
  94. }
  95. // https://dom.spec.whatwg.org/#dom-domtokenlist-contains
  96. bool DOMTokenList::contains(String const& token)
  97. {
  98. return m_token_set.contains_slow(token);
  99. }
  100. // https://dom.spec.whatwg.org/#dom-domtokenlist-add
  101. WebIDL::ExceptionOr<void> DOMTokenList::add(Vector<String> const& tokens)
  102. {
  103. // 1. For each token in tokens:
  104. for (auto const& token : tokens) {
  105. // a. If token is the empty string, then throw a "SyntaxError" DOMException.
  106. // b. If token contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException.
  107. TRY(validate_token(token));
  108. // 2. For each token in tokens, append token to this’s token set.
  109. append_to_ordered_set(m_token_set, token);
  110. }
  111. // 3. Run the update steps.
  112. run_update_steps();
  113. return {};
  114. }
  115. // https://dom.spec.whatwg.org/#dom-domtokenlist-remove
  116. WebIDL::ExceptionOr<void> DOMTokenList::remove(Vector<String> const& tokens)
  117. {
  118. // 1. For each token in tokens:
  119. for (auto const& token : tokens) {
  120. // a. If token is the empty string, then throw a "SyntaxError" DOMException.
  121. // b. If token contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException.
  122. TRY(validate_token(token));
  123. // 2. For each token in tokens, remove token from this’s token set.
  124. remove_from_ordered_set(m_token_set, token);
  125. }
  126. // 3. Run the update steps.
  127. run_update_steps();
  128. return {};
  129. }
  130. // https://dom.spec.whatwg.org/#dom-domtokenlist-toggle
  131. WebIDL::ExceptionOr<bool> DOMTokenList::toggle(String const& token, Optional<bool> force)
  132. {
  133. // 1. If token is the empty string, then throw a "SyntaxError" DOMException.
  134. // 2. If token contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException.
  135. TRY(validate_token(token));
  136. // 3. If this’s token set[token] exists, then:
  137. if (contains(token)) {
  138. // 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.
  139. if (!force.has_value() || !force.value()) {
  140. remove_from_ordered_set(m_token_set, token);
  141. run_update_steps();
  142. return false;
  143. }
  144. // b. Return true.
  145. return true;
  146. }
  147. // 4. Otherwise, if force not given or is true, append token to this’s token set, run the update steps, and return true.
  148. if (!force.has_value() || force.value()) {
  149. append_to_ordered_set(m_token_set, token);
  150. run_update_steps();
  151. return true;
  152. }
  153. // 5. Return false.
  154. return false;
  155. }
  156. // https://dom.spec.whatwg.org/#dom-domtokenlist-replace
  157. WebIDL::ExceptionOr<bool> DOMTokenList::replace(String const& token, String const& new_token)
  158. {
  159. // 1. If either token or newToken is the empty string, then throw a "SyntaxError" DOMException.
  160. TRY(validate_token_not_empty(token));
  161. TRY(validate_token_not_empty(new_token));
  162. // 2. If either token or newToken contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException.
  163. TRY(validate_token_not_whitespace(token));
  164. TRY(validate_token_not_whitespace(new_token));
  165. // 3. If this’s token set does not contain token, then return false.
  166. if (!contains(token))
  167. return false;
  168. // 4. Replace token in this’s token set with newToken.
  169. replace_in_ordered_set(m_token_set, token, new_token);
  170. // 5. Run the update steps.
  171. run_update_steps();
  172. // 6. Return true.
  173. return true;
  174. }
  175. // https://dom.spec.whatwg.org/#dom-domtokenlist-supports
  176. // https://dom.spec.whatwg.org/#concept-domtokenlist-validation
  177. WebIDL::ExceptionOr<bool> DOMTokenList::supports(StringView token)
  178. {
  179. static HashMap<FlyString, Vector<StringView>> supported_tokens_map = {
  180. // NOTE: The supported values for rel were taken from HTMLLinkElement::Relationship
  181. { HTML::AttributeNames::rel, { "alternate"sv, "stylesheet"sv, "preload"sv, "dns-prefetch"sv, "preconnect"sv, "icon"sv } },
  182. };
  183. // 1. If the associated attribute’s local name does not define supported tokens, throw a TypeError.
  184. auto supported_tokens = supported_tokens_map.get(m_associated_attribute);
  185. if (!supported_tokens.has_value())
  186. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Attribute {} does not define any supported tokens", m_associated_attribute)) };
  187. // AD-HOC: Other browsers return false for rel attributes on non-link elements for all attribute values we currently support.
  188. if (m_associated_attribute == HTML::AttributeNames::rel && !is<HTML::HTMLLinkElement>(*m_associated_element))
  189. return false;
  190. // 2. Let lowercase token be a copy of token, in ASCII lowercase.
  191. auto lowercase_token = token.to_lowercase_string();
  192. // 3. If lowercase token is present in supported tokens, return true.
  193. if (supported_tokens->contains_slow(lowercase_token))
  194. return true;
  195. // 4. Return false.
  196. return false;
  197. }
  198. // https://dom.spec.whatwg.org/#concept-ordered-set-serializer
  199. String DOMTokenList::serialize_ordered_set() const
  200. {
  201. StringBuilder builder;
  202. builder.join(' ', m_token_set);
  203. return MUST(builder.to_string());
  204. }
  205. // https://dom.spec.whatwg.org/#dom-domtokenlist-value
  206. String DOMTokenList::value() const
  207. {
  208. return m_associated_element->get_attribute_value(m_associated_attribute);
  209. }
  210. // https://dom.spec.whatwg.org/#ref-for-concept-element-attributes-set-value%E2%91%A2
  211. void DOMTokenList::set_value(String const& value)
  212. {
  213. JS::GCPtr<DOM::Element> associated_element = m_associated_element.ptr();
  214. if (!associated_element)
  215. return;
  216. MUST(associated_element->set_attribute(m_associated_attribute, value));
  217. }
  218. WebIDL::ExceptionOr<void> DOMTokenList::validate_token(StringView token) const
  219. {
  220. TRY(validate_token_not_empty(token));
  221. TRY(validate_token_not_whitespace(token));
  222. return {};
  223. }
  224. WebIDL::ExceptionOr<void> DOMTokenList::validate_token_not_empty(StringView token) const
  225. {
  226. if (token.is_empty())
  227. return WebIDL::SyntaxError::create(realm(), "Non-empty DOM tokens are not allowed"_fly_string);
  228. return {};
  229. }
  230. WebIDL::ExceptionOr<void> DOMTokenList::validate_token_not_whitespace(StringView token) const
  231. {
  232. if (any_of(token, Infra::is_ascii_whitespace))
  233. return WebIDL::InvalidCharacterError::create(realm(), "DOM tokens containing ASCII whitespace are not allowed"_fly_string);
  234. return {};
  235. }
  236. // https://dom.spec.whatwg.org/#concept-dtl-update
  237. void DOMTokenList::run_update_steps()
  238. {
  239. JS::GCPtr<DOM::Element> associated_element = m_associated_element.ptr();
  240. if (!associated_element)
  241. return;
  242. // 1. If the associated element does not have an associated attribute and token set is empty, then return.
  243. if (!associated_element->has_attribute(m_associated_attribute) && m_token_set.is_empty())
  244. return;
  245. // 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.
  246. MUST(associated_element->set_attribute(m_associated_attribute, serialize_ordered_set()));
  247. }
  248. WebIDL::ExceptionOr<JS::Value> DOMTokenList::item_value(size_t index) const
  249. {
  250. auto string = item(index);
  251. if (!string.has_value())
  252. return JS::js_undefined();
  253. return JS::PrimitiveString::create(vm(), string.release_value());
  254. }
  255. }