HTMLSelectElement.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/HTML/HTMLFormElement.h>
  8. #include <LibWeb/HTML/HTMLOptGroupElement.h>
  9. #include <LibWeb/HTML/HTMLOptionElement.h>
  10. #include <LibWeb/HTML/HTMLSelectElement.h>
  11. #include <LibWeb/HTML/Window.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(&window().cached_web_prototype("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-add
  39. WebIDL::ExceptionOr<void> HTMLSelectElement::add(HTMLOptionOrOptGroupElement element, Optional<HTMLElementOrElementIndex> before)
  40. {
  41. // Similarly, the add(element, before) method must act like its namesake method on that same options collection.
  42. return const_cast<HTMLOptionsCollection&>(*options()).add(move(element), move(before));
  43. }
  44. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list
  45. Vector<JS::Handle<HTMLOptionElement>> HTMLSelectElement::list_of_options() const
  46. {
  47. // The list of options for a select element consists of all the option element children of the select element,
  48. // and all the option element children of all the optgroup element children of the select element, in tree order.
  49. Vector<JS::Handle<HTMLOptionElement>> list;
  50. for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement const& option_element) {
  51. list.append(JS::make_handle(const_cast<HTMLOptionElement&>(option_element)));
  52. });
  53. for_each_child_of_type<HTMLOptGroupElement>([&](HTMLOptGroupElement const& optgroup_element) {
  54. optgroup_element.for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement const& option_element) {
  55. list.append(JS::make_handle(const_cast<HTMLOptionElement&>(option_element)));
  56. });
  57. });
  58. return list;
  59. }
  60. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-selectedindex
  61. int HTMLSelectElement::selected_index() const
  62. {
  63. // The selectedIndex IDL attribute, on getting, must return the index of the first option element in the list of options
  64. // in tree order that has its selectedness set to true, if any. If there isn't one, then it must return −1.
  65. int index = 0;
  66. for (auto const& option_element : list_of_options()) {
  67. if (option_element->selected())
  68. return index;
  69. ++index;
  70. }
  71. return -1;
  72. }
  73. void HTMLSelectElement::set_selected_index(int index)
  74. {
  75. // On setting, the selectedIndex attribute must set the selectedness of all the option elements in the list of options to false,
  76. // and then the option element in the list of options whose index is the given new value,
  77. // if any, must have its selectedness set to true and its dirtiness set to true.
  78. auto options = list_of_options();
  79. for (auto& option : options)
  80. option->m_selected = false;
  81. if (index < 0 || index >= static_cast<int>(options.size()))
  82. return;
  83. auto& selected_option = options[index];
  84. selected_option->m_selected = true;
  85. selected_option->m_dirty = true;
  86. }
  87. }