script/dom/html/
htmllabelelement.rs1use 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 local_name: LocalName,
48 prefix: Option<Prefix>,
49 document: &Document,
50 proto: Option<HandleObject>,
51 can_gc: CanGc,
52 ) -> DomRoot<HTMLLabelElement> {
53 Node::reflect_node_with_proto(
54 Box::new(HTMLLabelElement::new_inherited(
55 local_name, prefix, document,
56 )),
57 document,
58 proto,
59 can_gc,
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 fn activation_behavior(&self, _event: &Event, _target: &EventTarget, can_gc: CanGc) {
79 if let Some(e) = self.GetControl() {
80 e.Click(can_gc);
81 }
82 }
83}
84
85impl HTMLLabelElementMethods<crate::DomTypeHolder> for HTMLLabelElement {
86 fn GetForm(&self) -> Option<DomRoot<HTMLFormElement>> {
88 self.form_owner()
89 }
90
91 make_getter!(HtmlFor, "for");
93
94 make_atomic_setter!(SetHtmlFor, "for");
96
97 fn GetControl(&self) -> Option<DomRoot<HTMLElement>> {
99 let for_attr = match self.upcast::<Element>().get_attribute(&local_name!("for")) {
100 Some(for_attr) => for_attr,
101 None => return self.first_labelable_descendant(),
102 };
103
104 let for_value = for_attr.Value();
105
106 let maybe_found = self
116 .upcast::<Node>()
117 .GetRootNode(&GetRootNodeOptions::empty())
118 .traverse_preorder(ShadowIncluding::No)
119 .find_map(|e| {
120 if let Some(htmle) = e.downcast::<HTMLElement>() {
121 if htmle.upcast::<Element>().Id() == for_value {
122 Some(DomRoot::from_ref(htmle))
123 } else {
124 None
125 }
126 } else {
127 None
128 }
129 });
130 if let Some(ref maybe_labelable) = maybe_found {
133 if maybe_labelable.is_labelable_element() {
134 return maybe_found;
135 }
136 }
137 None
138 }
139}
140
141impl VirtualMethods for HTMLLabelElement {
142 fn super_type(&self) -> Option<&dyn VirtualMethods> {
143 Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
144 }
145
146 fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
147 match name {
148 &local_name!("for") => AttrValue::from_atomic(value.into()),
149 _ => self
150 .super_type()
151 .unwrap()
152 .parse_plain_attribute(name, value),
153 }
154 }
155
156 fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
157 self.super_type()
158 .unwrap()
159 .attribute_mutated(attr, mutation, can_gc);
160 if *attr.local_name() == local_name!("form") {
161 self.form_attribute_mutated(mutation, can_gc);
162 }
163 }
164}
165
166impl HTMLLabelElement {
167 pub(crate) fn first_labelable_descendant(&self) -> Option<DomRoot<HTMLElement>> {
168 self.upcast::<Node>()
169 .traverse_preorder(ShadowIncluding::No)
170 .filter_map(DomRoot::downcast::<HTMLElement>)
171 .find(|elem| elem.is_labelable_element())
172 }
173}
174
175impl FormControl for HTMLLabelElement {
176 fn form_owner(&self) -> Option<DomRoot<HTMLFormElement>> {
177 self.GetControl()
178 .map(DomRoot::upcast::<Element>)
179 .and_then(|elem| {
180 elem.as_maybe_form_control()
181 .and_then(|control| control.form_owner())
182 })
183 }
184
185 fn set_form_owner(&self, _: Option<&HTMLFormElement>) {
186 }
189
190 fn to_element(&self) -> &Element {
191 self.upcast::<Element>()
192 }
193}