11001027a0676981b91ae5a236b80c5a5ce712b5
[WebKit-https.git] / Source / WebCore / animation / AnimationTimeline.cpp
1 /*
2  * Copyright (C) Canon Inc. 2016
3  * Copyright (C) 2017 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #include "AnimationTimeline.h"
29
30 #include "Animation.h"
31 #include "AnimationEffect.h"
32 #include "AnimationList.h"
33 #include "CSSAnimation.h"
34 #include "CSSPropertyAnimation.h"
35 #include "CSSTransition.h"
36 #include "DocumentTimeline.h"
37 #include "Element.h"
38 #include "KeyframeEffect.h"
39 #include "RenderStyle.h"
40 #include "RenderView.h"
41 #include "StylePropertyShorthand.h"
42 #include "StyleResolver.h"
43 #include "WebAnimationUtilities.h"
44 #include <wtf/text/TextStream.h>
45 #include <wtf/text/WTFString.h>
46
47 namespace WebCore {
48
49 AnimationTimeline::AnimationTimeline()
50 {
51 }
52
53 AnimationTimeline::~AnimationTimeline()
54 {
55 }
56
57 void AnimationTimeline::forgetAnimation(WebAnimation* animation)
58 {
59     m_allAnimations.removeFirst(animation);
60 }
61
62 void AnimationTimeline::animationTimingDidChange(WebAnimation& animation)
63 {
64     if (m_animations.add(&animation)) {
65         m_allAnimations.append(makeWeakPtr(&animation));
66         auto* timeline = animation.timeline();
67         if (timeline && timeline != this)
68             timeline->removeAnimation(animation);
69     }
70 }
71
72 void AnimationTimeline::removeAnimation(WebAnimation& animation)
73 {
74     ASSERT(!animation.timeline() || animation.timeline() == this);
75     m_animations.remove(&animation);
76     if (is<KeyframeEffect>(animation.effect())) {
77         if (auto* target = downcast<KeyframeEffect>(animation.effect())->target())
78             animationWasRemovedFromElement(animation, *target);
79     }
80 }
81
82 Optional<double> AnimationTimeline::bindingsCurrentTime()
83 {
84     auto time = currentTime();
85     if (!time)
86         return WTF::nullopt;
87     return secondsToWebAnimationsAPITime(*time);
88 }
89
90 void AnimationTimeline::animationWasAddedToElement(WebAnimation& animation, Element& element)
91 {
92     [&] () -> ElementToAnimationsMap& {
93         if (is<CSSTransition>(animation) && downcast<CSSTransition>(animation).owningElement())
94             return m_elementToCSSTransitionsMap;
95         if (is<CSSAnimation>(animation) && downcast<CSSAnimation>(animation).owningElement())
96             return m_elementToCSSAnimationsMap;
97         return m_elementToAnimationsMap;
98     }().ensure(&element, [] {
99         return ListHashSet<RefPtr<WebAnimation>> { };
100     }).iterator->value.add(&animation);
101 }
102
103 static inline bool removeCSSTransitionFromMap(CSSTransition& transition, Element& element, HashMap<Element*, AnimationTimeline::PropertyToTransitionMap>& map)
104 {
105     auto iterator = map.find(&element);
106     if (iterator == map.end())
107         return false;
108
109     auto& cssTransitionsByProperty = iterator->value;
110
111     auto transitionIterator = cssTransitionsByProperty.find(transition.property());
112     if (transitionIterator == cssTransitionsByProperty.end() || transitionIterator->value != &transition)
113         return false;
114
115     cssTransitionsByProperty.remove(transitionIterator);
116
117     if (cssTransitionsByProperty.isEmpty())
118         map.remove(&element);
119     return true;
120 }
121
122 static inline void removeAnimationFromMapForElement(WebAnimation& animation, AnimationTimeline::ElementToAnimationsMap& map, Element& element)
123 {
124     auto iterator = map.find(&element);
125     if (iterator == map.end())
126         return;
127
128     auto& animations = iterator->value;
129     animations.remove(&animation);
130     if (!animations.size())
131         map.remove(iterator);
132 }
133
134 void AnimationTimeline::animationWasRemovedFromElement(WebAnimation& animation, Element& element)
135 {
136     removeAnimationFromMapForElement(animation, m_elementToCSSTransitionsMap, element);
137     removeAnimationFromMapForElement(animation, m_elementToCSSAnimationsMap, element);
138     removeAnimationFromMapForElement(animation, m_elementToAnimationsMap, element);
139
140     // Now, if we're dealing with a declarative animation, we remove it from either the m_elementToCSSAnimationByName
141     // or the m_elementToRunningCSSTransitionByCSSPropertyID map, whichever is relevant to this type of animation.
142     if (is<DeclarativeAnimation>(animation))
143         removeDeclarativeAnimationFromListsForOwningElement(animation, element);
144 }
145
146 void AnimationTimeline::removeDeclarativeAnimationFromListsForOwningElement(WebAnimation& animation, Element& element)
147 {
148     ASSERT(is<DeclarativeAnimation>(animation));
149
150     if (is<CSSAnimation>(animation)) {
151         auto iterator = m_elementToCSSAnimationByName.find(&element);
152         if (iterator != m_elementToCSSAnimationByName.end()) {
153             auto& cssAnimationsByName = iterator->value;
154             auto& name = downcast<CSSAnimation>(animation).animationName();
155             cssAnimationsByName.remove(name);
156             if (cssAnimationsByName.isEmpty())
157                 m_elementToCSSAnimationByName.remove(&element);
158         }
159     } else if (is<CSSTransition>(animation)) {
160         auto& transition = downcast<CSSTransition>(animation);
161         if (!removeCSSTransitionFromMap(transition, element, m_elementToRunningCSSTransitionByCSSPropertyID))
162             removeCSSTransitionFromMap(transition, element, m_elementToCompletedCSSTransitionByCSSPropertyID);
163     }
164 }
165
166 Vector<RefPtr<WebAnimation>> AnimationTimeline::animationsForElement(Element& element, Ordering ordering) const
167 {
168     Vector<RefPtr<WebAnimation>> animations;
169     if (m_elementToCSSTransitionsMap.contains(&element)) {
170         const auto& cssTransitions = m_elementToCSSTransitionsMap.get(&element);
171         if (ordering == Ordering::Sorted) {
172             Vector<RefPtr<WebAnimation>> sortedCSSTransitions;
173             sortedCSSTransitions.appendRange(cssTransitions.begin(), cssTransitions.end());
174             std::sort(sortedCSSTransitions.begin(), sortedCSSTransitions.end(), [](auto& lhs, auto& rhs) {
175                 // Sort transitions first by their generation time, and then by transition-property.
176                 // https://drafts.csswg.org/css-transitions-2/#animation-composite-order
177                 auto* lhsTransition = downcast<CSSTransition>(lhs.get());
178                 auto* rhsTransition = downcast<CSSTransition>(rhs.get());
179                 if (lhsTransition->generationTime() != rhsTransition->generationTime())
180                     return lhsTransition->generationTime() < rhsTransition->generationTime();
181                 return lhsTransition->transitionProperty().utf8() < rhsTransition->transitionProperty().utf8();
182             });
183             animations.appendVector(sortedCSSTransitions);
184         } else
185             animations.appendRange(cssTransitions.begin(), cssTransitions.end());
186     }
187     if (m_elementToCSSAnimationsMap.contains(&element)) {
188         const auto& cssAnimations = m_elementToCSSAnimationsMap.get(&element);
189         animations.appendRange(cssAnimations.begin(), cssAnimations.end());
190     }
191     if (m_elementToAnimationsMap.contains(&element)) {
192         const auto& webAnimations = m_elementToAnimationsMap.get(&element);
193         animations.appendRange(webAnimations.begin(), webAnimations.end());
194     }
195     return animations;
196 }
197
198 void AnimationTimeline::elementWasRemoved(Element& element)
199 {
200     for (auto& cssTransition : m_elementToCSSTransitionsMap.get(&element))
201         cssTransition->cancel(WebAnimation::Silently::Yes);
202     for (auto& cssAnimation : m_elementToCSSAnimationsMap.get(&element))
203         cssAnimation->cancel(WebAnimation::Silently::Yes);
204 }
205
206 void AnimationTimeline::removeAnimationsForElement(Element& element)
207 {
208     for (auto& animation : animationsForElement(element))
209         animation->remove();
210 }
211
212 void AnimationTimeline::cancelDeclarativeAnimationsForElement(Element& element)
213 {
214     for (auto& cssTransition : m_elementToCSSTransitionsMap.get(&element))
215         cssTransition->cancel();
216     for (auto& cssAnimation : m_elementToCSSAnimationsMap.get(&element))
217         cssAnimation->cancel();
218 }
219
220 static bool shouldConsiderAnimation(Element& element, const Animation& animation)
221 {
222     if (!animation.isValidAnimation())
223         return false;
224
225     static NeverDestroyed<const String> animationNameNone(MAKE_STATIC_STRING_IMPL("none"));
226
227     auto& name = animation.name();
228     if (name == animationNameNone || name.isEmpty())
229         return false;
230
231     if (auto* styleScope = Style::Scope::forOrdinal(element, animation.nameStyleScopeOrdinal()))
232         return styleScope->resolver().isAnimationNameValid(name);
233
234     return false;
235 }
236
237 void AnimationTimeline::updateCSSAnimationsForElement(Element& element, const RenderStyle* currentStyle, const RenderStyle& afterChangeStyle)
238 {
239     // In case this element is newly getting a "display: none" we need to cancel all of its animations and disregard new ones.
240     if (currentStyle && currentStyle->hasAnimations() && currentStyle->display() != DisplayType::None && afterChangeStyle.display() == DisplayType::None) {
241         if (m_elementToCSSAnimationByName.contains(&element)) {
242             for (const auto& cssAnimationsByNameMapItem : m_elementToCSSAnimationByName.take(&element))
243                 cancelDeclarativeAnimation(*cssAnimationsByNameMapItem.value);
244         }
245         return;
246     }
247
248     if (currentStyle && currentStyle->hasAnimations() && afterChangeStyle.hasAnimations() && *(currentStyle->animations()) == *(afterChangeStyle.animations()))
249         return;
250
251     // First, compile the list of animation names that were applied to this element up to this point.
252     HashSet<String> namesOfPreviousAnimations;
253     if (currentStyle && currentStyle->hasAnimations()) {
254         auto* previousAnimations = currentStyle->animations();
255         for (size_t i = 0; i < previousAnimations->size(); ++i) {
256             auto& previousAnimation = previousAnimations->animation(i);
257             if (shouldConsiderAnimation(element, previousAnimation))
258                 namesOfPreviousAnimations.add(previousAnimation.name());
259         }
260     }
261
262     // Create or get the CSSAnimations by animation name map for this element.
263     auto& cssAnimationsByName = m_elementToCSSAnimationByName.ensure(&element, [] {
264         return HashMap<String, RefPtr<CSSAnimation>> { };
265     }).iterator->value;
266
267     if (auto* currentAnimations = afterChangeStyle.animations()) {
268         for (size_t i = 0; i < currentAnimations->size(); ++i) {
269             auto& currentAnimation = currentAnimations->animation(i);
270             auto& name = currentAnimation.name();
271             if (namesOfPreviousAnimations.contains(name)) {
272                 // We've found the name of this animation in our list of previous animations, this means we've already
273                 // created a CSSAnimation object for it and need to ensure that this CSSAnimation is backed by the current
274                 // animation object for this animation name.
275                 if (auto cssAnimation = cssAnimationsByName.get(name))
276                     cssAnimation->setBackingAnimation(currentAnimation);
277             } else if (shouldConsiderAnimation(element, currentAnimation)) {
278                 // Otherwise we are dealing with a new animation name and must create a CSSAnimation for it.
279                 cssAnimationsByName.set(name, CSSAnimation::create(element, currentAnimation, currentStyle, afterChangeStyle));
280             }
281             // Remove the name of this animation from our list since it's now known to be current.
282             namesOfPreviousAnimations.remove(name);
283         }
284     }
285
286     // The animations names left in namesOfPreviousAnimations are now known to no longer apply so we need to
287     // remove the CSSAnimation object created for them.
288     for (const auto& nameOfAnimationToRemove : namesOfPreviousAnimations) {
289         if (auto animation = cssAnimationsByName.take(nameOfAnimationToRemove))
290             cancelDeclarativeAnimation(*animation);
291     }
292 }
293
294 RefPtr<WebAnimation> AnimationTimeline::cssAnimationForElementAndProperty(Element& element, CSSPropertyID property)
295 {
296     RefPtr<WebAnimation> matchingAnimation;
297     for (const auto& animation : m_elementToCSSAnimationsMap.get(&element)) {
298         auto* effect = animation->effect();
299         if (is<KeyframeEffect>(effect) && downcast<KeyframeEffect>(effect)->animatedProperties().contains(property))
300             matchingAnimation = animation;
301     }
302     return matchingAnimation;
303 }
304
305 static bool propertyInStyleMatchesValueForTransitionInMap(CSSPropertyID property, const RenderStyle& style, AnimationTimeline::PropertyToTransitionMap& transitions)
306 {
307     if (auto* transition = transitions.get(property)) {
308         if (CSSPropertyAnimation::propertiesEqual(property, &style, &transition->targetStyle()))
309             return true;
310     }
311     return false;
312 }
313
314 static double transitionCombinedDuration(const Animation* transition)
315 {
316     return std::max(0.0, transition->duration()) + transition->delay();
317 }
318
319 static bool transitionMatchesProperty(const Animation& transition, CSSPropertyID property)
320 {
321     auto mode = transition.animationMode();
322     if (mode == Animation::AnimateNone || mode == Animation::AnimateUnknownProperty)
323         return false;
324     if (mode == Animation::AnimateSingleProperty) {
325         auto transitionProperty = transition.property();
326         if (transitionProperty != property) {
327             auto shorthand = shorthandForProperty(transitionProperty);
328             for (size_t i = 0; i < shorthand.length(); ++i) {
329                 if (shorthand.properties()[i] == property)
330                     return true;
331             }
332             return false;
333         }
334     }
335     return true;
336 }
337
338 AnimationTimeline::PropertyToTransitionMap& AnimationTimeline::ensureRunningTransitionsByProperty(Element& element)
339 {
340     return m_elementToRunningCSSTransitionByCSSPropertyID.ensure(&element, [] {
341         return PropertyToTransitionMap { };
342     }).iterator->value;
343 }
344
345 void AnimationTimeline::updateCSSTransitionsForElement(Element& element, const RenderStyle& currentStyle, const RenderStyle& afterChangeStyle)
346 {
347     // In case this element is newly getting a "display: none" we need to cancel all of its transitions and disregard new ones.
348     if (currentStyle.hasTransitions() && currentStyle.display() != DisplayType::None && afterChangeStyle.display() == DisplayType::None) {
349         if (m_elementToRunningCSSTransitionByCSSPropertyID.contains(&element)) {
350             for (const auto& cssTransitionsByCSSPropertyIDMapItem : m_elementToRunningCSSTransitionByCSSPropertyID.take(&element))
351                 cancelDeclarativeAnimation(*cssTransitionsByCSSPropertyIDMapItem.value);
352         }
353         return;
354     }
355
356     // Section 3 "Starting of transitions" from the CSS Transitions Level 1 specification.
357     // https://drafts.csswg.org/css-transitions-1/#starting
358
359     auto& runningTransitionsByProperty = ensureRunningTransitionsByProperty(element);
360
361     auto& completedTransitionsByProperty = m_elementToCompletedCSSTransitionByCSSPropertyID.ensure(&element, [] {
362         return PropertyToTransitionMap { };
363     }).iterator->value;
364
365     auto generationTime = MonotonicTime::now();
366
367     auto numberOfProperties = CSSPropertyAnimation::getNumProperties();
368     for (int propertyIndex = 0; propertyIndex < numberOfProperties; ++propertyIndex) {
369         Optional<bool> isShorthand;
370         auto property = CSSPropertyAnimation::getPropertyAtIndex(propertyIndex, isShorthand);
371         if (isShorthand && *isShorthand)
372             continue;
373
374         const Animation* matchingBackingAnimation = nullptr;
375         if (auto* transitions = afterChangeStyle.transitions()) {
376             for (size_t i = 0; i < transitions->size(); ++i) {
377                 auto& backingAnimation = transitions->animation(i);
378                 if (transitionMatchesProperty(backingAnimation, property))
379                     matchingBackingAnimation = &backingAnimation;
380             }
381         }
382
383         // https://drafts.csswg.org/css-transitions-1/#before-change-style
384         // Define the before-change style as the computed values of all properties on the element as of the previous style change event, except with
385         // any styles derived from declarative animations such as CSS Transitions, CSS Animations, and SMIL Animations updated to the current time.
386         auto existingAnimation = cssAnimationForElementAndProperty(element, property);
387         const auto& beforeChangeStyle = existingAnimation ? downcast<CSSAnimation>(existingAnimation.get())->unanimatedStyle() : currentStyle;
388
389         if (!runningTransitionsByProperty.contains(property)
390             && !CSSPropertyAnimation::propertiesEqual(property, &beforeChangeStyle, &afterChangeStyle)
391             && CSSPropertyAnimation::canPropertyBeInterpolated(property, &beforeChangeStyle, &afterChangeStyle)
392             && !propertyInStyleMatchesValueForTransitionInMap(property, afterChangeStyle, completedTransitionsByProperty)
393             && matchingBackingAnimation && transitionCombinedDuration(matchingBackingAnimation) > 0) {
394             // 1. If all of the following are true:
395             //   - the element does not have a running transition for the property,
396             //   - the before-change style is different from and can be interpolated with the after-change style for that property,
397             //   - the element does not have a completed transition for the property or the end value of the completed transition is different from the after-change style for the property,
398             //   - there is a matching transition-property value, and
399             //   - the combined duration is greater than 0s,
400
401             // then implementations must remove the completed transition (if present) from the set of completed transitions
402             completedTransitionsByProperty.remove(property);
403
404             // and start a transition whose:
405             //   - start time is the time of the style change event plus the matching transition delay,
406             //   - end time is the start time plus the matching transition duration,
407             //   - start value is the value of the transitioning property in the before-change style,
408             //   - end value is the value of the transitioning property in the after-change style,
409             //   - reversing-adjusted start value is the same as the start value, and
410             //   - reversing shortening factor is 1.
411             auto delay = Seconds(matchingBackingAnimation->delay());
412             auto duration = Seconds(matchingBackingAnimation->duration());
413             auto& reversingAdjustedStartStyle = beforeChangeStyle;
414             auto reversingShorteningFactor = 1;
415             runningTransitionsByProperty.set(property, CSSTransition::create(element, property, generationTime, *matchingBackingAnimation, &beforeChangeStyle, afterChangeStyle, delay, duration, reversingAdjustedStartStyle, reversingShorteningFactor));
416         } else if (completedTransitionsByProperty.contains(property) && !propertyInStyleMatchesValueForTransitionInMap(property, afterChangeStyle, completedTransitionsByProperty)) {
417             // 2. Otherwise, if the element has a completed transition for the property and the end value of the completed transition is different from
418             //    the after-change style for the property, then implementations must remove the completed transition from the set of completed transitions.
419             completedTransitionsByProperty.remove(property);
420         }
421
422         bool hasRunningTransition = runningTransitionsByProperty.contains(property);
423         if ((hasRunningTransition || completedTransitionsByProperty.contains(property)) && !matchingBackingAnimation) {
424             // 3. If the element has a running transition or completed transition for the property, and there is not a matching transition-property
425             //    value, then implementations must cancel the running transition or remove the completed transition from the set of completed transitions.
426             if (hasRunningTransition)
427                 runningTransitionsByProperty.take(property)->cancel();
428             else
429                 completedTransitionsByProperty.remove(property);
430         }
431
432         if (matchingBackingAnimation && runningTransitionsByProperty.contains(property) && !propertyInStyleMatchesValueForTransitionInMap(property, afterChangeStyle, runningTransitionsByProperty)) {
433             auto previouslyRunningTransition = runningTransitionsByProperty.take(property);
434             auto& previouslyRunningTransitionCurrentStyle = previouslyRunningTransition->currentStyle();
435             // 4. If the element has a running transition for the property, there is a matching transition-property value, and the end value of the running
436             //    transition is not equal to the value of the property in the after-change style, then:
437             if (CSSPropertyAnimation::propertiesEqual(property, &previouslyRunningTransitionCurrentStyle, &afterChangeStyle) || !CSSPropertyAnimation::canPropertyBeInterpolated(property, &currentStyle, &afterChangeStyle)) {
438                 // 1. If the current value of the property in the running transition is equal to the value of the property in the after-change style,
439                 //    or if these two values cannot be interpolated, then implementations must cancel the running transition.
440                 cancelDeclarativeAnimation(*previouslyRunningTransition);
441             } else if (transitionCombinedDuration(matchingBackingAnimation) <= 0.0 || !CSSPropertyAnimation::canPropertyBeInterpolated(property, &previouslyRunningTransitionCurrentStyle, &afterChangeStyle)) {
442                 // 2. Otherwise, if the combined duration is less than or equal to 0s, or if the current value of the property in the running transition
443                 //    cannot be interpolated with the value of the property in the after-change style, then implementations must cancel the running transition.
444                 cancelDeclarativeAnimation(*previouslyRunningTransition);
445             } else if (CSSPropertyAnimation::propertiesEqual(property, &previouslyRunningTransition->reversingAdjustedStartStyle(), &afterChangeStyle)) {
446                 // 3. Otherwise, if the reversing-adjusted start value of the running transition is the same as the value of the property in the after-change
447                 //    style (see the section on reversing of transitions for why these case exists), implementations must cancel the running transition
448                 cancelDeclarativeAnimation(*previouslyRunningTransition);
449
450                 // and start a new transition whose:
451                 //   - reversing-adjusted start value is the end value of the running transition,
452                 //   - reversing shortening factor is the absolute value, clamped to the range [0, 1], of the sum of:
453                 //       1. the output of the timing function of the old transition at the time of the style change event, times the reversing shortening factor of the old transition
454                 //       2. 1 minus the reversing shortening factor of the old transition.
455                 //   - start time is the time of the style change event plus:
456                 //       1. if the matching transition delay is nonnegative, the matching transition delay, or
457                 //       2. if the matching transition delay is negative, the product of the new transition’s reversing shortening factor and the matching transition delay,
458                 //   - end time is the start time plus the product of the matching transition duration and the new transition’s reversing shortening factor,
459                 //   - start value is the current value of the property in the running transition,
460                 //   - end value is the value of the property in the after-change style
461                 auto& reversingAdjustedStartStyle = previouslyRunningTransition->targetStyle();
462                 double transformedProgress = 1;
463                 if (auto* effect = previouslyRunningTransition->effect()) {
464                     if (auto computedTimingProgress = effect->getComputedTiming().progress)
465                         transformedProgress = *computedTimingProgress;
466                 }
467                 auto reversingShorteningFactor = std::max(std::min(((transformedProgress * previouslyRunningTransition->reversingShorteningFactor()) + (1 - previouslyRunningTransition->reversingShorteningFactor())), 1.0), 0.0);
468                 auto delay = matchingBackingAnimation->delay() < 0 ? Seconds(matchingBackingAnimation->delay()) * reversingShorteningFactor : Seconds(matchingBackingAnimation->delay());
469                 auto duration = Seconds(matchingBackingAnimation->duration()) * reversingShorteningFactor;
470
471                 ensureRunningTransitionsByProperty(element).set(property, CSSTransition::create(element, property, generationTime, *matchingBackingAnimation, &previouslyRunningTransitionCurrentStyle, afterChangeStyle, delay, duration, reversingAdjustedStartStyle, reversingShorteningFactor));
472             } else {
473                 // 4. Otherwise, implementations must cancel the running transition
474                 cancelDeclarativeAnimation(*previouslyRunningTransition);
475
476                 // and start a new transition whose:
477                 //   - start time is the time of the style change event plus the matching transition delay,
478                 //   - end time is the start time plus the matching transition duration,
479                 //   - start value is the current value of the property in the running transition,
480                 //   - end value is the value of the property in the after-change style,
481                 //   - reversing-adjusted start value is the same as the start value, and
482                 //   - reversing shortening factor is 1.
483                 auto delay = Seconds(matchingBackingAnimation->delay());
484                 auto duration = Seconds(matchingBackingAnimation->duration());
485                 auto& reversingAdjustedStartStyle = currentStyle;
486                 auto reversingShorteningFactor = 1;
487                 ensureRunningTransitionsByProperty(element).set(property, CSSTransition::create(element, property, generationTime, *matchingBackingAnimation, &previouslyRunningTransitionCurrentStyle, afterChangeStyle, delay, duration, reversingAdjustedStartStyle, reversingShorteningFactor));
488             }
489         }
490     }
491 }
492
493 void AnimationTimeline::cancelDeclarativeAnimation(DeclarativeAnimation& animation)
494 {
495     animation.cancelFromStyle();
496     removeAnimation(animation);
497     m_allAnimations.removeFirst(&animation);
498 }
499
500 } // namespace WebCore