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 "core/animation/DocumentTimeline.h" 35#include "core/css/RuntimeCSSEnabled.h" 36#include "core/css/resolver/StyleResolver.h" 37#include "wtf/text/StringBuilder.h" 38#include <algorithm> 39 40namespace WebCore { 41 42CSSPropertyID ElementAnimation::camelCaseCSSPropertyNameToID(const String& propertyName) 43{ 44 if (propertyName.find('-') != kNotFound) 45 return CSSPropertyInvalid; 46 47 StringBuilder builder; 48 size_t position = 0; 49 size_t end; 50 while ((end = propertyName.find(isASCIIUpper, position)) != kNotFound) { 51 builder.append(propertyName.substring(position, end - position) + "-" + toASCIILower((propertyName)[end])); 52 position = end + 1; 53 } 54 builder.append(propertyName.substring(position)); 55 // Doesn't handle prefixed properties. 56 CSSPropertyID id = cssPropertyID(builder.toString()); 57 return id; 58} 59 60void ElementAnimation::animate(Element* element, Vector<Dictionary> keyframeDictionaryVector, double duration) 61{ 62 ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled()); 63 64 // FIXME: This test will not be neccessary once resolution of keyframe values occurs at 65 // animation application time. 66 if (!element->inActiveDocument()) 67 return; 68 element->document().updateStyleIfNeeded(); 69 if (!element->renderer()) 70 return; 71 72 startAnimation(element, keyframeDictionaryVector, duration); 73} 74 75void ElementAnimation::startAnimation(Element* element, Vector<Dictionary> keyframeDictionaryVector, double duration) 76{ 77 KeyframeAnimationEffect::KeyframeVector keyframes; 78 Vector<RefPtr<MutableStylePropertySet> > propertySetVector; 79 80 for (size_t i = 0; i < keyframeDictionaryVector.size(); ++i) { 81 RefPtr<MutableStylePropertySet> propertySet = MutableStylePropertySet::create(); 82 propertySetVector.append(propertySet); 83 84 RefPtr<Keyframe> keyframe = Keyframe::create(); 85 keyframes.append(keyframe); 86 87 double offset; 88 if (keyframeDictionaryVector[i].get("offset", offset)) { 89 keyframe->setOffset(offset); 90 } else { 91 // FIXME: Web Animations CSS engine does not yet implement handling of 92 // keyframes without specified offsets. This check can be removed when 93 // that funcitonality is implemented. 94 ASSERT_NOT_REACHED(); 95 return; 96 } 97 98 String compositeString; 99 keyframeDictionaryVector[i].get("composite", compositeString); 100 if (compositeString == "add") 101 keyframe->setComposite(AnimationEffect::CompositeAdd); 102 103 Vector<String> keyframeProperties; 104 keyframeDictionaryVector[i].getOwnPropertyNames(keyframeProperties); 105 106 for (size_t j = 0; j < keyframeProperties.size(); ++j) { 107 String property = keyframeProperties[j]; 108 CSSPropertyID id = camelCaseCSSPropertyNameToID(property); 109 110 // FIXME: There is no way to store invalid properties or invalid values 111 // in a Keyframe object, so for now I just skip over them. Eventually we 112 // will need to support getFrames(), which should return exactly the 113 // keyframes that were input through the API. We will add a layer to wrap 114 // KeyframeAnimationEffect, store input keyframes and implement getFrames. 115 if (id == CSSPropertyInvalid || !CSSAnimations::isAnimatableProperty(id)) 116 continue; 117 118 String value; 119 keyframeDictionaryVector[i].get(property, value); 120 propertySet->setProperty(id, value); 121 } 122 } 123 124 // FIXME: Replace this with code that just parses, when that code is available. 125 RefPtr<KeyframeAnimationEffect> effect = StyleResolver::createKeyframeAnimationEffect(*element, propertySetVector, keyframes); 126 127 // FIXME: Totally hardcoded Timing for now. Will handle timing parameters later. 128 Timing timing; 129 // FIXME: Currently there is no way to tell whether or not an iterationDuration 130 // has been specified (becauser the default argument is 0). So any animation 131 // created using Element.animate() will have a timing with hasIterationDuration() 132 // == true. 133 timing.hasIterationDuration = true; 134 timing.iterationDuration = std::max<double>(duration, 0); 135 136 RefPtr<Animation> animation = Animation::create(element, effect, timing); 137 DocumentTimeline* timeline = element->document().timeline(); 138 ASSERT(timeline); 139 timeline->play(animation.get()); 140} 141 142} // namespace WebCore 143