CSSPseudoElement.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim:set ts=2 sw=2 sts=2 et cindent: */
  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/. */
  6. #include "mozilla/dom/CSSPseudoElement.h"
  7. #include "mozilla/dom/CSSPseudoElementBinding.h"
  8. #include "mozilla/dom/Element.h"
  9. #include "mozilla/dom/KeyframeEffectBinding.h" // for ElementOrCSSPseudoElement class
  10. #include "mozilla/AnimationComparator.h"
  11. namespace mozilla {
  12. namespace dom {
  13. NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(CSSPseudoElement, mParentElement)
  14. NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(CSSPseudoElement, AddRef)
  15. NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(CSSPseudoElement, Release)
  16. CSSPseudoElement::CSSPseudoElement(Element* aElement,
  17. CSSPseudoElementType aType)
  18. : mParentElement(aElement)
  19. , mPseudoType(aType)
  20. {
  21. MOZ_ASSERT(aElement);
  22. MOZ_ASSERT(aType == CSSPseudoElementType::after ||
  23. aType == CSSPseudoElementType::before,
  24. "Unexpected Pseudo Type");
  25. }
  26. CSSPseudoElement::~CSSPseudoElement()
  27. {
  28. // Element might have been unlinked already, so we have to do null check.
  29. if (mParentElement) {
  30. mParentElement->DeleteProperty(
  31. GetCSSPseudoElementPropertyAtom(mPseudoType));
  32. }
  33. }
  34. ParentObject
  35. CSSPseudoElement::GetParentObject() const
  36. {
  37. return mParentElement->GetParentObject();
  38. }
  39. JSObject*
  40. CSSPseudoElement::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  41. {
  42. return CSSPseudoElementBinding::Wrap(aCx, this, aGivenProto);
  43. }
  44. void
  45. CSSPseudoElement::GetAnimations(const AnimationFilter& filter,
  46. nsTArray<RefPtr<Animation>>& aRetVal)
  47. {
  48. nsIDocument* doc = mParentElement->GetComposedDoc();
  49. if (doc) {
  50. doc->FlushPendingNotifications(Flush_Style);
  51. }
  52. Element::GetAnimationsUnsorted(mParentElement, mPseudoType, aRetVal);
  53. aRetVal.Sort(AnimationPtrComparator<RefPtr<Animation>>());
  54. }
  55. already_AddRefed<Animation>
  56. CSSPseudoElement::Animate(
  57. JSContext* aContext,
  58. JS::Handle<JSObject*> aKeyframes,
  59. const UnrestrictedDoubleOrKeyframeAnimationOptions& aOptions,
  60. ErrorResult& aError)
  61. {
  62. Nullable<ElementOrCSSPseudoElement> target;
  63. target.SetValue().SetAsCSSPseudoElement() = this;
  64. return Element::Animate(target, aContext, aKeyframes, aOptions, aError);
  65. }
  66. /* static */ already_AddRefed<CSSPseudoElement>
  67. CSSPseudoElement::GetCSSPseudoElement(Element* aElement,
  68. CSSPseudoElementType aType)
  69. {
  70. if (!aElement) {
  71. return nullptr;
  72. }
  73. nsIAtom* propName = CSSPseudoElement::GetCSSPseudoElementPropertyAtom(aType);
  74. RefPtr<CSSPseudoElement> pseudo =
  75. static_cast<CSSPseudoElement*>(aElement->GetProperty(propName));
  76. if (pseudo) {
  77. return pseudo.forget();
  78. }
  79. // CSSPseudoElement is a purely external interface created on-demand, and
  80. // when all references from script to the pseudo are dropped, we can drop the
  81. // CSSPseudoElement object, so use a non-owning reference from Element to
  82. // CSSPseudoElement.
  83. pseudo = new CSSPseudoElement(aElement, aType);
  84. nsresult rv = aElement->SetProperty(propName, pseudo, nullptr, true);
  85. if (NS_FAILED(rv)) {
  86. NS_WARNING("SetProperty failed");
  87. return nullptr;
  88. }
  89. return pseudo.forget();
  90. }
  91. /* static */ nsIAtom*
  92. CSSPseudoElement::GetCSSPseudoElementPropertyAtom(CSSPseudoElementType aType)
  93. {
  94. switch (aType) {
  95. case CSSPseudoElementType::before:
  96. return nsGkAtoms::cssPseudoElementBeforeProperty;
  97. case CSSPseudoElementType::after:
  98. return nsGkAtoms::cssPseudoElementAfterProperty;
  99. default:
  100. NS_NOTREACHED("Should not try to get CSSPseudoElement "
  101. "other than ::before or ::after");
  102. return nullptr;
  103. }
  104. }
  105. } // namespace dom
  106. } // namespace mozilla