HTMLSelectElement.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2021-2022, Andreas Kling <andreas@ladybird.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/HTMLSelectElementPrototype.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
  11. #include <LibWeb/DOM/Document.h>
  12. #include <LibWeb/DOM/ElementFactory.h>
  13. #include <LibWeb/DOM/Event.h>
  14. #include <LibWeb/DOM/ShadowRoot.h>
  15. #include <LibWeb/HTML/EventNames.h>
  16. #include <LibWeb/HTML/HTMLFormElement.h>
  17. #include <LibWeb/HTML/HTMLHRElement.h>
  18. #include <LibWeb/HTML/HTMLOptGroupElement.h>
  19. #include <LibWeb/HTML/HTMLOptionElement.h>
  20. #include <LibWeb/HTML/HTMLSelectElement.h>
  21. #include <LibWeb/HTML/Numbers.h>
  22. #include <LibWeb/HTML/Window.h>
  23. #include <LibWeb/Infra/Strings.h>
  24. #include <LibWeb/Layout/Node.h>
  25. #include <LibWeb/Namespace.h>
  26. #include <LibWeb/Page/Page.h>
  27. #include <LibWeb/Painting/Paintable.h>
  28. namespace Web::HTML {
  29. JS_DEFINE_ALLOCATOR(HTMLSelectElement);
  30. HTMLSelectElement::HTMLSelectElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  31. : HTMLElement(document, move(qualified_name))
  32. {
  33. }
  34. HTMLSelectElement::~HTMLSelectElement() = default;
  35. void HTMLSelectElement::initialize(JS::Realm& realm)
  36. {
  37. Base::initialize(realm);
  38. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLSelectElement);
  39. }
  40. void HTMLSelectElement::visit_edges(Cell::Visitor& visitor)
  41. {
  42. Base::visit_edges(visitor);
  43. visitor.visit(m_options);
  44. visitor.visit(m_selected_options);
  45. visitor.visit(m_inner_text_element);
  46. visitor.visit(m_chevron_icon_element);
  47. for (auto const& item : m_select_items) {
  48. if (item.has<SelectItemOption>())
  49. visitor.visit(item.get<SelectItemOption>().option_element);
  50. if (item.has<SelectItemOptionGroup>()) {
  51. auto item_option_group = item.get<SelectItemOptionGroup>();
  52. for (auto const& item : item_option_group.items)
  53. visitor.visit(item.option_element);
  54. }
  55. }
  56. }
  57. void HTMLSelectElement::adjust_computed_style(CSS::StyleProperties& style)
  58. {
  59. // AD-HOC: We rewrite `display: inline` to `display: inline-block`.
  60. // This is required for the internal shadow tree to work correctly in layout.
  61. if (style.display().is_inline_outside() && style.display().is_flow_inside())
  62. style.set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::InlineBlock)));
  63. }
  64. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-size
  65. WebIDL::UnsignedLong HTMLSelectElement::size() const
  66. {
  67. // The size IDL attribute must reflect the respective content attributes of the same name. The size IDL attribute has a default value of 0.
  68. if (auto size_string = get_attribute(HTML::AttributeNames::size); size_string.has_value()) {
  69. // The display size of a select element is the result of applying the rules for parsing non-negative integers
  70. // to the value of element's size attribute, if it has one and parsing it is successful.
  71. if (auto size = parse_non_negative_integer(*size_string); size.has_value())
  72. return *size;
  73. }
  74. // If applying those rules to the attribute's value is not successful or if the size attribute is absent,
  75. // then the element's display size is 4 if the element's multiple content attribute is present, and 1 otherwise.
  76. if (has_attribute(AttributeNames::multiple))
  77. return 4;
  78. return 1;
  79. }
  80. WebIDL::ExceptionOr<void> HTMLSelectElement::set_size(WebIDL::UnsignedLong size)
  81. {
  82. return set_attribute(HTML::AttributeNames::size, String::number(size));
  83. }
  84. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-options
  85. JS::GCPtr<HTMLOptionsCollection> const& HTMLSelectElement::options()
  86. {
  87. if (!m_options) {
  88. m_options = HTMLOptionsCollection::create(*this, [](DOM::Element const& element) {
  89. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list
  90. // The list of options for a select element consists of all the option element children of
  91. // the select element, and all the option element children of all the optgroup element children
  92. // of the select element, in tree order.
  93. return is<HTMLOptionElement>(element);
  94. });
  95. }
  96. return m_options;
  97. }
  98. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-length
  99. WebIDL::UnsignedLong HTMLSelectElement::length()
  100. {
  101. // 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.
  102. return const_cast<HTMLOptionsCollection&>(*options()).length();
  103. }
  104. WebIDL::ExceptionOr<void> HTMLSelectElement::set_length(WebIDL::UnsignedLong length)
  105. {
  106. // On setting, it must act like the attribute of the same name on the options collection.
  107. return const_cast<HTMLOptionsCollection&>(*options()).set_length(length);
  108. }
  109. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-item
  110. HTMLOptionElement* HTMLSelectElement::item(WebIDL::UnsignedLong index)
  111. {
  112. // 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.
  113. return verify_cast<HTMLOptionElement>(const_cast<HTMLOptionsCollection&>(*options()).item(index));
  114. }
  115. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-nameditem
  116. HTMLOptionElement* HTMLSelectElement::named_item(FlyString const& name)
  117. {
  118. // 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.
  119. return verify_cast<HTMLOptionElement>(const_cast<HTMLOptionsCollection&>(*options()).named_item(name));
  120. }
  121. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-add
  122. WebIDL::ExceptionOr<void> HTMLSelectElement::add(HTMLOptionOrOptGroupElement element, Optional<HTMLElementOrElementIndex> before)
  123. {
  124. // Similarly, the add(element, before) method must act like its namesake method on that same options collection.
  125. TRY(const_cast<HTMLOptionsCollection&>(*options()).add(move(element), move(before)));
  126. update_selectedness(); // Not in spec
  127. return {};
  128. }
  129. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-remove
  130. void HTMLSelectElement::remove()
  131. {
  132. // The remove() method must act like its namesake method on that same options collection when it has arguments,
  133. // and like its namesake method on the ChildNode interface implemented by the HTMLSelectElement ancestor interface Element when it has no arguments.
  134. ChildNode::remove_binding();
  135. }
  136. void HTMLSelectElement::remove(WebIDL::Long index)
  137. {
  138. const_cast<HTMLOptionsCollection&>(*options()).remove(index);
  139. }
  140. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-selectedoptions
  141. JS::NonnullGCPtr<DOM::HTMLCollection> HTMLSelectElement::selected_options()
  142. {
  143. // The selectedOptions IDL attribute must return an HTMLCollection rooted at the select node,
  144. // whose filter matches the elements in the list of options that have their selectedness set to true.
  145. if (!m_selected_options) {
  146. m_selected_options = DOM::HTMLCollection::create(*this, DOM::HTMLCollection::Scope::Descendants, [](Element const& element) {
  147. if (is<HTML::HTMLOptionElement>(element)) {
  148. auto const& option_element = verify_cast<HTMLOptionElement>(element);
  149. return option_element.selected();
  150. }
  151. return false;
  152. });
  153. }
  154. return *m_selected_options;
  155. }
  156. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-list
  157. Vector<JS::Handle<HTMLOptionElement>> HTMLSelectElement::list_of_options() const
  158. {
  159. // The list of options for a select element consists of all the option element children of the select element,
  160. // and all the option element children of all the optgroup element children of the select element, in tree order.
  161. Vector<JS::Handle<HTMLOptionElement>> list;
  162. for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement& option_element) {
  163. list.append(JS::make_handle(option_element));
  164. return IterationDecision::Continue;
  165. });
  166. for_each_child_of_type<HTMLOptGroupElement>([&](HTMLOptGroupElement const& optgroup_element) {
  167. optgroup_element.for_each_child_of_type<HTMLOptionElement>([&](HTMLOptionElement& option_element) {
  168. list.append(JS::make_handle(option_element));
  169. return IterationDecision::Continue;
  170. });
  171. return IterationDecision::Continue;
  172. });
  173. return list;
  174. }
  175. // https://html.spec.whatwg.org/multipage/form-elements.html#the-select-element:concept-form-reset-control
  176. void HTMLSelectElement::reset_algorithm()
  177. {
  178. // The reset algorithm for select elements is to go through all the option elements in the element's list of options,
  179. for (auto const& option_element : list_of_options()) {
  180. // set their selectedness to true if the option element has a selected attribute, and false otherwise,
  181. option_element->m_selected = option_element->has_attribute(AttributeNames::selected);
  182. // set their dirtiness to false,
  183. option_element->m_dirty = false;
  184. // and then have the option elements ask for a reset.
  185. option_element->ask_for_a_reset();
  186. }
  187. }
  188. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-selectedindex
  189. WebIDL::Long HTMLSelectElement::selected_index() const
  190. {
  191. // The selectedIndex IDL attribute, on getting, must return the index of the first option element in the list of options
  192. // in tree order that has its selectedness set to true, if any. If there isn't one, then it must return −1.
  193. WebIDL::Long index = 0;
  194. for (auto const& option_element : list_of_options()) {
  195. if (option_element->selected())
  196. return index;
  197. ++index;
  198. }
  199. return -1;
  200. }
  201. void HTMLSelectElement::set_selected_index(WebIDL::Long index)
  202. {
  203. // On setting, the selectedIndex attribute must set the selectedness of all the option elements in the list of options to false,
  204. // and then the option element in the list of options whose index is the given new value,
  205. // if any, must have its selectedness set to true and its dirtiness set to true.
  206. auto options = list_of_options();
  207. for (auto& option : options)
  208. option->m_selected = false;
  209. if (index < 0 || index >= static_cast<int>(options.size()))
  210. return;
  211. auto& selected_option = options[index];
  212. selected_option->m_selected = true;
  213. selected_option->m_dirty = true;
  214. }
  215. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  216. i32 HTMLSelectElement::default_tab_index_value() const
  217. {
  218. // See the base function for the spec comments.
  219. return 0;
  220. }
  221. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-type
  222. String const& HTMLSelectElement::type() const
  223. {
  224. // 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.
  225. static String const select_one = "select-one"_string;
  226. static String const select_multiple = "select-multiple"_string;
  227. if (!has_attribute(AttributeNames::multiple))
  228. return select_one;
  229. return select_multiple;
  230. }
  231. Optional<ARIA::Role> HTMLSelectElement::default_role() const
  232. {
  233. // https://www.w3.org/TR/html-aria/#el-select-multiple-or-size-greater-1
  234. if (has_attribute(AttributeNames::multiple))
  235. return ARIA::Role::listbox;
  236. if (has_attribute(AttributeNames::size)) {
  237. if (auto size_string = get_attribute(HTML::AttributeNames::size); size_string.has_value()) {
  238. if (auto size = size_string->to_number<int>(); size.has_value() && *size > 1)
  239. return ARIA::Role::listbox;
  240. }
  241. }
  242. // https://www.w3.org/TR/html-aria/#el-select
  243. return ARIA::Role::combobox;
  244. }
  245. String HTMLSelectElement::value() const
  246. {
  247. for (auto const& option_element : list_of_options())
  248. if (option_element->selected())
  249. return option_element->value();
  250. return ""_string;
  251. }
  252. WebIDL::ExceptionOr<void> HTMLSelectElement::set_value(String const& value)
  253. {
  254. for (auto const& option_element : list_of_options())
  255. option_element->set_selected(option_element->value() == value);
  256. update_inner_text_element();
  257. return {};
  258. }
  259. void HTMLSelectElement::queue_input_and_change_events()
  260. {
  261. // 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:
  262. queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
  263. // FIXME: 1. Set the select element's user interacted to true.
  264. // 2. Fire an event named input at the select element, with the bubbles and composed attributes initialized to true.
  265. auto input_event = DOM::Event::create(realm(), HTML::EventNames::input);
  266. input_event->set_bubbles(true);
  267. input_event->set_composed(true);
  268. dispatch_event(input_event);
  269. // 3. Fire an event named change at the select element, with the bubbles attribute initialized to true.
  270. auto change_event = DOM::Event::create(realm(), HTML::EventNames::change);
  271. change_event->set_bubbles(true);
  272. dispatch_event(*change_event);
  273. });
  274. }
  275. void HTMLSelectElement::set_is_open(bool open)
  276. {
  277. if (open == m_is_open)
  278. return;
  279. m_is_open = open;
  280. invalidate_style(DOM::StyleInvalidationReason::HTMLSelectElementSetIsOpen);
  281. }
  282. bool HTMLSelectElement::has_activation_behavior() const
  283. {
  284. return true;
  285. }
  286. static String strip_newlines(Optional<String> string)
  287. {
  288. // FIXME: Move this to a more general function
  289. if (!string.has_value())
  290. return {};
  291. StringBuilder builder;
  292. for (auto c : string.value().bytes_as_string_view()) {
  293. if (c == '\r' || c == '\n') {
  294. builder.append(' ');
  295. } else {
  296. builder.append(c);
  297. }
  298. }
  299. return MUST(Infra::strip_and_collapse_whitespace(MUST(builder.to_string())));
  300. }
  301. // https://html.spec.whatwg.org/multipage/input.html#show-the-picker,-if-applicable
  302. void HTMLSelectElement::show_the_picker_if_applicable()
  303. {
  304. // FIXME: Deduplicate with HTMLInputElement
  305. // To show the picker, if applicable for a select element:
  306. // 1. If element's relevant global object does not have transient activation, then return.
  307. auto& global_object = relevant_global_object(*this);
  308. if (!is<HTML::Window>(global_object))
  309. return;
  310. auto& relevant_global_object = static_cast<HTML::Window&>(global_object);
  311. if (!relevant_global_object.has_transient_activation())
  312. return;
  313. // 2. If element is not mutable, then return.
  314. if (!enabled())
  315. return;
  316. // 3. Consume user activation given element's relevant global object.
  317. relevant_global_object.consume_user_activation();
  318. // 4. If element's type attribute is in the File Upload state, then run these steps in parallel:
  319. // Not Applicable to select elements
  320. // 5. Otherwise, the user agent should show any relevant user interface for selecting a value for element,
  321. // in the way it normally would when the user interacts with the control. (If no such UI applies to element, then this step does nothing.)
  322. // If such a user interface is shown, it must respect the requirements stated in the relevant parts of the specification for how element
  323. // behaves given its type attribute state. (For example, various sections describe restrictions on the resulting value string.)
  324. // This step can have side effects, such as closing other pickers that were previously shown by this algorithm.
  325. // (If this closes a file selection picker, then per the above that will lead to firing either input and change events, or a cancel event.)
  326. // Populate select items
  327. m_select_items.clear();
  328. u32 id_counter = 1;
  329. for (auto const& child : children_as_vector()) {
  330. if (is<HTMLOptGroupElement>(*child)) {
  331. auto& opt_group_element = verify_cast<HTMLOptGroupElement>(*child);
  332. Vector<SelectItemOption> option_group_items;
  333. for (auto const& child : opt_group_element.children_as_vector()) {
  334. if (is<HTMLOptionElement>(*child)) {
  335. auto& option_element = verify_cast<HTMLOptionElement>(*child);
  336. option_group_items.append(SelectItemOption { id_counter++, option_element.selected(), option_element.disabled(), option_element, strip_newlines(option_element.text_content()), option_element.value() });
  337. }
  338. }
  339. m_select_items.append(SelectItemOptionGroup { opt_group_element.get_attribute(AttributeNames::label).value_or(String {}), option_group_items });
  340. }
  341. if (is<HTMLOptionElement>(*child)) {
  342. auto& option_element = verify_cast<HTMLOptionElement>(*child);
  343. m_select_items.append(SelectItemOption { id_counter++, option_element.selected(), option_element.disabled(), option_element, strip_newlines(option_element.text_content()), option_element.value() });
  344. }
  345. if (is<HTMLHRElement>(*child))
  346. m_select_items.append(SelectItemSeparator {});
  347. }
  348. // Request select dropdown
  349. auto weak_element = make_weak_ptr<HTMLSelectElement>();
  350. auto rect = get_bounding_client_rect();
  351. auto position = document().navigable()->to_top_level_position(Web::CSSPixelPoint { rect->x(), rect->y() });
  352. document().page().did_request_select_dropdown(weak_element, position, CSSPixels(rect->width()), m_select_items);
  353. set_is_open(true);
  354. }
  355. // https://html.spec.whatwg.org/multipage/input.html#dom-select-showpicker
  356. WebIDL::ExceptionOr<void> HTMLSelectElement::show_picker()
  357. {
  358. // FIXME: Deduplicate with HTMLInputElement
  359. // The showPicker() method steps are:
  360. // 1. If this is not mutable, then throw an "InvalidStateError" DOMException.
  361. if (!enabled())
  362. return WebIDL::InvalidStateError::create(realm(), "Element is not mutable"_string);
  363. // 2. If this's relevant settings object's origin is not same origin with this's relevant settings object's top-level origin,
  364. // and this is a select element, then throw a "SecurityError" DOMException.
  365. if (!relevant_settings_object(*this).origin().is_same_origin(relevant_settings_object(*this).top_level_origin)) {
  366. return WebIDL::SecurityError::create(realm(), "Cross origin pickers are not allowed"_string);
  367. }
  368. // 3. If this's relevant global object does not have transient activation, then throw a "NotAllowedError" DOMException.
  369. // FIXME: The global object we get here should probably not need casted to Window to check for transient activation
  370. auto& global_object = relevant_global_object(*this);
  371. if (!is<HTML::Window>(global_object) || !static_cast<HTML::Window&>(global_object).has_transient_activation()) {
  372. return WebIDL::NotAllowedError::create(realm(), "Too long since user activation to show picker"_string);
  373. }
  374. // FIXME: 4. If this is a select element, and this is not being rendered, then throw a "NotSupportedError" DOMException.
  375. // 5. Show the picker, if applicable, for this.
  376. show_the_picker_if_applicable();
  377. return {};
  378. }
  379. void HTMLSelectElement::activation_behavior(DOM::Event const& event)
  380. {
  381. if (event.is_trusted())
  382. show_the_picker_if_applicable();
  383. }
  384. void HTMLSelectElement::did_select_item(Optional<u32> const& id)
  385. {
  386. set_is_open(false);
  387. if (!id.has_value())
  388. return;
  389. for (auto const& option_element : list_of_options())
  390. option_element->set_selected(false);
  391. for (auto const& item : m_select_items) {
  392. if (item.has<SelectItemOption>()) {
  393. auto const& item_option = item.get<SelectItemOption>();
  394. if (item_option.id == *id)
  395. item_option.option_element->set_selected(true);
  396. }
  397. if (item.has<SelectItemOptionGroup>()) {
  398. auto item_option_group = item.get<SelectItemOptionGroup>();
  399. for (auto const& item_option : item_option_group.items) {
  400. if (item_option.id == *id)
  401. item_option.option_element->set_selected(true);
  402. }
  403. }
  404. }
  405. update_inner_text_element();
  406. queue_input_and_change_events();
  407. }
  408. void HTMLSelectElement::form_associated_element_was_inserted()
  409. {
  410. create_shadow_tree_if_needed();
  411. // Wait until children are ready
  412. queue_an_element_task(HTML::Task::Source::Microtask, [this] {
  413. update_selectedness();
  414. });
  415. }
  416. void HTMLSelectElement::form_associated_element_was_removed(DOM::Node*)
  417. {
  418. set_shadow_root(nullptr);
  419. }
  420. void HTMLSelectElement::computed_css_values_changed()
  421. {
  422. // Hide chevron icon when appearance is none
  423. if (m_chevron_icon_element) {
  424. auto appearance = computed_css_values()->appearance();
  425. if (appearance.has_value() && *appearance == CSS::Appearance::None) {
  426. MUST(m_chevron_icon_element->style_for_bindings()->set_property(CSS::PropertyID::Display, "none"_string));
  427. } else {
  428. MUST(m_chevron_icon_element->style_for_bindings()->set_property(CSS::PropertyID::Display, "block"_string));
  429. }
  430. }
  431. }
  432. void HTMLSelectElement::create_shadow_tree_if_needed()
  433. {
  434. if (shadow_root())
  435. return;
  436. auto shadow_root = heap().allocate<DOM::ShadowRoot>(realm(), document(), *this, Bindings::ShadowRootMode::Closed);
  437. set_shadow_root(shadow_root);
  438. auto border = DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  439. MUST(border->set_attribute(HTML::AttributeNames::style, R"~~~(
  440. display: flex;
  441. align-items: center;
  442. height: 100%;
  443. )~~~"_string));
  444. MUST(shadow_root->append_child(border));
  445. m_inner_text_element = DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  446. MUST(m_inner_text_element->set_attribute(HTML::AttributeNames::style, R"~~~(
  447. flex: 1;
  448. )~~~"_string));
  449. MUST(border->append_child(*m_inner_text_element));
  450. // FIXME: Find better way to add chevron icon
  451. auto chevron_fill_color = document().page().palette().base_text();
  452. auto chevron_svg = MUST(String::formatted("<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path fill=\"{}\" d=\"M7.41,8.58L12,13.17L16.59,8.58L18,10L12,16L6,10L7.41,8.58Z\"/></svg>", chevron_fill_color));
  453. m_chevron_icon_element = DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  454. MUST(m_chevron_icon_element->set_attribute(HTML::AttributeNames::style, R"~~~(
  455. width: 16px;
  456. height: 16px;
  457. margin-left: 4px;
  458. )~~~"_string));
  459. MUST(m_chevron_icon_element->set_inner_html(chevron_svg));
  460. MUST(border->append_child(*m_chevron_icon_element));
  461. update_inner_text_element();
  462. }
  463. void HTMLSelectElement::update_inner_text_element()
  464. {
  465. if (!m_inner_text_element)
  466. return;
  467. // Update inner text element to text content of selected option
  468. for (auto const& option_element : list_of_options()) {
  469. if (option_element->selected()) {
  470. m_inner_text_element->set_text_content(strip_newlines(option_element->text_content()));
  471. return;
  472. }
  473. }
  474. }
  475. // https://html.spec.whatwg.org/multipage/form-elements.html#selectedness-setting-algorithm
  476. void HTMLSelectElement::update_selectedness(JS::GCPtr<HTML::HTMLOptionElement> last_selected_option)
  477. {
  478. if (has_attribute(AttributeNames::multiple))
  479. return;
  480. // If element's multiple attribute is absent, and element's display size is 1,
  481. if (size() == 1) {
  482. bool has_selected_elements = false;
  483. for (auto const& option_element : list_of_options()) {
  484. if (option_element->selected()) {
  485. has_selected_elements = true;
  486. break;
  487. }
  488. }
  489. // and no option elements in the element's list of options have their selectedness set to true,
  490. if (!has_selected_elements) {
  491. // then set the selectedness of the first option element in the list of options in tree order
  492. // that is not disabled, if any, to true, and return.
  493. for (auto const& option_element : list_of_options()) {
  494. if (!option_element->disabled()) {
  495. option_element->set_selected_internal(true);
  496. update_inner_text_element();
  497. break;
  498. }
  499. }
  500. return;
  501. }
  502. }
  503. // If element's multiple attribute is absent,
  504. // and two or more option elements in element's list of options have their selectedness set to true,
  505. // then set the selectedness of all but the last option element with its selectedness set to true
  506. // in the list of options in tree order to false.
  507. int number_of_selected = 0;
  508. for (auto const& option_element : list_of_options()) {
  509. if (option_element->selected())
  510. ++number_of_selected;
  511. }
  512. // and two or more option elements in element's list of options have their selectedness set to true,
  513. if (number_of_selected >= 2) {
  514. // then set the selectedness of all but the last option element with its selectedness set to true
  515. // in the list of options in tree order to false.
  516. for (auto const& option_element : list_of_options()) {
  517. if (option_element == last_selected_option)
  518. continue;
  519. if (number_of_selected == 1) {
  520. break;
  521. }
  522. option_element->set_selected_internal(false);
  523. --number_of_selected;
  524. }
  525. }
  526. update_inner_text_element();
  527. }
  528. bool HTMLSelectElement::is_focusable() const
  529. {
  530. return enabled();
  531. }
  532. }