Bug 1174575 - Part 3: Implement KeyframeEffectReadOnly::GetTarget(). r=birtles

Implement GetTarget() and functions of CSSPseudoElement.
We use a strong reference from CSSPseudoElement to Element and a non-owning
reference from Element to CSSPseudoElement.
This commit is contained in:
Boris Chiou 2016-02-01 23:59:00 +01:00
parent 69302b6cb7
commit 9b88232de7
4 changed files with 126 additions and 13 deletions

View File

@ -6,15 +6,36 @@
#include "mozilla/dom/CSSPseudoElement.h"
#include "mozilla/dom/CSSPseudoElementBinding.h"
#include "mozilla/dom/Element.h"
namespace mozilla {
namespace dom {
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(CSSPseudoElement)
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(CSSPseudoElement, mParentElement)
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(CSSPseudoElement, AddRef)
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(CSSPseudoElement, Release)
CSSPseudoElement::CSSPseudoElement(Element* aElement,
nsCSSPseudoElements::Type aType)
: mParentElement(aElement)
, mPseudoType(aType)
{
MOZ_ASSERT(aElement);
MOZ_ASSERT(aType == nsCSSPseudoElements::ePseudo_after ||
aType == nsCSSPseudoElements::ePseudo_before,
"Unexpected Pseudo Type");
}
CSSPseudoElement::~CSSPseudoElement()
{
// Element might have been unlinked already, so we have to do null check.
if (mParentElement) {
mParentElement->DeleteProperty(
GetCSSPseudoElementPropertyAtom(mPseudoType));
}
}
JSObject*
CSSPseudoElement::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
@ -40,5 +61,51 @@ CSSPseudoElement::Animate(
return nullptr;
}
/* static */ already_AddRefed<CSSPseudoElement>
CSSPseudoElement::GetCSSPseudoElement(Element* aElement,
nsCSSPseudoElements::Type aType)
{
if (!aElement) {
return nullptr;
}
nsIAtom* propName = CSSPseudoElement::GetCSSPseudoElementPropertyAtom(aType);
RefPtr<CSSPseudoElement> pseudo =
static_cast<CSSPseudoElement*>(aElement->GetProperty(propName));
if (pseudo) {
return pseudo.forget();
}
// CSSPseudoElement is a purely external interface created on-demand, and
// when all references from script to the pseudo are dropped, we can drop the
// CSSPseudoElement object, so use a non-owning reference from Element to
// CSSPseudoElement.
pseudo = new CSSPseudoElement(aElement, aType);
nsresult rv = aElement->SetProperty(propName, pseudo, nullptr, true);
if (NS_FAILED(rv)) {
NS_WARNING("SetProperty failed");
return nullptr;
}
return pseudo.forget();
}
/* static */ nsIAtom*
CSSPseudoElement::GetCSSPseudoElementPropertyAtom(
nsCSSPseudoElements::Type aType)
{
switch (aType) {
case nsCSSPseudoElements::ePseudo_before:
return nsGkAtoms::cssPseudoElementBeforeProperty;
case nsCSSPseudoElements::ePseudo_after:
return nsGkAtoms::cssPseudoElementAfterProperty;
default:
NS_NOTREACHED("Should not try to get CSSPseudoElement "
"other than ::before or ::after");
return nullptr;
}
}
} // namespace dom
} // namespace mozilla

View File

@ -11,6 +11,8 @@
#include "mozilla/Attributes.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/RefPtr.h"
#include "nsCSSPseudoElements.h"
#include "nsWrapperCache.h"
namespace mozilla {
@ -27,20 +29,29 @@ public:
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(CSSPseudoElement)
protected:
virtual ~CSSPseudoElement() = default;
virtual ~CSSPseudoElement();
public:
ParentObject GetParentObject() const
{
// This will be implemented in later patch.
return ParentObject(nullptr, nullptr);
return mParentElement->GetParentObject();
}
virtual JSObject* WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) override;
void GetType(nsString& aRetVal) const { }
already_AddRefed<Element> ParentElement() const { return nullptr; }
void GetType(nsString& aRetVal) const
{
MOZ_ASSERT(nsCSSPseudoElements::GetPseudoAtom(mPseudoType),
"All pseudo-types allowed by this class should have a"
" corresponding atom");
nsCSSPseudoElements::GetPseudoAtom(mPseudoType)->ToString(aRetVal);
}
already_AddRefed<Element> ParentElement() const
{
RefPtr<Element> retVal(mParentElement);
return retVal.forget();
}
void GetAnimations(nsTArray<RefPtr<Animation>>& aRetVal);
already_AddRefed<Animation>
@ -48,6 +59,26 @@ public:
JS::Handle<JSObject*> aFrames,
const UnrestrictedDoubleOrKeyframeAnimationOptions& aOptions,
ErrorResult& aError);
// Given an element:pseudoType pair, returns the CSSPseudoElement stored as a
// property on |aElement|. If there is no CSSPseudoElement for the specified
// pseudo-type on element, a new CSSPseudoElement will be created and stored
// on the element.
static already_AddRefed<CSSPseudoElement>
GetCSSPseudoElement(Element* aElement, nsCSSPseudoElements::Type aType);
private:
// Only ::before and ::after are supported.
CSSPseudoElement(Element* aElement, nsCSSPseudoElements::Type aType);
static nsIAtom*
GetCSSPseudoElementPropertyAtom(nsCSSPseudoElements::Type aType);
// mParentElement needs to be an owning reference since if script is holding
// on to the pseudo-element, it needs to continue to be able to refer to
// the parent element.
RefPtr<Element> mParentElement;
nsCSSPseudoElements::Type mPseudoType;
};
} // namespace dom

View File

@ -1663,13 +1663,26 @@ void
KeyframeEffectReadOnly::GetTarget(
Nullable<OwningElementOrCSSPseudoElement>& aRv) const
{
// Currently we never return animations from the API whose effect
// targets a pseudo-element so this should never be called when
// mPseudoType is not 'none' (see bug 1174575).
MOZ_ASSERT(mPseudoType == nsCSSPseudoElements::ePseudo_NotPseudoElement,
"Requesting the target of a KeyframeEffect that targets a"
" pseudo-element is not yet supported.");
aRv.Value().SetAsElement() = mTarget;
if (!mTarget) {
aRv.SetNull();
return;
}
switch (mPseudoType) {
case nsCSSPseudoElements::ePseudo_before:
case nsCSSPseudoElements::ePseudo_after:
aRv.SetValue().SetAsCSSPseudoElement() =
CSSPseudoElement::GetCSSPseudoElement(mTarget, mPseudoType);
break;
case nsCSSPseudoElements::ePseudo_NotPseudoElement:
aRv.SetValue().SetAsElement() = mTarget;
break;
default:
NS_NOTREACHED("Animation of unsupported pseudo-type");
aRv.SetNull();
}
}
void

View File

@ -2112,6 +2112,8 @@ GK_ATOM(animationsOfAfterProperty, "AnimationsOfAfterProperty") // FrameAnimatio
GK_ATOM(animationEffectsProperty, "AnimationEffectsProperty") // EffectSet*
GK_ATOM(animationEffectsForBeforeProperty, "AnimationsEffectsForBeforeProperty") // EffectSet*
GK_ATOM(animationEffectsForAfterProperty, "AnimationsEffectsForAfterProperty") // EffectSet*
GK_ATOM(cssPseudoElementBeforeProperty, "CSSPseudoElementBeforeProperty") // CSSPseudoElement*
GK_ATOM(cssPseudoElementAfterProperty, "CSSPseudoElementAfterProperty") // CSSPseudoElement*
GK_ATOM(transitionsProperty, "TransitionsProperty") // FrameTransitions*
GK_ATOM(transitionsOfBeforeProperty, "TransitionsOfBeforeProperty") // FrameTransitions*
GK_ATOM(transitionsOfAfterProperty, "TransitionsOfAfterProperty") // FrameTransitions*