HTMLOptionElement.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. namespace Web::HTML {
  20. JS_DEFINE_ALLOCATOR(HTMLOptionElement);
  21. static u64 m_next_selectedness_update_index = 1;
  22. HTMLOptionElement::HTMLOptionElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  23. : HTMLElement(document, move(qualified_name))
  24. {
  25. }
  26. HTMLOptionElement::~HTMLOptionElement() = default;
  27. void HTMLOptionElement::initialize(JS::Realm& realm)
  28. {
  29. Base::initialize(realm);
  30. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLOptionElement);
  31. }
  32. void HTMLOptionElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
  33. {
  34. Base::attribute_changed(name, old_value, value, namespace_);
  35. if (name == HTML::AttributeNames::selected) {
  36. if (!value.has_value()) {
  37. // Whenever an option element's selected attribute is removed, if its dirtiness is false, its selectedness must be set to false.
  38. if (!m_dirty)
  39. set_selected_internal(false);
  40. } else {
  41. // Except where otherwise specified, when the element is created, its selectedness must be set to true
  42. // if the element has a selected attribute. Whenever an option element's selected attribute is added,
  43. // if its dirtiness is false, its selectedness must be set to true.
  44. if (!m_dirty)
  45. set_selected_internal(true);
  46. }
  47. }
  48. }
  49. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-selected
  50. void HTMLOptionElement::set_selected(bool selected)
  51. {
  52. // 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.
  53. set_selected_internal(selected);
  54. m_dirty = true;
  55. ask_for_a_reset();
  56. }
  57. void HTMLOptionElement::set_selected_internal(bool selected)
  58. {
  59. m_selected = selected;
  60. if (selected)
  61. m_selectedness_update_index = m_next_selectedness_update_index++;
  62. }
  63. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-value
  64. String HTMLOptionElement::value() const
  65. {
  66. // The value of an option element is the value of the value content attribute, if there is one.
  67. // ...or, if there is not, the value of the element's text IDL attribute.
  68. return attribute(HTML::AttributeNames::value).value_or(text());
  69. }
  70. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-value
  71. WebIDL::ExceptionOr<void> HTMLOptionElement::set_value(String const& value)
  72. {
  73. return set_attribute(HTML::AttributeNames::value, value);
  74. }
  75. static void concatenate_descendants_text_content(DOM::Node const* node, StringBuilder& builder)
  76. {
  77. // FIXME: SVGScriptElement should also be skipped, but it doesn't exist yet.
  78. if (is<HTMLScriptElement>(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-text
  88. String HTMLOptionElement::text() const
  89. {
  90. StringBuilder builder;
  91. // Concatenation of data of all the Text node descendants of the option element, in tree order,
  92. // excluding any that are descendants of descendants of the option element that are themselves
  93. // script or SVG script elements.
  94. for_each_child([&](auto const& node) {
  95. concatenate_descendants_text_content(&node, builder);
  96. return IterationDecision::Continue;
  97. });
  98. // Return the result of stripping and collapsing ASCII whitespace from the above concatenation.
  99. return MUST(Infra::strip_and_collapse_whitespace(builder.string_view()));
  100. }
  101. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-text
  102. void HTMLOptionElement::set_text(String const& text)
  103. {
  104. string_replace_all(text);
  105. }
  106. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-option-index
  107. int HTMLOptionElement::index() const
  108. {
  109. // 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.
  110. if (auto select_element = first_ancestor_of_type<HTMLSelectElement>()) {
  111. int index = 0;
  112. for (auto const& option_element : select_element->list_of_options()) {
  113. if (option_element.ptr() == this)
  114. return index;
  115. ++index;
  116. }
  117. }
  118. // If the option element is not in a list of options, then the option element's index is zero.
  119. return 0;
  120. }
  121. // https://html.spec.whatwg.org/multipage/form-elements.html#ask-for-a-reset
  122. void HTMLOptionElement::ask_for_a_reset()
  123. {
  124. // If an option element in the list of options asks for a reset, then run that select element's selectedness setting algorithm.
  125. if (auto* select = first_ancestor_of_type<HTMLSelectElement>()) {
  126. select->update_selectedness();
  127. }
  128. }
  129. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-option-disabled
  130. bool HTMLOptionElement::disabled() const
  131. {
  132. // 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.
  133. return has_attribute(AttributeNames::disabled)
  134. || (parent() && is<HTMLOptGroupElement>(parent()) && static_cast<HTMLOptGroupElement const&>(*parent()).has_attribute(AttributeNames::disabled));
  135. }
  136. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-form
  137. JS::GCPtr<HTMLFormElement> HTMLOptionElement::form() const
  138. {
  139. // The form IDL attribute's behavior depends on whether the option element is in a select element or not.
  140. // 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,
  141. // then the form IDL attribute must return the same value as the form IDL attribute on that select element.
  142. // Otherwise, it must return null.
  143. auto const* parent = parent_element();
  144. if (is<HTMLOptGroupElement>(parent))
  145. parent = parent->parent_element();
  146. if (is<HTML::HTMLSelectElement>(parent)) {
  147. auto const* select_element = verify_cast<HTMLSelectElement>(parent);
  148. return const_cast<HTMLFormElement*>(select_element->form());
  149. }
  150. return {};
  151. }
  152. Optional<ARIA::Role> HTMLOptionElement::default_role() const
  153. {
  154. // https://www.w3.org/TR/html-aria/#el-option
  155. // TODO: Only an option element that is in a list of options or that represents a suggestion in a datalist should return option
  156. return ARIA::Role::option;
  157. }
  158. void HTMLOptionElement::inserted()
  159. {
  160. Base::inserted();
  161. set_selected_internal(selected());
  162. // 1. The option HTML element insertion steps, given insertedNode, are:
  163. // If insertedNode's parent is a select element,
  164. // or insertedNode's parent is an optgroup element whose parent is a select element,
  165. // then run that select element's selectedness setting algorithm.
  166. if (is<HTMLSelectElement>(*parent()))
  167. static_cast<HTMLSelectElement&>(*parent()).update_selectedness();
  168. else if (is<HTMLOptGroupElement>(parent()) && parent()->parent() && is<HTMLSelectElement>(*parent()->parent()))
  169. static_cast<HTMLSelectElement&>(*parent()->parent()).update_selectedness();
  170. }
  171. void HTMLOptionElement::removed_from(Node* old_parent)
  172. {
  173. Base::removed_from(old_parent);
  174. // The option HTML element removing steps, given removedNode and oldParent, are:
  175. // 1. If oldParent is a select element, or oldParent is an optgroup element whose parent is a select element,
  176. // then run that select element's selectedness setting algorithm.
  177. if (old_parent) {
  178. if (is<HTMLSelectElement>(*old_parent))
  179. static_cast<HTMLSelectElement&>(*old_parent).update_selectedness();
  180. else if (is<HTMLOptGroupElement>(*old_parent) && old_parent->parent_element() && is<HTMLSelectElement>(old_parent->parent_element()))
  181. static_cast<HTMLSelectElement&>(*old_parent->parent_element()).update_selectedness();
  182. }
  183. }
  184. }