466052c1346a2b9adb748ab422339eb0f68af214
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / animation / EffectInput.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/EffectInput.h"
33
34 #include "bindings/v8/Dictionary.h"
35 #include "core/animation/AnimationHelpers.h"
36 #include "core/animation/KeyframeEffectModel.h"
37 #include "core/animation/StringKeyframe.h"
38 #include "core/css/parser/BisonCSSParser.h"
39 #include "core/css/resolver/CSSToStyleMap.h"
40 #include "core/css/resolver/StyleResolver.h"
41 #include "core/dom/Element.h"
42 #include "wtf/NonCopyingSort.h"
43
44 namespace WebCore {
45
46 PassRefPtrWillBeRawPtr<AnimationEffect> EffectInput::convert(Element* element, const Vector<Dictionary>& keyframeDictionaryVector, ExceptionState& exceptionState)
47 {
48     // FIXME: Remove the dependency on element.
49     if (!element)
50         return nullptr;
51
52     StyleSheetContents* styleSheetContents = element->document().elementSheet().contents();
53     StringKeyframeVector keyframes;
54     bool everyFrameHasOffset = true;
55     bool looselySortedByOffset = true;
56     double lastOffset = -std::numeric_limits<double>::infinity();
57
58     for (size_t i = 0; i < keyframeDictionaryVector.size(); ++i) {
59         RefPtrWillBeRawPtr<StringKeyframe> keyframe = StringKeyframe::create();
60
61         bool frameHasOffset = false;
62         double offset;
63         if (keyframeDictionaryVector[i].get("offset", offset)) {
64             // Keyframes with offsets outside the range [0.0, 1.0] are ignored.
65             if (std::isnan(offset) || offset < 0 || offset > 1)
66                 continue;
67
68             frameHasOffset = true;
69             // The JS value null gets converted to 0 so we need to check whether the original value is null.
70             if (offset == 0) {
71                 ScriptValue scriptValue;
72                 if (keyframeDictionaryVector[i].get("offset", scriptValue) && scriptValue.isNull())
73                     frameHasOffset = false;
74             }
75             if (frameHasOffset) {
76                 keyframe->setOffset(offset);
77                 if (offset < lastOffset)
78                     looselySortedByOffset = false;
79                 lastOffset = offset;
80             }
81         }
82         everyFrameHasOffset = everyFrameHasOffset && frameHasOffset;
83
84         keyframes.append(keyframe);
85
86         String compositeString;
87         keyframeDictionaryVector[i].get("composite", compositeString);
88         if (compositeString == "add")
89             keyframe->setComposite(AnimationEffect::CompositeAdd);
90
91         String timingFunctionString;
92         if (keyframeDictionaryVector[i].get("easing", timingFunctionString)) {
93             if (RefPtrWillBeRawPtr<CSSValue> timingFunctionValue = BisonCSSParser::parseAnimationTimingFunctionValue(timingFunctionString))
94                 keyframe->setEasing(CSSToStyleMap::mapAnimationTimingFunction(timingFunctionValue.get(), true));
95         }
96
97         Vector<String> keyframeProperties;
98         keyframeDictionaryVector[i].getOwnPropertyNames(keyframeProperties);
99         for (size_t j = 0; j < keyframeProperties.size(); ++j) {
100             String property = keyframeProperties[j];
101             CSSPropertyID id = camelCaseCSSPropertyNameToID(property);
102             if (id == CSSPropertyInvalid)
103                 continue;
104             String value;
105             keyframeDictionaryVector[i].get(property, value);
106             keyframe->setPropertyValue(id, value, styleSheetContents);
107         }
108     }
109
110     if (!looselySortedByOffset) {
111         if (!everyFrameHasOffset) {
112             exceptionState.throwDOMException(InvalidModificationError, "Keyframes are not loosely sorted by offset.");
113             return nullptr;
114         }
115         nonCopyingSort(keyframes.begin(), keyframes.end(), Keyframe::compareOffsets);
116     }
117
118     RefPtrWillBeRawPtr<StringKeyframeEffectModel> keyframeEffectModel = StringKeyframeEffectModel::create(keyframes);
119     if (!keyframeEffectModel->isReplaceOnly()) {
120         exceptionState.throwDOMException(NotSupportedError, "Partial keyframes are not supported.");
121         return nullptr;
122     }
123     keyframeEffectModel->forceConversionsToAnimatableValues(element);
124
125     return keyframeEffectModel;
126 }
127
128 } // namespace WebCore