HTMLSelectElement.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/HTML/HTMLFormElement.h>
  9. #include <LibWeb/HTML/HTMLOptGroupElement.h>
  10. #include <LibWeb/HTML/HTMLOptionElement.h>
  11. #include <LibWeb/HTML/HTMLSelectElement.h>
  12. namespace Web::HTML {
  13. HTMLSelectElement::HTMLSelectElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  14. : HTMLElement(document, move(qualified_name))
  15. {
  16. set_prototype(&Bindings::cached_web_prototype(realm(), "HTMLSelectElement"));
  17. }
  18. HTMLSelectElement::~HTMLSelectElement() = default;
  19. void HTMLSelectElement::visit_edges(Cell::Visitor& visitor)
  20. {
  21. Base::visit_edges(visitor);
  22. visitor.visit(m_options.ptr());
  23. }
  24. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-options
  25. JS::GCPtr<HTMLOptionsCollection> const& HTMLSelectElement::options()
  26. {
  27. if (!m_options) {
  28. m_options = HTMLOptionsCollection::create(*this, [](DOM::Element const& element) {
  29. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list
  30. // The list of options for a select element consists of all the option element children of
  31. // the select element, and all the option element children of all the optgroup element children
  32. // of the select element, in tree order.
  33. return is<HTMLOptionElement>(element);
  34. });
  35. }
  36. return m_options;
  37. }
  38. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-length
  39. size_t HTMLSelectElement::length()
  40. {
  41. // The length IDL attribute must return the number of nodes represented by the options collection. On setting, it must act like the attribute of the same name on the options collection.
  42. return const_cast<HTMLOptionsCollection&>(*options()).length();
  43. }
  44. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-item
  45. DOM::Element* HTMLSelectElement::item(size_t index)
  46. {
  47. // The item(index) method must return the value returned by the method of the same name on the options collection, when invoked with the same argument.
  48. return const_cast<HTMLOptionsCollection&>(*options()).item(index);
  49. }
  50. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-nameditem
  51. DOM::Element* HTMLSelectElement::named_item(FlyString const& name)
  52. {
  53. // The namedItem(name) method must return the value returned by the method of the same name on the options collection, when invoked with the same argument.
  54. return const_cast<HTMLOptionsCollection&>(*options()).named_item(name);
  55. }
  56. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-add
  57. WebIDL::ExceptionOr<void> HTMLSelectElement::add(HTMLOptionOrOptGroupElement element, Optional<HTMLElementOrElementIndex> before)
  58. {
  59. // Similarly, the add(element, before) method must act like its namesake method on that same options collection.
  60. return const_cast<HTMLOptionsCollection&>(*options()).add(move(element), move(before));
  61. }
  62. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list
  63. Vector<JS::Handle<HTMLOptionElement>> HTMLSelectElement::list_of_options() const
  64. {
  65. // The list of options for a select element consists of all the option element children of the select element,
  66. // and all the option element children of all the optgroup element children of the select element, in tree order.
  67. Vector<JS::Handle<HTMLOptionElement>> list;
  68. for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement const& option_element) {
  69. list.append(JS::make_handle(const_cast<HTMLOptionElement&>(option_element)));
  70. });
  71. for_each_child_of_type<HTMLOptGroupElement>([&](HTMLOptGroupElement const& optgroup_element) {
  72. optgroup_element.for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement const& option_element) {
  73. list.append(JS::make_handle(const_cast<HTMLOptionElement&>(option_element)));
  74. });
  75. });
  76. return list;
  77. }
  78. // https://html.spec.whatwg.org/multipage/form-elements.html#the-select-element:concept-form-reset-control
  79. void HTMLSelectElement::reset_algorithm()
  80. {
  81. // The reset algorithm for select elements is to go through all the option elements in the element's list of options,
  82. for (auto const& option_element : list_of_options()) {
  83. // set their selectedness to true if the option element has a selected attribute, and false otherwise,
  84. option_element->m_selected = option_element->has_attribute(AttributeNames::selected);
  85. // set their dirtiness to false,
  86. option_element->m_dirty = false;
  87. // and then have the option elements ask for a reset.
  88. option_element->ask_for_a_reset();
  89. }
  90. }
  91. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-selectedindex
  92. int HTMLSelectElement::selected_index() const
  93. {
  94. // The selectedIndex IDL attribute, on getting, must return the index of the first option element in the list of options
  95. // in tree order that has its selectedness set to true, if any. If there isn't one, then it must return −1.
  96. int index = 0;
  97. for (auto const& option_element : list_of_options()) {
  98. if (option_element->selected())
  99. return index;
  100. ++index;
  101. }
  102. return -1;
  103. }
  104. void HTMLSelectElement::set_selected_index(int index)
  105. {
  106. // On setting, the selectedIndex attribute must set the selectedness of all the option elements in the list of options to false,
  107. // and then the option element in the list of options whose index is the given new value,
  108. // if any, must have its selectedness set to true and its dirtiness set to true.
  109. auto options = list_of_options();
  110. for (auto& option : options)
  111. option->m_selected = false;
  112. if (index < 0 || index >= static_cast<int>(options.size()))
  113. return;
  114. auto& selected_option = options[index];
  115. selected_option->m_selected = true;
  116. selected_option->m_dirty = true;
  117. }
  118. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  119. i32 HTMLSelectElement::default_tab_index_value() const
  120. {
  121. // See the base function for the spec comments.
  122. return 0;
  123. }
  124. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-type
  125. DeprecatedString const& HTMLSelectElement::type() const
  126. {
  127. // The type IDL attribute, on getting, must return the string "select-one" if the multiple attribute is absent, and the string "select-multiple" if the multiple attribute is present.
  128. static DeprecatedString select_one = "select-one"sv;
  129. static DeprecatedString select_multiple = "select-multiple"sv;
  130. if (!has_attribute(AttributeNames::multiple))
  131. return select_one;
  132. return select_multiple;
  133. }
  134. }