HTMLOptionElement.cpp 6.7 KB

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