HTMLOptionElement.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/DOM/Node.h>
  9. #include <LibWeb/DOM/Text.h>
  10. #include <LibWeb/HTML/HTMLOptionElement.h>
  11. #include <LibWeb/HTML/HTMLScriptElement.h>
  12. #include <LibWeb/HTML/HTMLSelectElement.h>
  13. #include <LibWeb/HTML/Window.h>
  14. #include <ctype.h>
  15. namespace Web::HTML {
  16. HTMLOptionElement::HTMLOptionElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  17. : HTMLElement(document, move(qualified_name))
  18. {
  19. set_prototype(&window().cached_web_prototype("HTMLOptionElement"));
  20. }
  21. HTMLOptionElement::~HTMLOptionElement() = default;
  22. void HTMLOptionElement::parse_attribute(FlyString const& name, String const& value)
  23. {
  24. HTMLElement::parse_attribute(name, value);
  25. if (name == HTML::AttributeNames::selected) {
  26. // Except where otherwise specified, when the element is created, its selectedness must be set to true
  27. // if the element has a selected attribute. Whenever an option element's selected attribute is added,
  28. // if its dirtiness is false, its selectedness must be set to true.
  29. if (!m_dirty)
  30. m_selected = true;
  31. }
  32. }
  33. void HTMLOptionElement::did_remove_attribute(FlyString const& name)
  34. {
  35. HTMLElement::did_remove_attribute(name);
  36. if (name == HTML::AttributeNames::selected) {
  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. m_selected = false;
  40. }
  41. }
  42. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-selected
  43. void HTMLOptionElement::set_selected(bool selected)
  44. {
  45. // 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.
  46. m_selected = selected;
  47. m_dirty = true;
  48. ask_for_a_reset();
  49. }
  50. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-value
  51. String HTMLOptionElement::value() const
  52. {
  53. // The value of an option element is the value of the value content attribute, if there is one.
  54. if (auto value_attr = get_attribute(HTML::AttributeNames::value); !value_attr.is_null())
  55. return value_attr;
  56. // ...or, if there is not, the value of the element's text IDL attribute.
  57. return text();
  58. }
  59. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-value
  60. void HTMLOptionElement::set_value(String value)
  61. {
  62. set_attribute(HTML::AttributeNames::value, value);
  63. }
  64. // https://infra.spec.whatwg.org/#strip-and-collapse-ascii-whitespace
  65. static String strip_and_collapse_whitespace(StringView string)
  66. {
  67. // Replace any sequence of one or more consecutive code points that are ASCII whitespace in the string with a single U+0020 SPACE code point.
  68. StringBuilder builder;
  69. for (size_t i = 0; i < string.length(); ++i) {
  70. if (isspace(string[i])) {
  71. builder.append(' ');
  72. while (i < string.length()) {
  73. if (isspace(string[i])) {
  74. ++i;
  75. continue;
  76. }
  77. builder.append(string[i]);
  78. break;
  79. }
  80. continue;
  81. }
  82. builder.append(string[i]);
  83. }
  84. // ...and then remove any leading and trailing ASCII whitespace from that string.
  85. return builder.to_string().trim_whitespace();
  86. }
  87. static void concatenate_descendants_text_content(DOM::Node const* node, StringBuilder& builder)
  88. {
  89. // FIXME: SVGScriptElement should also be skipped, but it doesn't exist yet.
  90. if (is<HTMLScriptElement>(node))
  91. return;
  92. if (is<DOM::Text>(node))
  93. builder.append(verify_cast<DOM::Text>(node)->data());
  94. node->for_each_child([&](auto const& node) {
  95. concatenate_descendants_text_content(&node, builder);
  96. });
  97. }
  98. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-text
  99. String HTMLOptionElement::text() const
  100. {
  101. StringBuilder builder;
  102. // Concatenation of data of all the Text node descendants of the option element, in tree order,
  103. // excluding any that are descendants of descendants of the option element that are themselves
  104. // script or SVG script elements.
  105. for_each_child([&](auto const& node) {
  106. concatenate_descendants_text_content(&node, builder);
  107. });
  108. // Return the result of stripping and collapsing ASCII whitespace from the above concatenation.
  109. return strip_and_collapse_whitespace(builder.to_string());
  110. }
  111. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-text
  112. void HTMLOptionElement::set_text(String text)
  113. {
  114. string_replace_all(text);
  115. }
  116. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-option-index
  117. int HTMLOptionElement::index() const
  118. {
  119. // 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.
  120. if (auto select_element = first_ancestor_of_type<HTMLSelectElement>()) {
  121. int index = 0;
  122. for (auto const& option_element : select_element->list_of_options()) {
  123. if (option_element.ptr() == this)
  124. return index;
  125. ++index;
  126. }
  127. }
  128. // If the option element is not in a list of options, then the option element's index is zero.
  129. return 0;
  130. }
  131. // https://html.spec.whatwg.org/multipage/form-elements.html#ask-for-a-reset
  132. void HTMLOptionElement::ask_for_a_reset()
  133. {
  134. // FIXME: Implement this operation.
  135. }
  136. }