HTMLSelectElement.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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#dom-select-selectedindex
  79. int HTMLSelectElement::selected_index() const
  80. {
  81. // The selectedIndex IDL attribute, on getting, must return the index of the first option element in the list of options
  82. // in tree order that has its selectedness set to true, if any. If there isn't one, then it must return −1.
  83. int index = 0;
  84. for (auto const& option_element : list_of_options()) {
  85. if (option_element->selected())
  86. return index;
  87. ++index;
  88. }
  89. return -1;
  90. }
  91. void HTMLSelectElement::set_selected_index(int index)
  92. {
  93. // On setting, the selectedIndex attribute must set the selectedness of all the option elements in the list of options to false,
  94. // and then the option element in the list of options whose index is the given new value,
  95. // if any, must have its selectedness set to true and its dirtiness set to true.
  96. auto options = list_of_options();
  97. for (auto& option : options)
  98. option->m_selected = false;
  99. if (index < 0 || index >= static_cast<int>(options.size()))
  100. return;
  101. auto& selected_option = options[index];
  102. selected_option->m_selected = true;
  103. selected_option->m_dirty = true;
  104. }
  105. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  106. i32 HTMLSelectElement::default_tab_index_value() const
  107. {
  108. // See the base function for the spec comments.
  109. return 0;
  110. }
  111. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-type
  112. String const& HTMLSelectElement::type() const
  113. {
  114. // 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.
  115. static String select_one = "select-one"sv;
  116. static String select_multiple = "select-multiple"sv;
  117. if (!has_attribute(AttributeNames::multiple))
  118. return select_one;
  119. return select_multiple;
  120. }
  121. }