script/dom/html/
htmllabelelement.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, local_name};
7use js::rust::HandleObject;
8use style::attr::AttrValue;
9
10use crate::dom::activation::Activatable;
11use crate::dom::attr::Attr;
12use crate::dom::bindings::codegen::Bindings::AttrBinding::AttrMethods;
13use crate::dom::bindings::codegen::Bindings::ElementBinding::ElementMethods;
14use crate::dom::bindings::codegen::Bindings::HTMLElementBinding::HTMLElementMethods;
15use crate::dom::bindings::codegen::Bindings::HTMLLabelElementBinding::HTMLLabelElementMethods;
16use crate::dom::bindings::codegen::Bindings::NodeBinding::{GetRootNodeOptions, NodeMethods};
17use crate::dom::bindings::inheritance::Castable;
18use crate::dom::bindings::root::DomRoot;
19use crate::dom::bindings::str::DOMString;
20use crate::dom::document::Document;
21use crate::dom::element::{AttributeMutation, Element};
22use crate::dom::event::Event;
23use crate::dom::eventtarget::EventTarget;
24use crate::dom::html::htmlelement::HTMLElement;
25use crate::dom::html::htmlformelement::{FormControl, FormControlElementHelpers, HTMLFormElement};
26use crate::dom::node::{Node, ShadowIncluding};
27use crate::dom::virtualmethods::VirtualMethods;
28use crate::script_runtime::CanGc;
29
30#[dom_struct]
31pub(crate) struct HTMLLabelElement {
32    htmlelement: HTMLElement,
33}
34
35impl HTMLLabelElement {
36    fn new_inherited(
37        local_name: LocalName,
38        prefix: Option<Prefix>,
39        document: &Document,
40    ) -> HTMLLabelElement {
41        HTMLLabelElement {
42            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
43        }
44    }
45
46    pub(crate) fn new(
47        cx: &mut js::context::JSContext,
48        local_name: LocalName,
49        prefix: Option<Prefix>,
50        document: &Document,
51        proto: Option<HandleObject>,
52    ) -> DomRoot<HTMLLabelElement> {
53        Node::reflect_node_with_proto(
54            cx,
55            Box::new(HTMLLabelElement::new_inherited(
56                local_name, prefix, document,
57            )),
58            document,
59            proto,
60        )
61    }
62}
63
64impl Activatable for HTMLLabelElement {
65    fn as_element(&self) -> &Element {
66        self.upcast::<Element>()
67    }
68
69    fn is_instance_activatable(&self) -> bool {
70        true
71    }
72
73    // https://html.spec.whatwg.org/multipage/#the-label-element:activation_behaviour
74    // Basically this is telling us that if activation bubbles up to the label
75    // at all, we are free to do an implementation-dependent thing;
76    // firing a click event is an example, and the precise details of that
77    // click event (e.g. isTrusted) are not specified.
78    fn activation_behavior(
79        &self,
80        cx: &mut js::context::JSContext,
81        _event: &Event,
82        _target: &EventTarget,
83    ) {
84        if let Some(e) = self.GetControl() {
85            e.Click(CanGc::from_cx(cx));
86        }
87    }
88}
89
90impl HTMLLabelElementMethods<crate::DomTypeHolder> for HTMLLabelElement {
91    /// <https://html.spec.whatwg.org/multipage/#dom-fae-form>
92    fn GetForm(&self) -> Option<DomRoot<HTMLFormElement>> {
93        self.form_owner()
94    }
95
96    // https://html.spec.whatwg.org/multipage/#dom-label-htmlfor
97    make_getter!(HtmlFor, "for");
98
99    // https://html.spec.whatwg.org/multipage/#dom-label-htmlfor
100    make_atomic_setter!(SetHtmlFor, "for");
101
102    /// <https://html.spec.whatwg.org/multipage/#dom-label-control>
103    fn GetControl(&self) -> Option<DomRoot<HTMLElement>> {
104        let for_attr = match self.upcast::<Element>().get_attribute(&local_name!("for")) {
105            Some(for_attr) => for_attr,
106            None => return self.first_labelable_descendant(),
107        };
108
109        let for_value = for_attr.Value();
110
111        // "If the attribute is specified and there is an element in the tree
112        // whose ID is equal to the value of the for attribute, and the first
113        // such element in tree order is a labelable element, then that
114        // element is the label element's labeled control."
115        // Two subtle points here: we need to search the _tree_, which is
116        // not necessarily the document if we're detached from the document,
117        // and we only consider one element even if a later element with
118        // the same ID is labelable.
119
120        let maybe_found = self
121            .upcast::<Node>()
122            .GetRootNode(&GetRootNodeOptions::empty())
123            .traverse_preorder(ShadowIncluding::No)
124            .find_map(|e| {
125                if let Some(htmle) = e.downcast::<HTMLElement>() {
126                    if htmle.upcast::<Element>().Id() == for_value {
127                        Some(DomRoot::from_ref(htmle))
128                    } else {
129                        None
130                    }
131                } else {
132                    None
133                }
134            });
135        // We now have the element that we would return, but only return it
136        // if it's labelable.
137        if let Some(ref maybe_labelable) = maybe_found {
138            if maybe_labelable.is_labelable_element() {
139                return maybe_found;
140            }
141        }
142        None
143    }
144}
145
146impl VirtualMethods for HTMLLabelElement {
147    fn super_type(&self) -> Option<&dyn VirtualMethods> {
148        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
149    }
150
151    fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
152        match name {
153            &local_name!("for") => AttrValue::from_atomic(value.into()),
154            _ => self
155                .super_type()
156                .unwrap()
157                .parse_plain_attribute(name, value),
158        }
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        if *attr.local_name() == local_name!("form") {
171            self.form_attribute_mutated(mutation, CanGc::from_cx(cx));
172        }
173    }
174}
175
176impl HTMLLabelElement {
177    pub(crate) fn first_labelable_descendant(&self) -> Option<DomRoot<HTMLElement>> {
178        self.upcast::<Node>()
179            .traverse_preorder(ShadowIncluding::No)
180            .filter_map(DomRoot::downcast::<HTMLElement>)
181            .find(|elem| elem.is_labelable_element())
182    }
183}
184
185impl FormControl for HTMLLabelElement {
186    fn form_owner(&self) -> Option<DomRoot<HTMLFormElement>> {
187        self.GetControl()
188            .map(DomRoot::upcast::<Element>)
189            .and_then(|elem| {
190                elem.as_maybe_form_control()
191                    .and_then(|control| control.form_owner())
192            })
193    }
194
195    fn set_form_owner(&self, _: Option<&HTMLFormElement>) {
196        // Label is a special case for form owner, it reflects its control's
197        // form owner. Therefore it doesn't hold form owner itself.
198    }
199
200    fn to_element(&self) -> &Element {
201        self.upcast::<Element>()
202    }
203}