HTMLOptionElement.cpp 5.5 KB

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