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"
16 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(CSSPseudoElement
, mOriginatingElement
)
18 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(CSSPseudoElement
, AddRef
)
19 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(CSSPseudoElement
, Release
)
21 CSSPseudoElement::CSSPseudoElement(dom::Element
* aElement
,
22 PseudoStyleType aType
)
23 : mOriginatingElement(aElement
), mPseudoType(aType
) {
25 MOZ_ASSERT(aType
== PseudoStyleType::after
||
26 aType
== PseudoStyleType::before
||
27 aType
== PseudoStyleType::marker
,
28 "Unexpected Pseudo Type");
31 CSSPseudoElement::~CSSPseudoElement() {
32 // Element might have been unlinked already, so we have to do null check.
33 if (mOriginatingElement
) {
34 mOriginatingElement
->RemoveProperty(
35 GetCSSPseudoElementPropertyAtom(mPseudoType
));
39 ParentObject
CSSPseudoElement::GetParentObject() const {
40 return mOriginatingElement
->GetParentObject();
43 JSObject
* CSSPseudoElement::WrapObject(JSContext
* aCx
,
44 JS::Handle
<JSObject
*> aGivenProto
) {
45 return CSSPseudoElement_Binding::Wrap(aCx
, this, aGivenProto
);
49 already_AddRefed
<CSSPseudoElement
> CSSPseudoElement::GetCSSPseudoElement(
50 dom::Element
* aElement
, PseudoStyleType aType
) {
55 nsAtom
* propName
= CSSPseudoElement::GetCSSPseudoElementPropertyAtom(aType
);
56 RefPtr
<CSSPseudoElement
> pseudo
=
57 static_cast<CSSPseudoElement
*>(aElement
->GetProperty(propName
));
59 return pseudo
.forget();
62 // CSSPseudoElement is a purely external interface created on-demand, and
63 // when all references from script to the pseudo are dropped, we can drop the
64 // CSSPseudoElement object, so use a non-owning reference from Element to
66 pseudo
= new CSSPseudoElement(aElement
, aType
);
67 nsresult rv
= aElement
->SetProperty(propName
, pseudo
, nullptr, true);
69 NS_WARNING("SetProperty failed");
72 return pseudo
.forget();
76 nsAtom
* CSSPseudoElement::GetCSSPseudoElementPropertyAtom(
77 PseudoStyleType aType
) {
79 case PseudoStyleType::before
:
80 return nsGkAtoms::cssPseudoElementBeforeProperty
;
82 case PseudoStyleType::after
:
83 return nsGkAtoms::cssPseudoElementAfterProperty
;
85 case PseudoStyleType::marker
:
86 return nsGkAtoms::cssPseudoElementMarkerProperty
;
89 MOZ_ASSERT_UNREACHABLE(
90 "Should not try to get CSSPseudoElement "
91 "other than ::before, ::after or ::marker");
97 } // namespace mozilla