HTMLOptionElement.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2022-2024, Andreas Kling <andreas@ladybird.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/StringBuilder.h>
  8. #include <LibWeb/ARIA/Roles.h>
  9. #include <LibWeb/Bindings/HTMLOptionElementPrototype.h>
  10. #include <LibWeb/Bindings/Intrinsics.h>
  11. #include <LibWeb/DOM/Node.h>
  12. #include <LibWeb/DOM/Text.h>
  13. #include <LibWeb/HTML/HTMLOptGroupElement.h>
  14. #include <LibWeb/HTML/HTMLOptionElement.h>
  15. #include <LibWeb/HTML/HTMLScriptElement.h>
  16. #include <LibWeb/HTML/HTMLSelectElement.h>
  17. #include <LibWeb/HighResolutionTime/TimeOrigin.h>
  18. #include <LibWeb/Infra/Strings.h>
  19. #include <LibWeb/SVG/SVGScriptElement.h>
  20. namespace Web::HTML {
  21. GC_DEFINE_ALLOCATOR(HTMLOptionElement);
  22. static u64 m_next_selectedness_update_index = 1;
  23. HTMLOptionElement::HTMLOptionElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  24. : HTMLElement(document, move(qualified_name))
  25. {
  26. }
  27. HTMLOptionElement::~HTMLOptionElement() = default;
  28. void HTMLOptionElement::initialize(JS::Realm& realm)
  29. {
  30. Base::initialize(realm);
  31. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLOptionElement);
  32. }
  33. void HTMLOptionElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
  34. {
  35. Base::attribute_changed(name, old_value, value, namespace_);
  36. if (name == HTML::AttributeNames::selected) {
  37. if (!value.has_value()) {
  38. // Whenever an option element's selected attribute is removed, if its dirtiness is false, its selectedness must be set to false.
  39. if (!m_dirty)
  40. set_selected_internal(false);
  41. } else {
  42. // Except where otherwise specified, when the element is created, its selectedness must be set to true
  43. // if the element has a selected attribute. Whenever an option element's selected attribute is added,
  44. // if its dirtiness is false, its selectedness must be set to true.
  45. if (!m_dirty)
  46. set_selected_internal(true);
  47. }
  48. }
  49. }
  50. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-selected
  51. void HTMLOptionElement::set_selected(bool selected)
  52. {
  53. // On setting, it must set the element's selectedness to the new value, set its dirtiness to true, and then cause the element to ask for a reset.
  54. set_selected_internal(selected);
  55. m_dirty = true;
  56. ask_for_a_reset();
  57. }
  58. void HTMLOptionElement::set_selected_internal(bool selected)
  59. {
  60. m_selected = selected;
  61. if (selected)
  62. m_selectedness_update_index = m_next_selectedness_update_index++;
  63. }
  64. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-value
  65. String HTMLOptionElement::value() const
  66. {
  67. // The value of an option element is the value of the value content attribute, if there is one.
  68. // ...or, if there is not, the value of the element's text IDL attribute.
  69. return attribute(HTML::AttributeNames::value).value_or(text());
  70. }
  71. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-value
  72. WebIDL::ExceptionOr<void> HTMLOptionElement::set_value(String const& value)
  73. {
  74. return set_attribute(HTML::AttributeNames::value, value);
  75. }
  76. static void concatenate_descendants_text_content(DOM::Node const* node, StringBuilder& builder)
  77. {
  78. if (is<HTMLScriptElement>(node) || is<SVG::SVGScriptElement>(node))
  79. return;
  80. if (is<DOM::Text>(node))
  81. builder.append(verify_cast<DOM::Text>(node)->data());
  82. node->for_each_child([&](auto const& node) {
  83. concatenate_descendants_text_content(&node, builder);
  84. return IterationDecision::Continue;
  85. });
  86. }
  87. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-label
  88. String HTMLOptionElement::label() const
  89. {
  90. // The label IDL attribute, on getting, if there is a label content attribute,
  91. // must return that attribute's value; otherwise, it must return the element's label.
  92. if (auto label = attribute(HTML::AttributeNames::label); label.has_value())
  93. return label.release_value();
  94. return text();
  95. }
  96. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-label
  97. void HTMLOptionElement::set_label(String const& label)
  98. {
  99. MUST(set_attribute(HTML::AttributeNames::label, label));
  100. }
  101. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-text
  102. String HTMLOptionElement::text() const
  103. {
  104. StringBuilder builder;
  105. // Concatenation of data of all the Text node descendants of the option element, in tree order,
  106. // excluding any that are descendants of descendants of the option element that are themselves
  107. // script or SVG script elements.
  108. for_each_child([&](auto const& node) {
  109. concatenate_descendants_text_content(&node, builder);
  110. return IterationDecision::Continue;
  111. });
  112. // Return the result of stripping and collapsing ASCII whitespace from the above concatenation.
  113. return MUST(Infra::strip_and_collapse_whitespace(builder.string_view()));
  114. }
  115. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-text
  116. void HTMLOptionElement::set_text(String const& text)
  117. {
  118. string_replace_all(text);
  119. }
  120. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-option-index
  121. int HTMLOptionElement::index() const
  122. {
  123. // An option element's index is the number of option elements that are in the same list of options but that come before it in tree order.
  124. if (auto select_element = first_ancestor_of_type<HTMLSelectElement>()) {
  125. int index = 0;
  126. for (auto const& option_element : select_element->list_of_options()) {
  127. if (option_element.ptr() == this)
  128. return index;
  129. ++index;
  130. }
  131. }
  132. // If the option element is not in a list of options, then the option element's index is zero.
  133. return 0;
  134. }
  135. // https://html.spec.whatwg.org/multipage/form-elements.html#ask-for-a-reset
  136. void HTMLOptionElement::ask_for_a_reset()
  137. {
  138. // If an option element in the list of options asks for a reset, then run that select element's selectedness setting algorithm.
  139. if (auto* select = first_ancestor_of_type<HTMLSelectElement>()) {
  140. select->update_selectedness();
  141. }
  142. }
  143. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-option-disabled
  144. bool HTMLOptionElement::disabled() const
  145. {
  146. // An option element is disabled if its disabled attribute is present or if it is a child of an optgroup element whose disabled attribute is present.
  147. return has_attribute(AttributeNames::disabled)
  148. || (parent() && is<HTMLOptGroupElement>(parent()) && static_cast<HTMLOptGroupElement const&>(*parent()).has_attribute(AttributeNames::disabled));
  149. }
  150. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-form
  151. GC::Ptr<HTMLFormElement> HTMLOptionElement::form() const
  152. {
  153. // The form IDL attribute's behavior depends on whether the option element is in a select element or not.
  154. // If the option has a select element as its parent, or has an optgroup element as its parent and that optgroup element has a select element as its parent,
  155. // then the form IDL attribute must return the same value as the form IDL attribute on that select element.
  156. // Otherwise, it must return null.
  157. auto const* parent = parent_element();
  158. if (is<HTMLOptGroupElement>(parent))
  159. parent = parent->parent_element();
  160. if (is<HTML::HTMLSelectElement>(parent)) {
  161. auto const* select_element = verify_cast<HTMLSelectElement>(parent);
  162. return const_cast<HTMLFormElement*>(select_element->form());
  163. }
  164. return {};
  165. }
  166. Optional<ARIA::Role> HTMLOptionElement::default_role() const
  167. {
  168. // https://www.w3.org/TR/html-aria/#el-option
  169. // TODO: Only an option element that is in a list of options or that represents a suggestion in a datalist should return option
  170. return ARIA::Role::option;
  171. }
  172. void HTMLOptionElement::inserted()
  173. {
  174. Base::inserted();
  175. set_selected_internal(selected());
  176. // The option HTML element insertion steps, given insertedOption, are:
  177. // 1. For each ancestor of insertedOption's ancestors in reverse tree order:
  178. for (auto* ancestor = parent_node(); ancestor; ancestor = ancestor->parent_node()) {
  179. // 1. If ancestor is a select element, then run the selectedness setting algorithm given ancestor and return.
  180. if (is<HTMLSelectElement>(*ancestor)) {
  181. static_cast<HTMLSelectElement&>(*ancestor).update_selectedness();
  182. return;
  183. }
  184. }
  185. }
  186. void HTMLOptionElement::removed_from(Node* old_parent)
  187. {
  188. Base::removed_from(old_parent);
  189. // The option HTML element removing steps, given removedOption and oldParent, are:
  190. // 1. For each ancestor of oldParent's inclusive ancestors in reverse tree order:
  191. for (auto* ancestor = old_parent; ancestor; ancestor = ancestor->parent_node()) {
  192. // 1. If ancestor is a select element, then run the selectedness setting algorithm given ancestor and return.
  193. if (is<HTMLSelectElement>(*ancestor)) {
  194. static_cast<HTMLSelectElement&>(*ancestor).update_selectedness();
  195. return;
  196. }
  197. }
  198. }
  199. }