diff --git a/dom/base/MutationObservers.cpp b/dom/base/MutationObservers.cpp index f0a6818814eb..8fd30b2e8772 100644 --- a/dom/base/MutationObservers.cpp +++ b/dom/base/MutationObservers.cpp @@ -60,10 +60,7 @@ enum class IsRemoveNotification { // |Document* doc| #define IMPL_MUTATION_NOTIFICATION(func_, content_, params_, remove_) \ PR_BEGIN_MACRO \ - bool needsEnterLeave = doc->MayHaveDOMMutationObservers(); \ - if (needsEnterLeave) { \ - nsDOMMutationObserver::EnterMutationHandling(); \ - } \ + nsDOMMutationEnterLeave enterLeave(doc); \ nsINode* node = content_; \ COMPOSED_DOC_DECL \ NS_ASSERTION(node->OwnerDoc() == doc, "Bogus document"); \ @@ -98,17 +95,11 @@ enum class IsRemoveNotification { presShell->func_ params_; \ } \ } \ - if (needsEnterLeave) { \ - nsDOMMutationObserver::LeaveMutationHandling(); \ - } \ PR_END_MACRO #define IMPL_ANIMATION_NOTIFICATION(func_, content_, params_) \ PR_BEGIN_MACRO \ - bool needsEnterLeave = doc->MayHaveDOMMutationObservers(); \ - if (needsEnterLeave) { \ - nsDOMMutationObserver::EnterMutationHandling(); \ - } \ + nsDOMMutationEnterLeave enterLeave(doc); \ nsINode* node = content_; \ do { \ nsINode::nsSlots* slots = node->GetExistingSlots(); \ @@ -122,9 +113,6 @@ enum class IsRemoveNotification { node = node->GetParentNode(); \ } \ } while (node); \ - if (needsEnterLeave) { \ - nsDOMMutationObserver::LeaveMutationHandling(); \ - } \ PR_END_MACRO namespace mozilla { diff --git a/dom/base/nsDOMMutationObserver.h b/dom/base/nsDOMMutationObserver.h index ed0ec16339e9..6420c7fdba57 100644 --- a/dom/base/nsDOMMutationObserver.h +++ b/dom/base/nsDOMMutationObserver.h @@ -875,4 +875,22 @@ inline nsDOMMutationObserver* nsMutationReceiverBase::Observer() { : static_cast(mObserver); } +class MOZ_RAII nsDOMMutationEnterLeave { + public: + explicit nsDOMMutationEnterLeave(mozilla::dom::Document* aDoc) + : mNeeded(aDoc->MayHaveDOMMutationObservers()) { + if (mNeeded) { + nsDOMMutationObserver::EnterMutationHandling(); + } + } + ~nsDOMMutationEnterLeave() { + if (mNeeded) { + nsDOMMutationObserver::LeaveMutationHandling(); + } + } + + private: + const bool mNeeded; +}; + #endif