2009-10-02 14:37:25 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is the Mozilla SMIL module.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is the Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2009
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Daniel Holbert <dholbert@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/* representation of a SMIL-animatable CSS property on an element */
|
|
|
|
|
|
|
|
#include "nsSMILCSSProperty.h"
|
|
|
|
#include "nsSMILCSSValueType.h"
|
|
|
|
#include "nsSMILValue.h"
|
|
|
|
#include "nsCSSDeclaration.h"
|
|
|
|
#include "nsComputedDOMStyle.h"
|
|
|
|
#include "nsStyleAnimation.h"
|
|
|
|
#include "nsIContent.h"
|
2010-01-06 09:20:50 -08:00
|
|
|
#include "nsIDOMElement.h"
|
2009-10-02 14:37:25 -07:00
|
|
|
|
2010-05-14 10:04:51 -07:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2010-02-01 18:46:13 -08:00
|
|
|
// Helper function
|
2009-10-02 14:37:25 -07:00
|
|
|
static PRBool
|
|
|
|
GetCSSComputedValue(nsIContent* aElem,
|
|
|
|
nsCSSProperty aPropID,
|
|
|
|
nsAString& aResult)
|
|
|
|
{
|
2010-02-01 18:46:13 -08:00
|
|
|
NS_ABORT_IF_FALSE(!nsCSSProps::IsShorthand(aPropID),
|
|
|
|
"Can't look up computed value of shorthand property");
|
|
|
|
NS_ABORT_IF_FALSE(nsSMILCSSProperty::IsPropertyAnimatable(aPropID),
|
|
|
|
"Shouldn't get here for non-animatable properties");
|
2009-10-02 14:37:25 -07:00
|
|
|
|
|
|
|
nsIDocument* doc = aElem->GetCurrentDoc();
|
2009-12-23 16:19:29 -08:00
|
|
|
if (!doc) {
|
|
|
|
// This can happen if we process certain types of restyles mid-sample
|
|
|
|
// and remove anonymous animated content from the document as a result.
|
|
|
|
// See bug 534975.
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
2009-10-31 17:06:47 -07:00
|
|
|
|
2010-01-06 09:20:50 -08:00
|
|
|
nsIPresShell* shell = doc->GetPrimaryShell();
|
|
|
|
if (!shell) {
|
|
|
|
NS_WARNING("Unable to look up computed style -- no pres shell");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsComputedDOMStyle> computedStyle;
|
|
|
|
nsCOMPtr<nsIDOMElement> domElement(do_QueryInterface(aElem));
|
|
|
|
nsresult rv = NS_NewComputedDOMStyle(domElement, EmptyString(), shell,
|
|
|
|
getter_AddRefs(computedStyle));
|
|
|
|
|
2010-02-01 18:46:13 -08:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2009-10-02 14:37:25 -07:00
|
|
|
computedStyle->GetPropertyValue(aPropID, aResult);
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Class Methods
|
|
|
|
nsSMILCSSProperty::nsSMILCSSProperty(nsCSSProperty aPropID,
|
2010-05-14 10:04:51 -07:00
|
|
|
Element* aElement)
|
2009-10-02 14:37:25 -07:00
|
|
|
: mPropID(aPropID), mElement(aElement)
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(IsPropertyAnimatable(mPropID),
|
|
|
|
"Creating a nsSMILCSSProperty for a property "
|
|
|
|
"that's not supported for animation");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSMILValue
|
|
|
|
nsSMILCSSProperty::GetBaseValue() const
|
|
|
|
{
|
2010-03-26 12:24:13 -07: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:
|
|
|
|
nsSMILValue baseValue;
|
|
|
|
|
2010-02-01 18:46:13 -08:00
|
|
|
// SPECIAL CASE: Shorthands
|
|
|
|
if (nsCSSProps::IsShorthand(mPropID)) {
|
|
|
|
// We can't look up the base (computed-style) value of shorthand
|
|
|
|
// properties, because they aren't guaranteed to have a consistent computed
|
|
|
|
// value. However, that's not a problem, because it turns out the caller
|
|
|
|
// isn't going to end up using the value we return anyway. Base values only
|
|
|
|
// get used when there's interpolation or addition, and the shorthand
|
|
|
|
// properties we know about don't support those operations. So, we can just
|
|
|
|
// return a dummy value (initialized with the right type, so as not to
|
|
|
|
// indicate failure).
|
2010-03-26 12:24:13 -07:00
|
|
|
nsSMILValue tmpVal(&nsSMILCSSValueType::sSingleton);
|
|
|
|
baseValue.Swap(tmpVal);
|
|
|
|
return baseValue;
|
2010-02-01 18:46:13 -08:00
|
|
|
}
|
|
|
|
|
2010-03-26 12:24:13 -07:00
|
|
|
// GENERAL CASE: Non-Shorthands
|
2009-10-02 14:37:25 -07:00
|
|
|
// (1) Put empty string in override style for property mPropID
|
|
|
|
// (saving old override style value, so we can set it again when we're done)
|
|
|
|
nsCOMPtr<nsIDOMCSSStyleDeclaration> overrideStyle;
|
|
|
|
mElement->GetSMILOverrideStyle(getter_AddRefs(overrideStyle));
|
|
|
|
nsCOMPtr<nsICSSDeclaration> overrideDecl = do_QueryInterface(overrideStyle);
|
|
|
|
nsAutoString cachedOverrideStyleVal;
|
|
|
|
if (overrideDecl) {
|
|
|
|
overrideDecl->GetPropertyValue(mPropID, cachedOverrideStyleVal);
|
|
|
|
// (Don't bother clearing override style if it's already empty)
|
|
|
|
if (!cachedOverrideStyleVal.IsEmpty()) {
|
|
|
|
overrideDecl->SetPropertyValue(mPropID, EmptyString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// (2) Get Computed Style
|
|
|
|
nsAutoString computedStyleVal;
|
|
|
|
PRBool didGetComputedVal = GetCSSComputedValue(mElement, mPropID,
|
|
|
|
computedStyleVal);
|
|
|
|
|
|
|
|
// (3) Put cached override style back (if it's non-empty)
|
|
|
|
if (overrideDecl && !cachedOverrideStyleVal.IsEmpty()) {
|
|
|
|
overrideDecl->SetPropertyValue(mPropID, cachedOverrideStyleVal);
|
|
|
|
}
|
|
|
|
|
2010-03-26 12:24:13 -07:00
|
|
|
// (4) Populate our nsSMILValue from the computed style
|
2009-10-02 14:37:25 -07:00
|
|
|
if (didGetComputedVal) {
|
2010-04-02 09:34:31 -07:00
|
|
|
nsSMILCSSValueType::ValueFromString(mPropID, mElement, computedStyleVal,
|
|
|
|
PR_FALSE, baseValue);
|
2009-10-02 14:37:25 -07:00
|
|
|
}
|
|
|
|
return baseValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSMILCSSProperty::ValueFromString(const nsAString& aStr,
|
|
|
|
const nsISMILAnimationElement* aSrcElement,
|
2010-02-20 13:13:11 -08:00
|
|
|
nsSMILValue& aValue,
|
|
|
|
PRBool& aCanCache) const
|
2009-10-02 14:37:25 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(IsPropertyAnimatable(mPropID), NS_ERROR_FAILURE);
|
2010-02-01 18:46:13 -08:00
|
|
|
|
2010-04-02 09:34:31 -07:00
|
|
|
nsSMILCSSValueType::ValueFromString(mPropID, mElement, aStr,
|
|
|
|
PR_FALSE, aValue);
|
2010-02-20 13:13:11 -08:00
|
|
|
if (aValue.IsNull()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXXdholbert: For simplicity, just assume that all CSS values have to
|
|
|
|
// reparsed every sample. This prevents us from doing the "nothing's changed
|
|
|
|
// so don't recompose" optimization (bug 533291) for CSS properties & mapped
|
|
|
|
// attributes. If it ends up being expensive to always recompose those, we
|
|
|
|
// can be a little smarter here. We really only need to disable aCanCache
|
|
|
|
// for "inherit" & "currentColor" (whose values could change at any time), as
|
|
|
|
// well as for length-valued types (particularly those with em/ex/percent
|
|
|
|
// units, since their conversion ratios can change at any time).
|
|
|
|
aCanCache = PR_FALSE;
|
|
|
|
return NS_OK;
|
2009-10-02 14:37:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSMILCSSProperty::SetAnimValue(const nsSMILValue& aValue)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(IsPropertyAnimatable(mPropID), NS_ERROR_FAILURE);
|
|
|
|
|
2010-02-01 18:46:13 -08:00
|
|
|
// Convert nsSMILValue to string
|
2009-10-02 14:37:25 -07:00
|
|
|
nsAutoString valStr;
|
2010-02-01 18:46:13 -08:00
|
|
|
if (!nsSMILCSSValueType::ValueToString(aValue, valStr)) {
|
2009-10-02 14:37:25 -07:00
|
|
|
NS_WARNING("Failed to convert nsSMILValue for CSS property into a string");
|
2010-02-01 18:46:13 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
2009-10-02 14:37:25 -07:00
|
|
|
}
|
|
|
|
|
2010-02-01 18:46:13 -08:00
|
|
|
// Use string value to style the target element
|
|
|
|
nsCOMPtr<nsIDOMCSSStyleDeclaration> overrideStyle;
|
|
|
|
mElement->GetSMILOverrideStyle(getter_AddRefs(overrideStyle));
|
|
|
|
NS_ABORT_IF_FALSE(overrideStyle, "Need a non-null overrideStyle");
|
|
|
|
|
|
|
|
nsCOMPtr<nsICSSDeclaration> overrideDecl = do_QueryInterface(overrideStyle);
|
|
|
|
if (overrideDecl) {
|
|
|
|
overrideDecl->SetPropertyValue(mPropID, valStr);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2009-10-02 14:37:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSMILCSSProperty::ClearAnimValue()
|
|
|
|
{
|
2010-02-01 18:46:13 -08:00
|
|
|
// Put empty string in override style for our property
|
2009-10-02 14:37:25 -07:00
|
|
|
nsCOMPtr<nsIDOMCSSStyleDeclaration> overrideStyle;
|
|
|
|
mElement->GetSMILOverrideStyle(getter_AddRefs(overrideStyle));
|
|
|
|
nsCOMPtr<nsICSSDeclaration> overrideDecl = do_QueryInterface(overrideStyle);
|
|
|
|
if (overrideDecl) {
|
|
|
|
overrideDecl->SetPropertyValue(mPropID, EmptyString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Based on http://www.w3.org/TR/SVG/propidx.html
|
|
|
|
// static
|
|
|
|
PRBool
|
|
|
|
nsSMILCSSProperty::IsPropertyAnimatable(nsCSSProperty aPropID)
|
|
|
|
{
|
|
|
|
// NOTE: Right now, Gecko doesn't recognize the following properties from
|
|
|
|
// the SVG Property Index:
|
|
|
|
// alignment-baseline
|
|
|
|
// baseline-shift
|
|
|
|
// color-profile
|
|
|
|
// color-rendering
|
|
|
|
// glyph-orientation-horizontal
|
|
|
|
// glyph-orientation-vertical
|
|
|
|
// kerning
|
|
|
|
// writing-mode
|
|
|
|
|
|
|
|
switch (aPropID) {
|
|
|
|
case eCSSProperty_clip:
|
2009-10-21 14:57:57 -07:00
|
|
|
case eCSSProperty_clip_rule:
|
2009-12-10 09:26:27 -08:00
|
|
|
case eCSSProperty_clip_path:
|
2009-10-02 14:37:25 -07:00
|
|
|
case eCSSProperty_color:
|
2009-10-21 14:57:57 -07:00
|
|
|
case eCSSProperty_color_interpolation:
|
|
|
|
case eCSSProperty_color_interpolation_filters:
|
2009-12-10 09:26:28 -08:00
|
|
|
case eCSSProperty_cursor:
|
2009-10-21 14:57:57 -07:00
|
|
|
case eCSSProperty_display:
|
|
|
|
case eCSSProperty_dominant_baseline:
|
2009-12-10 09:26:27 -08:00
|
|
|
case eCSSProperty_fill:
|
2009-10-08 18:30:50 -07:00
|
|
|
case eCSSProperty_fill_opacity:
|
2009-10-21 14:57:57 -07:00
|
|
|
case eCSSProperty_fill_rule:
|
2009-12-10 09:26:27 -08:00
|
|
|
case eCSSProperty_filter:
|
2009-10-02 14:37:25 -07:00
|
|
|
case eCSSProperty_flood_color:
|
2009-10-08 18:30:50 -07:00
|
|
|
case eCSSProperty_flood_opacity:
|
2010-01-04 16:32:11 -08:00
|
|
|
case eCSSProperty_font:
|
2009-12-10 09:26:27 -08:00
|
|
|
case eCSSProperty_font_family:
|
2009-10-02 14:37:25 -07:00
|
|
|
case eCSSProperty_font_size:
|
2009-10-08 18:30:53 -07:00
|
|
|
case eCSSProperty_font_size_adjust:
|
2009-11-17 22:08:19 -08:00
|
|
|
case eCSSProperty_font_stretch:
|
2009-10-21 14:57:57 -07:00
|
|
|
case eCSSProperty_font_style:
|
|
|
|
case eCSSProperty_font_variant:
|
2009-11-17 22:08:19 -08:00
|
|
|
case eCSSProperty_font_weight:
|
2009-10-21 14:57:57 -07:00
|
|
|
case eCSSProperty_image_rendering:
|
2009-10-02 14:37:25 -07:00
|
|
|
case eCSSProperty_letter_spacing:
|
|
|
|
case eCSSProperty_lighting_color:
|
2010-01-04 16:32:11 -08:00
|
|
|
case eCSSProperty_marker:
|
2009-12-10 09:26:27 -08:00
|
|
|
case eCSSProperty_marker_end:
|
|
|
|
case eCSSProperty_marker_mid:
|
|
|
|
case eCSSProperty_marker_start:
|
|
|
|
case eCSSProperty_mask:
|
2009-10-08 18:30:50 -07:00
|
|
|
case eCSSProperty_opacity:
|
2010-01-04 16:32:11 -08:00
|
|
|
case eCSSProperty_overflow:
|
2009-10-21 14:57:57 -07:00
|
|
|
case eCSSProperty_pointer_events:
|
|
|
|
case eCSSProperty_shape_rendering:
|
2009-10-02 14:37:25 -07:00
|
|
|
case eCSSProperty_stop_color:
|
2009-10-08 18:30:50 -07:00
|
|
|
case eCSSProperty_stop_opacity:
|
2009-12-10 09:26:27 -08:00
|
|
|
case eCSSProperty_stroke:
|
|
|
|
case eCSSProperty_stroke_dasharray:
|
2009-10-02 14:37:25 -07:00
|
|
|
case eCSSProperty_stroke_dashoffset:
|
2009-10-21 14:57:57 -07:00
|
|
|
case eCSSProperty_stroke_linecap:
|
|
|
|
case eCSSProperty_stroke_linejoin:
|
2009-10-08 18:30:50 -07:00
|
|
|
case eCSSProperty_stroke_miterlimit:
|
|
|
|
case eCSSProperty_stroke_opacity:
|
2009-10-02 14:37:25 -07:00
|
|
|
case eCSSProperty_stroke_width:
|
2009-10-21 14:57:57 -07:00
|
|
|
case eCSSProperty_text_anchor:
|
|
|
|
case eCSSProperty_text_decoration:
|
|
|
|
case eCSSProperty_text_rendering:
|
|
|
|
case eCSSProperty_visibility:
|
2009-10-02 14:37:25 -07:00
|
|
|
case eCSSProperty_word_spacing:
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
// EXPLICITLY NON-ANIMATABLE PROPERTIES:
|
|
|
|
// (Some of these aren't supported at all in Gecko -- I've commented those
|
|
|
|
// ones out. If/when we add support for them, uncomment their line here)
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
// case eCSSProperty_enable_background:
|
|
|
|
// case eCSSProperty_glyph_orientation_horizontal:
|
|
|
|
// case eCSSProperty_glyph_orientation_vertical:
|
|
|
|
// case eCSSProperty_writing_mode:
|
|
|
|
case eCSSProperty_direction:
|
|
|
|
case eCSSProperty_unicode_bidi:
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|