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/core/v8/Dictionary.h"
35#include "core/animation/AnimationInputHelpers.h"
36#include "core/animation/KeyframeEffectModel.h"
37#include "core/animation/StringKeyframe.h"
38#include "core/css/resolver/StyleResolver.h"
39#include "core/dom/Document.h"
40#include "core/dom/Element.h"
41#include "wtf/NonCopyingSort.h"
42
43namespace blink {
44
45PassRefPtrWillBeRawPtr<AnimationEffect> EffectInput::convert(Element* element, const Vector<Dictionary>& keyframeDictionaryVector, ExceptionState& exceptionState)
46{
47    // FIXME: Remove the dependency on element.
48    if (!element)
49        return nullptr;
50
51    StyleSheetContents* styleSheetContents = element->document().elementSheet().contents();
52    StringKeyframeVector keyframes;
53    double lastOffset = 0;
54
55    for (size_t i = 0; i < keyframeDictionaryVector.size(); ++i) {
56        RefPtrWillBeRawPtr<StringKeyframe> keyframe = StringKeyframe::create();
57
58        ScriptValue scriptValue;
59        bool frameHasOffset = DictionaryHelper::get(keyframeDictionaryVector[i], "offset", scriptValue) && !scriptValue.isNull();
60
61        if (frameHasOffset) {
62            double offset;
63            DictionaryHelper::get(keyframeDictionaryVector[i], "offset", offset);
64
65            // Keyframes with offsets outside the range [0.0, 1.0] are an error.
66            if (std::isnan(offset)) {
67                exceptionState.throwDOMException(InvalidModificationError, "Non numeric offset provided");
68            }
69
70            if (offset < 0 || offset > 1) {
71                exceptionState.throwDOMException(InvalidModificationError, "Offsets provided outside the range [0, 1]");
72                return nullptr;
73            }
74
75            if (offset < lastOffset) {
76                exceptionState.throwDOMException(InvalidModificationError, "Keyframes with specified offsets are not sorted");
77                return nullptr;
78            }
79
80            lastOffset = offset;
81
82            keyframe->setOffset(offset);
83        }
84        keyframes.append(keyframe);
85
86        String compositeString;
87        DictionaryHelper::get(keyframeDictionaryVector[i], "composite", compositeString);
88        if (compositeString == "add")
89            keyframe->setComposite(AnimationEffect::CompositeAdd);
90
91        String timingFunctionString;
92        if (DictionaryHelper::get(keyframeDictionaryVector[i], "easing", timingFunctionString)) {
93            if (RefPtr<TimingFunction> timingFunction = AnimationInputHelpers::parseTimingFunction(timingFunctionString))
94                keyframe->setEasing(timingFunction);
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 = AnimationInputHelpers::keyframeAttributeToCSSPropertyID(property);
102            if (id == CSSPropertyInvalid)
103                continue;
104            String value;
105            DictionaryHelper::get(keyframeDictionaryVector[i], property, value);
106            keyframe->setPropertyValue(id, value, styleSheetContents);
107        }
108    }
109
110    RefPtrWillBeRawPtr<StringKeyframeEffectModel> keyframeEffectModel = StringKeyframeEffectModel::create(keyframes);
111    if (!keyframeEffectModel->isReplaceOnly()) {
112        exceptionState.throwDOMException(NotSupportedError, "Partial keyframes are not supported.");
113        return nullptr;
114    }
115    keyframeEffectModel->forceConversionsToAnimatableValues(element);
116
117    return keyframeEffectModel;
118}
119
120} // namespace blink
121