2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/animation/EffectInput.h"
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"
45 PassRefPtrWillBeRawPtr<AnimationEffect> EffectInput::convert(Element* element, const Vector<Dictionary>& keyframeDictionaryVector, ExceptionState& exceptionState)
47 // FIXME: Remove the dependency on element.
51 StyleSheetContents* styleSheetContents = element->document().elementSheet().contents();
52 StringKeyframeVector keyframes;
53 double lastOffset = 0;
55 for (size_t i = 0; i < keyframeDictionaryVector.size(); ++i) {
56 RefPtrWillBeRawPtr<StringKeyframe> keyframe = StringKeyframe::create();
58 ScriptValue scriptValue;
59 bool frameHasOffset = DictionaryHelper::get(keyframeDictionaryVector[i], "offset", scriptValue) && !scriptValue.isNull();
63 DictionaryHelper::get(keyframeDictionaryVector[i], "offset", offset);
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");
70 if (offset < 0 || offset > 1) {
71 exceptionState.throwDOMException(InvalidModificationError, "Offsets provided outside the range [0, 1]");
75 if (offset < lastOffset) {
76 exceptionState.throwDOMException(InvalidModificationError, "Keyframes with specified offsets are not sorted");
82 keyframe->setOffset(offset);
84 keyframes.append(keyframe);
86 String compositeString;
87 DictionaryHelper::get(keyframeDictionaryVector[i], "composite", compositeString);
88 if (compositeString == "add")
89 keyframe->setComposite(AnimationEffect::CompositeAdd);
91 String timingFunctionString;
92 if (DictionaryHelper::get(keyframeDictionaryVector[i], "easing", timingFunctionString)) {
93 if (RefPtr<TimingFunction> timingFunction = AnimationInputHelpers::parseTimingFunction(timingFunctionString))
94 keyframe->setEasing(timingFunction);
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)
105 DictionaryHelper::get(keyframeDictionaryVector[i], property, value);
106 keyframe->setPropertyValue(id, value, styleSheetContents);
110 RefPtrWillBeRawPtr<StringKeyframeEffectModel> keyframeEffectModel = StringKeyframeEffectModel::create(keyframes);
111 if (!keyframeEffectModel->isReplaceOnly()) {
112 exceptionState.throwDOMException(NotSupportedError, "Partial keyframes are not supported.");
115 keyframeEffectModel->forceConversionsToAnimatableValues(element);
117 return keyframeEffectModel;