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