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