1use std::cell::Ref;
6use std::ops::{Add, Div};
7
8use dom_struct::dom_struct;
9use html5ever::{LocalName, Prefix, QualName, local_name, ns};
10use js::context::JSContext;
11use js::rust::HandleObject;
12use script_bindings::cell::DomRefCell;
13use stylo_dom::ElementState;
14
15use crate::dom::bindings::codegen::Bindings::HTMLMeterElementBinding::HTMLMeterElementMethods;
16use crate::dom::bindings::codegen::Bindings::NodeBinding::Node_Binding::NodeMethods;
17use crate::dom::bindings::inheritance::Castable;
18use crate::dom::bindings::num::Finite;
19use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
20use crate::dom::bindings::str::DOMString;
21use crate::dom::document::Document;
22use crate::dom::element::attributes::storage::AttrRef;
23use crate::dom::element::{AttributeMutation, Element};
24use crate::dom::html::htmlelement::HTMLElement;
25use crate::dom::node::virtualmethods::VirtualMethods;
26use crate::dom::node::{BindContext, ChildrenMutation, Node, NodeTraits};
27use crate::dom::nodelist::NodeList;
28
29#[dom_struct]
30pub(crate) struct HTMLMeterElement {
31 htmlelement: HTMLElement,
32 labels_node_list: MutNullableDom<NodeList>,
33 shadow_tree: DomRefCell<Option<ShadowTree>>,
34}
35
36#[derive(Clone, JSTraceable, MallocSizeOf)]
38#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
39struct ShadowTree {
40 meter_value: Dom<Element>,
41}
42
43impl HTMLMeterElement {
45 fn new_inherited(
46 local_name: LocalName,
47 prefix: Option<Prefix>,
48 document: &Document,
49 ) -> HTMLMeterElement {
50 HTMLMeterElement {
51 htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
52 labels_node_list: MutNullableDom::new(None),
53 shadow_tree: Default::default(),
54 }
55 }
56
57 pub(crate) fn new(
58 cx: &mut js::context::JSContext,
59 local_name: LocalName,
60 prefix: Option<Prefix>,
61 document: &Document,
62 proto: Option<HandleObject>,
63 ) -> DomRoot<HTMLMeterElement> {
64 Node::reflect_node_with_proto(
65 cx,
66 Box::new(HTMLMeterElement::new_inherited(
67 local_name, prefix, document,
68 )),
69 document,
70 proto,
71 )
72 }
73
74 fn create_shadow_tree(&self, cx: &mut JSContext) {
75 let document = self.owner_document();
76 let root = self.upcast::<Element>().attach_ua_shadow_root(cx, true);
77
78 let meter_value = Element::create(
79 cx,
80 QualName::new(None, ns!(html), local_name!("div")),
81 None,
82 &document,
83 crate::dom::element::ElementCreator::ScriptCreated,
84 crate::dom::element::CustomElementCreationMode::Asynchronous,
85 None,
86 );
87 root.upcast::<Node>()
88 .AppendChild(cx, meter_value.upcast::<Node>())
89 .unwrap();
90
91 let _ = self.shadow_tree.borrow_mut().insert(ShadowTree {
92 meter_value: meter_value.as_traced(),
93 });
94 self.upcast::<Node>()
95 .dirty(crate::dom::node::NodeDamage::Other);
96 }
97
98 fn shadow_tree(&self, cx: &mut JSContext) -> Ref<'_, ShadowTree> {
99 if !self.upcast::<Element>().is_shadow_host() {
100 self.create_shadow_tree(cx);
101 }
102
103 Ref::filter_map(self.shadow_tree.borrow(), Option::as_ref)
104 .ok()
105 .expect("UA shadow tree was not created")
106 }
107
108 fn update_state(&self, cx: &mut JSContext) {
109 let value = *self.Value();
110 let low = *self.Low();
111 let high = *self.High();
112 let min = *self.Min();
113 let max = *self.Max();
114 let optimum = *self.Optimum();
115
116 let element_state = if optimum < low {
121 if value < low {
122 ElementState::OPTIMUM
123 } else if value <= high {
124 ElementState::SUB_OPTIMUM
125 } else {
126 ElementState::SUB_SUB_OPTIMUM
127 }
128 }
129 else if optimum > high {
134 if value > high {
135 ElementState::OPTIMUM
136 } else if value >= low {
137 ElementState::SUB_OPTIMUM
138 } else {
139 ElementState::SUB_SUB_OPTIMUM
140 }
141 }
142 else if (low..=high).contains(&value) {
146 ElementState::OPTIMUM
147 } else {
148 ElementState::SUB_OPTIMUM
149 };
150
151 self.upcast::<Element>()
153 .set_state(ElementState::METER_OPTIMUM_STATES, false);
154 self.upcast::<Element>().set_state(element_state, true);
155
156 let shadow_tree = self.shadow_tree(cx);
158 let position = (value - min) / (max - min) * 100.0;
159 let style = format!("width: {position}%");
160 shadow_tree
161 .meter_value
162 .set_string_attribute(cx, &local_name!("style"), style.into());
163 }
164}
165
166impl HTMLMeterElementMethods<crate::DomTypeHolder> for HTMLMeterElement {
167 make_labels_getter!(Labels, labels_node_list);
169
170 fn Value(&self) -> Finite<f64> {
172 let min = *self.Min();
173 let max = *self.Max();
174
175 Finite::wrap(
176 self.upcast::<Element>()
177 .get_string_attribute(&local_name!("value"))
178 .parse_floating_point_number()
179 .map_or(0.0, |candidate_actual_value| {
180 candidate_actual_value.clamp(min, max)
181 }),
182 )
183 }
184
185 fn SetValue(&self, cx: &mut js::context::JSContext, value: Finite<f64>) {
187 let mut string_value = DOMString::from((*value).to_string());
188 string_value.set_best_representation_of_the_floating_point_number();
189 self.upcast::<Element>()
190 .set_string_attribute(cx, &local_name!("value"), string_value);
191 }
192
193 fn Min(&self) -> Finite<f64> {
195 Finite::wrap(
196 self.upcast::<Element>()
197 .get_string_attribute(&local_name!("min"))
198 .parse_floating_point_number()
199 .unwrap_or(0.0),
200 )
201 }
202
203 fn SetMin(&self, cx: &mut js::context::JSContext, value: Finite<f64>) {
205 let mut string_value = DOMString::from((*value).to_string());
206 string_value.set_best_representation_of_the_floating_point_number();
207 self.upcast::<Element>()
208 .set_string_attribute(cx, &local_name!("min"), string_value);
209 }
210
211 fn Max(&self) -> Finite<f64> {
213 Finite::wrap(
214 self.upcast::<Element>()
215 .get_string_attribute(&local_name!("max"))
216 .parse_floating_point_number()
217 .unwrap_or(1.0)
218 .max(*self.Min()),
219 )
220 }
221
222 fn SetMax(&self, cx: &mut js::context::JSContext, value: Finite<f64>) {
224 let mut string_value = DOMString::from((*value).to_string());
225 string_value.set_best_representation_of_the_floating_point_number();
226 self.upcast::<Element>()
227 .set_string_attribute(cx, &local_name!("max"), string_value);
228 }
229
230 fn Low(&self) -> Finite<f64> {
232 let min = *self.Min();
233 let max = *self.Max();
234
235 Finite::wrap(
236 self.upcast::<Element>()
237 .get_string_attribute(&local_name!("low"))
238 .parse_floating_point_number()
239 .map_or(min, |candidate_low_boundary| {
240 candidate_low_boundary.clamp(min, max)
241 }),
242 )
243 }
244
245 fn SetLow(&self, cx: &mut js::context::JSContext, value: Finite<f64>) {
247 let mut string_value = DOMString::from((*value).to_string());
248 string_value.set_best_representation_of_the_floating_point_number();
249 self.upcast::<Element>()
250 .set_string_attribute(cx, &local_name!("low"), string_value);
251 }
252
253 fn High(&self) -> Finite<f64> {
255 let max: f64 = *self.Max();
256 let low: f64 = *self.Low();
257
258 Finite::wrap(
259 self.upcast::<Element>()
260 .get_string_attribute(&local_name!("high"))
261 .parse_floating_point_number()
262 .map_or(max, |candidate_high_boundary| {
263 if candidate_high_boundary < low {
264 return low;
265 }
266
267 candidate_high_boundary.clamp(*self.Min(), max)
268 }),
269 )
270 }
271
272 fn SetHigh(&self, cx: &mut js::context::JSContext, value: Finite<f64>) {
274 let mut string_value = DOMString::from((*value).to_string());
275 string_value.set_best_representation_of_the_floating_point_number();
276 self.upcast::<Element>()
277 .set_string_attribute(cx, &local_name!("high"), string_value);
278 }
279
280 fn Optimum(&self) -> Finite<f64> {
282 let max = *self.Max();
283 let min = *self.Min();
284
285 Finite::wrap(
286 self.upcast::<Element>()
287 .get_string_attribute(&local_name!("optimum"))
288 .parse_floating_point_number()
289 .map_or(max.add(min).div(2.0), |candidate_optimum_point| {
290 candidate_optimum_point.clamp(min, max)
291 }),
292 )
293 }
294
295 fn SetOptimum(&self, cx: &mut js::context::JSContext, value: Finite<f64>) {
297 let mut string_value = DOMString::from((*value).to_string());
298 string_value.set_best_representation_of_the_floating_point_number();
299 self.upcast::<Element>()
300 .set_string_attribute(cx, &local_name!("optimum"), string_value);
301 }
302}
303
304impl VirtualMethods for HTMLMeterElement {
305 fn super_type(&self) -> Option<&dyn VirtualMethods> {
306 Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
307 }
308
309 fn attribute_mutated(
310 &self,
311 cx: &mut js::context::JSContext,
312 attr: AttrRef<'_>,
313 mutation: AttributeMutation,
314 ) {
315 self.super_type()
316 .unwrap()
317 .attribute_mutated(cx, attr, mutation);
318
319 let is_important_attribute = matches!(
320 attr.local_name(),
321 &local_name!("high") |
322 &local_name!("low") |
323 &local_name!("min") |
324 &local_name!("max") |
325 &local_name!("optimum") |
326 &local_name!("value")
327 );
328 if is_important_attribute {
329 self.update_state(cx);
330 }
331 }
332
333 fn children_changed(&self, cx: &mut JSContext, mutation: &ChildrenMutation) {
334 self.super_type().unwrap().children_changed(cx, mutation);
335
336 self.update_state(cx);
337 }
338
339 fn bind_to_tree(&self, cx: &mut JSContext, context: &BindContext) {
340 self.super_type().unwrap().bind_to_tree(cx, context);
341
342 self.update_state(cx);
343 }
344}