Skip to main content

script/dom/html/
htmlformcontrolscollection.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 dom_struct::dom_struct;
6use js::context::JSContext;
7use script_bindings::reflector::reflect_dom_object_with_cx;
8use stylo_atoms::Atom;
9
10use crate::dom::bindings::codegen::Bindings::HTMLCollectionBinding::HTMLCollectionMethods;
11use crate::dom::bindings::codegen::Bindings::HTMLFormControlsCollectionBinding::HTMLFormControlsCollectionMethods;
12use crate::dom::bindings::codegen::Bindings::NodeBinding::{GetRootNodeOptions, NodeMethods};
13use crate::dom::bindings::codegen::UnionTypes::RadioNodeListOrElement;
14use crate::dom::bindings::inheritance::Castable;
15use crate::dom::bindings::reflector::DomGlobal;
16use crate::dom::bindings::root::{Dom, DomRoot};
17use crate::dom::bindings::str::DOMString;
18use crate::dom::element::Element;
19use crate::dom::html::htmlcollection::{CollectionFilter, HTMLCollection};
20use crate::dom::html::htmlformelement::HTMLFormElement;
21use crate::dom::node::Node;
22use crate::dom::radionodelist::RadioNodeList;
23use crate::dom::window::Window;
24
25#[dom_struct]
26pub(crate) struct HTMLFormControlsCollection {
27    collection: HTMLCollection,
28    form: Dom<HTMLFormElement>,
29}
30
31impl HTMLFormControlsCollection {
32    fn new_inherited(
33        form: &HTMLFormElement,
34        filter: Box<dyn CollectionFilter + 'static>,
35    ) -> HTMLFormControlsCollection {
36        let root_of_form = form
37            .upcast::<Node>()
38            .GetRootNode(&GetRootNodeOptions::empty());
39        HTMLFormControlsCollection {
40            collection: HTMLCollection::new_inherited(&root_of_form, filter),
41            form: Dom::from_ref(form),
42        }
43    }
44
45    pub(crate) fn new(
46        cx: &mut JSContext,
47        window: &Window,
48        form: &HTMLFormElement,
49        filter: Box<dyn CollectionFilter + 'static>,
50    ) -> DomRoot<HTMLFormControlsCollection> {
51        reflect_dom_object_with_cx(
52            Box::new(HTMLFormControlsCollection::new_inherited(form, filter)),
53            window,
54            cx,
55        )
56    }
57}
58
59impl HTMLFormControlsCollectionMethods<crate::DomTypeHolder> for HTMLFormControlsCollection {
60    // FIXME: This shouldn't need to be implemented here since HTMLCollection (the parent of
61    // HTMLFormControlsCollection) implements Length
62    /// <https://dom.spec.whatwg.org/#dom-htmlcollection-length>
63    fn Length(&self) -> u32 {
64        self.collection.Length()
65    }
66
67    /// <https://html.spec.whatwg.org/multipage/#dom-htmlformcontrolscollection-nameditem>
68    fn NamedItem(&self, cx: &mut JSContext, name: DOMString) -> Option<RadioNodeListOrElement> {
69        // Step 1
70        if name.is_empty() {
71            return None;
72        }
73
74        let name = Atom::from(name);
75
76        let mut filter_map = self.collection.elements_iter().filter_map(|elem| {
77            if elem.get_name().is_some_and(|n| n == name) ||
78                elem.get_id().is_some_and(|i| i == name)
79            {
80                Some(elem)
81            } else {
82                None
83            }
84        });
85
86        if let Some(elem) = filter_map.next() {
87            let mut peekable = filter_map.peekable();
88            // Step 2
89            if peekable.peek().is_none() {
90                Some(RadioNodeListOrElement::Element(elem))
91            } else {
92                // Step 4-5
93                let global = self.global();
94                let window = global.as_window();
95                // There is only one way to get an HTMLCollection,
96                // specifically HTMLFormElement::Elements(),
97                // and the collection filter excludes image inputs.
98                Some(RadioNodeListOrElement::RadioNodeList(
99                    RadioNodeList::new_controls_except_image_inputs(cx, window, &self.form, &name),
100                ))
101            }
102        // Step 3
103        } else {
104            None
105        }
106    }
107
108    /// <https://html.spec.whatwg.org/multipage/#dom-htmlformcontrolscollection-nameditem>
109    fn NamedGetter(&self, cx: &mut JSContext, name: DOMString) -> Option<RadioNodeListOrElement> {
110        self.NamedItem(cx, name)
111    }
112
113    /// <https://html.spec.whatwg.org/multipage/#the-htmlformcontrolscollection-interface:supported-property-names>
114    fn SupportedPropertyNames(&self) -> Vec<DOMString> {
115        self.collection.SupportedPropertyNames()
116    }
117
118    // FIXME: This shouldn't need to be implemented here since HTMLCollection (the parent of
119    // HTMLFormControlsCollection) implements IndexedGetter.
120    // https://github.com/servo/servo/issues/5875
121    //
122    /// <https://dom.spec.whatwg.org/#dom-htmlcollection-item>
123    fn IndexedGetter(&self, index: u32) -> Option<DomRoot<Element>> {
124        self.collection.IndexedGetter(index)
125    }
126}