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 "mozilla/dom/CSSPseudoElement.h"
8 #include "mozilla/dom/CSSPseudoElementBinding.h"
9 #include "mozilla/dom/Element.h"
10 #include "mozilla/dom/KeyframeEffectBinding.h"
11 #include "mozilla/AnimationComparator.h"
13 namespace mozilla::dom
{
15 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(CSSPseudoElement
, mOriginatingElement
)
17 CSSPseudoElement::CSSPseudoElement(dom::Element
* aElement
,
18 PseudoStyleType aType
)
19 : mOriginatingElement(aElement
), mPseudoType(aType
) {
21 MOZ_ASSERT(AnimationUtils::IsSupportedPseudoForAnimations(aType
),
22 "Unexpected Pseudo Type");
25 CSSPseudoElement::~CSSPseudoElement() {
26 // Element might have been unlinked already, so we have to do null check.
27 if (mOriginatingElement
) {
28 mOriginatingElement
->RemoveProperty(
29 GetCSSPseudoElementPropertyAtom(mPseudoType
));
33 ParentObject
CSSPseudoElement::GetParentObject() const {
34 return mOriginatingElement
->GetParentObject();
37 JSObject
* CSSPseudoElement::WrapObject(JSContext
* aCx
,
38 JS::Handle
<JSObject
*> aGivenProto
) {
39 return CSSPseudoElement_Binding::Wrap(aCx
, this, aGivenProto
);
43 already_AddRefed
<CSSPseudoElement
> CSSPseudoElement::GetCSSPseudoElement(
44 dom::Element
* aElement
, PseudoStyleType aType
) {
49 nsAtom
* propName
= CSSPseudoElement::GetCSSPseudoElementPropertyAtom(aType
);
50 RefPtr
<CSSPseudoElement
> pseudo
=
51 static_cast<CSSPseudoElement
*>(aElement
->GetProperty(propName
));
53 return pseudo
.forget();
56 // CSSPseudoElement is a purely external interface created on-demand, and
57 // when all references from script to the pseudo are dropped, we can drop the
58 // CSSPseudoElement object, so use a non-owning reference from Element to
60 pseudo
= new CSSPseudoElement(aElement
, aType
);
61 nsresult rv
= aElement
->SetProperty(propName
, pseudo
, nullptr, true);
63 NS_WARNING("SetProperty failed");
66 return pseudo
.forget();
70 nsAtom
* CSSPseudoElement::GetCSSPseudoElementPropertyAtom(
71 PseudoStyleType aType
) {
73 case PseudoStyleType::before
:
74 return nsGkAtoms::cssPseudoElementBeforeProperty
;
76 case PseudoStyleType::after
:
77 return nsGkAtoms::cssPseudoElementAfterProperty
;
79 case PseudoStyleType::marker
:
80 return nsGkAtoms::cssPseudoElementMarkerProperty
;
83 MOZ_ASSERT_UNREACHABLE(
84 "Should not try to get CSSPseudoElement "
85 "other than ::before, ::after or ::marker");
90 } // namespace mozilla::dom