HTMLSelectElement.cpp 3.8 KB

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