Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / dom / smil / nsSMILCSSProperty.cpp
bloba94f9e1fe570a67b40b9ba67123a2d0804f29f25
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* representation of a SMIL-animatable CSS property on an element */
8 #include "nsSMILCSSProperty.h"
9 #include "nsSMILCSSValueType.h"
10 #include "nsSMILValue.h"
11 #include "nsComputedDOMStyle.h"
12 #include "nsCSSProps.h"
13 #include "mozilla/dom/Element.h"
14 #include "nsIDOMElement.h"
15 #include "nsIDocument.h"
17 using namespace mozilla::dom;
19 // Helper function
20 static bool
21 GetCSSComputedValue(Element* aElem,
22 nsCSSProperty aPropID,
23 nsAString& aResult)
25 NS_ABORT_IF_FALSE(!nsCSSProps::IsShorthand(aPropID),
26 "Can't look up computed value of shorthand property");
27 NS_ABORT_IF_FALSE(nsSMILCSSProperty::IsPropertyAnimatable(aPropID),
28 "Shouldn't get here for non-animatable properties");
30 nsIDocument* doc = aElem->GetCurrentDoc();
31 if (!doc) {
32 // This can happen if we process certain types of restyles mid-sample
33 // and remove anonymous animated content from the document as a result.
34 // See bug 534975.
35 return false;
38 nsIPresShell* shell = doc->GetShell();
39 if (!shell) {
40 NS_WARNING("Unable to look up computed style -- no pres shell");
41 return false;
44 nsRefPtr<nsComputedDOMStyle> computedStyle =
45 NS_NewComputedDOMStyle(aElem, EmptyString(), shell);
47 computedStyle->GetPropertyValue(aPropID, aResult);
48 return true;
51 // Class Methods
52 nsSMILCSSProperty::nsSMILCSSProperty(nsCSSProperty aPropID,
53 Element* aElement)
54 : mPropID(aPropID), mElement(aElement)
56 NS_ABORT_IF_FALSE(IsPropertyAnimatable(mPropID),
57 "Creating a nsSMILCSSProperty for a property "
58 "that's not supported for animation");
61 nsSMILValue
62 nsSMILCSSProperty::GetBaseValue() const
64 // To benefit from Return Value Optimization and avoid copy constructor calls
65 // due to our use of return-by-value, we must return the exact same object
66 // from ALL return points. This function must only return THIS variable:
67 nsSMILValue baseValue;
69 // SPECIAL CASE: (a) Shorthands
70 // (b) 'display'
71 if (nsCSSProps::IsShorthand(mPropID) || mPropID == eCSSProperty_display) {
72 // We can't look up the base (computed-style) value of shorthand
73 // properties because they aren't guaranteed to have a consistent computed
74 // value.
76 // Also, although we can look up the base value of the display property,
77 // doing so involves clearing and resetting the property which can cause
78 // frames to be recreated which we'd like to avoid.
80 // In either case, just return a dummy value (initialized with the right
81 // type, so as not to indicate failure).
82 nsSMILValue tmpVal(&nsSMILCSSValueType::sSingleton);
83 baseValue.Swap(tmpVal);
84 return baseValue;
87 // GENERAL CASE: Non-Shorthands
88 // (1) Put empty string in override style for property mPropID
89 // (saving old override style value, so we can set it again when we're done)
90 nsICSSDeclaration* overrideDecl = mElement->GetSMILOverrideStyle();
91 nsAutoString cachedOverrideStyleVal;
92 if (overrideDecl) {
93 overrideDecl->GetPropertyValue(mPropID, cachedOverrideStyleVal);
94 // (Don't bother clearing override style if it's already empty)
95 if (!cachedOverrideStyleVal.IsEmpty()) {
96 overrideDecl->SetPropertyValue(mPropID, EmptyString());
100 // (2) Get Computed Style
101 nsAutoString computedStyleVal;
102 bool didGetComputedVal = GetCSSComputedValue(mElement, mPropID,
103 computedStyleVal);
105 // (3) Put cached override style back (if it's non-empty)
106 if (overrideDecl && !cachedOverrideStyleVal.IsEmpty()) {
107 overrideDecl->SetPropertyValue(mPropID, cachedOverrideStyleVal);
110 // (4) Populate our nsSMILValue from the computed style
111 if (didGetComputedVal) {
112 // When we parse animation values we check if they are context-sensitive or
113 // not so that we don't cache animation values whose meaning may change.
114 // For base values however this is unnecessary since on each sample the
115 // compositor will fetch the (computed) base value and compare it against
116 // the cached (computed) value and detect changes for us.
117 nsSMILCSSValueType::ValueFromString(mPropID, mElement,
118 computedStyleVal, baseValue,
119 nullptr);
121 return baseValue;
124 nsresult
125 nsSMILCSSProperty::ValueFromString(const nsAString& aStr,
126 const SVGAnimationElement* aSrcElement,
127 nsSMILValue& aValue,
128 bool& aPreventCachingOfSandwich) const
130 NS_ENSURE_TRUE(IsPropertyAnimatable(mPropID), NS_ERROR_FAILURE);
132 nsSMILCSSValueType::ValueFromString(mPropID, mElement, aStr, aValue,
133 &aPreventCachingOfSandwich);
135 if (aValue.IsNull()) {
136 return NS_ERROR_FAILURE;
139 // XXX Due to bug 536660 (or at least that seems to be the most likely
140 // culprit), when we have animation setting display:none on a <use> element,
141 // if we DON'T set the property every sample, chaos ensues.
142 if (!aPreventCachingOfSandwich && mPropID == eCSSProperty_display) {
143 aPreventCachingOfSandwich = true;
145 return NS_OK;
148 nsresult
149 nsSMILCSSProperty::SetAnimValue(const nsSMILValue& aValue)
151 NS_ENSURE_TRUE(IsPropertyAnimatable(mPropID), NS_ERROR_FAILURE);
153 // Convert nsSMILValue to string
154 nsAutoString valStr;
155 if (!nsSMILCSSValueType::ValueToString(aValue, valStr)) {
156 NS_WARNING("Failed to convert nsSMILValue for CSS property into a string");
157 return NS_ERROR_FAILURE;
160 // Use string value to style the target element
161 nsICSSDeclaration* overrideDecl = mElement->GetSMILOverrideStyle();
162 if (overrideDecl) {
163 nsAutoString oldValStr;
164 overrideDecl->GetPropertyValue(mPropID, oldValStr);
165 if (valStr.Equals(oldValStr)) {
166 return NS_OK;
168 overrideDecl->SetPropertyValue(mPropID, valStr);
170 return NS_OK;
173 void
174 nsSMILCSSProperty::ClearAnimValue()
176 // Put empty string in override style for our property
177 nsICSSDeclaration* overrideDecl = mElement->GetSMILOverrideStyle();
178 if (overrideDecl) {
179 overrideDecl->SetPropertyValue(mPropID, EmptyString());
183 // Based on http://www.w3.org/TR/SVG/propidx.html
184 // static
185 bool
186 nsSMILCSSProperty::IsPropertyAnimatable(nsCSSProperty aPropID)
188 // NOTE: Right now, Gecko doesn't recognize the following properties from
189 // the SVG Property Index:
190 // alignment-baseline
191 // baseline-shift
192 // color-profile
193 // color-rendering
194 // glyph-orientation-horizontal
195 // glyph-orientation-vertical
196 // kerning
197 // writing-mode
199 switch (aPropID) {
200 case eCSSProperty_clip:
201 case eCSSProperty_clip_rule:
202 case eCSSProperty_clip_path:
203 case eCSSProperty_color:
204 case eCSSProperty_color_interpolation:
205 case eCSSProperty_color_interpolation_filters:
206 case eCSSProperty_cursor:
207 case eCSSProperty_display:
208 case eCSSProperty_dominant_baseline:
209 case eCSSProperty_fill:
210 case eCSSProperty_fill_opacity:
211 case eCSSProperty_fill_rule:
212 case eCSSProperty_filter:
213 case eCSSProperty_flood_color:
214 case eCSSProperty_flood_opacity:
215 case eCSSProperty_font:
216 case eCSSProperty_font_family:
217 case eCSSProperty_font_size:
218 case eCSSProperty_font_size_adjust:
219 case eCSSProperty_font_stretch:
220 case eCSSProperty_font_style:
221 case eCSSProperty_font_variant:
222 case eCSSProperty_font_weight:
223 case eCSSProperty_height:
224 case eCSSProperty_image_rendering:
225 case eCSSProperty_letter_spacing:
226 case eCSSProperty_lighting_color:
227 case eCSSProperty_marker:
228 case eCSSProperty_marker_end:
229 case eCSSProperty_marker_mid:
230 case eCSSProperty_marker_start:
231 case eCSSProperty_mask:
232 case eCSSProperty_mask_type:
233 case eCSSProperty_opacity:
234 case eCSSProperty_overflow:
235 case eCSSProperty_pointer_events:
236 case eCSSProperty_shape_rendering:
237 case eCSSProperty_stop_color:
238 case eCSSProperty_stop_opacity:
239 case eCSSProperty_stroke:
240 case eCSSProperty_stroke_dasharray:
241 case eCSSProperty_stroke_dashoffset:
242 case eCSSProperty_stroke_linecap:
243 case eCSSProperty_stroke_linejoin:
244 case eCSSProperty_stroke_miterlimit:
245 case eCSSProperty_stroke_opacity:
246 case eCSSProperty_stroke_width:
247 case eCSSProperty_text_anchor:
248 case eCSSProperty_text_decoration:
249 case eCSSProperty_text_decoration_line:
250 case eCSSProperty_text_rendering:
251 case eCSSProperty_vector_effect:
252 case eCSSProperty_width:
253 case eCSSProperty_visibility:
254 case eCSSProperty_word_spacing:
255 return true;
257 // EXPLICITLY NON-ANIMATABLE PROPERTIES:
258 // (Some of these aren't supported at all in Gecko -- I've commented those
259 // ones out. If/when we add support for them, uncomment their line here)
260 // ----------------------------------------------------------------------
261 // case eCSSProperty_enable_background:
262 // case eCSSProperty_glyph_orientation_horizontal:
263 // case eCSSProperty_glyph_orientation_vertical:
264 // case eCSSProperty_writing_mode:
265 case eCSSProperty_direction:
266 case eCSSProperty_unicode_bidi:
267 return false;
269 default:
270 return false;