DOMTokenList.cpp 8.9 KB

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