script/dom/html/
htmlprogresselement.rs1use std::cell::Ref;
6
7use dom_struct::dom_struct;
8use html5ever::{LocalName, Prefix, local_name};
9use js::rust::HandleObject;
10
11use crate::dom::attr::Attr;
12use crate::dom::bindings::cell::DomRefCell;
13use crate::dom::bindings::codegen::Bindings::ElementBinding::Element_Binding::ElementMethods;
14use crate::dom::bindings::codegen::Bindings::HTMLProgressElementBinding::HTMLProgressElementMethods;
15use crate::dom::bindings::codegen::Bindings::NodeBinding::Node_Binding::NodeMethods;
16use crate::dom::bindings::inheritance::Castable;
17use crate::dom::bindings::num::Finite;
18use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
19use crate::dom::bindings::str::DOMString;
20use crate::dom::document::Document;
21use crate::dom::element::{AttributeMutation, Element};
22use crate::dom::html::htmldivelement::HTMLDivElement;
23use crate::dom::html::htmlelement::HTMLElement;
24use crate::dom::node::{BindContext, Node, NodeTraits};
25use crate::dom::nodelist::NodeList;
26use crate::dom::virtualmethods::VirtualMethods;
27use crate::script_runtime::CanGc;
28
29#[dom_struct]
30pub(crate) struct HTMLProgressElement {
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 progress_bar: Dom<HTMLDivElement>,
41}
42
43impl HTMLProgressElement {
44 fn new_inherited(
45 local_name: LocalName,
46 prefix: Option<Prefix>,
47 document: &Document,
48 ) -> HTMLProgressElement {
49 HTMLProgressElement {
50 htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
51 labels_node_list: MutNullableDom::new(None),
52 shadow_tree: Default::default(),
53 }
54 }
55
56 #[cfg_attr(crown, allow(crown::unrooted_must_root))]
57 pub(crate) fn new(
58 local_name: LocalName,
59 prefix: Option<Prefix>,
60 document: &Document,
61 proto: Option<HandleObject>,
62 can_gc: CanGc,
63 ) -> DomRoot<HTMLProgressElement> {
64 Node::reflect_node_with_proto(
65 Box::new(HTMLProgressElement::new_inherited(
66 local_name, prefix, document,
67 )),
68 document,
69 proto,
70 can_gc,
71 )
72 }
73
74 fn create_shadow_tree(&self, can_gc: CanGc) {
75 let document = self.owner_document();
76 let root = self.upcast::<Element>().attach_ua_shadow_root(true, can_gc);
77
78 let progress_bar = HTMLDivElement::new(local_name!("div"), None, &document, None, can_gc);
79 progress_bar
81 .upcast::<Element>()
82 .SetId("-servo-progress-bar".into(), can_gc);
83 root.upcast::<Node>()
84 .AppendChild(progress_bar.upcast::<Node>(), can_gc)
85 .unwrap();
86
87 let _ = self.shadow_tree.borrow_mut().insert(ShadowTree {
88 progress_bar: progress_bar.as_traced(),
89 });
90 self.upcast::<Node>()
91 .dirty(crate::dom::node::NodeDamage::Other);
92 }
93
94 fn shadow_tree(&self, can_gc: CanGc) -> Ref<'_, ShadowTree> {
95 if !self.upcast::<Element>().is_shadow_host() {
96 self.create_shadow_tree(can_gc);
97 }
98
99 Ref::filter_map(self.shadow_tree.borrow(), Option::as_ref)
100 .ok()
101 .expect("UA shadow tree was not created")
102 }
103
104 fn update_state(&self, can_gc: CanGc) {
106 let shadow_tree = self.shadow_tree(can_gc);
107 let position = (*self.Value() / *self.Max()) * 100.0;
108 let style = format!("width: {}%", position);
109
110 shadow_tree
111 .progress_bar
112 .upcast::<Element>()
113 .set_string_attribute(&local_name!("style"), style.into(), can_gc);
114 }
115}
116
117impl HTMLProgressElementMethods<crate::DomTypeHolder> for HTMLProgressElement {
118 make_labels_getter!(Labels, labels_node_list);
120
121 fn Value(&self) -> Finite<f64> {
123 self.upcast::<Element>()
130 .get_string_attribute(&local_name!("value"))
131 .parse_floating_point_number()
132 .map_or(Finite::wrap(0.0), |v| {
133 if v < 0.0 {
134 Finite::wrap(0.0)
135 } else {
136 Finite::wrap(v.min(*self.Max()))
137 }
138 })
139 }
140
141 fn SetValue(&self, new_val: Finite<f64>, can_gc: CanGc) {
143 if *new_val >= 0.0 {
144 let mut string_value = DOMString::from_string((*new_val).to_string());
145
146 string_value.set_best_representation_of_the_floating_point_number();
147
148 self.upcast::<Element>().set_string_attribute(
149 &local_name!("value"),
150 string_value,
151 can_gc,
152 );
153 }
154 }
155
156 fn Max(&self) -> Finite<f64> {
158 self.upcast::<Element>()
162 .get_string_attribute(&local_name!("max"))
163 .parse_floating_point_number()
164 .map_or(Finite::wrap(1.0), |m| {
165 if m <= 0.0 {
166 Finite::wrap(1.0)
167 } else {
168 Finite::wrap(m)
169 }
170 })
171 }
172
173 fn SetMax(&self, new_val: Finite<f64>, can_gc: CanGc) {
175 if *new_val > 0.0 {
176 let mut string_value = DOMString::from_string((*new_val).to_string());
177
178 string_value.set_best_representation_of_the_floating_point_number();
179
180 self.upcast::<Element>().set_string_attribute(
181 &local_name!("max"),
182 string_value,
183 can_gc,
184 );
185 }
186 }
187
188 fn Position(&self) -> Finite<f64> {
190 let value = self
191 .upcast::<Element>()
192 .get_string_attribute(&local_name!("value"));
193 if value.is_empty() {
194 Finite::wrap(-1.0)
195 } else {
196 let value = self.Value();
197 let max = self.Max();
198 Finite::wrap(*value / *max)
206 }
207 }
208}
209
210impl VirtualMethods for HTMLProgressElement {
211 fn super_type(&self) -> Option<&dyn VirtualMethods> {
212 Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
213 }
214
215 fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
216 self.super_type()
217 .unwrap()
218 .attribute_mutated(attr, mutation, can_gc);
219
220 let is_important_attribute = matches!(
221 attr.local_name(),
222 &local_name!("value") | &local_name!("max")
223 );
224 if is_important_attribute {
225 self.update_state(CanGc::note());
226 }
227 }
228
229 fn bind_to_tree(&self, context: &BindContext, can_gc: CanGc) {
230 self.super_type().unwrap().bind_to_tree(context, can_gc);
231
232 self.update_state(CanGc::note());
233 }
234}