e5143b9de27edb1659738d2a6f45414d8a3428fd
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / animation / ElementAnimation.cpp
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "core/animation/ElementAnimation.h"
33
34 #include "bindings/v8/Dictionary.h"
35 #include "bindings/v8/ScriptValue.h"
36 #include "core/animation/DocumentTimeline.h"
37 #include "core/animation/css/CSSAnimations.h"
38 #include "core/css/parser/BisonCSSParser.h"
39 #include "core/css/resolver/StyleResolver.h"
40 #include "wtf/text/StringBuilder.h"
41 #include <algorithm>
42
43 namespace WebCore {
44
45 CSSPropertyID ElementAnimation::camelCaseCSSPropertyNameToID(const String& propertyName)
46 {
47     if (propertyName.find('-') != kNotFound)
48         return CSSPropertyInvalid;
49
50     StringBuilder builder;
51     size_t position = 0;
52     size_t end;
53     while ((end = propertyName.find(isASCIIUpper, position)) != kNotFound) {
54         builder.append(propertyName.substring(position, end - position) + "-" + toASCIILower((propertyName)[end]));
55         position = end + 1;
56     }
57     builder.append(propertyName.substring(position));
58     // Doesn't handle prefixed properties.
59     CSSPropertyID id = cssPropertyID(builder.toString());
60     return id;
61 }
62
63 void ElementAnimation::populateTiming(Timing& timing, Dictionary timingInputDictionary)
64 {
65     // FIXME: This method needs to be refactored to handle invalid
66     // null, NaN, Infinity values better.
67     // See: http://www.w3.org/TR/WebIDL/#es-double
68     double startDelay = 0;
69     timingInputDictionary.get("delay", startDelay);
70     if (!std::isnan(startDelay) && !std::isinf(startDelay))
71         timing.startDelay = startDelay;
72
73     String fillMode;
74     timingInputDictionary.get("fill", fillMode);
75     if (fillMode == "none") {
76         timing.fillMode = Timing::FillModeNone;
77     } else if (fillMode == "backwards") {
78         timing.fillMode = Timing::FillModeBackwards;
79     } else if (fillMode == "both") {
80         timing.fillMode = Timing::FillModeBoth;
81     } else if (fillMode == "forwards") {
82         timing.fillMode = Timing::FillModeForwards;
83     }
84
85     double iterationStart = 0;
86     timingInputDictionary.get("iterationStart", iterationStart);
87     if (!std::isnan(iterationStart) && !std::isinf(iterationStart))
88         timing.iterationStart = std::max<double>(iterationStart, 0);
89
90     double iterationCount = 1;
91     timingInputDictionary.get("iterations", iterationCount);
92     if (!std::isnan(iterationCount))
93         timing.iterationCount = std::max<double>(iterationCount, 0);
94
95     v8::Local<v8::Value> iterationDurationValue;
96     bool hasIterationDurationValue = timingInputDictionary.get("duration", iterationDurationValue);
97     if (hasIterationDurationValue) {
98         double iterationDuration = iterationDurationValue->NumberValue();
99         if (!std::isnan(iterationDuration) && iterationDuration >= 0) {
100             timing.iterationDuration = iterationDuration;
101             timing.hasIterationDuration = true;
102         }
103     }
104
105     double playbackRate = 1;
106     timingInputDictionary.get("playbackRate", playbackRate);
107     if (!std::isnan(playbackRate) && !std::isinf(playbackRate))
108         timing.playbackRate = playbackRate;
109
110     String direction;
111     timingInputDictionary.get("direction", direction);
112     if (direction == "reverse") {
113         timing.direction = Timing::PlaybackDirectionReverse;
114     } else if (direction == "alternate") {
115         timing.direction = Timing::PlaybackDirectionAlternate;
116     } else if (direction == "alternate-reverse") {
117         timing.direction = Timing::PlaybackDirectionAlternateReverse;
118     }
119
120     timing.assertValid();
121 }
122
123 static bool checkDocumentAndRenderer(Element* element)
124 {
125     if (!element->inActiveDocument())
126         return false;
127     element->document().updateStyleIfNeeded();
128     if (!element->renderer())
129         return false;
130     return true;
131 }
132
133 Animation* ElementAnimation::animate(Element* element, Vector<Dictionary> keyframeDictionaryVector, Dictionary timingInput)
134 {
135     ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled());
136
137     // FIXME: This test will not be neccessary once resolution of keyframe values occurs at
138     // animation application time.
139     if (!checkDocumentAndRenderer(element))
140         return 0;
141
142     return startAnimation(element, keyframeDictionaryVector, timingInput);
143 }
144
145 Animation* ElementAnimation::animate(Element* element, Vector<Dictionary> keyframeDictionaryVector, double timingInput)
146 {
147     ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled());
148
149     // FIXME: This test will not be neccessary once resolution of keyframe values occurs at
150     // animation application time.
151     if (!checkDocumentAndRenderer(element))
152         return 0;
153
154     return startAnimation(element, keyframeDictionaryVector, timingInput);
155 }
156
157 Animation* ElementAnimation::animate(Element* element, Vector<Dictionary> keyframeDictionaryVector)
158 {
159     ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled());
160
161     // FIXME: This test will not be neccessary once resolution of keyframe values occurs at
162     // animation application time.
163     if (!checkDocumentAndRenderer(element))
164         return 0;
165
166     return startAnimation(element, keyframeDictionaryVector);
167 }
168
169 static PassRefPtr<KeyframeEffectModel> createKeyframeEffectModel(Element* element, Vector<Dictionary> keyframeDictionaryVector)
170 {
171     KeyframeEffectModel::KeyframeVector keyframes;
172     Vector<RefPtr<MutableStylePropertySet> > propertySetVector;
173
174     for (size_t i = 0; i < keyframeDictionaryVector.size(); ++i) {
175         RefPtr<MutableStylePropertySet> propertySet = MutableStylePropertySet::create();
176         propertySetVector.append(propertySet);
177
178         RefPtr<Keyframe> keyframe = Keyframe::create();
179         keyframes.append(keyframe);
180
181         double offset;
182         if (keyframeDictionaryVector[i].get("offset", offset)) {
183             keyframe->setOffset(offset);
184         }
185
186         String compositeString;
187         keyframeDictionaryVector[i].get("composite", compositeString);
188         if (compositeString == "add")
189             keyframe->setComposite(AnimationEffect::CompositeAdd);
190
191         Vector<String> keyframeProperties;
192         keyframeDictionaryVector[i].getOwnPropertyNames(keyframeProperties);
193
194         for (size_t j = 0; j < keyframeProperties.size(); ++j) {
195             String property = keyframeProperties[j];
196             CSSPropertyID id = ElementAnimation::camelCaseCSSPropertyNameToID(property);
197
198             // FIXME: There is no way to store invalid properties or invalid values
199             // in a Keyframe object, so for now I just skip over them. Eventually we
200             // will need to support getFrames(), which should return exactly the
201             // keyframes that were input through the API. We will add a layer to wrap
202             // KeyframeEffectModel, store input keyframes and implement getFrames.
203             if (id == CSSPropertyInvalid || !CSSAnimations::isAnimatableProperty(id))
204                 continue;
205
206             String value;
207             keyframeDictionaryVector[i].get(property, value);
208             propertySet->setProperty(id, value);
209         }
210     }
211
212     // FIXME: Replace this with code that just parses, when that code is available.
213     RefPtr<KeyframeEffectModel> effect = StyleResolver::createKeyframeEffectModel(*element, propertySetVector, keyframes);
214     return effect;
215 }
216
217 Animation* ElementAnimation::startAnimation(Element* element, Vector<Dictionary> keyframeDictionaryVector, Dictionary timingInput)
218 {
219     RefPtr<KeyframeEffectModel> effect = createKeyframeEffectModel(element, keyframeDictionaryVector);
220
221     Timing timing;
222     populateTiming(timing, timingInput);
223
224     RefPtr<Animation> animation = Animation::create(element, effect, timing);
225     DocumentTimeline* timeline = element->document().timeline();
226     ASSERT(timeline);
227     timeline->play(animation.get());
228
229     return animation.get();
230 }
231
232 Animation* ElementAnimation::startAnimation(Element* element, Vector<Dictionary> keyframeDictionaryVector, double timingInput)
233 {
234     RefPtr<KeyframeEffectModel> effect = createKeyframeEffectModel(element, keyframeDictionaryVector);
235
236     Timing timing;
237     if (!std::isnan(timingInput)) {
238         timing.hasIterationDuration = true;
239         timing.iterationDuration = std::max<double>(timingInput, 0);
240     }
241
242     RefPtr<Animation> animation = Animation::create(element, effect, timing);
243     DocumentTimeline* timeline = element->document().timeline();
244     ASSERT(timeline);
245     timeline->play(animation.get());
246
247     return animation.get();
248 }
249
250 Animation* ElementAnimation::startAnimation(Element* element, Vector<Dictionary> keyframeDictionaryVector)
251 {
252     RefPtr<KeyframeEffectModel> effect = createKeyframeEffectModel(element, keyframeDictionaryVector);
253
254     Timing timing;
255
256     RefPtr<Animation> animation = Animation::create(element, effect, timing);
257     DocumentTimeline* timeline = element->document().timeline();
258     ASSERT(timeline);
259     timeline->play(animation.get());
260
261     return animation.get();
262 }
263
264 } // namespace WebCore