Merge mozilla-central into tracemonkey.
[mozilla-central.git] / content / smil / nsSMILMappedAttribute.cpp
blob2092460cdaf2f432b65ec8d90bfdd42ee7f9ba7b
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is the Mozilla SMIL module.
17 * The Initial Developer of the Original Code is the Mozilla Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2010
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Daniel Holbert <dholbert@mozilla.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 /* representation of a SMIL-animatable mapped attribute on an element */
39 #include "nsSMILMappedAttribute.h"
40 #include "nsPropertyTable.h"
41 #include "nsContentErrors.h" // For NS_PROPTABLE_PROP_OVERWRITTEN
42 #include "nsSMILValue.h"
43 #include "nsSMILCSSValueType.h"
44 #include "nsIContent.h"
45 #include "nsIDocument.h"
46 #include "nsIPresShell.h"
47 #include "nsCSSProps.h"
49 // Callback function, for freeing string buffers stored in property table
50 static void
51 ReleaseStringBufferPropertyValue(void* aObject, /* unused */
52 nsIAtom* aPropertyName, /* unused */
53 void* aPropertyValue,
54 void* aData /* unused */)
56 nsStringBuffer* buf = static_cast<nsStringBuffer*>(aPropertyValue);
57 buf->Release();
61 nsresult
62 nsSMILMappedAttribute::ValueFromString(const nsAString& aStr,
63 const nsISMILAnimationElement* aSrcElement,
64 nsSMILValue& aValue,
65 PRBool& aPreventCachingOfSandwich) const
67 NS_ENSURE_TRUE(IsPropertyAnimatable(mPropID), NS_ERROR_FAILURE);
69 nsSMILCSSValueType::ValueFromString(mPropID, mElement, aStr, aValue);
70 if (aValue.IsNull()) {
71 return NS_ERROR_FAILURE;
74 // XXXdholbert: For simplicity, just assume that all CSS values have to
75 // reparsed every sample. See note in nsSMILCSSProperty::ValueFromString.
76 aPreventCachingOfSandwich = PR_TRUE;
77 return NS_OK;
80 nsSMILValue
81 nsSMILMappedAttribute::GetBaseValue() const
83 nsAutoString baseStringValue;
84 nsRefPtr<nsIAtom> attrName = GetAttrNameAtom();
85 PRBool success = mElement->GetAttr(kNameSpaceID_None, attrName,
86 baseStringValue);
87 nsSMILValue baseValue;
88 if (success) {
89 nsSMILCSSValueType::ValueFromString(mPropID, mElement,
90 baseStringValue, baseValue);
91 } else {
92 // Attribute is unset -- use computed value.
93 // FIRST: Temporarily clear animated value, to make sure it doesn't pollute
94 // the computed value. (We want base value, _without_ animations applied.)
95 void* buf = mElement->UnsetProperty(SMIL_MAPPED_ATTR_ANIMVAL,
96 attrName, nsnull);
97 FlushChangesToTargetAttr();
99 // SECOND: we use nsSMILCSSProperty::GetBaseValue to look up the property's
100 // computed value. NOTE: This call will temporarily clear the SMIL
101 // override-style for the corresponding CSS property on our target element.
102 // This prevents any animations that target the CSS property from affecting
103 // animations that target the mapped attribute.
104 baseValue = nsSMILCSSProperty::GetBaseValue();
106 // FINALLY: If we originally had an animated value set, then set it again.
107 if (buf) {
108 mElement->SetProperty(SMIL_MAPPED_ATTR_ANIMVAL, attrName, buf,
109 ReleaseStringBufferPropertyValue);
110 FlushChangesToTargetAttr();
113 return baseValue;
116 nsresult
117 nsSMILMappedAttribute::SetAnimValue(const nsSMILValue& aValue)
119 NS_ENSURE_TRUE(IsPropertyAnimatable(mPropID), NS_ERROR_FAILURE);
121 // Convert nsSMILValue to string
122 nsAutoString valStr;
123 if (!nsSMILCSSValueType::ValueToString(aValue, valStr)) {
124 NS_WARNING("Failed to convert nsSMILValue for mapped attr into a string");
125 return NS_ERROR_FAILURE;
128 // Set the string as this mapped attribute's animated value.
129 nsStringBuffer* valStrBuf =
130 nsCSSValue::BufferFromString(nsString(valStr)).get();
131 nsRefPtr<nsIAtom> attrName = GetAttrNameAtom();
132 nsresult rv = mElement->SetProperty(SMIL_MAPPED_ATTR_ANIMVAL,
133 attrName, valStrBuf,
134 ReleaseStringBufferPropertyValue);
135 if (rv == NS_PROPTABLE_PROP_OVERWRITTEN) {
136 rv = NS_OK;
138 FlushChangesToTargetAttr();
140 return rv;
143 void
144 nsSMILMappedAttribute::ClearAnimValue()
146 nsRefPtr<nsIAtom> attrName = GetAttrNameAtom();
147 mElement->DeleteProperty(SMIL_MAPPED_ATTR_ANIMVAL, attrName);
148 FlushChangesToTargetAttr();
151 void
152 nsSMILMappedAttribute::FlushChangesToTargetAttr() const
154 // Clear animated content-style-rule
155 mElement->DeleteProperty(SMIL_MAPPED_ATTR_ANIMVAL,
156 SMIL_MAPPED_ATTR_STYLERULE_ATOM);
157 nsIDocument* doc = mElement->GetCurrentDoc();
159 // Request animation restyle
160 if (doc) {
161 nsIPresShell* shell = doc->GetShell();
162 if (shell) {
163 shell->RestyleForAnimation(mElement, eRestyle_Self);
168 already_AddRefed<nsIAtom>
169 nsSMILMappedAttribute::GetAttrNameAtom() const
171 return do_GetAtom(nsCSSProps::GetStringValue(mPropID));