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::context::JSContext;
8use js::rust::HandleObject;
9
10use crate::dom::bindings::codegen::Bindings::HTMLLegendElementBinding::HTMLLegendElementMethods;
11use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
12use crate::dom::bindings::inheritance::Castable;
13use crate::dom::bindings::root::{DomRoot, MutNullableDom};
14use crate::dom::document::Document;
15use crate::dom::element::Element;
16use crate::dom::html::htmlelement::HTMLElement;
17use crate::dom::html::htmlfieldsetelement::HTMLFieldSetElement;
18use crate::dom::html::htmlformelement::{FormControl, HTMLFormElement};
19use crate::dom::node::{BindContext, Node, UnbindContext};
20use crate::dom::virtualmethods::VirtualMethods;
21use crate::script_runtime::CanGc;
22
23#[dom_struct]
24pub(crate) struct HTMLLegendElement {
25    htmlelement: HTMLElement,
26    form_owner: MutNullableDom<HTMLFormElement>,
27}
28
29impl HTMLLegendElement {
30    fn new_inherited(
31        local_name: LocalName,
32        prefix: Option<Prefix>,
33        document: &Document,
34    ) -> HTMLLegendElement {
35        HTMLLegendElement {
36            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
37            form_owner: Default::default(),
38        }
39    }
40
41    pub(crate) fn new(
42        cx: &mut js::context::JSContext,
43        local_name: LocalName,
44        prefix: Option<Prefix>,
45        document: &Document,
46        proto: Option<HandleObject>,
47    ) -> DomRoot<HTMLLegendElement> {
48        Node::reflect_node_with_proto(
49            cx,
50            Box::new(HTMLLegendElement::new_inherited(
51                local_name, prefix, document,
52            )),
53            document,
54            proto,
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, cx: &mut JSContext, context: &BindContext) {
65        if let Some(s) = self.super_type() {
66            s.bind_to_tree(cx, context);
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}