HTMLSelectElement.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. namespace Web::HTML {
  12. HTMLSelectElement::HTMLSelectElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  13. : FormAssociatedElement(document, move(qualified_name))
  14. {
  15. }
  16. HTMLSelectElement::~HTMLSelectElement() = default;
  17. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-options
  18. RefPtr<HTMLOptionsCollection> const& HTMLSelectElement::options()
  19. {
  20. if (!m_options) {
  21. m_options = HTMLOptionsCollection::create(*this, [](DOM::Element const& element) {
  22. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list
  23. // The list of options for a select element consists of all the option element children of
  24. // the select element, and all the option element children of all the optgroup element children
  25. // of the select element, in tree order.
  26. return is<HTMLOptionElement>(element);
  27. });
  28. }
  29. return m_options;
  30. }
  31. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list
  32. NonnullRefPtrVector<HTMLOptionElement> HTMLSelectElement::list_of_options() const
  33. {
  34. // The list of options for a select element consists of all the option element children of the select element,
  35. // and all the option element children of all the optgroup element children of the select element, in tree order.
  36. NonnullRefPtrVector<HTMLOptionElement> list;
  37. for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement const& option_element) {
  38. list.append(option_element);
  39. });
  40. for_each_child_of_type<HTMLOptGroupElement>([&](HTMLOptGroupElement const& optgroup_element) {
  41. optgroup_element.for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement const& option_element) {
  42. list.append(option_element);
  43. });
  44. });
  45. return list;
  46. }
  47. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-selectedindex
  48. int HTMLSelectElement::selected_index() const
  49. {
  50. // The selectedIndex IDL attribute, on getting, must return the index of the first option element in the list of options
  51. // in tree order that has its selectedness set to true, if any. If there isn't one, then it must return −1.
  52. int index = 0;
  53. for (auto const& option_element : list_of_options()) {
  54. if (option_element.selected())
  55. return index;
  56. ++index;
  57. }
  58. return -1;
  59. }
  60. void HTMLSelectElement::set_selected_index(int index)
  61. {
  62. // On setting, the selectedIndex attribute must set the selectedness of all the option elements in the list of options to false,
  63. // and then the option element in the list of options whose index is the given new value,
  64. // if any, must have its selectedness set to true and its dirtiness set to true.
  65. auto options = list_of_options();
  66. for (auto& option : options)
  67. option.m_selected = false;
  68. if (index < 0 || index >= static_cast<int>(options.size()))
  69. return;
  70. auto& selected_option = options[index];
  71. selected_option.m_selected = true;
  72. selected_option.m_dirty = true;
  73. }
  74. }