DOMTokenList.cpp 9.5 KB

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