DOMTokenList.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/CharacterTypes.h>
  8. #include <AK/StringBuilder.h>
  9. #include <LibWeb/DOM/DOMException.h>
  10. #include <LibWeb/DOM/DOMTokenList.h>
  11. #include <LibWeb/DOM/Document.h>
  12. #include <LibWeb/DOM/Element.h>
  13. #include <LibWeb/HTML/Window.h>
  14. namespace {
  15. // https://infra.spec.whatwg.org/#set-append
  16. inline void append_to_ordered_set(Vector<String>& set, String item)
  17. {
  18. if (!set.contains_slow(item))
  19. set.append(move(item));
  20. }
  21. // https://infra.spec.whatwg.org/#list-remove
  22. inline void remove_from_ordered_set(Vector<String>& set, StringView item)
  23. {
  24. set.remove_first_matching([&](auto const& value) { return value == item; });
  25. }
  26. // https://infra.spec.whatwg.org/#set-replace
  27. inline void replace_in_ordered_set(Vector<String>& set, StringView item, String replacement)
  28. {
  29. auto item_index = set.find_first_index(item);
  30. VERIFY(item_index.has_value());
  31. auto replacement_index = set.find_first_index(replacement);
  32. if (!replacement_index.has_value()) {
  33. set[*item_index] = move(replacement);
  34. return;
  35. }
  36. auto index_to_set = min(*item_index, *replacement_index);
  37. auto index_to_remove = max(*item_index, *replacement_index);
  38. if (index_to_set == index_to_remove)
  39. return;
  40. set[index_to_set] = move(replacement);
  41. set.remove(index_to_remove);
  42. }
  43. }
  44. namespace Web::DOM {
  45. DOMTokenList* DOMTokenList::create(Element const& associated_element, FlyString associated_attribute)
  46. {
  47. auto& realm = associated_element.document().window().realm();
  48. return realm.heap().allocate<DOMTokenList>(realm, associated_element, move(associated_attribute));
  49. }
  50. // https://dom.spec.whatwg.org/#ref-for-domtokenlist%E2%91%A0%E2%91%A2
  51. DOMTokenList::DOMTokenList(Element const& associated_element, FlyString associated_attribute)
  52. : Bindings::LegacyPlatformObject(associated_element.window().cached_web_prototype("DOMTokenList"))
  53. , m_associated_element(associated_element)
  54. , m_associated_attribute(move(associated_attribute))
  55. {
  56. auto value = associated_element.get_attribute(m_associated_attribute);
  57. associated_attribute_changed(value);
  58. }
  59. // https://dom.spec.whatwg.org/#ref-for-domtokenlist%E2%91%A0%E2%91%A1
  60. void DOMTokenList::associated_attribute_changed(StringView value)
  61. {
  62. m_token_set.clear();
  63. if (value.is_empty())
  64. return;
  65. auto split_values = value.split_view(' ');
  66. for (auto const& split_value : split_values)
  67. append_to_ordered_set(m_token_set, split_value);
  68. }
  69. // https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-indices%E2%91%A3
  70. bool DOMTokenList::is_supported_property_index(u32 index) const
  71. {
  72. return index < m_token_set.size();
  73. }
  74. // https://dom.spec.whatwg.org/#dom-domtokenlist-item
  75. String const& DOMTokenList::item(size_t index) const
  76. {
  77. static const String null_string {};
  78. // 1. If index is equal to or greater than this’s token set’s size, then return null.
  79. if (index >= m_token_set.size())
  80. return null_string;
  81. // 2. Return this’s token set[index].
  82. return m_token_set[index];
  83. }
  84. // https://dom.spec.whatwg.org/#dom-domtokenlist-contains
  85. bool DOMTokenList::contains(StringView token)
  86. {
  87. return m_token_set.contains_slow(token);
  88. }
  89. // https://dom.spec.whatwg.org/#dom-domtokenlist-add
  90. WebIDL::ExceptionOr<void> DOMTokenList::add(Vector<String> const& tokens)
  91. {
  92. // 1. For each token in tokens:
  93. for (auto const& token : tokens) {
  94. // a. If token is the empty string, then throw a "SyntaxError" DOMException.
  95. // b. If token contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException.
  96. TRY(validate_token(token));
  97. // 2. For each token in tokens, append token to this’s token set.
  98. append_to_ordered_set(m_token_set, token);
  99. }
  100. // 3. Run the update steps.
  101. run_update_steps();
  102. return {};
  103. }
  104. // https://dom.spec.whatwg.org/#dom-domtokenlist-remove
  105. WebIDL::ExceptionOr<void> DOMTokenList::remove(Vector<String> const& tokens)
  106. {
  107. // 1. For each token in tokens:
  108. for (auto const& token : tokens) {
  109. // a. If token is the empty string, then throw a "SyntaxError" DOMException.
  110. // b. If token contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException.
  111. TRY(validate_token(token));
  112. // 2. For each token in tokens, remove token from this’s token set.
  113. remove_from_ordered_set(m_token_set, token);
  114. }
  115. // 3. Run the update steps.
  116. run_update_steps();
  117. return {};
  118. }
  119. // https://dom.spec.whatwg.org/#dom-domtokenlist-toggle
  120. WebIDL::ExceptionOr<bool> DOMTokenList::toggle(String const& token, Optional<bool> force)
  121. {
  122. // 1. If token is the empty string, then throw a "SyntaxError" DOMException.
  123. // 2. If token contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException.
  124. TRY(validate_token(token));
  125. // 3. If this’s token set[token] exists, then:
  126. if (contains(token)) {
  127. // 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.
  128. if (!force.has_value() || !force.value()) {
  129. remove_from_ordered_set(m_token_set, token);
  130. run_update_steps();
  131. return false;
  132. }
  133. // b. Return true.
  134. return true;
  135. }
  136. // 4. Otherwise, if force not given or is true, append token to this’s token set, run the update steps, and return true.
  137. if (!force.has_value() || force.value()) {
  138. append_to_ordered_set(m_token_set, token);
  139. run_update_steps();
  140. return true;
  141. }
  142. // 5. Return false.
  143. return false;
  144. }
  145. // https://dom.spec.whatwg.org/#dom-domtokenlist-replace
  146. WebIDL::ExceptionOr<bool> DOMTokenList::replace(String const& token, String const& new_token)
  147. {
  148. // 1. If either token or newToken is the empty string, then throw a "SyntaxError" DOMException.
  149. // 2. If either token or newToken contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException.
  150. TRY(validate_token(token));
  151. TRY(validate_token(new_token));
  152. // 3. If this’s token set does not contain token, then return false.
  153. if (!contains(token))
  154. return false;
  155. // 4. Replace token in this’s token set with newToken.
  156. replace_in_ordered_set(m_token_set, token, new_token);
  157. // 5. Run the update steps.
  158. run_update_steps();
  159. // 6. Return true.
  160. return true;
  161. }
  162. // https://dom.spec.whatwg.org/#dom-domtokenlist-supports
  163. // https://dom.spec.whatwg.org/#concept-domtokenlist-validation
  164. WebIDL::ExceptionOr<bool> DOMTokenList::supports([[maybe_unused]] StringView token)
  165. {
  166. // FIXME: Implement this fully when any use case defines supported tokens.
  167. // 1. If the associated attribute’s local name does not define supported tokens, throw a TypeError.
  168. return WebIDL::SimpleException {
  169. WebIDL::SimpleExceptionType::TypeError,
  170. String::formatted("Attribute {} does not define any supported tokens", m_associated_attribute)
  171. };
  172. // 2. Let lowercase token be a copy of token, in ASCII lowercase.
  173. // 3. If lowercase token is present in supported tokens, return true.
  174. // 4. Return false.
  175. }
  176. // https://dom.spec.whatwg.org/#dom-domtokenlist-value
  177. String DOMTokenList::value() const
  178. {
  179. StringBuilder builder;
  180. builder.join(' ', m_token_set);
  181. return builder.build();
  182. }
  183. // https://dom.spec.whatwg.org/#ref-for-concept-element-attributes-set-value%E2%91%A2
  184. void DOMTokenList::set_value(String value)
  185. {
  186. JS::GCPtr<DOM::Element> associated_element = m_associated_element.ptr();
  187. if (!associated_element)
  188. return;
  189. associated_element->set_attribute(m_associated_attribute, move(value));
  190. }
  191. WebIDL::ExceptionOr<void> DOMTokenList::validate_token(StringView token) const
  192. {
  193. if (token.is_empty())
  194. return SyntaxError::create(global_object(), "Non-empty DOM tokens are not allowed");
  195. if (any_of(token, is_ascii_space))
  196. return InvalidCharacterError::create(global_object(), "DOM tokens containing ASCII whitespace are not allowed");
  197. return {};
  198. }
  199. // https://dom.spec.whatwg.org/#concept-dtl-update
  200. void DOMTokenList::run_update_steps()
  201. {
  202. JS::GCPtr<DOM::Element> associated_element = m_associated_element.ptr();
  203. if (!associated_element)
  204. return;
  205. // 1. If the associated element does not have an associated attribute and token set is empty, then return.
  206. if (!associated_element->has_attribute(m_associated_attribute) && m_token_set.is_empty())
  207. return;
  208. // 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.
  209. associated_element->set_attribute(m_associated_attribute, value());
  210. }
  211. JS::Value DOMTokenList::item_value(size_t index) const
  212. {
  213. auto const& string = item(index);
  214. if (string.is_null())
  215. return JS::js_undefined();
  216. return JS::js_string(vm(), string);
  217. }
  218. }