diff --git a/components/constellation/tracing.rs b/components/constellation/tracing.rs index 08e6fecc099..fd00d62067b 100644 --- a/components/constellation/tracing.rs +++ b/components/constellation/tracing.rs @@ -236,6 +236,7 @@ mod from_script { Self::StopGamepadHapticEffect(..) => target_variant!("StopGamepadHapticEffect"), Self::ShutdownComplete => target_variant!("ShutdownComplete"), Self::ShowNotification(..) => target_variant!("ShowNotification"), + Self::ShowSelectElementMenu(..) => target_variant!("ShowSelectElementMenu"), } } } diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 78580bb20f6..6528a8e641d 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -75,10 +75,6 @@ use uuid::Uuid; use webgpu::swapchain::WebGPUContextId; use webrender_api::units::DeviceIntRect; -use super::bindings::codegen::Bindings::XPathEvaluatorBinding::XPathEvaluatorMethods; -use super::canvasrenderingcontext2d::CanvasRenderingContext2D; -use super::clipboardevent::ClipboardEventType; -use super::performancepainttiming::PerformancePaintTiming; use crate::animation_timeline::AnimationTimeline; use crate::animations::Animations; use crate::canvas_context::CanvasContext as _; @@ -105,6 +101,7 @@ use crate::dom::bindings::codegen::Bindings::TouchBinding::TouchMethods; use crate::dom::bindings::codegen::Bindings::WindowBinding::{ FrameRequestCallback, ScrollBehavior, WindowMethods, }; +use crate::dom::bindings::codegen::Bindings::XPathEvaluatorBinding::XPathEvaluatorMethods; use crate::dom::bindings::codegen::Bindings::XPathNSResolverBinding::XPathNSResolver; use crate::dom::bindings::codegen::UnionTypes::{NodeOrString, StringOrElementCreationOptions}; use crate::dom::bindings::error::{Error, ErrorInfo, ErrorResult, Fallible}; @@ -120,8 +117,9 @@ use crate::dom::bindings::weakref::WeakRef; use crate::dom::bindings::xmlname::{ matches_name_production, namespace_from_domstring, validate_and_extract, }; +use crate::dom::canvasrenderingcontext2d::CanvasRenderingContext2D; use crate::dom::cdatasection::CDATASection; -use crate::dom::clipboardevent::ClipboardEvent; +use crate::dom::clipboardevent::{ClipboardEvent, ClipboardEventType}; use crate::dom::comment::Comment; use crate::dom::compositionevent::CompositionEvent; use crate::dom::cssstylesheet::CSSStyleSheet; @@ -171,6 +169,7 @@ use crate::dom::nodeiterator::NodeIterator; use crate::dom::nodelist::NodeList; use crate::dom::pagetransitionevent::PageTransitionEvent; use crate::dom::performanceentry::PerformanceEntry; +use crate::dom::performancepainttiming::PerformancePaintTiming; use crate::dom::pointerevent::{PointerEvent, PointerId}; use crate::dom::processinginstruction::ProcessingInstruction; use crate::dom::promise::Promise; @@ -1274,7 +1273,7 @@ impl Document { } } - fn send_to_embedder(&self, msg: EmbedderMsg) { + pub(crate) fn send_to_embedder(&self, msg: EmbedderMsg) { let window = self.window(); window.send_to_embedder(msg); } @@ -1312,7 +1311,7 @@ impl Document { let node = unsafe { node::from_untrusted_compositor_node_address(hit_test_result.node) }; let Some(el) = node - .inclusive_ancestors(ShadowIncluding::No) + .inclusive_ancestors(ShadowIncluding::Yes) .filter_map(DomRoot::downcast::) .next() else { diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index 5ce2473585e..db0b4c63c5f 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -514,7 +514,6 @@ impl Element { #[allow(clippy::too_many_arguments)] pub(crate) fn attach_shadow( &self, - // TODO: remove is_ua_widget argument is_ua_widget: IsUserAgentWidget, mode: ShadowRootMode, clonable: bool, @@ -4379,6 +4378,12 @@ impl Element { let element = self.downcast::().unwrap(); Some(element as &dyn Activatable) }, + NodeTypeId::Element(ElementTypeId::HTMLElement( + HTMLElementTypeId::HTMLSelectElement, + )) => { + let element = self.downcast::().unwrap(); + Some(element as &dyn Activatable) + }, NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLElement)) => { let element = self.downcast::().unwrap(); Some(element as &dyn Activatable) diff --git a/components/script/dom/htmloptionelement.rs b/components/script/dom/htmloptionelement.rs index e8471f408d2..d6e8be04b64 100644 --- a/components/script/dom/htmloptionelement.rs +++ b/components/script/dom/htmloptionelement.rs @@ -93,12 +93,7 @@ impl HTMLOptionElement { } fn pick_if_selected_and_reset(&self) { - if let Some(select) = self - .upcast::() - .ancestors() - .filter_map(DomRoot::downcast::) - .next() - { + if let Some(select) = self.owner_select_element() { if self.Selected() { select.pick_option(self); } @@ -108,51 +103,53 @@ impl HTMLOptionElement { // https://html.spec.whatwg.org/multipage/#concept-option-index fn index(&self) -> i32 { - if let Some(parent) = self.upcast::().GetParentNode() { - if let Some(select_parent) = parent.downcast::() { - // return index in parent select's list of options - return self.index_in_select(select_parent); - } else if parent.is::() { - if let Some(grandparent) = parent.GetParentNode() { - if let Some(select_grandparent) = grandparent.downcast::() { - // return index in grandparent select's list of options - return self.index_in_select(select_grandparent); - } - } - } - } - // "If the option element is not in a list of options, - // then the option element's index is zero." - // self is neither a child of a select, nor a grandchild of a select - // via an optgroup, so it is not in a list of options - 0 + let Some(owner_select) = self.owner_select_element() else { + return 0; + }; + + let Some(position) = owner_select.list_of_options().position(|n| &*n == self) else { + // An option should always be in it's owner's list of options, but it's not worth a browser panic + warn!("HTMLOptionElement called index_in_select at a select that did not contain it"); + return 0; + }; + + position.try_into().unwrap_or(0) } - fn index_in_select(&self, select: &HTMLSelectElement) -> i32 { - match select.list_of_options().position(|n| &*n == self) { - Some(index) => index.try_into().unwrap_or(0), - None => { - // shouldn't happen but not worth a browser panic - warn!( - "HTMLOptionElement called index_in_select at a select that did not contain it" - ); - 0 - }, + fn owner_select_element(&self) -> Option> { + let parent = self.upcast::().GetParentNode()?; + + if parent.is::() { + DomRoot::downcast::(parent.GetParentNode()?) + } else { + DomRoot::downcast::(parent) } } fn update_select_validity(&self, can_gc: CanGc) { - if let Some(select) = self - .upcast::() - .ancestors() - .filter_map(DomRoot::downcast::) - .next() - { + if let Some(select) = self.owner_select_element() { select .validity_state() .perform_validation_and_update(ValidationFlags::all(), can_gc); } } + + /// + /// + /// Note that this is not equivalent to . + pub(crate) fn displayed_label(&self) -> DOMString { + // > The label of an option element is the value of the label content attribute, if there is one + // > and its value is not the empty string, or, otherwise, the value of the element's text IDL attribute. + let label = self + .upcast::() + .get_string_attribute(&local_name!("label")); + + if label.is_empty() { + return self.Text(); + } + + label + } } // FIXME(ajeffrey): Provide a way of buffering DOMStrings other than using Strings @@ -175,7 +172,7 @@ fn collect_text(element: &Element, value: &mut String) { } impl HTMLOptionElementMethods for HTMLOptionElement { - // https://html.spec.whatwg.org/multipage/#dom-option + /// fn Option( window: &Window, proto: Option, @@ -217,19 +214,19 @@ impl HTMLOptionElementMethods for HTMLOptionElement { // https://html.spec.whatwg.org/multipage/#dom-option-disabled make_bool_setter!(SetDisabled, "disabled"); - // https://html.spec.whatwg.org/multipage/#dom-option-text + /// fn Text(&self) -> DOMString { let mut content = String::new(); collect_text(self.upcast(), &mut content); DOMString::from(str_join(split_html_space_chars(&content), " ")) } - // https://html.spec.whatwg.org/multipage/#dom-option-text + /// fn SetText(&self, value: DOMString, can_gc: CanGc) { self.upcast::().SetTextContent(Some(value), can_gc) } - // https://html.spec.whatwg.org/multipage/#dom-option-form + /// fn GetForm(&self) -> Option> { let parent = self.upcast::().GetParentNode().and_then(|p| { if p.is::() { @@ -242,7 +239,7 @@ impl HTMLOptionElementMethods for HTMLOptionElement { parent.and_then(|p| p.downcast::().and_then(|s| s.GetForm())) } - // https://html.spec.whatwg.org/multipage/#attr-option-value + /// fn Value(&self) -> DOMString { let element = self.upcast::(); let attr = &local_name!("value"); @@ -256,7 +253,7 @@ impl HTMLOptionElementMethods for HTMLOptionElement { // https://html.spec.whatwg.org/multipage/#attr-option-value make_setter!(SetValue, "value"); - // https://html.spec.whatwg.org/multipage/#attr-option-label + /// fn Label(&self) -> DOMString { let element = self.upcast::(); let attr = &local_name!("label"); @@ -276,12 +273,12 @@ impl HTMLOptionElementMethods for HTMLOptionElement { // https://html.spec.whatwg.org/multipage/#dom-option-defaultselected make_bool_setter!(SetDefaultSelected, "selected"); - // https://html.spec.whatwg.org/multipage/#dom-option-selected + /// fn Selected(&self) -> bool { self.selectedness.get() } - // https://html.spec.whatwg.org/multipage/#dom-option-selected + /// fn SetSelected(&self, selected: bool) { self.dirtiness.set(true); self.selectedness.set(selected); @@ -289,7 +286,7 @@ impl HTMLOptionElementMethods for HTMLOptionElement { self.update_select_validity(CanGc::note()); } - // https://html.spec.whatwg.org/multipage/#dom-option-index + /// fn Index(&self) -> i32 { self.index() } @@ -337,6 +334,13 @@ impl VirtualMethods for HTMLOptionElement { } self.update_select_validity(can_gc); }, + local_name!("label") => { + // The label of the selected option is displayed inside the select element, so we need to repaint + // when it changes + if let Some(select_element) = self.owner_select_element() { + select_element.update_shadow_tree(CanGc::note()); + } + }, _ => {}, } } diff --git a/components/script/dom/htmloptionscollection.rs b/components/script/dom/htmloptionscollection.rs index 1b31ea4bfcc..e38749b3aa5 100644 --- a/components/script/dom/htmloptionscollection.rs +++ b/components/script/dom/htmloptionscollection.rs @@ -240,11 +240,11 @@ impl HTMLOptionsCollectionMethods for HTMLOptionsCollectio } /// - fn SetSelectedIndex(&self, index: i32) { + fn SetSelectedIndex(&self, index: i32, can_gc: CanGc) { self.upcast() .root_node() .downcast::() .expect("HTMLOptionsCollection not rooted on a HTMLSelectElement") - .SetSelectedIndex(index) + .SetSelectedIndex(index, can_gc) } } diff --git a/components/script/dom/htmlselectelement.rs b/components/script/dom/htmlselectelement.rs index 3804f5a3bfe..e349b58f9bb 100644 --- a/components/script/dom/htmlselectelement.rs +++ b/components/script/dom/htmlselectelement.rs @@ -5,42 +5,75 @@ 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::{DomRoot, MutNullableDom}; +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, Node, NodeTraits, UnbindContext}; +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 { @@ -68,9 +101,15 @@ pub(crate) struct HTMLSelectElement { form_owner: MutNullableDom, labels_node_list: MutNullableDom, validity_state: MutNullableDom, + shadow_tree: DomRefCell>, } -static DEFAULT_SELECT_SIZE: u32 = 0; +/// 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( @@ -89,6 +128,7 @@ impl HTMLSelectElement { form_owner: Default::default(), labels_node_list: Default::default(), validity_state: Default::default(), + shadow_tree: Default::default(), } } @@ -215,10 +255,178 @@ impl HTMLSelectElement { 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::()) + .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::()) + .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::()) + .unwrap(); + + root.upcast::() + .AppendChild(select_box.upcast::()) + .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 { - // https://html.spec.whatwg.org/multipage/#dom-select-add + /// fn Add( &self, element: HTMLOptionElementOrHTMLOptGroupElement, @@ -233,7 +441,7 @@ impl HTMLSelectElementMethods for HTMLSelectElement { // https://html.spec.whatwg.org/multipage/#dom-fe-disabled make_bool_setter!(SetDisabled, "disabled"); - // https://html.spec.whatwg.org/multipage/#dom-fae-form + /// fn GetForm(&self) -> Option> { self.form_owner() } @@ -262,7 +470,7 @@ impl HTMLSelectElementMethods for HTMLSelectElement { // https://html.spec.whatwg.org/multipage/#dom-select-size make_uint_setter!(SetSize, "size", DEFAULT_SELECT_SIZE); - // https://html.spec.whatwg.org/multipage/#dom-select-type + /// fn Type(&self) -> DOMString { DOMString::from(if self.Multiple() { "select-multiple" @@ -274,7 +482,7 @@ impl HTMLSelectElementMethods for HTMLSelectElement { // https://html.spec.whatwg.org/multipage/#dom-lfe-labels make_labels_getter!(Labels, labels_node_list); - // https://html.spec.whatwg.org/multipage/#dom-select-options + /// fn Options(&self) -> DomRoot { self.options.or_init(|| { let window = self.owner_window(); @@ -282,27 +490,27 @@ impl HTMLSelectElementMethods for HTMLSelectElement { }) } - // https://html.spec.whatwg.org/multipage/#dom-select-length + /// fn Length(&self) -> u32 { self.Options().Length() } - // https://html.spec.whatwg.org/multipage/#dom-select-length + /// fn SetLength(&self, length: u32, can_gc: CanGc) { self.Options().SetLength(length, can_gc) } - // https://html.spec.whatwg.org/multipage/#dom-select-item + /// fn Item(&self, index: u32) -> Option> { self.Options().upcast().Item(index) } - // https://html.spec.whatwg.org/multipage/#dom-select-item + /// fn IndexedGetter(&self, index: u32) -> Option> { self.Options().IndexedGetter(index) } - // https://html.spec.whatwg.org/multipage/#dom-select-setter + /// fn IndexedSetter( &self, index: u32, @@ -312,33 +520,31 @@ impl HTMLSelectElementMethods for HTMLSelectElement { self.Options().IndexedSetter(index, value, can_gc) } - // https://html.spec.whatwg.org/multipage/#dom-select-nameditem + /// fn NamedItem(&self, name: DOMString) -> Option> { self.Options() .NamedGetter(name) .and_then(DomRoot::downcast::) } - // https://html.spec.whatwg.org/multipage/#dom-select-remove + /// fn Remove_(&self, index: i32) { self.Options().Remove(index) } - // https://html.spec.whatwg.org/multipage/#dom-select-remove + /// fn Remove(&self) { self.upcast::().Remove() } - // https://html.spec.whatwg.org/multipage/#dom-select-value + /// fn Value(&self) -> DOMString { - self.list_of_options() - .filter(|opt_elem| opt_elem.Selected()) + self.selected_option() .map(|opt_elem| opt_elem.Value()) - .next() .unwrap_or_default() } - // https://html.spec.whatwg.org/multipage/#dom-select-value + /// fn SetValue(&self, value: DOMString) { let mut opt_iter = self.list_of_options(); // Reset until we find an