1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use cssparser::{Parser, ParserInput};
use dom_struct::dom_struct;
use script_bindings::cell::DomRefCell;
use script_bindings::codegen::GenericBindings::AnimationEffectBinding::{
AnimationEffectMethods, ComputedEffectTiming, EffectTiming, FillMode, OptionalEffectTiming,
PlaybackDirection,
};
use script_bindings::codegen::GenericBindings::WindowBinding::WindowMethods;
use script_bindings::error::{Error, Fallible};
use script_bindings::num::Finite;
use script_bindings::reflector::Reflector;
use script_bindings::root::Dom;
use style::parser::Parse;
use style::stylesheets::CssRuleType;
use style::values::generics::easing::TimingKeyword;
use style::values::specified::TimingFunction;
use style_traits::{ParsingMode, ToCss};
use crate::css::parser_context_for_document;
use crate::dom::Window;
use crate::dom::bindings::codegen::UnionTypes::UnrestrictedDoubleOrString;
/// <https://drafts.csswg.org/web-animations-1/#animationeffect>
#[dom_struct]
pub(crate) struct AnimationEffect {
reflector: Reflector,
/// The window that this `AnimationEffect` was constructed in.
window: Dom<Window>,
specified_timing_properties: DomRefCell<SpecifiedTimingProperties>,
}
#[derive(Clone, JSTraceable, MallocSizeOf)]
struct SpecifiedTimingProperties {
/// <https://drafts.csswg.org/web-animations-1/#start-delay>
start_delay: Finite<f64>,
/// <https://drafts.csswg.org/web-animations-1/#end-delay>
end_delay: Finite<f64>,
/// <https://drafts.csswg.org/web-animations-1/#fill-mode>
fill_mode: FillMode,
/// <https://drafts.csswg.org/web-animations-1/#iteration-count>
iteration_count: f64,
/// <https://drafts.csswg.org/web-animations-1/#iteration-start>
iteration_start: Finite<f64>,
/// <https://drafts.csswg.org/web-animations-1/#iteration-duration>
iteration_duration: IterationDurationOrAuto,
/// <https://drafts.csswg.org/web-animations-1/#playback-direction>
playback_direction: PlaybackDirection,
/// <https://drafts.csswg.org/css-easing-2/#easing-function>
#[no_trace]
easing_function: TimingFunction,
}
impl AnimationEffect {
pub(crate) fn new_inherited(window: &Window) -> Self {
Self {
reflector: Reflector::new(),
window: Dom::from_ref(window),
// The default values of the timing properties specified here don't matter.
// There is no way to construct a AnimationEffect without subsequently initializing them,
// even if they're not passed to the constructor.
specified_timing_properties: DomRefCell::new(SpecifiedTimingProperties {
start_delay: Default::default(),
end_delay: Default::default(),
fill_mode: FillMode::None,
iteration_count: Default::default(),
iteration_start: Default::default(),
iteration_duration: IterationDurationOrAuto::Auto,
playback_direction: PlaybackDirection::Normal,
easing_function: TimingFunction::Keyword(TimingKeyword::Linear),
}),
}
}
pub(crate) fn window(&self) -> &Window {
&self.window
}
/// <https://drafts.csswg.org/web-animations-1/#update-the-timing-properties-of-an-animation-effect>
pub(crate) fn update_the_timing_properties(
&self,
input: &OptionalEffectTiming,
) -> Fallible<()> {
// Step 1. If the iterationStart member of input exists and is less than zero,
// throw a TypeError and abort this procedure.
if input
.iterationStart
.is_some_and(|iteration_start| *iteration_start < 0.0)
{
return Err(Error::Type(
c"Negative values for iterationStart are not allowed".to_owned(),
));
}
// Step 2. If the iterations member of input exists, and is less than zero or is the value NaN,
// throw a TypeError and abort this procedure.
if input
.iterations
.is_some_and(|iterations| iterations < 0.0 || iterations.is_nan())
{
return Err(Error::Type(
c"\"iterations\" must be a positive number".to_owned(),
));
}
// Step 3. If the duration member of input exists, and is less than zero, is the value NaN,
// or is a string other than the lowercase auto, throw a TypeError and abort this procedure.
let Ok(duration) = input
.duration
.as_ref()
.map(|duration| match duration {
UnrestrictedDoubleOrString::UnrestrictedDouble(double) => {
if *double < 0.0 || double.is_nan() {
Err(())
} else {
Ok(IterationDurationOrAuto::Duration(*double))
}
},
UnrestrictedDoubleOrString::String(string) => {
if string == "auto" {
Ok(IterationDurationOrAuto::Auto)
} else {
Err(())
}
},
})
.transpose()
else {
return Err(Error::Type(
c"\"duration\" must be a positive number".to_owned(),
));
};
// Step 4. If the easing member of input exists but cannot be parsed using the <easing-function> production [CSS-EASING-1],
// throw a TypeError and abort this procedure.
let Ok(easing) = input
.easing
.as_ref()
.map(|easing| {
let easing = easing.str();
let mut parser_input = ParserInput::new(&easing);
let mut parser = Parser::new(&mut parser_input);
// None of these values should matter
let document = self.window.Document();
let urlextradata = document.url().into_url().into();
let parser_context = parser_context_for_document(
&document,
CssRuleType::Style,
ParsingMode::DEFAULT,
&urlextradata,
);
TimingFunction::parse(&parser_context, &mut parser).map_err(|_| ())
})
.transpose()
else {
return Err(Error::Type(
c"\"easing\" is not a valid timing function".to_owned(),
));
};
// Step 5. Assign each member that exists in input to the corresponding timing property of effect as follows:
// delay → start delay
let mut specified_timing_properties = self.specified_timing_properties.borrow_mut();
if let Some(start_delay) = input.delay {
specified_timing_properties.start_delay = start_delay;
}
// endDelay → end delay
if let Some(end_delay) = input.endDelay {
specified_timing_properties.end_delay = end_delay;
}
// fill → fill mode
if let Some(fill) = input.fill {
specified_timing_properties.fill_mode = fill;
}
// iterationStart → iteration start
if let Some(iteration_start) = input.iterationStart {
specified_timing_properties.iteration_start = iteration_start;
}
// iterations → iteration count
if let Some(iterations) = input.iterations {
specified_timing_properties.iteration_count = iterations;
}
// duration → iteration duration
if let Some(duration) = duration {
specified_timing_properties.iteration_duration = duration;
}
// direction → playback direction
if let Some(direction) = input.direction {
specified_timing_properties.playback_direction = direction;
}
// easing → easing function
if let Some(easing) = easing {
specified_timing_properties.easing_function = easing;
}
Ok(())
}
}
impl AnimationEffectMethods<crate::DomTypeHolder> for AnimationEffect {
/// <https://drafts.csswg.org/web-animations-1/#dom-animationeffect-gettiming>
fn GetTiming(&self) -> EffectTiming {
// > Returns the specified timing properties for this animation effect.
let specified_timing_properties = self.specified_timing_properties.borrow();
EffectTiming {
delay: specified_timing_properties.start_delay,
direction: specified_timing_properties.playback_direction,
duration: specified_timing_properties.iteration_duration.into(),
easing: specified_timing_properties
.easing_function
.to_css_string()
.into(),
endDelay: specified_timing_properties.end_delay,
fill: specified_timing_properties.fill_mode,
iterationStart: specified_timing_properties.iteration_start,
iterations: specified_timing_properties.iteration_count,
}
}
/// <https://drafts.csswg.org/web-animations-1/#dom-animationeffect-getcomputedtiming>
fn GetComputedTiming(&self) -> ComputedEffectTiming {
// > Returns the calculated timing properties for this animation effect.
let specified_timing_properties = self.specified_timing_properties.borrow();
// > while getTiming() can return the string auto, getComputedTiming() must return a number
// > corresponding to the calculated value of the iteration duration as defined in the description
// > of the duration member of the EffectTiming interface.
// > In this level of the specification, that simply means that an auto value is replaced by zero.
let computed_duration = specified_timing_properties
.iteration_duration
.computed_value();
// > likewise, while getTiming() can return the string auto, getComputedTiming() must return the
// > specific FillMode used for timing calculations as defined in the description of the fill
// > member of the EffectTiming interface.
// > In this level of the specification, that simply means that an auto value is replaced by the none FillMode.
let computed_fill_mode = if specified_timing_properties.fill_mode == FillMode::Auto {
FillMode::None
} else {
specified_timing_properties.fill_mode
};
ComputedEffectTiming {
parent: EffectTiming {
delay: specified_timing_properties.start_delay,
direction: specified_timing_properties.playback_direction,
duration: UnrestrictedDoubleOrString::UnrestrictedDouble(computed_duration),
easing: specified_timing_properties
.easing_function
.to_css_string()
.into(),
endDelay: specified_timing_properties.end_delay,
fill: computed_fill_mode,
iterationStart: specified_timing_properties.iteration_start,
iterations: specified_timing_properties.iteration_count,
},
// FIXME: These are just placeholder values
endTime: None,
activeDuration: None,
localTime: None,
progress: None,
currentIteration: None,
}
}
/// <https://drafts.csswg.org/web-animations-1/#dom-animationeffect-updatetiming>
fn UpdateTiming(&self, timing: &OptionalEffectTiming) -> Fallible<()> {
// > Updates the specified timing properties of this animation effect by performing the procedure
// > to update the timing properties of an animation effect passing the timing parameter as input.
self.update_the_timing_properties(timing)
}
}
#[derive(Clone, Copy, Debug, JSTraceable, MallocSizeOf)]
enum IterationDurationOrAuto {
Duration(f64),
Auto,
}
impl IterationDurationOrAuto {
fn computed_value(&self) -> f64 {
match self {
IterationDurationOrAuto::Auto => 0.0,
IterationDurationOrAuto::Duration(double) => *double,
}
}
}
impl From<IterationDurationOrAuto> for UnrestrictedDoubleOrString {
fn from(value: IterationDurationOrAuto) -> Self {
match value {
IterationDurationOrAuto::Auto => UnrestrictedDoubleOrString::String("auto".into()),
IterationDurationOrAuto::Duration(double) => {
UnrestrictedDoubleOrString::UnrestrictedDouble(double)
},
}
}
}