script/dom/animations/keyframeeffect.rs
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use dom_struct::dom_struct;
6use js::context::JSContext;
7use js::jsapi::JSObject;
8use js::rust::HandleObject;
9use script_bindings::codegen::GenericUnionTypes::UnrestrictedDoubleOrKeyframeEffectOptions;
10use script_bindings::reflector::reflect_dom_object_with_proto_and_cx;
11use script_bindings::root::{Dom, DomRoot};
12
13use crate::dom::animationeffect::AnimationEffect;
14use crate::dom::bindings::codegen::Bindings::KeyframeEffectBinding::KeyframeEffectMethods;
15use crate::dom::bindings::root::MutNullableDom;
16use crate::dom::element::Element;
17use crate::dom::window::Window;
18
19/// <https://drafts.csswg.org/web-animations-1/#keyframeeffect>
20#[dom_struct]
21pub(crate) struct KeyframeEffect {
22 animationeffect: AnimationEffect,
23
24 /// The window that this keyframe was constructed in
25 window: Dom<Window>,
26
27 /// <https://drafts.csswg.org/web-animations-1/#effect-target-target-element>
28 // FIXME: Store a target pseudo-selector
29 // to fully match the concept of the effect target
30 //
31 // https://drafts.csswg.org/web-animations-1/#effect-target-target-pseudo-selector
32 // https://drafts.csswg.org/web-animations-1/#keyframe-effect-effect-target.
33 target_element: MutNullableDom<Element>,
34}
35
36impl KeyframeEffect {
37 pub(crate) fn new_inherited(window: &Window) -> Self {
38 Self {
39 window: Dom::from_ref(window),
40 animationeffect: AnimationEffect::new_inherited(),
41 target_element: Default::default(),
42 }
43 }
44
45 fn new_with_proto_and_cx(
46 cx: &mut JSContext,
47 window: &Window,
48 proto: Option<HandleObject>,
49 ) -> DomRoot<Self> {
50 reflect_dom_object_with_proto_and_cx(
51 Box::new(Self::new_inherited(window)),
52 window,
53 proto,
54 cx,
55 )
56 }
57
58 pub(crate) fn new(cx: &mut JSContext, window: &Window) -> DomRoot<Self> {
59 Self::new_with_proto_and_cx(cx, window, None)
60 }
61}
62
63impl KeyframeEffectMethods<crate::DomTypeHolder> for KeyframeEffect {
64 /// <https://drafts.csswg.org/web-animations-1/#dom-keyframeeffect-keyframeeffect>
65 fn Constructor(
66 cx: &mut JSContext,
67 window: &Window,
68 _: Option<HandleObject>,
69 target: Option<&Element>,
70 keyframes: *mut JSObject,
71 _options: UnrestrictedDoubleOrKeyframeEffectOptions,
72 ) -> DomRoot<KeyframeEffect> {
73 // Step 1. Create a new KeyframeEffect object, effect.
74 let effect = KeyframeEffect::new(cx, window);
75
76 // Step 2. Set the target element of effect to target.
77 effect.target_element.set(target);
78
79 // TODO: Step 3. Set the target pseudo-selector to the result corresponding to
80 // the first matching condition below:
81
82 // TODO: Step 4. Let timing input be the result corresponding to the first matching
83 // condition below:
84
85 // Step 5. Call the procedure to update the timing properties of an animation effect of
86 // effect from timing input.
87 // If that procedure causes an exception to be thrown, propagate the exception and abort this procedure.
88
89 // TODO: Step 6. If options is a KeyframeEffectOptions object, assign the composite property of effect
90 // to the corresponding value from options.
91 //
92 // When assigning this property, the error-handling defined for the corresponding setter on the
93 // KeyframeEffect interface is applied. If the setter requires an exception to be thrown for the value
94 // specified by options, this procedure must throw the same exception and abort all further steps.
95
96 // Step 7. Initialize the set of keyframes by performing the procedure defined for setKeyframes()
97 // passing keyframes as the input.
98 effect.SetKeyframes(cx, keyframes);
99
100 effect
101 }
102
103 /// <https://drafts.csswg.org/web-animations-1/#dom-keyframeeffect-setkeyframes>
104 fn SetKeyframes(&self, _cx: &mut JSContext, _keyframes: *mut JSObject) {
105 // > This effect’s set of keyframes is replaced with the result of performing the procedure to
106 // > process a keyframes argument. If that procedure throws an exception, this effect’s
107 // > keyframes are not modified.
108 // FIXME: Implement this.
109 }
110}