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    pub(crate) fn new(
41        local_name: LocalName,
42        prefix: Option<Prefix>,
43        document: &Document,
44        proto: Option<HandleObject>,
45        can_gc: CanGc,
46    ) -> DomRoot<HTMLLegendElement> {
47        Node::reflect_node_with_proto(
48            Box::new(HTMLLegendElement::new_inherited(
49                local_name, prefix, document,
50            )),
51            document,
52            proto,
53            can_gc,
54        )
55    }
56}
57
58impl VirtualMethods for HTMLLegendElement {
59    fn super_type(&self) -> Option<&dyn VirtualMethods> {
60        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
61    }
62
63    fn bind_to_tree(&self, context: &BindContext, can_gc: CanGc) {
64        if let Some(s) = self.super_type() {
65            s.bind_to_tree(context, can_gc);
66        }
67
68        self.upcast::<Element>()
69            .check_ancestors_disabled_state_for_form_control();
70    }
71
72    fn unbind_from_tree(&self, context: &UnbindContext, can_gc: CanGc) {
73        self.super_type().unwrap().unbind_from_tree(context, can_gc);
74
75        let node = self.upcast::<Node>();
76        let el = self.upcast::<Element>();
77        if node
78            .ancestors()
79            .any(|ancestor| ancestor.is::<HTMLFieldSetElement>())
80        {
81            el.check_ancestors_disabled_state_for_form_control();
82        } else {
83            el.check_disabled_attribute();
84        }
85    }
86}
87
88impl HTMLLegendElementMethods<crate::DomTypeHolder> for HTMLLegendElement {
89    /// <https://html.spec.whatwg.org/multipage/#dom-legend-form>
90    fn GetForm(&self) -> Option<DomRoot<HTMLFormElement>> {
91        let parent = self.upcast::<Node>().GetParentElement()?;
92        if parent.is::<HTMLFieldSetElement>() {
93            return self.form_owner();
94        }
95        None
96    }
97}
98
99impl FormControl for HTMLLegendElement {
100    fn form_owner(&self) -> Option<DomRoot<HTMLFormElement>> {
101        self.form_owner.get()
102    }
103
104    fn set_form_owner(&self, form: Option<&HTMLFormElement>) {
105        self.form_owner.set(form);
106    }
107
108    fn to_element(&self) -> &Element {
109        self.upcast::<Element>()
110    }
111}