script/dom/html/
htmllabelelement.rs1use dom_struct::dom_struct;
6use html5ever::{LocalName, Prefix, local_name, ns};
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
100 .upcast::<Element>()
101 .get_attribute(&ns!(), &local_name!("for"))
102 {
103 Some(for_attr) => for_attr,
104 None => return self.first_labelable_descendant(),
105 };
106
107 let for_value = for_attr.Value();
108
109 let maybe_found = self
119 .upcast::<Node>()
120 .GetRootNode(&GetRootNodeOptions::empty())
121 .traverse_preorder(ShadowIncluding::No)
122 .find_map(|e| {
123 if let Some(htmle) = e.downcast::<HTMLElement>() {
124 if htmle.upcast::<Element>().Id() == for_value {
125 Some(DomRoot::from_ref(htmle))
126 } else {
127 None
128 }
129 } else {
130 None
131 }
132 });
133 if let Some(ref maybe_labelable) = maybe_found {
136 if maybe_labelable.is_labelable_element() {
137 return maybe_found;
138 }
139 }
140 None
141 }
142}
143
144impl VirtualMethods for HTMLLabelElement {
145 fn super_type(&self) -> Option<&dyn VirtualMethods> {
146 Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
147 }
148
149 fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
150 match name {
151 &local_name!("for") => AttrValue::from_atomic(value.into()),
152 _ => self
153 .super_type()
154 .unwrap()
155 .parse_plain_attribute(name, value),
156 }
157 }
158
159 fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
160 self.super_type()
161 .unwrap()
162 .attribute_mutated(attr, mutation, can_gc);
163 if *attr.local_name() == local_name!("form") {
164 self.form_attribute_mutated(mutation, can_gc);
165 }
166 }
167}
168
169impl HTMLLabelElement {
170 pub(crate) fn first_labelable_descendant(&self) -> Option<DomRoot<HTMLElement>> {
171 self.upcast::<Node>()
172 .traverse_preorder(ShadowIncluding::No)
173 .filter_map(DomRoot::downcast::<HTMLElement>)
174 .find(|elem| elem.is_labelable_element())
175 }
176}
177
178impl FormControl for HTMLLabelElement {
179 fn form_owner(&self) -> Option<DomRoot<HTMLFormElement>> {
180 self.GetControl()
181 .map(DomRoot::upcast::<Element>)
182 .and_then(|elem| {
183 elem.as_maybe_form_control()
184 .and_then(|control| control.form_owner())
185 })
186 }
187
188 fn set_form_owner(&self, _: Option<&HTMLFormElement>) {
189 }
192
193 fn to_element(&self) -> &Element {
194 self.upcast::<Element>()
195 }
196}