1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_AnimationUtils_h
8 #define mozilla_dom_AnimationUtils_h
10 #include "mozilla/PseudoStyleType.h"
11 #include "mozilla/TimeStamp.h"
12 #include "mozilla/dom/Nullable.h"
13 #include "nsRFPService.h"
14 #include "nsStringFwd.h"
29 class AnimationUtils
{
31 using Document
= dom::Document
;
33 static dom::Nullable
<double> TimeDurationToDouble(
34 const dom::Nullable
<TimeDuration
>& aTime
, RTPCallerType aRTPCallerType
) {
35 dom::Nullable
<double> result
;
37 if (!aTime
.IsNull()) {
38 // 0 is an inappropriate mixin for this this area; however CSS Animations
39 // needs to have it's Time Reduction Logic refactored, so it's currently
40 // only clamping for RFP mode. RFP mode gives a much lower time precision,
41 // so we accept the security leak here for now
42 result
.SetValue(nsRFPService::ReduceTimePrecisionAsMSecsRFPOnly(
43 aTime
.Value().ToMilliseconds(), 0, aRTPCallerType
));
49 static dom::Nullable
<TimeDuration
> DoubleToTimeDuration(
50 const dom::Nullable
<double>& aTime
) {
51 dom::Nullable
<TimeDuration
> result
;
53 if (!aTime
.IsNull()) {
54 result
.SetValue(TimeDuration::FromMilliseconds(aTime
.Value()));
60 static void LogAsyncAnimationFailure(nsCString
& aMessage
,
61 const nsIContent
* aContent
= nullptr);
64 * Get the document from the JS context to use when parsing CSS properties.
66 static Document
* GetCurrentRealmDocument(JSContext
* aCx
);
69 * Get the document from the global object, or nullptr if the document has
70 * no window, to use when constructing DOM object without entering the
71 * target window's compartment (see KeyframeEffect constructor).
73 static Document
* GetDocumentFromGlobal(JSObject
* aGlobalObject
);
76 * Returns true if the given frame has an animated scale.
78 static bool FrameHasAnimatedScale(const nsIFrame
* aFrame
);
81 * Returns true if the given (pseudo-)element has any transitions that are
82 * current (playing or waiting to play) or in effect (e.g. filling forwards).
84 static bool HasCurrentTransitions(const dom::Element
* aElement
,
85 PseudoStyleType aPseudoType
);
88 * Returns true if this pseudo style type is supported by animations.
89 * Note: This doesn't include PseudoStyleType::NotPseudo.
91 static bool IsSupportedPseudoForAnimations(PseudoStyleType aType
) {
92 // FIXME: Bug 1615469: Support first-line and first-letter for Animation.
93 return aType
== PseudoStyleType::before
||
94 aType
== PseudoStyleType::after
|| aType
== PseudoStyleType::marker
;
98 * Returns true if the difference between |aFirst| and |aSecond| is within
99 * the animation time tolerance (i.e. 1 microsecond).
101 static bool IsWithinAnimationTimeTolerance(const TimeDuration
& aFirst
,
102 const TimeDuration
& aSecond
) {
103 if (aFirst
== TimeDuration::Forever() ||
104 aSecond
== TimeDuration::Forever()) {
105 return aFirst
== aSecond
;
108 TimeDuration diff
= aFirst
>= aSecond
? aFirst
- aSecond
: aSecond
- aFirst
;
109 return diff
<= TimeDuration::FromMicroseconds(1);
112 // Returns the target element for restyling.
114 // If |aPseudoType| is ::after, ::before or ::marker, returns the generated
115 // content element of which |aElement| is the parent. If |aPseudoType| is any
116 // other pseudo type (other than PseudoStyleType::NotPseudo) returns nullptr.
117 // Otherwise, returns |aElement|.
118 static dom::Element
* GetElementForRestyle(dom::Element
* aElement
,
119 PseudoStyleType aPseudoType
);
121 // Returns the pair of |Element, PseudoStyleType| from an element which could
122 // be an element or a pseudo element (i.e. an element used for restyling and
125 // Animation module usually uses a pair of (Element*, PseudoStyleType) to
126 // represent the animation target, and the |Element| in the pair is the
127 // generated content container if it's a pseudo element.
128 static std::pair
<const dom::Element
*, PseudoStyleType
> GetElementPseudoPair(
129 const dom::Element
* aElementOrPseudo
);
132 } // namespace mozilla