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, NoGC};
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, cx: &JSContext) -> u32 {
64        self.collection.Length(cx)
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
77                .collection
78                .elements_iter(cx.no_gc())
79                .filter_map(|elem| {
80                    if elem.get_name().is_some_and(|n| n == name) ||
81                        elem.get_id().is_some_and(|i| i == name)
82                    {
83                        Some(elem)
84                    } else {
85                        None
86                    }
87                })
88                .peekable();
89
90            // This if is more complicated because of lifetimes.
91            // Step 2
92            if filter_map.peek().is_none() {
93                return None;
94            } else {
95                let elem = filter_map.next().unwrap();
96                if filter_map.peek().is_none() {
97                    return Some(RadioNodeListOrElement::Element(elem.as_rooted()));
98                }
99            }
100        }
101        // Step 4-5
102        let global = self.global();
103        let window = global.as_window();
104        // There is only one way to get an HTMLCollection,
105        // specifically HTMLFormElement::Elements(),
106        // and the collection filter excludes image inputs.
107        Some(RadioNodeListOrElement::RadioNodeList(
108            RadioNodeList::new_controls_except_image_inputs(cx, window, &self.form, &name),
109        ))
110    }
111
112    /// <https://html.spec.whatwg.org/multipage/#dom-htmlformcontrolscollection-nameditem>
113    fn NamedGetter(&self, cx: &mut JSContext, name: DOMString) -> Option<RadioNodeListOrElement> {
114        self.NamedItem(cx, name)
115    }
116
117    /// <https://html.spec.whatwg.org/multipage/#the-htmlformcontrolscollection-interface:supported-property-names>
118    fn SupportedPropertyNames(&self, no_gc: &NoGC) -> Vec<DOMString> {
119        self.collection.SupportedPropertyNames(no_gc)
120    }
121
122    // FIXME: This shouldn't need to be implemented here since HTMLCollection (the parent of
123    // HTMLFormControlsCollection) implements IndexedGetter.
124    // https://github.com/servo/servo/issues/5875
125    //
126    /// <https://dom.spec.whatwg.org/#dom-htmlcollection-item>
127    fn IndexedGetter(&self, cx: &JSContext, index: u32) -> Option<DomRoot<Element>> {
128        self.collection.IndexedGetter(cx, index)
129    }
130}