HTMLSelectElement.cpp 7.4 KB

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