script/dom/html/
htmllegendelement.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 html5ever::{LocalName, Prefix};
7use js::rust::HandleObject;
8
9use crate::dom::bindings::codegen::Bindings::HTMLLegendElementBinding::HTMLLegendElementMethods;
10use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
11use crate::dom::bindings::inheritance::Castable;
12use crate::dom::bindings::root::{DomRoot, MutNullableDom};
13use crate::dom::document::Document;
14use crate::dom::element::Element;
15use crate::dom::html::htmlelement::HTMLElement;
16use crate::dom::html::htmlfieldsetelement::HTMLFieldSetElement;
17use crate::dom::html::htmlformelement::{FormControl, HTMLFormElement};
18use crate::dom::node::{BindContext, Node, UnbindContext};
19use crate::dom::virtualmethods::VirtualMethods;
20use crate::script_runtime::CanGc;
21
22#[dom_struct]
23pub(crate) struct HTMLLegendElement {
24    htmlelement: HTMLElement,
25    form_owner: MutNullableDom<HTMLFormElement>,
26}
27
28impl HTMLLegendElement {
29    fn new_inherited(
30        local_name: LocalName,
31        prefix: Option<Prefix>,
32        document: &Document,
33    ) -> HTMLLegendElement {
34        HTMLLegendElement {
35            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
36            form_owner: Default::default(),
37        }
38    }
39
40    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
41    pub(crate) fn new(
42        local_name: LocalName,
43        prefix: Option<Prefix>,
44        document: &Document,
45        proto: Option<HandleObject>,
46        can_gc: CanGc,
47    ) -> DomRoot<HTMLLegendElement> {
48        Node::reflect_node_with_proto(
49            Box::new(HTMLLegendElement::new_inherited(
50                local_name, prefix, document,
51            )),
52            document,
53            proto,
54            can_gc,
55        )
56    }
57}
58
59impl VirtualMethods for HTMLLegendElement {
60    fn super_type(&self) -> Option<&dyn VirtualMethods> {
61        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
62    }
63
64    fn bind_to_tree(&self, context: &BindContext, can_gc: CanGc) {
65        if let Some(s) = self.super_type() {
66            s.bind_to_tree(context, can_gc);
67        }
68
69        self.upcast::<Element>()
70            .check_ancestors_disabled_state_for_form_control();
71    }
72
73    fn unbind_from_tree(&self, context: &UnbindContext, can_gc: CanGc) {
74        self.super_type().unwrap().unbind_from_tree(context, can_gc);
75
76        let node = self.upcast::<Node>();
77        let el = self.upcast::<Element>();
78        if node
79            .ancestors()
80            .any(|ancestor| ancestor.is::<HTMLFieldSetElement>())
81        {
82            el.check_ancestors_disabled_state_for_form_control();
83        } else {
84            el.check_disabled_attribute();
85        }
86    }
87}
88
89impl HTMLLegendElementMethods<crate::DomTypeHolder> for HTMLLegendElement {
90    // https://html.spec.whatwg.org/multipage/#dom-legend-form
91    fn GetForm(&self) -> Option<DomRoot<HTMLFormElement>> {
92        let parent = self.upcast::<Node>().GetParentElement()?;
93        if parent.is::<HTMLFieldSetElement>() {
94            return self.form_owner();
95        }
96        None
97    }
98}
99
100impl FormControl for HTMLLegendElement {
101    fn form_owner(&self) -> Option<DomRoot<HTMLFormElement>> {
102        self.form_owner.get()
103    }
104
105    fn set_form_owner(&self, form: Option<&HTMLFormElement>) {
106        self.form_owner.set(form);
107    }
108
109    fn to_element(&self) -> &Element {
110        self.upcast::<Element>()
111    }
112}