Bug 1634779 - pt 2. Partially revert Bug 1603006 r=kmag
[gecko.git] / dom / animation / CSSPseudoElement.cpp
blobfe2a70869dbe3636c9a4b09c9946ac79bd3bcae0
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 {
14 namespace dom {
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) {
24 MOZ_ASSERT(aElement);
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);
48 /* static */
49 already_AddRefed<CSSPseudoElement> CSSPseudoElement::GetCSSPseudoElement(
50 dom::Element* aElement, PseudoStyleType aType) {
51 if (!aElement) {
52 return nullptr;
55 nsAtom* propName = CSSPseudoElement::GetCSSPseudoElementPropertyAtom(aType);
56 RefPtr<CSSPseudoElement> pseudo =
57 static_cast<CSSPseudoElement*>(aElement->GetProperty(propName));
58 if (pseudo) {
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
65 // CSSPseudoElement.
66 pseudo = new CSSPseudoElement(aElement, aType);
67 nsresult rv = aElement->SetProperty(propName, pseudo, nullptr, true);
68 if (NS_FAILED(rv)) {
69 NS_WARNING("SetProperty failed");
70 return nullptr;
72 return pseudo.forget();
75 /* static */
76 nsAtom* CSSPseudoElement::GetCSSPseudoElementPropertyAtom(
77 PseudoStyleType aType) {
78 switch (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;
88 default:
89 MOZ_ASSERT_UNREACHABLE(
90 "Should not try to get CSSPseudoElement "
91 "other than ::before, ::after or ::marker");
92 return nullptr;
96 } // namespace dom
97 } // namespace mozilla