DOMTokenList.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. GC_DEFINE_ALLOCATOR(DOMTokenList);
  47. GC::Ref<DOMTokenList> DOMTokenList::create(Element& associated_element, FlyString associated_attribute)
  48. {
  49. auto& realm = associated_element.realm();
  50. return realm.create<DOMTokenList>(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. // https://html.spec.whatwg.org/multipage/links.html#linkTypes
  175. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#attr-iframe-sandbox
  176. static HashMap<SupportedTokenKey, Vector<StringView>> supported_tokens_map = {
  177. { { HTML::TagNames::link, HTML::AttributeNames::rel }, { "modulepreload"sv, "preload"sv, "preconnect"sv, "dns-prefetch"sv, "stylesheet"sv, "icon"sv, "alternate"sv, "prefetch"sv, "prerender"sv, "next"sv, "manifest"sv, "apple-touch-icon"sv, "apple-touch-icon-precomposed"sv, "canonical"sv } },
  178. { { HTML::TagNames::a, HTML::AttributeNames::rel }, { "noreferrer"sv, "noopener"sv, "opener"sv } },
  179. { { HTML::TagNames::area, HTML::AttributeNames::rel }, { "noreferrer"sv, "noopener"sv, "opener"sv } },
  180. { { HTML::TagNames::form, HTML::AttributeNames::rel }, { "noreferrer"sv, "noopener"sv, "opener"sv } },
  181. { { HTML::TagNames::iframe, HTML::AttributeNames::sandbox }, { "allow-downloads"sv, "allow-forms"sv, "allow-modals"sv, "allow-orientation-lock"sv, "allow-pointer-lock"sv, "allow-popups"sv, "allow-popups-to-escape-sandbox"sv, "allow-presentation"sv, "allow-same-origin"sv, "allow-scripts"sv, "allow-top-navigation"sv, "allow-top-navigation-by-user-activation"sv, "allow-top-navigation-to-custom-protocols"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_element->local_name(), 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. // 2. Let lowercase token be a copy of token, in ASCII lowercase.
  188. auto lowercase_token = token.to_lowercase_string();
  189. // 3. If lowercase token is present in supported tokens, return true.
  190. if (supported_tokens->contains_slow(lowercase_token))
  191. return true;
  192. // 4. Return false.
  193. return false;
  194. }
  195. // https://dom.spec.whatwg.org/#concept-ordered-set-serializer
  196. String DOMTokenList::serialize_ordered_set() const
  197. {
  198. StringBuilder builder;
  199. builder.join(' ', m_token_set);
  200. return MUST(builder.to_string());
  201. }
  202. // https://dom.spec.whatwg.org/#dom-domtokenlist-value
  203. String DOMTokenList::value() const
  204. {
  205. return m_associated_element->get_attribute_value(m_associated_attribute);
  206. }
  207. // https://dom.spec.whatwg.org/#ref-for-concept-element-attributes-set-value%E2%91%A2
  208. void DOMTokenList::set_value(String const& value)
  209. {
  210. GC::Ptr<DOM::Element> associated_element = m_associated_element.ptr();
  211. if (!associated_element)
  212. return;
  213. MUST(associated_element->set_attribute(m_associated_attribute, value));
  214. }
  215. WebIDL::ExceptionOr<void> DOMTokenList::validate_token(StringView token) const
  216. {
  217. TRY(validate_token_not_empty(token));
  218. TRY(validate_token_not_whitespace(token));
  219. return {};
  220. }
  221. WebIDL::ExceptionOr<void> DOMTokenList::validate_token_not_empty(StringView token) const
  222. {
  223. if (token.is_empty())
  224. return WebIDL::SyntaxError::create(realm(), "Non-empty DOM tokens are not allowed"_string);
  225. return {};
  226. }
  227. WebIDL::ExceptionOr<void> DOMTokenList::validate_token_not_whitespace(StringView token) const
  228. {
  229. if (any_of(token, Infra::is_ascii_whitespace))
  230. return WebIDL::InvalidCharacterError::create(realm(), "DOM tokens containing ASCII whitespace are not allowed"_string);
  231. return {};
  232. }
  233. // https://dom.spec.whatwg.org/#concept-dtl-update
  234. void DOMTokenList::run_update_steps()
  235. {
  236. GC::Ptr<DOM::Element> associated_element = m_associated_element.ptr();
  237. if (!associated_element)
  238. return;
  239. // 1. If the associated element does not have an associated attribute and token set is empty, then return.
  240. if (!associated_element->has_attribute(m_associated_attribute) && m_token_set.is_empty())
  241. return;
  242. // 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.
  243. MUST(associated_element->set_attribute(m_associated_attribute, serialize_ordered_set()));
  244. }
  245. Optional<JS::Value> DOMTokenList::item_value(size_t index) const
  246. {
  247. auto string = item(index);
  248. if (!string.has_value())
  249. return {};
  250. return JS::PrimitiveString::create(vm(), string.release_value());
  251. }
  252. }