script/dom/html/
htmlselectelement.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::default::Default;
6use std::iter;
7
8use dom_struct::dom_struct;
9use embedder_traits::{EmbedderMsg, FormControl as EmbedderFormControl};
10use embedder_traits::{SelectElementOption, SelectElementOptionOrOptgroup};
11use euclid::{Point2D, Rect, Size2D};
12use html5ever::{LocalName, Prefix, QualName, local_name, ns};
13use js::rust::HandleObject;
14use style::attr::AttrValue;
15use stylo_dom::ElementState;
16use webrender_api::units::DeviceIntRect;
17use base::generic_channel;
18use crate::dom::bindings::refcounted::Trusted;
19use crate::dom::event::{EventBubbles, EventCancelable, EventComposed};
20use crate::dom::bindings::codegen::GenericBindings::HTMLOptGroupElementBinding::HTMLOptGroupElement_Binding::HTMLOptGroupElementMethods;
21use crate::dom::activation::Activatable;
22use crate::dom::attr::Attr;
23use crate::dom::bindings::cell::{DomRefCell, Ref};
24use crate::dom::bindings::codegen::Bindings::ElementBinding::ElementMethods;
25use crate::dom::bindings::codegen::Bindings::HTMLCollectionBinding::HTMLCollectionMethods;
26use crate::dom::bindings::codegen::Bindings::HTMLOptionElementBinding::HTMLOptionElementMethods;
27use crate::dom::bindings::codegen::Bindings::HTMLOptionsCollectionBinding::HTMLOptionsCollectionMethods;
28use crate::dom::bindings::codegen::Bindings::HTMLSelectElementBinding::HTMLSelectElementMethods;
29use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
30use crate::dom::bindings::codegen::GenericBindings::CharacterDataBinding::CharacterData_Binding::CharacterDataMethods;
31use crate::dom::bindings::codegen::UnionTypes::{
32    HTMLElementOrLong, HTMLOptionElementOrHTMLOptGroupElement,
33};
34use crate::dom::bindings::error::ErrorResult;
35use crate::dom::bindings::inheritance::Castable;
36use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
37use crate::dom::bindings::str::DOMString;
38use crate::dom::characterdata::CharacterData;
39use crate::dom::document::Document;
40use crate::dom::element::{AttributeMutation, CustomElementCreationMode, Element, ElementCreator};
41use crate::dom::event::Event;
42use crate::dom::eventtarget::EventTarget;
43use crate::dom::html::htmlcollection::CollectionFilter;
44use crate::dom::html::htmlelement::HTMLElement;
45use crate::dom::html::htmlfieldsetelement::HTMLFieldSetElement;
46use crate::dom::html::htmlformelement::{FormControl, FormDatum, FormDatumValue, HTMLFormElement};
47use crate::dom::html::htmloptgroupelement::HTMLOptGroupElement;
48use crate::dom::html::htmloptionelement::HTMLOptionElement;
49use crate::dom::html::htmloptionscollection::HTMLOptionsCollection;
50use crate::dom::node::{BindContext, ChildrenMutation, Node, NodeTraits, UnbindContext};
51use crate::dom::nodelist::NodeList;
52use crate::dom::text::Text;
53use crate::dom::validation::{Validatable, is_barred_by_datalist_ancestor};
54use crate::dom::validitystate::{ValidationFlags, ValidityState};
55use crate::dom::virtualmethods::VirtualMethods;
56use crate::script_runtime::CanGc;
57
58const DEFAULT_SELECT_SIZE: u32 = 0;
59
60const SELECT_BOX_STYLE: &str = "
61    display: flex;
62    align-items: center;
63    height: 100%;
64";
65
66const TEXT_CONTAINER_STYLE: &str = "flex: 1;";
67
68const CHEVRON_CONTAINER_STYLE: &str = "
69    font-size: 16px;
70    margin: 4px;
71";
72
73#[derive(JSTraceable, MallocSizeOf)]
74struct OptionsFilter;
75impl CollectionFilter for OptionsFilter {
76    fn filter<'a>(&self, elem: &'a Element, root: &'a Node) -> bool {
77        if !elem.is::<HTMLOptionElement>() {
78            return false;
79        }
80
81        let node = elem.upcast::<Node>();
82        if root.is_parent_of(node) {
83            return true;
84        }
85
86        match node.GetParentNode() {
87            Some(optgroup) => optgroup.is::<HTMLOptGroupElement>() && root.is_parent_of(&optgroup),
88            None => false,
89        }
90    }
91}
92
93#[dom_struct]
94pub(crate) struct HTMLSelectElement {
95    htmlelement: HTMLElement,
96    options: MutNullableDom<HTMLOptionsCollection>,
97    form_owner: MutNullableDom<HTMLFormElement>,
98    labels_node_list: MutNullableDom<NodeList>,
99    validity_state: MutNullableDom<ValidityState>,
100    shadow_tree: DomRefCell<Option<ShadowTree>>,
101}
102
103/// Holds handles to all elements in the UA shadow tree
104#[derive(Clone, JSTraceable, MallocSizeOf)]
105#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
106struct ShadowTree {
107    selected_option: Dom<Text>,
108}
109
110impl HTMLSelectElement {
111    fn new_inherited(
112        local_name: LocalName,
113        prefix: Option<Prefix>,
114        document: &Document,
115    ) -> HTMLSelectElement {
116        HTMLSelectElement {
117            htmlelement: HTMLElement::new_inherited_with_state(
118                ElementState::ENABLED | ElementState::VALID,
119                local_name,
120                prefix,
121                document,
122            ),
123            options: Default::default(),
124            form_owner: Default::default(),
125            labels_node_list: Default::default(),
126            validity_state: Default::default(),
127            shadow_tree: Default::default(),
128        }
129    }
130
131    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
132    pub(crate) fn new(
133        local_name: LocalName,
134        prefix: Option<Prefix>,
135        document: &Document,
136        proto: Option<HandleObject>,
137        can_gc: CanGc,
138    ) -> DomRoot<HTMLSelectElement> {
139        let n = Node::reflect_node_with_proto(
140            Box::new(HTMLSelectElement::new_inherited(
141                local_name, prefix, document,
142            )),
143            document,
144            proto,
145            can_gc,
146        );
147
148        n.upcast::<Node>().set_weird_parser_insertion_mode();
149        n
150    }
151
152    /// <https://html.spec.whatwg.org/multipage/#concept-select-option-list>
153    pub(crate) fn list_of_options(
154        &self,
155    ) -> impl Iterator<Item = DomRoot<HTMLOptionElement>> + use<'_> {
156        self.upcast::<Node>().children().flat_map(|node| {
157            if node.is::<HTMLOptionElement>() {
158                let node = DomRoot::downcast::<HTMLOptionElement>(node).unwrap();
159                Choice3::First(iter::once(node))
160            } else if node.is::<HTMLOptGroupElement>() {
161                Choice3::Second(node.children().filter_map(DomRoot::downcast))
162            } else {
163                Choice3::Third(iter::empty())
164            }
165        })
166    }
167
168    // https://html.spec.whatwg.org/multipage/#placeholder-label-option
169    fn get_placeholder_label_option(&self) -> Option<DomRoot<HTMLOptionElement>> {
170        if self.Required() && !self.Multiple() && self.display_size() == 1 {
171            self.list_of_options().next().filter(|node| {
172                let parent = node.upcast::<Node>().GetParentNode();
173                node.Value().is_empty() && parent.as_deref() == Some(self.upcast())
174            })
175        } else {
176            None
177        }
178    }
179
180    // https://html.spec.whatwg.org/multipage/#the-select-element:concept-form-reset-control
181    pub(crate) fn reset(&self) {
182        for opt in self.list_of_options() {
183            opt.set_selectedness(opt.DefaultSelected());
184            opt.set_dirtiness(false);
185        }
186        self.ask_for_reset();
187    }
188
189    // https://html.spec.whatwg.org/multipage/#ask-for-a-reset
190    pub(crate) fn ask_for_reset(&self) {
191        if self.Multiple() {
192            return;
193        }
194
195        let mut first_enabled: Option<DomRoot<HTMLOptionElement>> = None;
196        let mut last_selected: Option<DomRoot<HTMLOptionElement>> = None;
197
198        for opt in self.list_of_options() {
199            if opt.Selected() {
200                opt.set_selectedness(false);
201                last_selected = Some(DomRoot::from_ref(&opt));
202            }
203            let element = opt.upcast::<Element>();
204            if first_enabled.is_none() && !element.disabled_state() {
205                first_enabled = Some(DomRoot::from_ref(&opt));
206            }
207        }
208
209        if let Some(last_selected) = last_selected {
210            last_selected.set_selectedness(true);
211        } else if self.display_size() == 1 {
212            if let Some(first_enabled) = first_enabled {
213                first_enabled.set_selectedness(true);
214            }
215        }
216    }
217
218    pub(crate) fn push_form_data(&self, data_set: &mut Vec<FormDatum>) {
219        if self.Name().is_empty() {
220            return;
221        }
222        for opt in self.list_of_options() {
223            let element = opt.upcast::<Element>();
224            if opt.Selected() && element.enabled_state() {
225                data_set.push(FormDatum {
226                    ty: self.Type(),
227                    name: self.Name(),
228                    value: FormDatumValue::String(opt.Value()),
229                });
230            }
231        }
232    }
233
234    // https://html.spec.whatwg.org/multipage/#concept-select-pick
235    pub(crate) fn pick_option(&self, picked: &HTMLOptionElement) {
236        if !self.Multiple() {
237            let picked = picked.upcast();
238            for opt in self.list_of_options() {
239                if opt.upcast::<HTMLElement>() != picked {
240                    opt.set_selectedness(false);
241                }
242            }
243        }
244    }
245
246    // https://html.spec.whatwg.org/multipage/#concept-select-size
247    fn display_size(&self) -> u32 {
248        if self.Size() == 0 {
249            if self.Multiple() { 4 } else { 1 }
250        } else {
251            self.Size()
252        }
253    }
254
255    fn create_shadow_tree(&self, can_gc: CanGc) {
256        let document = self.owner_document();
257        let root = self.upcast::<Element>().attach_ua_shadow_root(true, can_gc);
258
259        let select_box = Element::create(
260            QualName::new(None, ns!(html), local_name!("div")),
261            None,
262            &document,
263            ElementCreator::ScriptCreated,
264            CustomElementCreationMode::Asynchronous,
265            None,
266            can_gc,
267        );
268        select_box.set_string_attribute(&local_name!("style"), SELECT_BOX_STYLE.into(), can_gc);
269
270        let text_container = Element::create(
271            QualName::new(None, ns!(html), local_name!("div")),
272            None,
273            &document,
274            ElementCreator::ScriptCreated,
275            CustomElementCreationMode::Asynchronous,
276            None,
277            can_gc,
278        );
279        text_container.set_string_attribute(
280            &local_name!("style"),
281            TEXT_CONTAINER_STYLE.into(),
282            can_gc,
283        );
284        select_box
285            .upcast::<Node>()
286            .AppendChild(text_container.upcast::<Node>(), can_gc)
287            .unwrap();
288
289        let text = Text::new(DOMString::new(), &document, can_gc);
290        let _ = self.shadow_tree.borrow_mut().insert(ShadowTree {
291            selected_option: text.as_traced(),
292        });
293        text_container
294            .upcast::<Node>()
295            .AppendChild(text.upcast::<Node>(), can_gc)
296            .unwrap();
297
298        let chevron_container = Element::create(
299            QualName::new(None, ns!(html), local_name!("div")),
300            None,
301            &document,
302            ElementCreator::ScriptCreated,
303            CustomElementCreationMode::Asynchronous,
304            None,
305            can_gc,
306        );
307        chevron_container.set_string_attribute(
308            &local_name!("style"),
309            CHEVRON_CONTAINER_STYLE.into(),
310            can_gc,
311        );
312        chevron_container
313            .upcast::<Node>()
314            .set_text_content_for_element(Some("▾".into()), can_gc);
315        select_box
316            .upcast::<Node>()
317            .AppendChild(chevron_container.upcast::<Node>(), can_gc)
318            .unwrap();
319
320        root.upcast::<Node>()
321            .AppendChild(select_box.upcast::<Node>(), can_gc)
322            .unwrap();
323    }
324
325    fn shadow_tree(&self, can_gc: CanGc) -> Ref<'_, ShadowTree> {
326        if !self.upcast::<Element>().is_shadow_host() {
327            self.create_shadow_tree(can_gc);
328        }
329
330        Ref::filter_map(self.shadow_tree.borrow(), Option::as_ref)
331            .ok()
332            .expect("UA shadow tree was not created")
333    }
334
335    pub(crate) fn update_shadow_tree(&self, can_gc: CanGc) {
336        let shadow_tree = self.shadow_tree(can_gc);
337
338        let selected_option_text = self
339            .selected_option()
340            .or_else(|| self.list_of_options().next())
341            .map(|option| option.displayed_label())
342            .unwrap_or_default();
343
344        // Replace newlines with whitespace, then collapse and trim whitespace
345        let displayed_text = itertools::join(selected_option_text.split_whitespace(), " ");
346
347        shadow_tree
348            .selected_option
349            .upcast::<CharacterData>()
350            .SetData(displayed_text.trim().into());
351    }
352
353    pub(crate) fn selected_option(&self) -> Option<DomRoot<HTMLOptionElement>> {
354        self.list_of_options()
355            .find(|opt_elem| opt_elem.Selected())
356            .or_else(|| self.list_of_options().next())
357    }
358
359    pub(crate) fn show_menu(&self) -> Option<usize> {
360        let (ipc_sender, ipc_receiver) =
361            generic_channel::channel().expect("Failed to create IPC channel!");
362
363        // Collect list of optgroups and options
364        let mut index = 0;
365        let mut embedder_option_from_option = |option: &HTMLOptionElement| {
366            let embedder_option = SelectElementOption {
367                id: index,
368                label: option.displayed_label().into(),
369                is_disabled: option.Disabled(),
370            };
371            index += 1;
372            embedder_option
373        };
374        let options = self
375            .upcast::<Node>()
376            .children()
377            .flat_map(|child| {
378                if let Some(option) = child.downcast::<HTMLOptionElement>() {
379                    return Some(embedder_option_from_option(option).into());
380                }
381
382                if let Some(optgroup) = child.downcast::<HTMLOptGroupElement>() {
383                    let options = optgroup
384                        .upcast::<Node>()
385                        .children()
386                        .flat_map(DomRoot::downcast::<HTMLOptionElement>)
387                        .map(|option| embedder_option_from_option(&option))
388                        .collect();
389                    let label = optgroup.Label().into();
390
391                    return Some(SelectElementOptionOrOptgroup::Optgroup { label, options });
392                }
393
394                None
395            })
396            .collect();
397
398        let rect = self.upcast::<Node>().border_box().unwrap_or_default();
399        let rect = Rect::new(
400            Point2D::new(rect.origin.x.to_px(), rect.origin.y.to_px()),
401            Size2D::new(rect.size.width.to_px(), rect.size.height.to_px()),
402        );
403
404        let selected_index = self.list_of_options().position(|option| option.Selected());
405
406        let document = self.owner_document();
407        document.send_to_embedder(EmbedderMsg::ShowFormControl(
408            document.webview_id(),
409            DeviceIntRect::from_untyped(&rect.to_box2d()),
410            EmbedderFormControl::SelectElement(options, selected_index, ipc_sender),
411        ));
412
413        let Ok(response) = ipc_receiver.recv() else {
414            log::error!("Failed to receive response");
415            return None;
416        };
417
418        response
419    }
420
421    /// <https://html.spec.whatwg.org/multipage/#send-select-update-notifications>
422    fn send_update_notifications(&self) {
423        // > When the user agent is to send select update notifications, queue an element task on the
424        // > user interaction task source given the select element to run these steps:
425        let this = Trusted::new(self);
426        self.owner_global()
427            .task_manager()
428            .user_interaction_task_source()
429            .queue(task!(send_select_update_notification: move || {
430                let this = this.root();
431
432                // TODO: Step 1. Set the select element's user validity to true.
433
434                // Step 2. Fire an event named input at the select element, with the bubbles and composed
435                // attributes initialized to true.
436                this.upcast::<EventTarget>()
437                    .fire_event_with_params(
438                        atom!("input"),
439                        EventBubbles::Bubbles,
440                        EventCancelable::NotCancelable,
441                        EventComposed::Composed,
442                        CanGc::note(),
443                    );
444
445                // Step 3. Fire an event named change at the select element, with the bubbles attribute initialized
446                // to true.
447                this.upcast::<EventTarget>()
448                    .fire_bubbling_event(atom!("change"), CanGc::note());
449            }));
450    }
451}
452
453impl HTMLSelectElementMethods<crate::DomTypeHolder> for HTMLSelectElement {
454    /// <https://html.spec.whatwg.org/multipage/#dom-select-add>
455    fn Add(
456        &self,
457        element: HTMLOptionElementOrHTMLOptGroupElement,
458        before: Option<HTMLElementOrLong>,
459    ) -> ErrorResult {
460        self.Options().Add(element, before)
461    }
462
463    // https://html.spec.whatwg.org/multipage/#dom-fe-disabled
464    make_bool_getter!(Disabled, "disabled");
465
466    // https://html.spec.whatwg.org/multipage/#dom-fe-disabled
467    make_bool_setter!(SetDisabled, "disabled");
468
469    /// <https://html.spec.whatwg.org/multipage/#dom-fae-form>
470    fn GetForm(&self) -> Option<DomRoot<HTMLFormElement>> {
471        self.form_owner()
472    }
473
474    // https://html.spec.whatwg.org/multipage/#dom-select-multiple
475    make_bool_getter!(Multiple, "multiple");
476
477    // https://html.spec.whatwg.org/multipage/#dom-select-multiple
478    make_bool_setter!(SetMultiple, "multiple");
479
480    // https://html.spec.whatwg.org/multipage/#dom-fe-name
481    make_getter!(Name, "name");
482
483    // https://html.spec.whatwg.org/multipage/#dom-fe-name
484    make_atomic_setter!(SetName, "name");
485
486    // https://html.spec.whatwg.org/multipage/#dom-select-required
487    make_bool_getter!(Required, "required");
488
489    // https://html.spec.whatwg.org/multipage/#dom-select-required
490    make_bool_setter!(SetRequired, "required");
491
492    // https://html.spec.whatwg.org/multipage/#dom-select-size
493    make_uint_getter!(Size, "size", DEFAULT_SELECT_SIZE);
494
495    // https://html.spec.whatwg.org/multipage/#dom-select-size
496    make_uint_setter!(SetSize, "size", DEFAULT_SELECT_SIZE);
497
498    /// <https://html.spec.whatwg.org/multipage/#dom-select-type>
499    fn Type(&self) -> DOMString {
500        DOMString::from(if self.Multiple() {
501            "select-multiple"
502        } else {
503            "select-one"
504        })
505    }
506
507    // https://html.spec.whatwg.org/multipage/#dom-lfe-labels
508    make_labels_getter!(Labels, labels_node_list);
509
510    /// <https://html.spec.whatwg.org/multipage/#dom-select-options>
511    fn Options(&self) -> DomRoot<HTMLOptionsCollection> {
512        self.options.or_init(|| {
513            let window = self.owner_window();
514            HTMLOptionsCollection::new(&window, self, Box::new(OptionsFilter), CanGc::note())
515        })
516    }
517
518    /// <https://html.spec.whatwg.org/multipage/#dom-select-length>
519    fn Length(&self) -> u32 {
520        self.Options().Length()
521    }
522
523    /// <https://html.spec.whatwg.org/multipage/#dom-select-length>
524    fn SetLength(&self, length: u32, can_gc: CanGc) {
525        self.Options().SetLength(length, can_gc)
526    }
527
528    /// <https://html.spec.whatwg.org/multipage/#dom-select-item>
529    fn Item(&self, index: u32) -> Option<DomRoot<Element>> {
530        self.Options().upcast().Item(index)
531    }
532
533    /// <https://html.spec.whatwg.org/multipage/#dom-select-item>
534    fn IndexedGetter(&self, index: u32) -> Option<DomRoot<Element>> {
535        self.Options().IndexedGetter(index)
536    }
537
538    /// <https://html.spec.whatwg.org/multipage/#dom-select-setter>
539    fn IndexedSetter(
540        &self,
541        index: u32,
542        value: Option<&HTMLOptionElement>,
543        can_gc: CanGc,
544    ) -> ErrorResult {
545        self.Options().IndexedSetter(index, value, can_gc)
546    }
547
548    /// <https://html.spec.whatwg.org/multipage/#dom-select-nameditem>
549    fn NamedItem(&self, name: DOMString) -> Option<DomRoot<HTMLOptionElement>> {
550        self.Options()
551            .NamedGetter(name)
552            .and_then(DomRoot::downcast::<HTMLOptionElement>)
553    }
554
555    /// <https://html.spec.whatwg.org/multipage/#dom-select-remove>
556    fn Remove_(&self, index: i32) {
557        self.Options().Remove(index)
558    }
559
560    /// <https://html.spec.whatwg.org/multipage/#dom-select-remove>
561    fn Remove(&self) {
562        self.upcast::<Element>().Remove(CanGc::note())
563    }
564
565    /// <https://html.spec.whatwg.org/multipage/#dom-select-value>
566    fn Value(&self) -> DOMString {
567        self.list_of_options()
568            .find(|opt_elem| opt_elem.Selected())
569            .map(|opt_elem| opt_elem.Value())
570            .unwrap_or_default()
571    }
572
573    /// <https://html.spec.whatwg.org/multipage/#dom-select-value>
574    fn SetValue(&self, value: DOMString) {
575        let mut opt_iter = self.list_of_options();
576        // Reset until we find an <option> with a matching value
577        for opt in opt_iter.by_ref() {
578            if opt.Value() == value {
579                opt.set_selectedness(true);
580                opt.set_dirtiness(true);
581                break;
582            }
583            opt.set_selectedness(false);
584        }
585        // Reset remaining <option> elements
586        for opt in opt_iter {
587            opt.set_selectedness(false);
588        }
589
590        self.validity_state()
591            .perform_validation_and_update(ValidationFlags::VALUE_MISSING, CanGc::note());
592    }
593
594    /// <https://html.spec.whatwg.org/multipage/#dom-select-selectedindex>
595    fn SelectedIndex(&self) -> i32 {
596        self.list_of_options()
597            .enumerate()
598            .filter(|(_, opt_elem)| opt_elem.Selected())
599            .map(|(i, _)| i as i32)
600            .next()
601            .unwrap_or(-1)
602    }
603
604    /// <https://html.spec.whatwg.org/multipage/#dom-select-selectedindex>
605    fn SetSelectedIndex(&self, index: i32, can_gc: CanGc) {
606        let mut selection_did_change = false;
607
608        let mut opt_iter = self.list_of_options();
609        for opt in opt_iter.by_ref().take(index as usize) {
610            selection_did_change |= opt.Selected();
611            opt.set_selectedness(false);
612        }
613        if let Some(selected_option) = opt_iter.next() {
614            selection_did_change |= !selected_option.Selected();
615            selected_option.set_selectedness(true);
616            selected_option.set_dirtiness(true);
617
618            // Reset remaining <option> elements
619            for opt in opt_iter {
620                selection_did_change |= opt.Selected();
621                opt.set_selectedness(false);
622            }
623        }
624
625        if selection_did_change {
626            self.update_shadow_tree(can_gc);
627        }
628    }
629
630    /// <https://html.spec.whatwg.org/multipage/#dom-cva-willvalidate>
631    fn WillValidate(&self) -> bool {
632        self.is_instance_validatable()
633    }
634
635    /// <https://html.spec.whatwg.org/multipage/#dom-cva-validity>
636    fn Validity(&self) -> DomRoot<ValidityState> {
637        self.validity_state()
638    }
639
640    /// <https://html.spec.whatwg.org/multipage/#dom-cva-checkvalidity>
641    fn CheckValidity(&self, can_gc: CanGc) -> bool {
642        self.check_validity(can_gc)
643    }
644
645    /// <https://html.spec.whatwg.org/multipage/#dom-cva-reportvalidity>
646    fn ReportValidity(&self, can_gc: CanGc) -> bool {
647        self.report_validity(can_gc)
648    }
649
650    /// <https://html.spec.whatwg.org/multipage/#dom-cva-validationmessage>
651    fn ValidationMessage(&self) -> DOMString {
652        self.validation_message()
653    }
654
655    /// <https://html.spec.whatwg.org/multipage/#dom-cva-setcustomvalidity>
656    fn SetCustomValidity(&self, error: DOMString) {
657        self.validity_state().set_custom_error_message(error);
658    }
659}
660
661impl VirtualMethods for HTMLSelectElement {
662    fn super_type(&self) -> Option<&dyn VirtualMethods> {
663        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
664    }
665
666    fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
667        self.super_type()
668            .unwrap()
669            .attribute_mutated(attr, mutation, can_gc);
670        match *attr.local_name() {
671            local_name!("required") => {
672                self.validity_state()
673                    .perform_validation_and_update(ValidationFlags::VALUE_MISSING, can_gc);
674            },
675            local_name!("disabled") => {
676                let el = self.upcast::<Element>();
677                match mutation {
678                    AttributeMutation::Set(_) => {
679                        el.set_disabled_state(true);
680                        el.set_enabled_state(false);
681                    },
682                    AttributeMutation::Removed => {
683                        el.set_disabled_state(false);
684                        el.set_enabled_state(true);
685                        el.check_ancestors_disabled_state_for_form_control();
686                    },
687                }
688
689                self.validity_state()
690                    .perform_validation_and_update(ValidationFlags::VALUE_MISSING, can_gc);
691            },
692            local_name!("form") => {
693                self.form_attribute_mutated(mutation, can_gc);
694            },
695            _ => {},
696        }
697    }
698
699    fn bind_to_tree(&self, context: &BindContext, can_gc: CanGc) {
700        if let Some(s) = self.super_type() {
701            s.bind_to_tree(context, can_gc);
702        }
703
704        self.upcast::<Element>()
705            .check_ancestors_disabled_state_for_form_control();
706    }
707
708    fn unbind_from_tree(&self, context: &UnbindContext, can_gc: CanGc) {
709        self.super_type().unwrap().unbind_from_tree(context, can_gc);
710
711        let node = self.upcast::<Node>();
712        let el = self.upcast::<Element>();
713        if node
714            .ancestors()
715            .any(|ancestor| ancestor.is::<HTMLFieldSetElement>())
716        {
717            el.check_ancestors_disabled_state_for_form_control();
718        } else {
719            el.check_disabled_attribute();
720        }
721    }
722
723    fn children_changed(&self, mutation: &ChildrenMutation) {
724        if let Some(s) = self.super_type() {
725            s.children_changed(mutation);
726        }
727
728        self.update_shadow_tree(CanGc::note());
729    }
730
731    fn parse_plain_attribute(&self, local_name: &LocalName, value: DOMString) -> AttrValue {
732        match *local_name {
733            local_name!("size") => AttrValue::from_u32(value.into(), DEFAULT_SELECT_SIZE),
734            _ => self
735                .super_type()
736                .unwrap()
737                .parse_plain_attribute(local_name, value),
738        }
739    }
740}
741
742impl FormControl for HTMLSelectElement {
743    fn form_owner(&self) -> Option<DomRoot<HTMLFormElement>> {
744        self.form_owner.get()
745    }
746
747    fn set_form_owner(&self, form: Option<&HTMLFormElement>) {
748        self.form_owner.set(form);
749    }
750
751    fn to_element(&self) -> &Element {
752        self.upcast::<Element>()
753    }
754}
755
756impl Validatable for HTMLSelectElement {
757    fn as_element(&self) -> &Element {
758        self.upcast()
759    }
760
761    fn validity_state(&self) -> DomRoot<ValidityState> {
762        self.validity_state
763            .or_init(|| ValidityState::new(&self.owner_window(), self.upcast(), CanGc::note()))
764    }
765
766    fn is_instance_validatable(&self) -> bool {
767        // https://html.spec.whatwg.org/multipage/#enabling-and-disabling-form-controls%3A-the-disabled-attribute%3Abarred-from-constraint-validation
768        // https://html.spec.whatwg.org/multipage/#the-datalist-element%3Abarred-from-constraint-validation
769        !self.upcast::<Element>().disabled_state() && !is_barred_by_datalist_ancestor(self.upcast())
770    }
771
772    fn perform_validation(
773        &self,
774        validate_flags: ValidationFlags,
775        _can_gc: CanGc,
776    ) -> ValidationFlags {
777        let mut failed_flags = ValidationFlags::empty();
778
779        // https://html.spec.whatwg.org/multipage/#suffering-from-being-missing
780        // https://html.spec.whatwg.org/multipage/#the-select-element%3Asuffering-from-being-missing
781        if validate_flags.contains(ValidationFlags::VALUE_MISSING) && self.Required() {
782            let placeholder = self.get_placeholder_label_option();
783            let is_value_missing = !self
784                .list_of_options()
785                .any(|e| e.Selected() && placeholder != Some(e));
786            failed_flags.set(ValidationFlags::VALUE_MISSING, is_value_missing);
787        }
788
789        failed_flags
790    }
791}
792
793impl Activatable for HTMLSelectElement {
794    fn as_element(&self) -> &Element {
795        self.upcast()
796    }
797
798    fn is_instance_activatable(&self) -> bool {
799        true
800    }
801
802    fn activation_behavior(&self, _event: &Event, _target: &EventTarget, can_gc: CanGc) {
803        let Some(selected_value) = self.show_menu() else {
804            // The user did not select a value
805            return;
806        };
807
808        self.SetSelectedIndex(selected_value as i32, can_gc);
809        self.send_update_notifications();
810    }
811}
812
813enum Choice3<I, J, K> {
814    First(I),
815    Second(J),
816    Third(K),
817}
818
819impl<I, J, K, T> Iterator for Choice3<I, J, K>
820where
821    I: Iterator<Item = T>,
822    J: Iterator<Item = T>,
823    K: Iterator<Item = T>,
824{
825    type Item = T;
826
827    fn next(&mut self) -> Option<T> {
828        match *self {
829            Choice3::First(ref mut i) => i.next(),
830            Choice3::Second(ref mut j) => j.next(),
831            Choice3::Third(ref mut k) => k.next(),
832        }
833    }
834
835    fn size_hint(&self) -> (usize, Option<usize>) {
836        match *self {
837            Choice3::First(ref i) => i.size_hint(),
838            Choice3::Second(ref j) => j.size_hint(),
839            Choice3::Third(ref k) => k.size_hint(),
840        }
841    }
842}