HTMLOptionElement.cpp 5.7 KB

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