DOMTokenList.cpp 8.3 KB

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