HTMLSelectElement.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/HTMLSelectElementPrototype.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. #include <LibWeb/HTML/Window.h>
  13. namespace Web::HTML {
  14. HTMLSelectElement::HTMLSelectElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  15. : HTMLElement(document, move(qualified_name))
  16. {
  17. set_prototype(&window().ensure_web_prototype<Bindings::HTMLSelectElementPrototype>("HTMLSelectElement"));
  18. }
  19. HTMLSelectElement::~HTMLSelectElement() = default;
  20. void HTMLSelectElement::visit_edges(Cell::Visitor& visitor)
  21. {
  22. Base::visit_edges(visitor);
  23. visitor.visit(m_options.ptr());
  24. }
  25. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-options
  26. JS::GCPtr<HTMLOptionsCollection> const& HTMLSelectElement::options()
  27. {
  28. if (!m_options) {
  29. m_options = HTMLOptionsCollection::create(*this, [](DOM::Element const& element) {
  30. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list
  31. // The list of options for a select element consists of all the option element children of
  32. // the select element, and all the option element children of all the optgroup element children
  33. // of the select element, in tree order.
  34. return is<HTMLOptionElement>(element);
  35. });
  36. }
  37. return m_options;
  38. }
  39. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-add
  40. DOM::ExceptionOr<void> HTMLSelectElement::add(HTMLOptionOrOptGroupElement element, Optional<HTMLElementOrElementIndex> before)
  41. {
  42. // Similarly, the add(element, before) method must act like its namesake method on that same options collection.
  43. return const_cast<HTMLOptionsCollection&>(*options()).add(move(element), move(before));
  44. }
  45. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list
  46. Vector<JS::Handle<HTMLOptionElement>> HTMLSelectElement::list_of_options() const
  47. {
  48. // The list of options for a select element consists of all the option element children of the select element,
  49. // and all the option element children of all the optgroup element children of the select element, in tree order.
  50. Vector<JS::Handle<HTMLOptionElement>> list;
  51. for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement const& option_element) {
  52. list.append(JS::make_handle(const_cast<HTMLOptionElement&>(option_element)));
  53. });
  54. for_each_child_of_type<HTMLOptGroupElement>([&](HTMLOptGroupElement const& optgroup_element) {
  55. optgroup_element.for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement const& option_element) {
  56. list.append(JS::make_handle(const_cast<HTMLOptionElement&>(option_element)));
  57. });
  58. });
  59. return list;
  60. }
  61. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-selectedindex
  62. int HTMLSelectElement::selected_index() const
  63. {
  64. // The selectedIndex IDL attribute, on getting, must return the index of the first option element in the list of options
  65. // in tree order that has its selectedness set to true, if any. If there isn't one, then it must return −1.
  66. int index = 0;
  67. for (auto const& option_element : list_of_options()) {
  68. if (option_element->selected())
  69. return index;
  70. ++index;
  71. }
  72. return -1;
  73. }
  74. void HTMLSelectElement::set_selected_index(int index)
  75. {
  76. // On setting, the selectedIndex attribute must set the selectedness of all the option elements in the list of options to false,
  77. // and then the option element in the list of options whose index is the given new value,
  78. // if any, must have its selectedness set to true and its dirtiness set to true.
  79. auto options = list_of_options();
  80. for (auto& option : options)
  81. option->m_selected = false;
  82. if (index < 0 || index >= static_cast<int>(options.size()))
  83. return;
  84. auto& selected_option = options[index];
  85. selected_option->m_selected = true;
  86. selected_option->m_dirty = true;
  87. }
  88. }