script/dom/html/
htmlprogresselement.rs1use std::cell::Ref;
6
7use dom_struct::dom_struct;
8use html5ever::{LocalName, Prefix, QualName, local_name, ns};
9use js::context::JSContext;
10use js::rust::HandleObject;
11use script_bindings::cell::DomRefCell;
12
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::attributes::storage::AttrRef;
22use crate::dom::element::{AttributeMutation, CustomElementCreationMode, Element, ElementCreator};
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<Element>,
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 pub(crate) fn new(
57 cx: &mut js::context::JSContext,
58 local_name: LocalName,
59 prefix: Option<Prefix>,
60 document: &Document,
61 proto: Option<HandleObject>,
62 ) -> DomRoot<HTMLProgressElement> {
63 Node::reflect_node_with_proto(
64 cx,
65 Box::new(HTMLProgressElement::new_inherited(
66 local_name, prefix, document,
67 )),
68 document,
69 proto,
70 )
71 }
72
73 fn create_shadow_tree(&self, cx: &mut JSContext) {
74 let document = self.owner_document();
75 let root = self.upcast::<Element>().attach_ua_shadow_root(cx, true);
76
77 let progress_bar = Element::create(
78 cx,
79 QualName::new(None, ns!(html), local_name!("div")),
80 None,
81 &document,
82 ElementCreator::ScriptCreated,
83 CustomElementCreationMode::Asynchronous,
84 None,
85 );
86
87 progress_bar.SetId(cx, "-servo-progress-bar".into());
89 root.upcast::<Node>()
90 .AppendChild(cx, progress_bar.upcast::<Node>())
91 .unwrap();
92
93 let _ = self.shadow_tree.borrow_mut().insert(ShadowTree {
94 progress_bar: progress_bar.as_traced(),
95 });
96 self.upcast::<Node>()
97 .dirty(crate::dom::node::NodeDamage::Other);
98 }
99
100 fn shadow_tree(&self, cx: &mut JSContext) -> Ref<'_, ShadowTree> {
101 if !self.upcast::<Element>().is_shadow_host() {
102 self.create_shadow_tree(cx);
103 }
104
105 Ref::filter_map(self.shadow_tree.borrow(), Option::as_ref)
106 .ok()
107 .expect("UA shadow tree was not created")
108 }
109
110 fn update_state(&self, cx: &mut JSContext) {
112 let shadow_tree = self.shadow_tree(cx);
113 let position = (*self.Value() / *self.Max()) * 100.0;
114 let style = format!("width: {}%", position);
115
116 shadow_tree
117 .progress_bar
118 .set_string_attribute(cx, &local_name!("style"), style.into());
119 }
120}
121
122impl HTMLProgressElementMethods<crate::DomTypeHolder> for HTMLProgressElement {
123 make_labels_getter!(Labels, labels_node_list);
125
126 fn Value(&self) -> Finite<f64> {
128 self.upcast::<Element>()
135 .get_string_attribute(&local_name!("value"))
136 .parse_floating_point_number()
137 .map_or(Finite::wrap(0.0), |v| {
138 if v < 0.0 {
139 Finite::wrap(0.0)
140 } else {
141 Finite::wrap(v.min(*self.Max()))
142 }
143 })
144 }
145
146 fn SetValue(&self, cx: &mut JSContext, new_val: Finite<f64>) {
148 if *new_val >= 0.0 {
149 let mut string_value = DOMString::from((*new_val).to_string());
150 string_value.set_best_representation_of_the_floating_point_number();
151 self.upcast::<Element>()
152 .set_string_attribute(cx, &local_name!("value"), string_value);
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, cx: &mut JSContext, new_val: Finite<f64>) {
175 if *new_val > 0.0 {
176 let mut string_value = DOMString::from((*new_val).to_string());
177 string_value.set_best_representation_of_the_floating_point_number();
178 self.upcast::<Element>()
179 .set_string_attribute(cx, &local_name!("max"), string_value);
180 }
181 }
182
183 fn Position(&self) -> Finite<f64> {
185 let value = self
186 .upcast::<Element>()
187 .get_string_attribute(&local_name!("value"));
188 if value.is_empty() {
189 Finite::wrap(-1.0)
190 } else {
191 let value = self.Value();
192 let max = self.Max();
193 Finite::wrap(*value / *max)
201 }
202 }
203}
204
205impl VirtualMethods for HTMLProgressElement {
206 fn super_type(&self) -> Option<&dyn VirtualMethods> {
207 Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
208 }
209
210 fn attribute_mutated(
211 &self,
212 cx: &mut js::context::JSContext,
213 attr: AttrRef<'_>,
214 mutation: AttributeMutation,
215 ) {
216 self.super_type()
217 .unwrap()
218 .attribute_mutated(cx, attr, mutation);
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(cx);
226 }
227 }
228
229 fn bind_to_tree(&self, cx: &mut JSContext, context: &BindContext) {
230 self.super_type().unwrap().bind_to_tree(cx, context);
231
232 self.update_state(cx);
233 }
234}