script/dom/html/
htmlfieldsetelement.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;
6
7use dom_struct::dom_struct;
8use html5ever::{LocalName, Prefix, local_name};
9use js::context::JSContext;
10use js::rust::HandleObject;
11use stylo_dom::ElementState;
12
13use crate::dom::attr::Attr;
14use crate::dom::bindings::codegen::Bindings::HTMLFieldSetElementBinding::HTMLFieldSetElementMethods;
15use crate::dom::bindings::inheritance::{Castable, ElementTypeId, HTMLElementTypeId, NodeTypeId};
16use crate::dom::bindings::root::{DomRoot, MutNullableDom};
17use crate::dom::bindings::str::DOMString;
18use crate::dom::customelementregistry::CallbackReaction;
19use crate::dom::document::Document;
20use crate::dom::element::{AttributeMutation, Element};
21use crate::dom::html::htmlcollection::HTMLCollection;
22use crate::dom::html::htmlelement::HTMLElement;
23use crate::dom::html::htmlformelement::{FormControl, HTMLFormElement};
24use crate::dom::html::htmllegendelement::HTMLLegendElement;
25use crate::dom::node::{Node, NodeTraits, ShadowIncluding};
26use crate::dom::validation::Validatable;
27use crate::dom::validitystate::ValidityState;
28use crate::dom::virtualmethods::VirtualMethods;
29use crate::script_runtime::CanGc;
30use crate::script_thread::ScriptThread;
31
32#[dom_struct]
33pub(crate) struct HTMLFieldSetElement {
34    htmlelement: HTMLElement,
35    form_owner: MutNullableDom<HTMLFormElement>,
36    validity_state: MutNullableDom<ValidityState>,
37}
38
39impl HTMLFieldSetElement {
40    fn new_inherited(
41        local_name: LocalName,
42        prefix: Option<Prefix>,
43        document: &Document,
44    ) -> HTMLFieldSetElement {
45        HTMLFieldSetElement {
46            htmlelement: HTMLElement::new_inherited_with_state(
47                ElementState::ENABLED | ElementState::VALID,
48                local_name,
49                prefix,
50                document,
51            ),
52            form_owner: Default::default(),
53            validity_state: Default::default(),
54        }
55    }
56
57    pub(crate) fn new(
58        cx: &mut js::context::JSContext,
59        local_name: LocalName,
60        prefix: Option<Prefix>,
61        document: &Document,
62        proto: Option<HandleObject>,
63    ) -> DomRoot<HTMLFieldSetElement> {
64        Node::reflect_node_with_proto(
65            cx,
66            Box::new(HTMLFieldSetElement::new_inherited(
67                local_name, prefix, document,
68            )),
69            document,
70            proto,
71        )
72    }
73
74    pub(crate) fn update_validity(&self, can_gc: CanGc) {
75        let has_invalid_child = self
76            .upcast::<Node>()
77            .traverse_preorder(ShadowIncluding::No)
78            .flat_map(DomRoot::downcast::<Element>)
79            .any(|element| element.is_invalid(false, can_gc));
80
81        self.upcast::<Element>()
82            .set_state(ElementState::VALID, !has_invalid_child);
83        self.upcast::<Element>()
84            .set_state(ElementState::INVALID, has_invalid_child);
85    }
86}
87
88impl HTMLFieldSetElementMethods<crate::DomTypeHolder> for HTMLFieldSetElement {
89    /// <https://html.spec.whatwg.org/multipage/#dom-fieldset-elements>
90    fn Elements(&self, can_gc: CanGc) -> DomRoot<HTMLCollection> {
91        HTMLCollection::new_with_filter_fn(
92            &self.owner_window(),
93            self.upcast(),
94            |element, _| {
95                element
96                    .downcast::<HTMLElement>()
97                    .is_some_and(HTMLElement::is_listed_element)
98            },
99            can_gc,
100        )
101    }
102
103    // https://html.spec.whatwg.org/multipage/#dom-fieldset-disabled
104    make_bool_getter!(Disabled, "disabled");
105
106    // https://html.spec.whatwg.org/multipage/#dom-fieldset-disabled
107    make_bool_setter!(SetDisabled, "disabled");
108
109    // https://html.spec.whatwg.org/multipage/#dom-fe-name
110    make_atomic_setter!(SetName, "name");
111
112    // https://html.spec.whatwg.org/multipage/#dom-fe-name
113    make_getter!(Name, "name");
114
115    /// <https://html.spec.whatwg.org/multipage/#dom-fae-form>
116    fn GetForm(&self) -> Option<DomRoot<HTMLFormElement>> {
117        self.form_owner()
118    }
119
120    /// <https://html.spec.whatwg.org/multipage/#dom-cva-willvalidate>
121    fn WillValidate(&self) -> bool {
122        self.is_instance_validatable()
123    }
124
125    /// <https://html.spec.whatwg.org/multipage/#dom-cva-validity>
126    fn Validity(&self, can_gc: CanGc) -> DomRoot<ValidityState> {
127        self.validity_state(can_gc)
128    }
129
130    /// <https://html.spec.whatwg.org/multipage/#dom-cva-checkvalidity>
131    fn CheckValidity(&self, cx: &mut JSContext) -> bool {
132        self.check_validity(cx)
133    }
134
135    /// <https://html.spec.whatwg.org/multipage/#dom-cva-reportvalidity>
136    fn ReportValidity(&self, cx: &mut JSContext) -> bool {
137        self.report_validity(cx)
138    }
139
140    /// <https://html.spec.whatwg.org/multipage/#dom-cva-validationmessage>
141    fn ValidationMessage(&self) -> DOMString {
142        self.validation_message()
143    }
144
145    /// <https://html.spec.whatwg.org/multipage/#dom-cva-setcustomvalidity>
146    fn SetCustomValidity(&self, error: DOMString, can_gc: CanGc) {
147        self.validity_state(can_gc).set_custom_error_message(error);
148    }
149
150    /// <https://html.spec.whatwg.org/multipage/#dom-fieldset-type>
151    fn Type(&self) -> DOMString {
152        "fieldset".into()
153    }
154}
155
156impl VirtualMethods for HTMLFieldSetElement {
157    fn super_type(&self) -> Option<&dyn VirtualMethods> {
158        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
159    }
160
161    fn attribute_mutated(
162        &self,
163        cx: &mut js::context::JSContext,
164        attr: &Attr,
165        mutation: AttributeMutation,
166    ) {
167        self.super_type()
168            .unwrap()
169            .attribute_mutated(cx, attr, mutation);
170        match *attr.local_name() {
171            local_name!("disabled") => {
172                let disabled_state = match mutation {
173                    AttributeMutation::Set(None, _) => true,
174                    AttributeMutation::Set(Some(_), _) => {
175                        // Fieldset was already disabled before.
176                        return;
177                    },
178                    AttributeMutation::Removed => false,
179                };
180                let node = self.upcast::<Node>();
181                let element = self.upcast::<Element>();
182                element.set_disabled_state(disabled_state);
183                element.set_enabled_state(!disabled_state);
184                let mut found_legend = false;
185                let children = node.children().filter(|node| {
186                    if found_legend {
187                        true
188                    } else if node.is::<HTMLLegendElement>() {
189                        found_legend = true;
190                        false
191                    } else {
192                        true
193                    }
194                });
195                let fields = children.flat_map(|child| {
196                    child
197                        .traverse_preorder(ShadowIncluding::No)
198                        .filter(|descendant| match descendant.type_id() {
199                            NodeTypeId::Element(ElementTypeId::HTMLElement(
200                                HTMLElementTypeId::HTMLButtonElement |
201                                HTMLElementTypeId::HTMLInputElement |
202                                HTMLElementTypeId::HTMLSelectElement |
203                                HTMLElementTypeId::HTMLTextAreaElement,
204                            )) => true,
205                            NodeTypeId::Element(ElementTypeId::HTMLElement(
206                                HTMLElementTypeId::HTMLElement,
207                            )) => descendant
208                                .downcast::<HTMLElement>()
209                                .unwrap()
210                                .is_form_associated_custom_element(),
211                            _ => false,
212                        })
213                });
214                if disabled_state {
215                    for field in fields {
216                        let element = field.downcast::<Element>().unwrap();
217                        if element.enabled_state() {
218                            element.set_disabled_state(true);
219                            element.set_enabled_state(false);
220                            if element
221                                .downcast::<HTMLElement>()
222                                .is_some_and(|h| h.is_form_associated_custom_element())
223                            {
224                                ScriptThread::enqueue_callback_reaction(
225                                    element,
226                                    CallbackReaction::FormDisabled(true),
227                                    None,
228                                );
229                            }
230                        }
231                    }
232                } else {
233                    for field in fields {
234                        let element = field.downcast::<Element>().unwrap();
235                        if element.disabled_state() {
236                            element.check_disabled_attribute();
237                            element.check_ancestors_disabled_state_for_form_control();
238                            // Fire callback only if this has actually enabled the custom element
239                            if element.enabled_state() &&
240                                element
241                                    .downcast::<HTMLElement>()
242                                    .is_some_and(|h| h.is_form_associated_custom_element())
243                            {
244                                ScriptThread::enqueue_callback_reaction(
245                                    element,
246                                    CallbackReaction::FormDisabled(false),
247                                    None,
248                                );
249                            }
250                        }
251                    }
252                }
253            },
254            local_name!("form") => {
255                self.form_attribute_mutated(mutation, CanGc::from_cx(cx));
256            },
257            _ => {},
258        }
259    }
260}
261
262impl FormControl for HTMLFieldSetElement {
263    fn form_owner(&self) -> Option<DomRoot<HTMLFormElement>> {
264        self.form_owner.get()
265    }
266
267    fn set_form_owner(&self, form: Option<&HTMLFormElement>) {
268        self.form_owner.set(form);
269    }
270
271    fn to_element(&self) -> &Element {
272        self.upcast::<Element>()
273    }
274}
275
276impl Validatable for HTMLFieldSetElement {
277    fn as_element(&self) -> &Element {
278        self.upcast()
279    }
280
281    fn validity_state(&self, can_gc: CanGc) -> DomRoot<ValidityState> {
282        self.validity_state
283            .or_init(|| ValidityState::new(&self.owner_window(), self.upcast(), can_gc))
284    }
285
286    fn is_instance_validatable(&self) -> bool {
287        // fieldset is not a submittable element (https://html.spec.whatwg.org/multipage/#category-submit)
288        false
289    }
290}