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/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"
46 PassRefPtrWillBeRawPtr<AnimationEffect> EffectInput::convert(Element* element, const Vector<Dictionary>& keyframeDictionaryVector, ExceptionState& exceptionState)
48 // FIXME: Remove the dependency on element.
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();
58 for (size_t i = 0; i < keyframeDictionaryVector.size(); ++i) {
59 RefPtrWillBeRawPtr<StringKeyframe> keyframe = StringKeyframe::create();
61 bool frameHasOffset = false;
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)
68 frameHasOffset = true;
69 // The JS value null gets converted to 0 so we need to check whether the original value is null.
71 ScriptValue scriptValue;
72 if (keyframeDictionaryVector[i].get("offset", scriptValue) && scriptValue.isNull())
73 frameHasOffset = false;
76 keyframe->setOffset(offset);
77 if (offset < lastOffset)
78 looselySortedByOffset = false;
82 everyFrameHasOffset = everyFrameHasOffset && frameHasOffset;
84 keyframes.append(keyframe);
86 String compositeString;
87 keyframeDictionaryVector[i].get("composite", compositeString);
88 if (compositeString == "add")
89 keyframe->setComposite(AnimationEffect::CompositeAdd);
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));
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)
105 keyframeDictionaryVector[i].get(property, value);
106 keyframe->setPropertyValue(id, value, styleSheetContents);
110 if (!looselySortedByOffset) {
111 if (!everyFrameHasOffset) {
112 exceptionState.throwDOMException(InvalidModificationError, "Keyframes are not loosely sorted by offset.");
115 nonCopyingSort(keyframes.begin(), keyframes.end(), Keyframe::compareOffsets);
118 RefPtrWillBeRawPtr<StringKeyframeEffectModel> keyframeEffectModel = StringKeyframeEffectModel::create(keyframes);
119 if (!keyframeEffectModel->isReplaceOnly()) {
120 exceptionState.throwDOMException(NotSupportedError, "Partial keyframes are not supported.");
123 keyframeEffectModel->forceConversionsToAnimatableValues(element);
125 return keyframeEffectModel;
128 } // namespace WebCore