HTMLOptionElement.cpp 6.1 KB

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