/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use std::default::Default; use std::iter; use webrender_api::units::DeviceIntRect; use ipc_channel::ipc; use dom_struct::dom_struct; use html5ever::{LocalName, Prefix, local_name}; use js::rust::HandleObject; use style::attr::AttrValue; use stylo_dom::ElementState; use embedder_traits::{SelectElementOptionOrOptgroup, SelectElementOption}; use euclid::{Size2D, Point2D, Rect}; use embedder_traits::EmbedderMsg; use crate::dom::bindings::codegen::GenericBindings::HTMLOptGroupElementBinding::HTMLOptGroupElement_Binding::HTMLOptGroupElementMethods; use crate::dom::activation::Activatable; use crate::dom::attr::Attr; use crate::dom::bindings::cell::{DomRefCell, Ref}; use crate::dom::bindings::codegen::Bindings::ElementBinding::ElementMethods; use crate::dom::bindings::codegen::Bindings::HTMLCollectionBinding::HTMLCollectionMethods; use crate::dom::bindings::codegen::Bindings::HTMLOptionElementBinding::HTMLOptionElementMethods; use crate::dom::bindings::codegen::Bindings::HTMLOptionsCollectionBinding::HTMLOptionsCollectionMethods; use crate::dom::bindings::codegen::Bindings::HTMLSelectElementBinding::HTMLSelectElementMethods; use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods; use crate::dom::bindings::codegen::Bindings::ShadowRootBinding::{ ShadowRootMode, SlotAssignmentMode, }; use crate::dom::bindings::codegen::GenericBindings::CharacterDataBinding::CharacterData_Binding::CharacterDataMethods; use crate::dom::bindings::codegen::UnionTypes::{ HTMLElementOrLong, HTMLOptionElementOrHTMLOptGroupElement, }; use crate::dom::bindings::error::ErrorResult; use crate::dom::bindings::inheritance::Castable; use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom}; use crate::dom::bindings::str::DOMString; use crate::dom::characterdata::CharacterData; use crate::dom::document::Document; use crate::dom::element::{AttributeMutation, Element}; use crate::dom::event::Event; use crate::dom::eventtarget::EventTarget; use crate::dom::htmlcollection::CollectionFilter; use crate::dom::htmldivelement::HTMLDivElement; use crate::dom::htmlelement::HTMLElement; use crate::dom::htmlfieldsetelement::HTMLFieldSetElement; use crate::dom::htmlformelement::{FormControl, FormDatum, FormDatumValue, HTMLFormElement}; use crate::dom::htmloptgroupelement::HTMLOptGroupElement; use crate::dom::htmloptionelement::HTMLOptionElement; use crate::dom::htmloptionscollection::HTMLOptionsCollection; use crate::dom::node::{BindContext, ChildrenMutation, Node, NodeTraits, UnbindContext}; use crate::dom::nodelist::NodeList; use crate::dom::shadowroot::IsUserAgentWidget; use crate::dom::text::Text; use crate::dom::validation::{Validatable, is_barred_by_datalist_ancestor}; use crate::dom::validitystate::{ValidationFlags, ValidityState}; use crate::dom::virtualmethods::VirtualMethods; use crate::script_runtime::CanGc; const DEFAULT_SELECT_SIZE: u32 = 0; const SELECT_BOX_STYLE: &str = " display: flex; align-items: center; height: 100%; "; const TEXT_CONTAINER_STYLE: &str = "flex: 1;"; const CHEVRON_CONTAINER_STYLE: &str = " font-size: 16px; margin: 4px; "; #[derive(JSTraceable, MallocSizeOf)] struct OptionsFilter; impl CollectionFilter for OptionsFilter { fn filter<'a>(&self, elem: &'a Element, root: &'a Node) -> bool { if !elem.is::() { return false; } let node = elem.upcast::(); if root.is_parent_of(node) { return true; } match node.GetParentNode() { Some(optgroup) => optgroup.is::() && root.is_parent_of(&optgroup), None => false, } } } #[dom_struct] pub(crate) struct HTMLSelectElement { htmlelement: HTMLElement, options: MutNullableDom, form_owner: MutNullableDom, labels_node_list: MutNullableDom, validity_state: MutNullableDom, shadow_tree: DomRefCell>, } /// Holds handles to all elements in the UA shadow tree #[derive(Clone, JSTraceable, MallocSizeOf)] #[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)] struct ShadowTree { selected_option: Dom, } impl HTMLSelectElement { fn new_inherited( local_name: LocalName, prefix: Option, document: &Document, ) -> HTMLSelectElement { HTMLSelectElement { htmlelement: HTMLElement::new_inherited_with_state( ElementState::ENABLED | ElementState::VALID, local_name, prefix, document, ), options: Default::default(), form_owner: Default::default(), labels_node_list: Default::default(), validity_state: Default::default(), shadow_tree: Default::default(), } } #[cfg_attr(crown, allow(crown::unrooted_must_root))] pub(crate) fn new( local_name: LocalName, prefix: Option, document: &Document, proto: Option, can_gc: CanGc, ) -> DomRoot { let n = Node::reflect_node_with_proto( Box::new(HTMLSelectElement::new_inherited( local_name, prefix, document, )), document, proto, can_gc, ); n.upcast::().set_weird_parser_insertion_mode(); n } // https://html.spec.whatwg.org/multipage/#concept-select-option-list pub(crate) fn list_of_options( &self, ) -> impl Iterator> + use<'_> { self.upcast::().children().flat_map(|node| { if node.is::() { let node = DomRoot::downcast::(node).unwrap(); Choice3::First(iter::once(node)) } else if node.is::() { Choice3::Second(node.children().filter_map(DomRoot::downcast)) } else { Choice3::Third(iter::empty()) } }) } // https://html.spec.whatwg.org/multipage/#placeholder-label-option fn get_placeholder_label_option(&self) -> Option> { if self.Required() && !self.Multiple() && self.display_size() == 1 { self.list_of_options().next().filter(|node| { let parent = node.upcast::().GetParentNode(); node.Value().is_empty() && parent.as_deref() == Some(self.upcast()) }) } else { None } } // https://html.spec.whatwg.org/multipage/#the-select-element:concept-form-reset-control pub(crate) fn reset(&self) { for opt in self.list_of_options() { opt.set_selectedness(opt.DefaultSelected()); opt.set_dirtiness(false); } self.ask_for_reset(); } // https://html.spec.whatwg.org/multipage/#ask-for-a-reset pub(crate) fn ask_for_reset(&self) { if self.Multiple() { return; } let mut first_enabled: Option> = None; let mut last_selected: Option> = None; for opt in self.list_of_options() { if opt.Selected() { opt.set_selectedness(false); last_selected = Some(DomRoot::from_ref(&opt)); } let element = opt.upcast::(); if first_enabled.is_none() && !element.disabled_state() { first_enabled = Some(DomRoot::from_ref(&opt)); } } if let Some(last_selected) = last_selected { last_selected.set_selectedness(true); } else if self.display_size() == 1 { if let Some(first_enabled) = first_enabled { first_enabled.set_selectedness(true); } } } pub(crate) fn push_form_data(&self, data_set: &mut Vec) { if self.Name().is_empty() { return; } for opt in self.list_of_options() { let element = opt.upcast::(); if opt.Selected() && element.enabled_state() { data_set.push(FormDatum { ty: self.Type(), name: self.Name(), value: FormDatumValue::String(opt.Value()), }); } } } // https://html.spec.whatwg.org/multipage/#concept-select-pick pub(crate) fn pick_option(&self, picked: &HTMLOptionElement) { if !self.Multiple() { let picked = picked.upcast(); for opt in self.list_of_options() { if opt.upcast::() != picked { opt.set_selectedness(false); } } } } // https://html.spec.whatwg.org/multipage/#concept-select-size fn display_size(&self) -> u32 { if self.Size() == 0 { if self.Multiple() { 4 } else { 1 } } else { self.Size() } } fn create_shadow_tree(&self, can_gc: CanGc) { let document = self.owner_document(); let root = self .upcast::() .attach_shadow( IsUserAgentWidget::Yes, ShadowRootMode::Closed, false, false, false, SlotAssignmentMode::Manual, can_gc, ) .expect("Attaching UA shadow root failed"); let select_box = HTMLDivElement::new(local_name!("div"), None, &document, None, can_gc); select_box.upcast::().set_string_attribute( &local_name!("style"), SELECT_BOX_STYLE.into(), can_gc, ); let text_container = HTMLDivElement::new(local_name!("div"), None, &document, None, can_gc); text_container.upcast::().set_string_attribute( &local_name!("style"), TEXT_CONTAINER_STYLE.into(), can_gc, ); select_box .upcast::() .AppendChild(text_container.upcast::(), can_gc) .unwrap(); let text = Text::new(DOMString::new(), &document, can_gc); let _ = self.shadow_tree.borrow_mut().insert(ShadowTree { selected_option: text.as_traced(), }); text_container .upcast::() .AppendChild(text.upcast::(), can_gc) .unwrap(); let chevron_container = HTMLDivElement::new(local_name!("div"), None, &document, None, can_gc); chevron_container.upcast::().set_string_attribute( &local_name!("style"), CHEVRON_CONTAINER_STYLE.into(), can_gc, ); chevron_container .upcast::() .SetTextContent(Some("▾".into()), can_gc); select_box .upcast::() .AppendChild(chevron_container.upcast::(), can_gc) .unwrap(); root.upcast::() .AppendChild(select_box.upcast::(), can_gc) .unwrap(); } fn shadow_tree(&self, can_gc: CanGc) -> Ref<'_, ShadowTree> { if !self.upcast::().is_shadow_host() { self.create_shadow_tree(can_gc); } Ref::filter_map(self.shadow_tree.borrow(), Option::as_ref) .ok() .expect("UA shadow tree was not created") } pub(crate) fn update_shadow_tree(&self, can_gc: CanGc) { let shadow_tree = self.shadow_tree(can_gc); let selected_option_text = self .selected_option() .or_else(|| self.list_of_options().next()) .map(|option| option.displayed_label()) .unwrap_or_default(); // Replace newlines with whitespace, then collapse and trim whitespace let displayed_text = itertools::join(selected_option_text.split_whitespace(), " "); shadow_tree .selected_option .upcast::() .SetData(displayed_text.trim().into()); } pub(crate) fn selection_changed(&self, can_gc: CanGc) { self.update_shadow_tree(can_gc); self.upcast::() .fire_bubbling_event(atom!("change"), can_gc); } fn selected_option(&self) -> Option> { self.list_of_options().find(|opt_elem| opt_elem.Selected()) } pub(crate) fn show_menu(&self, can_gc: CanGc) -> Option { let (ipc_sender, ipc_receiver) = ipc::channel().expect("Failed to create IPC channel!"); // Collect list of optgroups and options let mut index = 0; let mut embedder_option_from_option = |option: &HTMLOptionElement| { let embedder_option = SelectElementOption { id: index, label: option.displayed_label().into(), is_disabled: option.Disabled(), }; index += 1; embedder_option }; let options = self .upcast::() .children() .flat_map(|child| { if let Some(option) = child.downcast::() { return Some(embedder_option_from_option(option).into()); } if let Some(optgroup) = child.downcast::() { let options = optgroup .upcast::() .children() .flat_map(DomRoot::downcast::) .map(|option| embedder_option_from_option(&option)) .collect(); let label = optgroup.Label().into(); return Some(SelectElementOptionOrOptgroup::Optgroup { label, options }); } None }) .collect(); let rect = self.upcast::().bounding_content_box_or_zero(can_gc); let rect = Rect::new( Point2D::new(rect.origin.x.to_px(), rect.origin.y.to_px()), Size2D::new(rect.size.width.to_px(), rect.size.height.to_px()), ); let selected_index = self.list_of_options().position(|option| option.Selected()); let document = self.owner_document(); document.send_to_embedder(EmbedderMsg::ShowSelectElementMenu( document.webview_id(), options, selected_index, DeviceIntRect::from_untyped(&rect.to_box2d()), ipc_sender, )); let Ok(response) = ipc_receiver.recv() else { log::error!("Failed to receive response"); return None; }; if response.is_some() && response != selected_index { self.selection_changed(can_gc); } response } } impl HTMLSelectElementMethods for HTMLSelectElement { /// fn Add( &self, element: HTMLOptionElementOrHTMLOptGroupElement, before: Option, ) -> ErrorResult { self.Options().Add(element, before) } // https://html.spec.whatwg.org/multipage/#dom-fe-disabled make_bool_getter!(Disabled, "disabled"); // https://html.spec.whatwg.org/multipage/#dom-fe-disabled make_bool_setter!(SetDisabled, "disabled"); /// fn GetForm(&self) -> Option> { self.form_owner() } // https://html.spec.whatwg.org/multipage/#dom-select-multiple make_bool_getter!(Multiple, "multiple"); // https://html.spec.whatwg.org/multipage/#dom-select-multiple make_bool_setter!(SetMultiple, "multiple"); // https://html.spec.whatwg.org/multipage/#dom-fe-name make_getter!(Name, "name"); // https://html.spec.whatwg.org/multipage/#dom-fe-name make_atomic_setter!(SetName, "name"); // https://html.spec.whatwg.org/multipage/#dom-select-required make_bool_getter!(Required, "required"); // https://html.spec.whatwg.org/multipage/#dom-select-required make_bool_setter!(SetRequired, "required"); // https://html.spec.whatwg.org/multipage/#dom-select-size make_uint_getter!(Size, "size", DEFAULT_SELECT_SIZE); // https://html.spec.whatwg.org/multipage/#dom-select-size make_uint_setter!(SetSize, "size", DEFAULT_SELECT_SIZE); /// fn Type(&self) -> DOMString { DOMString::from(if self.Multiple() { "select-multiple" } else { "select-one" }) } // https://html.spec.whatwg.org/multipage/#dom-lfe-labels make_labels_getter!(Labels, labels_node_list); /// fn Options(&self) -> DomRoot { self.options.or_init(|| { let window = self.owner_window(); HTMLOptionsCollection::new(&window, self, Box::new(OptionsFilter), CanGc::note()) }) } /// fn Length(&self) -> u32 { self.Options().Length() } /// fn SetLength(&self, length: u32, can_gc: CanGc) { self.Options().SetLength(length, can_gc) } /// fn Item(&self, index: u32) -> Option> { self.Options().upcast().Item(index) } /// fn IndexedGetter(&self, index: u32) -> Option> { self.Options().IndexedGetter(index) } /// fn IndexedSetter( &self, index: u32, value: Option<&HTMLOptionElement>, can_gc: CanGc, ) -> ErrorResult { self.Options().IndexedSetter(index, value, can_gc) } /// fn NamedItem(&self, name: DOMString) -> Option> { self.Options() .NamedGetter(name) .and_then(DomRoot::downcast::) } /// fn Remove_(&self, index: i32) { self.Options().Remove(index) } /// fn Remove(&self) { self.upcast::().Remove(CanGc::note()) } /// fn Value(&self) -> DOMString { self.selected_option() .map(|opt_elem| opt_elem.Value()) .unwrap_or_default() } /// fn SetValue(&self, value: DOMString) { let mut opt_iter = self.list_of_options(); // Reset until we find an