Merge mozilla-central to autoland. a=merge CLOSED TREE
[gecko.git] / dom / animation / AnimationUtils.cpp
blobdfb6ce1b46f24d253d57d3c24359ed9c7f971e6b
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 #include "AnimationUtils.h"
9 #include "mozilla/dom/Animation.h"
10 #include "mozilla/dom/Document.h"
11 #include "mozilla/dom/KeyframeEffect.h"
12 #include "mozilla/EffectSet.h"
13 #include "nsDebug.h"
14 #include "nsAtom.h"
15 #include "nsIContent.h"
16 #include "nsLayoutUtils.h"
17 #include "nsGlobalWindowInner.h"
18 #include "nsString.h"
19 #include "xpcpublic.h" // For xpc::NativeGlobal
21 using namespace mozilla::dom;
23 namespace mozilla {
25 /* static */
26 void AnimationUtils::LogAsyncAnimationFailure(nsCString& aMessage,
27 const nsIContent* aContent) {
28 if (aContent) {
29 aMessage.AppendLiteral(" [");
30 aMessage.Append(nsAtomCString(aContent->NodeInfo()->NameAtom()));
32 nsAtom* id = aContent->GetID();
33 if (id) {
34 aMessage.AppendLiteral(" with id '");
35 aMessage.Append(nsAtomCString(aContent->GetID()));
36 aMessage.Append('\'');
38 aMessage.Append(']');
40 aMessage.Append('\n');
41 printf_stderr("%s", aMessage.get());
44 /* static */
45 Document* AnimationUtils::GetCurrentRealmDocument(JSContext* aCx) {
46 nsGlobalWindowInner* win = xpc::CurrentWindowOrNull(aCx);
47 if (!win) {
48 return nullptr;
50 return win->GetDoc();
53 /* static */
54 Document* AnimationUtils::GetDocumentFromGlobal(JSObject* aGlobalObject) {
55 nsGlobalWindowInner* win = xpc::WindowOrNull(aGlobalObject);
56 if (!win) {
57 return nullptr;
59 return win->GetDoc();
62 /* static */
63 bool AnimationUtils::FrameHasAnimatedScale(const nsIFrame* aFrame) {
64 EffectSet* effectSet = EffectSet::GetForFrame(
65 aFrame, nsCSSPropertyIDSet::TransformLikeProperties());
66 if (!effectSet) {
67 return false;
70 for (const dom::KeyframeEffect* effect : *effectSet) {
71 if (effect->ContainsAnimatedScale(aFrame)) {
72 return true;
76 return false;
79 /* static */
80 bool AnimationUtils::HasCurrentTransitions(const Element* aElement,
81 PseudoStyleType aPseudoType) {
82 MOZ_ASSERT(aElement);
84 EffectSet* effectSet = EffectSet::Get(aElement, aPseudoType);
85 if (!effectSet) {
86 return false;
89 for (const dom::KeyframeEffect* effect : *effectSet) {
90 // If |effect| is current, it must have an associated Animation
91 // so we don't need to null-check the result of GetAnimation().
92 if (effect->IsCurrent() && effect->GetAnimation()->AsCSSTransition()) {
93 return true;
97 return false;
100 /*static*/ Element* AnimationUtils::GetElementForRestyle(
101 Element* aElement, PseudoStyleType aPseudoType) {
102 if (aPseudoType == PseudoStyleType::NotPseudo) {
103 return aElement;
106 if (aPseudoType == PseudoStyleType::before) {
107 return nsLayoutUtils::GetBeforePseudo(aElement);
110 if (aPseudoType == PseudoStyleType::after) {
111 return nsLayoutUtils::GetAfterPseudo(aElement);
114 if (aPseudoType == PseudoStyleType::marker) {
115 return nsLayoutUtils::GetMarkerPseudo(aElement);
118 MOZ_ASSERT_UNREACHABLE(
119 "Should not try to get the element to restyle for a pseudo other that "
120 ":before, :after or ::marker");
121 return nullptr;
124 /*static*/ std::pair<const Element*, PseudoStyleType>
125 AnimationUtils::GetElementPseudoPair(const Element* aElementOrPseudo) {
126 MOZ_ASSERT(aElementOrPseudo);
128 if (aElementOrPseudo->IsGeneratedContentContainerForBefore()) {
129 return {aElementOrPseudo->GetParent()->AsElement(),
130 PseudoStyleType::before};
133 if (aElementOrPseudo->IsGeneratedContentContainerForAfter()) {
134 return {aElementOrPseudo->GetParent()->AsElement(), PseudoStyleType::after};
137 if (aElementOrPseudo->IsGeneratedContentContainerForMarker()) {
138 return {aElementOrPseudo->GetParent()->AsElement(),
139 PseudoStyleType::marker};
142 return {aElementOrPseudo, PseudoStyleType::NotPseudo};
145 } // namespace mozilla