HTMLSelectElement.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. }
  17. HTMLSelectElement::~HTMLSelectElement() = default;
  18. void HTMLSelectElement::initialize(JS::Realm& realm)
  19. {
  20. Base::initialize(realm);
  21. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLSelectElementPrototype>(realm, "HTMLSelectElement"));
  22. }
  23. void HTMLSelectElement::visit_edges(Cell::Visitor& visitor)
  24. {
  25. Base::visit_edges(visitor);
  26. visitor.visit(m_options.ptr());
  27. }
  28. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-options
  29. JS::GCPtr<HTMLOptionsCollection> const& HTMLSelectElement::options()
  30. {
  31. if (!m_options) {
  32. m_options = HTMLOptionsCollection::create(*this, [](DOM::Element const& element) {
  33. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list
  34. // The list of options for a select element consists of all the option element children of
  35. // the select element, and all the option element children of all the optgroup element children
  36. // of the select element, in tree order.
  37. return is<HTMLOptionElement>(element);
  38. });
  39. }
  40. return m_options;
  41. }
  42. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-length
  43. size_t HTMLSelectElement::length()
  44. {
  45. // 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.
  46. return const_cast<HTMLOptionsCollection&>(*options()).length();
  47. }
  48. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-item
  49. DOM::Element* HTMLSelectElement::item(size_t index)
  50. {
  51. // 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.
  52. return const_cast<HTMLOptionsCollection&>(*options()).item(index);
  53. }
  54. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-nameditem
  55. DOM::Element* HTMLSelectElement::named_item(FlyString const& name)
  56. {
  57. // 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.
  58. return const_cast<HTMLOptionsCollection&>(*options()).named_item(name);
  59. }
  60. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-add
  61. WebIDL::ExceptionOr<void> HTMLSelectElement::add(HTMLOptionOrOptGroupElement element, Optional<HTMLElementOrElementIndex> before)
  62. {
  63. // Similarly, the add(element, before) method must act like its namesake method on that same options collection.
  64. return const_cast<HTMLOptionsCollection&>(*options()).add(move(element), move(before));
  65. }
  66. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list
  67. Vector<JS::Handle<HTMLOptionElement>> HTMLSelectElement::list_of_options() const
  68. {
  69. // The list of options for a select element consists of all the option element children of the select element,
  70. // and all the option element children of all the optgroup element children of the select element, in tree order.
  71. Vector<JS::Handle<HTMLOptionElement>> list;
  72. for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement& option_element) {
  73. list.append(JS::make_handle(option_element));
  74. });
  75. for_each_child_of_type<HTMLOptGroupElement>([&](HTMLOptGroupElement const& optgroup_element) {
  76. optgroup_element.for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement& option_element) {
  77. list.append(JS::make_handle(option_element));
  78. });
  79. });
  80. return list;
  81. }
  82. // https://html.spec.whatwg.org/multipage/form-elements.html#the-select-element:concept-form-reset-control
  83. void HTMLSelectElement::reset_algorithm()
  84. {
  85. // The reset algorithm for select elements is to go through all the option elements in the element's list of options,
  86. for (auto const& option_element : list_of_options()) {
  87. // set their selectedness to true if the option element has a selected attribute, and false otherwise,
  88. option_element->m_selected = option_element->has_attribute(AttributeNames::selected);
  89. // set their dirtiness to false,
  90. option_element->m_dirty = false;
  91. // and then have the option elements ask for a reset.
  92. option_element->ask_for_a_reset();
  93. }
  94. }
  95. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-selectedindex
  96. int HTMLSelectElement::selected_index() const
  97. {
  98. // The selectedIndex IDL attribute, on getting, must return the index of the first option element in the list of options
  99. // in tree order that has its selectedness set to true, if any. If there isn't one, then it must return −1.
  100. int index = 0;
  101. for (auto const& option_element : list_of_options()) {
  102. if (option_element->selected())
  103. return index;
  104. ++index;
  105. }
  106. return -1;
  107. }
  108. void HTMLSelectElement::set_selected_index(int index)
  109. {
  110. // On setting, the selectedIndex attribute must set the selectedness of all the option elements in the list of options to false,
  111. // and then the option element in the list of options whose index is the given new value,
  112. // if any, must have its selectedness set to true and its dirtiness set to true.
  113. auto options = list_of_options();
  114. for (auto& option : options)
  115. option->m_selected = false;
  116. if (index < 0 || index >= static_cast<int>(options.size()))
  117. return;
  118. auto& selected_option = options[index];
  119. selected_option->m_selected = true;
  120. selected_option->m_dirty = true;
  121. }
  122. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  123. i32 HTMLSelectElement::default_tab_index_value() const
  124. {
  125. // See the base function for the spec comments.
  126. return 0;
  127. }
  128. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-type
  129. String const& HTMLSelectElement::type() const
  130. {
  131. // 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.
  132. static String const select_one = "select-one"_string;
  133. static String const select_multiple = "select-multiple"_string;
  134. if (!has_attribute(AttributeNames::multiple))
  135. return select_one;
  136. return select_multiple;
  137. }
  138. Optional<ARIA::Role> HTMLSelectElement::default_role() const
  139. {
  140. // https://www.w3.org/TR/html-aria/#el-select-multiple-or-size-greater-1
  141. if (has_attribute("multiple"))
  142. return ARIA::Role::listbox;
  143. if (has_attribute("size")) {
  144. auto size_attribute = deprecated_attribute("size").to_int();
  145. if (size_attribute.has_value() && size_attribute.value() > 1)
  146. return ARIA::Role::listbox;
  147. }
  148. // https://www.w3.org/TR/html-aria/#el-select
  149. return ARIA::Role::combobox;
  150. }
  151. void HTMLSelectElement::set_is_open(bool open)
  152. {
  153. if (open == m_is_open)
  154. return;
  155. m_is_open = open;
  156. invalidate_style();
  157. }
  158. }