HTMLSelectElement.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
  4. * Copyright (c) 2023, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/ElementFactory.h>
  12. #include <LibWeb/DOM/Event.h>
  13. #include <LibWeb/DOM/ShadowRoot.h>
  14. #include <LibWeb/HTML/EventNames.h>
  15. #include <LibWeb/HTML/HTMLFormElement.h>
  16. #include <LibWeb/HTML/HTMLHRElement.h>
  17. #include <LibWeb/HTML/HTMLOptGroupElement.h>
  18. #include <LibWeb/HTML/HTMLOptionElement.h>
  19. #include <LibWeb/HTML/HTMLSelectElement.h>
  20. #include <LibWeb/HTML/Numbers.h>
  21. #include <LibWeb/Infra/Strings.h>
  22. #include <LibWeb/Layout/Node.h>
  23. #include <LibWeb/Namespace.h>
  24. #include <LibWeb/Page/Page.h>
  25. namespace Web::HTML {
  26. JS_DEFINE_ALLOCATOR(HTMLSelectElement);
  27. HTMLSelectElement::HTMLSelectElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  28. : HTMLElement(document, move(qualified_name))
  29. {
  30. }
  31. HTMLSelectElement::~HTMLSelectElement() = default;
  32. void HTMLSelectElement::initialize(JS::Realm& realm)
  33. {
  34. Base::initialize(realm);
  35. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLSelectElement);
  36. }
  37. void HTMLSelectElement::visit_edges(Cell::Visitor& visitor)
  38. {
  39. Base::visit_edges(visitor);
  40. visitor.visit(m_options);
  41. visitor.visit(m_inner_text_element);
  42. visitor.visit(m_chevron_icon_element);
  43. for (auto const& item : m_select_items) {
  44. if (item.has<SelectItemOption>())
  45. visitor.visit(item.get<SelectItemOption>().option_element);
  46. if (item.has<SelectItemOptionGroup>()) {
  47. auto item_option_group = item.get<SelectItemOptionGroup>();
  48. for (auto const& item : item_option_group.items)
  49. visitor.visit(item.option_element);
  50. }
  51. }
  52. }
  53. void HTMLSelectElement::adjust_computed_style(CSS::StyleProperties& style)
  54. {
  55. // AD-HOC: We rewrite `display: inline` to `display: inline-block`.
  56. // This is required for the internal shadow tree to work correctly in layout.
  57. if (style.display().is_inline_outside() && style.display().is_flow_inside())
  58. style.set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::InlineBlock)));
  59. }
  60. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-size
  61. WebIDL::UnsignedLong HTMLSelectElement::size() const
  62. {
  63. // The size IDL attribute must reflect the respective content attributes of the same name. The size IDL attribute has a default value of 0.
  64. if (auto size_string = get_attribute(HTML::AttributeNames::size); size_string.has_value()) {
  65. if (auto size = parse_non_negative_integer(*size_string); size.has_value())
  66. return *size;
  67. }
  68. return 0;
  69. }
  70. WebIDL::ExceptionOr<void> HTMLSelectElement::set_size(WebIDL::UnsignedLong size)
  71. {
  72. return set_attribute(HTML::AttributeNames::size, MUST(String::number(size)));
  73. }
  74. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-options
  75. JS::GCPtr<HTMLOptionsCollection> const& HTMLSelectElement::options()
  76. {
  77. if (!m_options) {
  78. m_options = HTMLOptionsCollection::create(*this, [](DOM::Element const& element) {
  79. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list
  80. // The list of options for a select element consists of all the option element children of
  81. // the select element, and all the option element children of all the optgroup element children
  82. // of the select element, in tree order.
  83. return is<HTMLOptionElement>(element);
  84. });
  85. }
  86. return m_options;
  87. }
  88. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-length
  89. WebIDL::UnsignedLong HTMLSelectElement::length()
  90. {
  91. // 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.
  92. return const_cast<HTMLOptionsCollection&>(*options()).length();
  93. }
  94. WebIDL::ExceptionOr<void> HTMLSelectElement::set_length(WebIDL::UnsignedLong length)
  95. {
  96. // On setting, it must act like the attribute of the same name on the options collection.
  97. return const_cast<HTMLOptionsCollection&>(*options()).set_length(length);
  98. }
  99. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-item
  100. DOM::Element* HTMLSelectElement::item(size_t index)
  101. {
  102. // 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.
  103. return const_cast<HTMLOptionsCollection&>(*options()).item(index);
  104. }
  105. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-nameditem
  106. DOM::Element* HTMLSelectElement::named_item(FlyString const& name)
  107. {
  108. // 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.
  109. return const_cast<HTMLOptionsCollection&>(*options()).named_item(name);
  110. }
  111. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-add
  112. WebIDL::ExceptionOr<void> HTMLSelectElement::add(HTMLOptionOrOptGroupElement element, Optional<HTMLElementOrElementIndex> before)
  113. {
  114. // Similarly, the add(element, before) method must act like its namesake method on that same options collection.
  115. return const_cast<HTMLOptionsCollection&>(*options()).add(move(element), move(before));
  116. }
  117. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-remove
  118. void HTMLSelectElement::remove()
  119. {
  120. // The remove() method must act like its namesake method on that same options collection when it has arguments,
  121. // and like its namesake method on the ChildNode interface implemented by the HTMLSelectElement ancestor interface Element when it has no arguments.
  122. ChildNode::remove_binding();
  123. }
  124. void HTMLSelectElement::remove(WebIDL::Long index)
  125. {
  126. const_cast<HTMLOptionsCollection&>(*options()).remove(index);
  127. }
  128. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list
  129. Vector<JS::Handle<HTMLOptionElement>> HTMLSelectElement::list_of_options() const
  130. {
  131. // The list of options for a select element consists of all the option element children of the select element,
  132. // and all the option element children of all the optgroup element children of the select element, in tree order.
  133. Vector<JS::Handle<HTMLOptionElement>> list;
  134. for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement& option_element) {
  135. list.append(JS::make_handle(option_element));
  136. });
  137. for_each_child_of_type<HTMLOptGroupElement>([&](HTMLOptGroupElement const& optgroup_element) {
  138. optgroup_element.for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement& option_element) {
  139. list.append(JS::make_handle(option_element));
  140. });
  141. });
  142. return list;
  143. }
  144. // https://html.spec.whatwg.org/multipage/form-elements.html#the-select-element:concept-form-reset-control
  145. void HTMLSelectElement::reset_algorithm()
  146. {
  147. // The reset algorithm for select elements is to go through all the option elements in the element's list of options,
  148. for (auto const& option_element : list_of_options()) {
  149. // set their selectedness to true if the option element has a selected attribute, and false otherwise,
  150. option_element->m_selected = option_element->has_attribute(AttributeNames::selected);
  151. // set their dirtiness to false,
  152. option_element->m_dirty = false;
  153. // and then have the option elements ask for a reset.
  154. option_element->ask_for_a_reset();
  155. }
  156. }
  157. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-selectedindex
  158. int HTMLSelectElement::selected_index() const
  159. {
  160. // The selectedIndex IDL attribute, on getting, must return the index of the first option element in the list of options
  161. // in tree order that has its selectedness set to true, if any. If there isn't one, then it must return −1.
  162. int index = 0;
  163. for (auto const& option_element : list_of_options()) {
  164. if (option_element->selected())
  165. return index;
  166. ++index;
  167. }
  168. return -1;
  169. }
  170. void HTMLSelectElement::set_selected_index(int index)
  171. {
  172. // On setting, the selectedIndex attribute must set the selectedness of all the option elements in the list of options to false,
  173. // and then the option element in the list of options whose index is the given new value,
  174. // if any, must have its selectedness set to true and its dirtiness set to true.
  175. auto options = list_of_options();
  176. for (auto& option : options)
  177. option->m_selected = false;
  178. if (index < 0 || index >= static_cast<int>(options.size()))
  179. return;
  180. auto& selected_option = options[index];
  181. selected_option->m_selected = true;
  182. selected_option->m_dirty = true;
  183. }
  184. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  185. i32 HTMLSelectElement::default_tab_index_value() const
  186. {
  187. // See the base function for the spec comments.
  188. return 0;
  189. }
  190. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-type
  191. String const& HTMLSelectElement::type() const
  192. {
  193. // 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.
  194. static String const select_one = "select-one"_string;
  195. static String const select_multiple = "select-multiple"_string;
  196. if (!has_attribute(AttributeNames::multiple))
  197. return select_one;
  198. return select_multiple;
  199. }
  200. Optional<ARIA::Role> HTMLSelectElement::default_role() const
  201. {
  202. // https://www.w3.org/TR/html-aria/#el-select-multiple-or-size-greater-1
  203. if (has_attribute(AttributeNames::multiple))
  204. return ARIA::Role::listbox;
  205. if (has_attribute(AttributeNames::size)) {
  206. if (auto size_string = get_attribute(HTML::AttributeNames::size); size_string.has_value()) {
  207. if (auto size = size_string->to_number<int>(); size.has_value() && *size > 1)
  208. return ARIA::Role::listbox;
  209. }
  210. }
  211. // https://www.w3.org/TR/html-aria/#el-select
  212. return ARIA::Role::combobox;
  213. }
  214. String HTMLSelectElement::value() const
  215. {
  216. for (auto const& option_element : list_of_options())
  217. if (option_element->selected())
  218. return option_element->value();
  219. return ""_string;
  220. }
  221. WebIDL::ExceptionOr<void> HTMLSelectElement::set_value(String const& value)
  222. {
  223. for (auto const& option_element : list_of_options())
  224. option_element->set_selected(option_element->value() == value);
  225. update_inner_text_element();
  226. queue_input_and_change_events();
  227. return {};
  228. }
  229. void HTMLSelectElement::queue_input_and_change_events()
  230. {
  231. // When the user agent is to send select update notifications, queue an element task on the user interaction task source given the select element to run these steps:
  232. queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
  233. // FIXME: 1. Set the select element's user interacted to true.
  234. // 2. Fire an event named input at the select element, with the bubbles and composed attributes initialized to true.
  235. auto input_event = DOM::Event::create(realm(), HTML::EventNames::input);
  236. input_event->set_bubbles(true);
  237. input_event->set_composed(true);
  238. dispatch_event(input_event);
  239. // 3. Fire an event named change at the select element, with the bubbles attribute initialized to true.
  240. auto change_event = DOM::Event::create(realm(), HTML::EventNames::change);
  241. change_event->set_bubbles(true);
  242. dispatch_event(*change_event);
  243. });
  244. }
  245. void HTMLSelectElement::set_is_open(bool open)
  246. {
  247. if (open == m_is_open)
  248. return;
  249. m_is_open = open;
  250. invalidate_style();
  251. }
  252. bool HTMLSelectElement::has_activation_behavior() const
  253. {
  254. return true;
  255. }
  256. static String strip_newlines(Optional<String> string)
  257. {
  258. // FIXME: Move this to a more general function
  259. if (!string.has_value())
  260. return {};
  261. StringBuilder builder;
  262. for (auto c : string.value().bytes_as_string_view()) {
  263. if (c == '\r' || c == '\n') {
  264. builder.append(' ');
  265. } else {
  266. builder.append(c);
  267. }
  268. }
  269. return MUST(Infra::strip_and_collapse_whitespace(MUST(builder.to_string())));
  270. }
  271. void HTMLSelectElement::activation_behavior(DOM::Event const&)
  272. {
  273. // Populate select items
  274. m_select_items.clear();
  275. u32 id_counter = 1;
  276. for (auto const& child : children_as_vector()) {
  277. if (is<HTMLOptGroupElement>(*child)) {
  278. auto& opt_group_element = verify_cast<HTMLOptGroupElement>(*child);
  279. Vector<SelectItemOption> option_group_items;
  280. for (auto const& child : opt_group_element.children_as_vector()) {
  281. if (is<HTMLOptionElement>(*child)) {
  282. auto& option_element = verify_cast<HTMLOptionElement>(*child);
  283. option_group_items.append(SelectItemOption { id_counter++, strip_newlines(option_element.text_content()), option_element.value(), option_element.selected(), option_element.disabled(), option_element });
  284. }
  285. }
  286. m_select_items.append(SelectItemOptionGroup { opt_group_element.get_attribute(AttributeNames::label).value_or(String {}), option_group_items });
  287. }
  288. if (is<HTMLOptionElement>(*child)) {
  289. auto& option_element = verify_cast<HTMLOptionElement>(*child);
  290. m_select_items.append(SelectItemOption { id_counter++, strip_newlines(option_element.text_content()), option_element.value(), option_element.selected(), option_element.disabled(), option_element });
  291. }
  292. if (is<HTMLHRElement>(*child))
  293. m_select_items.append(SelectItemSeparator {});
  294. }
  295. // Request select dropdown
  296. auto weak_element = make_weak_ptr<HTMLSelectElement>();
  297. auto rect = get_bounding_client_rect();
  298. auto position = document().navigable()->to_top_level_position(Web::CSSPixelPoint { rect->x(), rect->y() });
  299. document().page().did_request_select_dropdown(weak_element, position, CSSPixels(rect->width()), m_select_items);
  300. set_is_open(true);
  301. }
  302. void HTMLSelectElement::did_select_item(Optional<u32> const& id)
  303. {
  304. set_is_open(false);
  305. if (!id.has_value())
  306. return;
  307. for (auto const& option_element : list_of_options())
  308. option_element->set_selected(false);
  309. for (auto const& item : m_select_items) {
  310. if (item.has<SelectItemOption>()) {
  311. auto const& item_option = item.get<SelectItemOption>();
  312. if (item_option.id == *id)
  313. item_option.option_element->set_selected(true);
  314. }
  315. if (item.has<SelectItemOptionGroup>()) {
  316. auto item_option_group = item.get<SelectItemOptionGroup>();
  317. for (auto const& item_option : item_option_group.items) {
  318. if (item_option.id == *id)
  319. item_option.option_element->set_selected(true);
  320. }
  321. }
  322. }
  323. update_inner_text_element();
  324. queue_input_and_change_events();
  325. }
  326. void HTMLSelectElement::form_associated_element_was_inserted()
  327. {
  328. create_shadow_tree_if_needed();
  329. // Wait until children are ready
  330. queue_an_element_task(HTML::Task::Source::Microtask, [this] {
  331. // Select first option when no other option is selected
  332. if (selected_index() == -1) {
  333. auto options = list_of_options();
  334. if (options.size() > 0) {
  335. options.at(0)->set_selected(true);
  336. }
  337. }
  338. update_inner_text_element();
  339. });
  340. }
  341. void HTMLSelectElement::form_associated_element_was_removed(DOM::Node*)
  342. {
  343. set_shadow_root(nullptr);
  344. }
  345. void HTMLSelectElement::computed_css_values_changed()
  346. {
  347. // Hide chevron icon when appearance is none
  348. if (m_chevron_icon_element) {
  349. auto appearance = computed_css_values()->appearance();
  350. if (appearance.has_value() && *appearance == CSS::Appearance::None) {
  351. MUST(m_chevron_icon_element->style_for_bindings()->set_property(CSS::PropertyID::Display, "none"_string));
  352. } else {
  353. MUST(m_chevron_icon_element->style_for_bindings()->set_property(CSS::PropertyID::Display, "block"_string));
  354. }
  355. }
  356. }
  357. void HTMLSelectElement::create_shadow_tree_if_needed()
  358. {
  359. if (shadow_root_internal())
  360. return;
  361. auto shadow_root = heap().allocate<DOM::ShadowRoot>(realm(), document(), *this, Bindings::ShadowRootMode::Closed);
  362. set_shadow_root(shadow_root);
  363. auto border = DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  364. MUST(border->set_attribute(HTML::AttributeNames::style, R"~~~(
  365. display: flex;
  366. align-items: center;
  367. height: 100%;
  368. )~~~"_string));
  369. MUST(shadow_root->append_child(border));
  370. m_inner_text_element = DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  371. MUST(m_inner_text_element->set_attribute(HTML::AttributeNames::style, R"~~~(
  372. flex: 1;
  373. )~~~"_string));
  374. MUST(border->append_child(*m_inner_text_element));
  375. // FIXME: Find better way to add chevron icon
  376. m_chevron_icon_element = DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  377. MUST(m_chevron_icon_element->set_attribute(HTML::AttributeNames::style, R"~~~(
  378. width: 16px;
  379. height: 16px;
  380. margin-left: 4px;
  381. )~~~"_string));
  382. MUST(m_chevron_icon_element->set_inner_html("<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M7.41,8.58L12,13.17L16.59,8.58L18,10L12,16L6,10L7.41,8.58Z\" /></svg>"sv));
  383. MUST(border->append_child(*m_chevron_icon_element));
  384. update_inner_text_element();
  385. }
  386. void HTMLSelectElement::update_inner_text_element()
  387. {
  388. if (!m_inner_text_element)
  389. return;
  390. // Update inner text element to text content of selected option
  391. for (auto const& option_element : list_of_options()) {
  392. if (option_element->selected()) {
  393. m_inner_text_element->set_text_content(strip_newlines(option_element->text_content()));
  394. return;
  395. }
  396. }
  397. }
  398. }