2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-09-26 01:04:31 +04:00
|
|
|
|
2013-04-15 02:56:34 +04:00
|
|
|
#include "nsSVGAnimatedTransformList.h"
|
2015-05-29 23:10:41 +03:00
|
|
|
|
2013-04-15 02:56:34 +04:00
|
|
|
#include "mozilla/dom/SVGAnimatedTransformList.h"
|
2013-03-20 07:20:38 +04:00
|
|
|
#include "mozilla/dom/SVGAnimationElement.h"
|
2015-05-29 23:10:41 +03:00
|
|
|
#include "mozilla/Move.h"
|
2013-10-01 11:50:40 +04:00
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
|
|
|
#include "nsSVGTransform.h"
|
2013-03-20 07:20:38 +04:00
|
|
|
#include "nsSMILValue.h"
|
|
|
|
#include "SVGContentUtils.h"
|
|
|
|
#include "SVGTransformListSMILType.h"
|
2014-04-07 23:52:12 +04:00
|
|
|
#include "nsIDOMMutationEvent.h"
|
2011-09-26 01:04:31 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2013-04-15 02:56:34 +04:00
|
|
|
using namespace dom;
|
|
|
|
|
2013-03-20 07:20:38 +04:00
|
|
|
nsresult
|
2013-04-15 02:56:34 +04:00
|
|
|
nsSVGAnimatedTransformList::SetBaseValueString(const nsAString& aValue)
|
2013-03-20 07:20:38 +04:00
|
|
|
{
|
|
|
|
SVGTransformList newBaseValue;
|
|
|
|
nsresult rv = newBaseValue.SetValueFromString(aValue);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2011-09-26 01:04:31 +04:00
|
|
|
|
2013-05-05 11:20:25 +04:00
|
|
|
return SetBaseValue(newBaseValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSVGAnimatedTransformList::SetBaseValue(const SVGTransformList& aValue)
|
|
|
|
{
|
2013-04-15 02:56:34 +04:00
|
|
|
SVGAnimatedTransformList *domWrapper =
|
|
|
|
SVGAnimatedTransformList::GetDOMWrapperIfExists(this);
|
2013-03-20 07:20:38 +04:00
|
|
|
if (domWrapper) {
|
|
|
|
// We must send this notification *before* changing mBaseVal! If the length
|
|
|
|
// of our baseVal is being reduced, our baseVal's DOM wrapper list may have
|
|
|
|
// to remove DOM items from itself, and any removed DOM items need to copy
|
|
|
|
// their internal counterpart values *before* we change them.
|
|
|
|
//
|
2013-05-05 11:20:25 +04:00
|
|
|
domWrapper->InternalBaseValListWillChangeLengthTo(aValue.Length());
|
2013-03-20 07:20:38 +04:00
|
|
|
}
|
2011-09-26 01:04:31 +04:00
|
|
|
|
2016-03-15 03:37:36 +03:00
|
|
|
// (This bool will be copied to our member-var, if attr-change succeeds.)
|
|
|
|
bool hadTransform = HasTransform();
|
|
|
|
|
2013-03-20 07:20:38 +04:00
|
|
|
// We don't need to call DidChange* here - we're only called by
|
|
|
|
// nsSVGElement::ParseAttribute under Element::SetAttr,
|
|
|
|
// which takes care of notifying.
|
2011-09-26 01:04:31 +04:00
|
|
|
|
2013-05-05 11:20:25 +04:00
|
|
|
nsresult rv = mBaseVal.CopyFrom(aValue);
|
2013-03-20 07:20:38 +04:00
|
|
|
if (NS_FAILED(rv) && domWrapper) {
|
|
|
|
// Attempting to increase mBaseVal's length failed - reduce domWrapper
|
|
|
|
// back to the same length:
|
|
|
|
domWrapper->InternalBaseValListWillChangeLengthTo(mBaseVal.Length());
|
|
|
|
} else {
|
|
|
|
mIsAttrSet = true;
|
2016-03-15 03:37:36 +03:00
|
|
|
mHadTransformBeforeLastBaseValChange = hadTransform;
|
2013-03-20 07:20:38 +04:00
|
|
|
}
|
|
|
|
return rv;
|
2012-12-23 08:54:20 +04:00
|
|
|
}
|
|
|
|
|
2013-03-20 07:20:38 +04:00
|
|
|
void
|
2013-04-15 02:56:34 +04:00
|
|
|
nsSVGAnimatedTransformList::ClearBaseValue()
|
2011-09-26 01:04:31 +04:00
|
|
|
{
|
2016-03-15 03:37:36 +03:00
|
|
|
mHadTransformBeforeLastBaseValChange = HasTransform();
|
|
|
|
|
2013-04-15 02:56:34 +04:00
|
|
|
SVGAnimatedTransformList *domWrapper =
|
|
|
|
SVGAnimatedTransformList::GetDOMWrapperIfExists(this);
|
2013-03-20 07:20:38 +04:00
|
|
|
if (domWrapper) {
|
|
|
|
// We must send this notification *before* changing mBaseVal! (See above.)
|
|
|
|
domWrapper->InternalBaseValListWillChangeLengthTo(0);
|
2011-09-26 01:04:31 +04:00
|
|
|
}
|
2013-03-20 07:20:38 +04:00
|
|
|
mBaseVal.Clear();
|
|
|
|
mIsAttrSet = false;
|
|
|
|
// Caller notifies
|
2011-09-26 01:04:31 +04:00
|
|
|
}
|
|
|
|
|
2013-03-20 07:20:38 +04:00
|
|
|
nsresult
|
2013-04-15 02:56:34 +04:00
|
|
|
nsSVGAnimatedTransformList::SetAnimValue(const SVGTransformList& aValue,
|
|
|
|
nsSVGElement *aElement)
|
2011-09-26 01:04:31 +04:00
|
|
|
{
|
2014-04-07 23:52:12 +04:00
|
|
|
bool prevSet = HasTransform() || aElement->GetAnimateMotionTransform();
|
2013-04-15 02:56:34 +04:00
|
|
|
SVGAnimatedTransformList *domWrapper =
|
|
|
|
SVGAnimatedTransformList::GetDOMWrapperIfExists(this);
|
2013-03-20 07:20:38 +04:00
|
|
|
if (domWrapper) {
|
|
|
|
// A new animation may totally change the number of items in the animVal
|
|
|
|
// list, replacing what was essentially a mirror of the baseVal list, or
|
|
|
|
// else replacing and overriding an existing animation. When this happens
|
|
|
|
// we must try and keep our animVal's DOM wrapper in sync (see the comment
|
2013-04-15 02:56:34 +04:00
|
|
|
// in SVGAnimatedTransformList::InternalBaseValListWillChangeLengthTo).
|
2013-03-20 07:20:38 +04:00
|
|
|
//
|
|
|
|
// It's not possible for us to reliably distinguish between calls to this
|
|
|
|
// method that are setting a new sample for an existing animation, and
|
|
|
|
// calls that are setting the first sample of an animation that will
|
|
|
|
// override an existing animation. Happily it's cheap to just blindly
|
|
|
|
// notify our animVal's DOM wrapper of its internal counterpart's new value
|
|
|
|
// each time this method is called, so that's what we do.
|
|
|
|
//
|
|
|
|
// Note that we must send this notification *before* setting or changing
|
|
|
|
// mAnimVal! (See the comment in SetBaseValueString above.)
|
|
|
|
//
|
|
|
|
domWrapper->InternalAnimValListWillChangeLengthTo(aValue.Length());
|
|
|
|
}
|
2011-09-26 01:04:31 +04:00
|
|
|
if (!mAnimVal) {
|
2013-03-20 07:20:38 +04:00
|
|
|
mAnimVal = new SVGTransformList();
|
|
|
|
}
|
|
|
|
nsresult rv = mAnimVal->CopyFrom(aValue);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// OOM. We clear the animation, and, importantly, ClearAnimValue() ensures
|
|
|
|
// that mAnimVal and its DOM wrapper (if any) will have the same length!
|
|
|
|
ClearAnimValue(aElement);
|
|
|
|
return rv;
|
2011-09-26 01:04:31 +04:00
|
|
|
}
|
2014-04-07 23:52:12 +04:00
|
|
|
int32_t modType;
|
|
|
|
if(prevSet) {
|
|
|
|
modType = nsIDOMMutationEvent::MODIFICATION;
|
|
|
|
} else {
|
|
|
|
modType = nsIDOMMutationEvent::ADDITION;
|
|
|
|
}
|
|
|
|
aElement->DidAnimateTransformList(modType);
|
2013-03-20 07:20:38 +04:00
|
|
|
return NS_OK;
|
2012-12-23 08:54:20 +04:00
|
|
|
}
|
|
|
|
|
2013-03-20 07:20:38 +04:00
|
|
|
void
|
2013-04-15 02:56:34 +04:00
|
|
|
nsSVGAnimatedTransformList::ClearAnimValue(nsSVGElement *aElement)
|
2011-09-26 01:04:31 +04:00
|
|
|
{
|
2013-04-15 02:56:34 +04:00
|
|
|
SVGAnimatedTransformList *domWrapper =
|
|
|
|
SVGAnimatedTransformList::GetDOMWrapperIfExists(this);
|
2013-03-20 07:20:38 +04:00
|
|
|
if (domWrapper) {
|
|
|
|
// When all animation ends, animVal simply mirrors baseVal, which may have
|
|
|
|
// a different number of items to the last active animated value. We must
|
|
|
|
// keep the length of our animVal's DOM wrapper list in sync, and again we
|
|
|
|
// must do that before touching mAnimVal. See comments above.
|
|
|
|
//
|
|
|
|
domWrapper->InternalAnimValListWillChangeLengthTo(mBaseVal.Length());
|
2011-09-26 01:04:31 +04:00
|
|
|
}
|
2013-03-20 07:20:38 +04:00
|
|
|
mAnimVal = nullptr;
|
2014-04-07 23:52:12 +04:00
|
|
|
int32_t modType;
|
|
|
|
if (HasTransform() || aElement->GetAnimateMotionTransform()) {
|
|
|
|
modType = nsIDOMMutationEvent::MODIFICATION;
|
|
|
|
} else {
|
|
|
|
modType = nsIDOMMutationEvent::REMOVAL;
|
|
|
|
}
|
|
|
|
aElement->DidAnimateTransformList(modType);
|
2013-03-20 07:20:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-04-15 02:56:34 +04:00
|
|
|
nsSVGAnimatedTransformList::IsExplicitlySet() const
|
2013-03-20 07:20:38 +04:00
|
|
|
{
|
|
|
|
// Like other methods of this name, we need to know when a transform value has
|
|
|
|
// been explicitly set.
|
|
|
|
//
|
|
|
|
// There are three ways an animated list can become set:
|
|
|
|
// 1) Markup -- we set mIsAttrSet to true on any successful call to
|
|
|
|
// SetBaseValueString and clear it on ClearBaseValue (as called by
|
|
|
|
// nsSVGElement::UnsetAttr or a failed nsSVGElement::ParseAttribute)
|
|
|
|
// 2) DOM call -- simply fetching the baseVal doesn't mean the transform value
|
|
|
|
// has been set. It is set if that baseVal has one or more transforms in
|
|
|
|
// the list.
|
|
|
|
// 3) Animation -- which will cause the mAnimVal member to be allocated
|
|
|
|
return mIsAttrSet || !mBaseVal.IsEmpty() || mAnimVal;
|
2011-09-26 01:04:31 +04:00
|
|
|
}
|
|
|
|
|
2017-03-30 07:10:07 +03:00
|
|
|
UniquePtr<nsISMILAttr>
|
2013-04-15 02:56:34 +04:00
|
|
|
nsSVGAnimatedTransformList::ToSMILAttr(nsSVGElement* aSVGElement)
|
2011-09-26 01:04:31 +04:00
|
|
|
{
|
2017-03-30 07:10:07 +03:00
|
|
|
return MakeUnique<SMILAnimatedTransformList>(this, aSVGElement);
|
2011-09-26 01:04:31 +04:00
|
|
|
}
|
|
|
|
|
2013-03-20 07:20:38 +04:00
|
|
|
nsresult
|
2013-04-15 02:56:34 +04:00
|
|
|
nsSVGAnimatedTransformList::SMILAnimatedTransformList::ValueFromString(
|
2013-03-20 07:20:38 +04:00
|
|
|
const nsAString& aStr,
|
|
|
|
const dom::SVGAnimationElement* aSrcElement,
|
|
|
|
nsSMILValue& aValue,
|
|
|
|
bool& aPreventCachingOfSandwich) const
|
2011-09-26 01:04:31 +04:00
|
|
|
{
|
2013-03-20 07:20:38 +04:00
|
|
|
NS_ENSURE_TRUE(aSrcElement, NS_ERROR_FAILURE);
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aValue.IsNull(),
|
|
|
|
"aValue should have been cleared before calling ValueFromString");
|
2013-03-20 07:20:38 +04:00
|
|
|
|
|
|
|
const nsAttrValue* typeAttr = aSrcElement->GetAnimAttr(nsGkAtoms::type);
|
|
|
|
const nsIAtom* transformType = nsGkAtoms::translate; // default val
|
|
|
|
if (typeAttr) {
|
|
|
|
if (typeAttr->Type() != nsAttrValue::eAtom) {
|
|
|
|
// Recognized values of |type| are parsed as an atom -- so if we have
|
|
|
|
// something other than an atom, then we know already our |type| is
|
|
|
|
// invalid.
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
transformType = typeAttr->GetAtomValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
ParseValue(aStr, transformType, aValue);
|
|
|
|
aPreventCachingOfSandwich = false;
|
|
|
|
return aValue.IsNull() ? NS_ERROR_FAILURE : NS_OK;
|
2011-09-26 01:04:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-15 02:56:34 +04:00
|
|
|
nsSVGAnimatedTransformList::SMILAnimatedTransformList::ParseValue(
|
2013-03-20 07:20:38 +04:00
|
|
|
const nsAString& aSpec,
|
|
|
|
const nsIAtom* aTransformType,
|
|
|
|
nsSMILValue& aResult)
|
2011-09-26 01:04:31 +04:00
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aResult.IsNull(), "Unexpected type for SMIL value");
|
2013-03-20 07:20:38 +04:00
|
|
|
|
2013-11-11 12:03:59 +04:00
|
|
|
static_assert(SVGTransformSMILData::NUM_SIMPLE_PARAMS == 3,
|
|
|
|
"nsSVGSMILTransform constructor should be expecting array "
|
|
|
|
"with 3 params");
|
2013-03-20 07:20:38 +04:00
|
|
|
|
|
|
|
float params[3] = { 0.f };
|
|
|
|
int32_t numParsed = ParseParameterList(aSpec, params, 3);
|
|
|
|
uint16_t transformType;
|
|
|
|
|
|
|
|
if (aTransformType == nsGkAtoms::translate) {
|
|
|
|
// tx [ty=0]
|
|
|
|
if (numParsed != 1 && numParsed != 2)
|
|
|
|
return;
|
|
|
|
transformType = SVG_TRANSFORM_TRANSLATE;
|
|
|
|
} else if (aTransformType == nsGkAtoms::scale) {
|
|
|
|
// sx [sy=sx]
|
|
|
|
if (numParsed != 1 && numParsed != 2)
|
|
|
|
return;
|
|
|
|
if (numParsed == 1) {
|
|
|
|
params[1] = params[0];
|
2011-09-26 01:04:31 +04:00
|
|
|
}
|
2013-03-20 07:20:38 +04:00
|
|
|
transformType = SVG_TRANSFORM_SCALE;
|
|
|
|
} else if (aTransformType == nsGkAtoms::rotate) {
|
|
|
|
// r [cx=0 cy=0]
|
|
|
|
if (numParsed != 1 && numParsed != 3)
|
|
|
|
return;
|
|
|
|
transformType = SVG_TRANSFORM_ROTATE;
|
|
|
|
} else if (aTransformType == nsGkAtoms::skewX) {
|
|
|
|
// x-angle
|
|
|
|
if (numParsed != 1)
|
|
|
|
return;
|
|
|
|
transformType = SVG_TRANSFORM_SKEWX;
|
|
|
|
} else if (aTransformType == nsGkAtoms::skewY) {
|
|
|
|
// y-angle
|
|
|
|
if (numParsed != 1)
|
|
|
|
return;
|
|
|
|
transformType = SVG_TRANSFORM_SKEWY;
|
|
|
|
} else {
|
|
|
|
return;
|
2011-09-26 01:04:31 +04:00
|
|
|
}
|
|
|
|
|
2013-05-31 02:34:53 +04:00
|
|
|
nsSMILValue val(SVGTransformListSMILType::Singleton());
|
2013-03-20 07:20:38 +04:00
|
|
|
SVGTransformSMILData transform(transformType, params);
|
|
|
|
if (NS_FAILED(SVGTransformListSMILType::AppendTransform(transform, val))) {
|
|
|
|
return; // OOM
|
2011-09-26 01:04:31 +04:00
|
|
|
}
|
2013-03-20 07:20:38 +04:00
|
|
|
|
|
|
|
// Success! Populate our outparam with parsed value.
|
2015-05-29 23:10:41 +03:00
|
|
|
aResult = Move(val);
|
2011-09-26 01:04:31 +04:00
|
|
|
}
|
|
|
|
|
2013-03-20 07:20:38 +04:00
|
|
|
int32_t
|
2013-04-15 02:56:34 +04:00
|
|
|
nsSVGAnimatedTransformList::SMILAnimatedTransformList::ParseParameterList(
|
2013-03-20 07:20:38 +04:00
|
|
|
const nsAString& aSpec,
|
|
|
|
float* aVars,
|
|
|
|
int32_t aNVars)
|
2011-09-26 01:04:31 +04:00
|
|
|
{
|
2013-10-01 11:50:40 +04:00
|
|
|
nsCharSeparatedTokenizerTemplate<IsSVGWhitespace>
|
|
|
|
tokenizer(aSpec, ',', nsCharSeparatedTokenizer::SEPARATOR_OPTIONAL);
|
2013-03-20 07:20:38 +04:00
|
|
|
|
|
|
|
int numArgsFound = 0;
|
|
|
|
|
2013-10-01 11:50:40 +04:00
|
|
|
while (tokenizer.hasMoreTokens()) {
|
|
|
|
float f;
|
|
|
|
if (!SVGContentUtils::ParseNumber(tokenizer.nextToken(), f)) {
|
2017-07-06 15:00:35 +03:00
|
|
|
return -1;
|
2013-10-01 11:50:40 +04:00
|
|
|
}
|
2013-03-20 07:20:38 +04:00
|
|
|
if (numArgsFound < aNVars) {
|
|
|
|
aVars[numArgsFound] = f;
|
|
|
|
}
|
|
|
|
numArgsFound++;
|
2011-09-26 01:04:31 +04:00
|
|
|
}
|
2013-03-20 07:20:38 +04:00
|
|
|
return numArgsFound;
|
2011-09-26 01:04:31 +04:00
|
|
|
}
|
|
|
|
|
2013-03-20 07:20:38 +04:00
|
|
|
nsSMILValue
|
2013-04-15 02:56:34 +04:00
|
|
|
nsSVGAnimatedTransformList::SMILAnimatedTransformList::GetBaseValue() const
|
2011-09-26 01:04:31 +04:00
|
|
|
{
|
2013-03-20 07:20:38 +04:00
|
|
|
// To benefit from Return Value Optimization and avoid copy constructor calls
|
|
|
|
// due to our use of return-by-value, we must return the exact same object
|
|
|
|
// from ALL return points. This function must only return THIS variable:
|
2013-05-31 02:34:53 +04:00
|
|
|
nsSMILValue val(SVGTransformListSMILType::Singleton());
|
2013-03-20 07:20:38 +04:00
|
|
|
if (!SVGTransformListSMILType::AppendTransforms(mVal->mBaseVal, val)) {
|
|
|
|
val = nsSMILValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
2011-09-26 01:04:31 +04:00
|
|
|
}
|
|
|
|
|
2013-03-20 07:20:38 +04:00
|
|
|
nsresult
|
2013-04-15 02:56:34 +04:00
|
|
|
nsSVGAnimatedTransformList::SMILAnimatedTransformList::SetAnimValue(
|
2013-03-20 07:20:38 +04:00
|
|
|
const nsSMILValue& aNewAnimValue)
|
2011-09-26 01:04:31 +04:00
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aNewAnimValue.mType == SVGTransformListSMILType::Singleton(),
|
|
|
|
"Unexpected type to assign animated value");
|
2013-03-20 07:20:38 +04:00
|
|
|
SVGTransformList animVal;
|
|
|
|
if (!SVGTransformListSMILType::GetTransforms(aNewAnimValue,
|
|
|
|
animVal.mItems)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mVal->SetAnimValue(animVal, mElement);
|
2011-09-26 01:04:31 +04:00
|
|
|
}
|
|
|
|
|
2013-03-20 07:20:38 +04:00
|
|
|
void
|
2013-04-15 02:56:34 +04:00
|
|
|
nsSVGAnimatedTransformList::SMILAnimatedTransformList::ClearAnimValue()
|
2011-09-26 01:04:31 +04:00
|
|
|
{
|
2013-03-20 07:20:38 +04:00
|
|
|
if (mVal->mAnimVal) {
|
|
|
|
mVal->ClearAnimValue(mElement);
|
|
|
|
}
|
2011-09-26 01:04:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|