This source file includes following definitions.
- m_animatedPropertyType
- create
- hasValidAttributeType
- calculateAnimatedValue
- calculateToAtEndOfDurationValue
- calculateFromAndToValues
- calculateFromAndByValues
- findElementInstances
- resetAnimatedType
- applyCSSPropertyToTarget
- removeCSSPropertyFromTarget
- applyCSSPropertyToTargetAndInstances
- removeCSSPropertyFromTargetAndInstances
- notifyTargetAboutAnimValChange
- notifyTargetAndInstancesAboutAnimValChange
- clearAnimatedType
- applyResultsToTarget
- animatedPropertyTypeSupportsAddition
- isAdditive
- calculateDistance
- setTargetElement
- setAttributeName
- resetAnimatedPropertyType
- ensureAnimator
#include "config.h"
#include "core/svg/SVGAnimateElement.h"
#include "CSSPropertyNames.h"
#include "core/css/parser/BisonCSSParser.h"
#include "core/css/StylePropertySet.h"
#include "core/dom/QualifiedName.h"
#include "core/svg/SVGAnimatedTypeAnimator.h"
#include "core/svg/SVGDocumentExtensions.h"
#include "core/svg/SVGElementInstance.h"
namespace WebCore {
SVGAnimateElement::SVGAnimateElement(const QualifiedName& tagName, Document& document)
: SVGAnimationElement(tagName, document)
, m_animatedPropertyType(AnimatedString)
{
ASSERT(isSVGAnimateElement(*this));
ScriptWrappable::init(this);
}
PassRefPtr<SVGAnimateElement> SVGAnimateElement::create(Document& document)
{
return adoptRef(new SVGAnimateElement(SVGNames::animateTag, document));
}
SVGAnimateElement::~SVGAnimateElement()
{
if (targetElement())
clearAnimatedType(targetElement());
}
bool SVGAnimateElement::hasValidAttributeType()
{
SVGElement* targetElement = this->targetElement();
if (!targetElement)
return false;
return m_animatedPropertyType != AnimatedUnknown && !hasInvalidCSSAttributeType();
}
void SVGAnimateElement::calculateAnimatedValue(float percentage, unsigned repeatCount, SVGSMILElement* resultElement)
{
ASSERT(resultElement);
SVGElement* targetElement = this->targetElement();
if (!targetElement || !isSVGAnimateElement(*resultElement))
return;
ASSERT(m_animatedPropertyType == determineAnimatedPropertyType());
ASSERT(percentage >= 0 && percentage <= 1);
ASSERT(m_animatedPropertyType != AnimatedTransformList || isSVGAnimateTransformElement(*this));
ASSERT(m_animatedPropertyType != AnimatedUnknown);
ASSERT(m_animator);
ASSERT(m_animator->type() == m_animatedPropertyType);
ASSERT(m_fromProperty);
ASSERT(m_fromProperty->type() == m_animatedPropertyType);
ASSERT(m_toProperty);
SVGAnimateElement* resultAnimationElement = toSVGAnimateElement(resultElement);
ASSERT(resultAnimationElement->m_animatedProperty);
ASSERT(resultAnimationElement->m_animatedPropertyType == m_animatedPropertyType);
if (isSVGSetElement(*this))
percentage = 1;
if (calcMode() == CalcModeDiscrete)
percentage = percentage < 0.5 ? 0 : 1;
m_animator->setContextElement(targetElement);
SVGPropertyBase* toAtEndOfDurationProperty = m_toAtEndOfDurationProperty ? m_toAtEndOfDurationProperty.get() : m_toProperty.get();
m_animator->calculateAnimatedValue(percentage, repeatCount, m_fromProperty.get(), m_toProperty.get(), toAtEndOfDurationProperty, resultAnimationElement->m_animatedProperty.get());
}
bool SVGAnimateElement::calculateToAtEndOfDurationValue(const String& toAtEndOfDurationString)
{
if (toAtEndOfDurationString.isEmpty())
return false;
m_toAtEndOfDurationProperty = ensureAnimator()->constructFromString(toAtEndOfDurationString);
return true;
}
bool SVGAnimateElement::calculateFromAndToValues(const String& fromString, const String& toString)
{
SVGElement* targetElement = this->targetElement();
if (!targetElement)
return false;
determinePropertyValueTypes(fromString, toString);
ensureAnimator()->calculateFromAndToValues(m_fromProperty, m_toProperty, fromString, toString);
ASSERT(m_animatedPropertyType == m_animator->type());
return true;
}
bool SVGAnimateElement::calculateFromAndByValues(const String& fromString, const String& byString)
{
SVGElement* targetElement = this->targetElement();
if (!targetElement)
return false;
if (animationMode() == ByAnimation && !isAdditive())
return false;
if (animationMode() == FromByAnimation && !animatedPropertyTypeSupportsAddition())
return false;
ASSERT(!isSVGSetElement(*this));
determinePropertyValueTypes(fromString, byString);
ensureAnimator()->calculateFromAndByValues(m_fromProperty, m_toProperty, fromString, byString);
ASSERT(m_animatedPropertyType == m_animator->type());
return true;
}
namespace {
Vector<SVGElement*> findElementInstances(SVGElement* targetElement)
{
ASSERT(targetElement);
Vector<SVGElement*> animatedElements;
animatedElements.append(targetElement);
const HashSet<SVGElementInstance*>& instances = targetElement->instancesForElement();
const HashSet<SVGElementInstance*>::const_iterator end = instances.end();
for (HashSet<SVGElementInstance*>::const_iterator it = instances.begin(); it != end; ++it) {
if (SVGElement* shadowTreeElement = (*it)->shadowTreeElement())
animatedElements.append(shadowTreeElement);
}
return animatedElements;
}
}
void SVGAnimateElement::resetAnimatedType()
{
SVGAnimatedTypeAnimator* animator = ensureAnimator();
ASSERT(m_animatedPropertyType == animator->type());
SVGElement* targetElement = this->targetElement();
const QualifiedName& attributeName = this->attributeName();
ShouldApplyAnimation shouldApply = shouldApplyAnimation(targetElement, attributeName);
if (shouldApply == DontApplyAnimation)
return;
if (shouldApply == ApplyXMLAnimation) {
m_animatedElements = findElementInstances(targetElement);
ASSERT(!m_animatedElements.isEmpty());
Vector<SVGElement*>::const_iterator end = m_animatedElements.end();
for (Vector<SVGElement*>::const_iterator it = m_animatedElements.begin(); it != end; ++it)
document().accessSVGExtensions().addElementReferencingTarget(this, *it);
if (!m_animatedProperty)
m_animatedProperty = animator->startAnimValAnimation(m_animatedElements);
else
m_animatedProperty = animator->resetAnimValToBaseVal(m_animatedElements);
return;
}
ASSERT(m_animatedElements.isEmpty());
String baseValue;
if (shouldApply == ApplyCSSAnimation) {
ASSERT(SVGAnimationElement::isTargetAttributeCSSProperty(targetElement, attributeName));
computeCSSPropertyValue(targetElement, cssPropertyID(attributeName.localName()), baseValue);
}
m_animatedProperty = animator->constructFromString(baseValue);
}
static inline void applyCSSPropertyToTarget(SVGElement* targetElement, CSSPropertyID id, const String& value)
{
ASSERT_WITH_SECURITY_IMPLICATION(!targetElement->m_deletionHasBegun);
MutableStylePropertySet* propertySet = targetElement->ensureAnimatedSMILStyleProperties();
if (!propertySet->setProperty(id, value, false, 0))
return;
targetElement->setNeedsStyleRecalc(LocalStyleChange);
}
static inline void removeCSSPropertyFromTarget(SVGElement* targetElement, CSSPropertyID id)
{
ASSERT_WITH_SECURITY_IMPLICATION(!targetElement->m_deletionHasBegun);
targetElement->ensureAnimatedSMILStyleProperties()->removeProperty(id);
targetElement->setNeedsStyleRecalc(LocalStyleChange);
}
static inline void applyCSSPropertyToTargetAndInstances(SVGElement* targetElement, const QualifiedName& attributeName, const String& valueAsString)
{
ASSERT(targetElement);
if (attributeName == anyQName() || !targetElement->inDocument() || !targetElement->parentNode())
return;
CSSPropertyID id = cssPropertyID(attributeName.localName());
SVGElementInstance::InstanceUpdateBlocker blocker(targetElement);
applyCSSPropertyToTarget(targetElement, id, valueAsString);
const HashSet<SVGElementInstance*>& instances = targetElement->instancesForElement();
const HashSet<SVGElementInstance*>::const_iterator end = instances.end();
for (HashSet<SVGElementInstance*>::const_iterator it = instances.begin(); it != end; ++it) {
if (SVGElement* shadowTreeElement = (*it)->shadowTreeElement())
applyCSSPropertyToTarget(shadowTreeElement, id, valueAsString);
}
}
static inline void removeCSSPropertyFromTargetAndInstances(SVGElement* targetElement, const QualifiedName& attributeName)
{
ASSERT(targetElement);
if (attributeName == anyQName() || !targetElement->inDocument() || !targetElement->parentNode())
return;
CSSPropertyID id = cssPropertyID(attributeName.localName());
SVGElementInstance::InstanceUpdateBlocker blocker(targetElement);
removeCSSPropertyFromTarget(targetElement, id);
const HashSet<SVGElementInstance*>& instances = targetElement->instancesForElement();
const HashSet<SVGElementInstance*>::const_iterator end = instances.end();
for (HashSet<SVGElementInstance*>::const_iterator it = instances.begin(); it != end; ++it) {
if (SVGElement* shadowTreeElement = (*it)->shadowTreeElement())
removeCSSPropertyFromTarget(shadowTreeElement, id);
}
}
static inline void notifyTargetAboutAnimValChange(SVGElement* targetElement, const QualifiedName& attributeName)
{
ASSERT_WITH_SECURITY_IMPLICATION(!targetElement->m_deletionHasBegun);
targetElement->invalidateSVGAttributes();
targetElement->svgAttributeChanged(attributeName);
}
static inline void notifyTargetAndInstancesAboutAnimValChange(SVGElement* targetElement, const QualifiedName& attributeName)
{
ASSERT(targetElement);
if (attributeName == anyQName() || !targetElement->inDocument() || !targetElement->parentNode())
return;
SVGElementInstance::InstanceUpdateBlocker blocker(targetElement);
notifyTargetAboutAnimValChange(targetElement, attributeName);
const HashSet<SVGElementInstance*>& instances = targetElement->instancesForElement();
const HashSet<SVGElementInstance*>::const_iterator end = instances.end();
for (HashSet<SVGElementInstance*>::const_iterator it = instances.begin(); it != end; ++it) {
if (SVGElement* shadowTreeElement = (*it)->shadowTreeElement())
notifyTargetAboutAnimValChange(shadowTreeElement, attributeName);
}
}
void SVGAnimateElement::clearAnimatedType(SVGElement* targetElement)
{
if (!m_animatedProperty)
return;
if (!targetElement) {
m_animatedProperty.clear();
return;
}
if (m_animatedElements.isEmpty()) {
removeCSSPropertyFromTargetAndInstances(targetElement, attributeName());
m_animatedProperty.clear();
return;
}
if (m_animator) {
m_animator->stopAnimValAnimation(m_animatedElements);
notifyTargetAndInstancesAboutAnimValChange(targetElement, attributeName());
}
m_animatedElements.clear();
m_animatedProperty.clear();
}
void SVGAnimateElement::applyResultsToTarget()
{
ASSERT(m_animatedPropertyType != AnimatedTransformList || isSVGAnimateTransformElement(*this));
ASSERT(m_animatedPropertyType != AnimatedUnknown);
ASSERT(m_animator);
if (!m_animatedProperty)
return;
if (m_animatedElements.isEmpty()) {
applyCSSPropertyToTargetAndInstances(targetElement(), attributeName(), m_animatedProperty->valueAsString());
return;
}
notifyTargetAndInstancesAboutAnimValChange(targetElement(), attributeName());
}
bool SVGAnimateElement::animatedPropertyTypeSupportsAddition() const
{
switch (m_animatedPropertyType) {
case AnimatedBoolean:
case AnimatedEnumeration:
case AnimatedPreserveAspectRatio:
case AnimatedString:
case AnimatedUnknown:
return false;
default:
return true;
}
}
bool SVGAnimateElement::isAdditive() const
{
if (animationMode() == ByAnimation || animationMode() == FromByAnimation)
if (!animatedPropertyTypeSupportsAddition())
return false;
return SVGAnimationElement::isAdditive();
}
float SVGAnimateElement::calculateDistance(const String& fromString, const String& toString)
{
SVGElement* targetElement = this->targetElement();
if (!targetElement)
return -1;
return ensureAnimator()->calculateDistance(fromString, toString);
}
void SVGAnimateElement::setTargetElement(SVGElement* target)
{
SVGAnimationElement::setTargetElement(target);
resetAnimatedPropertyType();
}
void SVGAnimateElement::setAttributeName(const QualifiedName& attributeName)
{
SVGAnimationElement::setAttributeName(attributeName);
resetAnimatedPropertyType();
}
void SVGAnimateElement::resetAnimatedPropertyType()
{
ASSERT(!m_animatedProperty);
m_fromProperty.clear();
m_toProperty.clear();
m_toAtEndOfDurationProperty.clear();
m_animator.clear();
m_animatedPropertyType = determineAnimatedPropertyType();
}
SVGAnimatedTypeAnimator* SVGAnimateElement::ensureAnimator()
{
if (!m_animator)
m_animator = SVGAnimatedTypeAnimator::create(m_animatedPropertyType, this, targetElement());
ASSERT(m_animatedPropertyType == m_animator->type());
return m_animator.get();
}
}