From f575c3bb9f37c7beba3950b33aeb05f94f8f77a8 Mon Sep 17 00:00:00 2001 From: Olli Pettay Date: Thu, 7 Mar 2024 12:52:30 +0000 Subject: [PATCH] Bug 1883138 - Ensure HasParserNotified returns true only when parser has actually notified, r=hsivonen Differential Revision: https://phabricator.services.mozilla.com/D203324 --- dom/base/Document.h | 7 ++++++- dom/base/MutationObservers.cpp | 3 --- parser/html/nsHtml5TreeOperation.cpp | 11 +++++++---- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/dom/base/Document.h b/dom/base/Document.h index 8eccf66133f3..a52c61addffc 100644 --- a/dom/base/Document.h +++ b/dom/base/Document.h @@ -3878,7 +3878,12 @@ class Document : public nsINode, void SetAllowDeclarativeShadowRoots(bool aAllowDeclarativeShadowRoots); bool AllowsDeclarativeShadowRoots() const; - void SuspendDOMNotifications() { mSuspendDOMNotifications = true; } + void SuspendDOMNotifications() { + MOZ_ASSERT(IsHTMLDocument(), + "Currently suspending DOM notifications is supported only on " + "HTML documents."); + mSuspendDOMNotifications = true; + } void ResumeDOMNotifications() { mSuspendDOMNotifications = false; } diff --git a/dom/base/MutationObservers.cpp b/dom/base/MutationObservers.cpp index a4bcdf88d4dd..e3e7c8b02ef9 100644 --- a/dom/base/MutationObservers.cpp +++ b/dom/base/MutationObservers.cpp @@ -81,9 +81,6 @@ template OwnerDoc(); - if (doc->DOMNotificationsSuspended()) { - return; - } nsDOMMutationEnterLeave enterLeave(doc); #ifdef DEBUG diff --git a/parser/html/nsHtml5TreeOperation.cpp b/parser/html/nsHtml5TreeOperation.cpp index cb6fda5a5b54..9adfad34bdeb 100644 --- a/parser/html/nsHtml5TreeOperation.cpp +++ b/parser/html/nsHtml5TreeOperation.cpp @@ -257,9 +257,10 @@ nsresult nsHtml5TreeOperation::Append(nsIContent* aNode, nsIContent* aParent, MOZ_ASSERT(aBuilder); MOZ_ASSERT(aBuilder->IsInDocUpdate()); ErrorResult rv; - nsHtml5OtherDocUpdate update(aParent->OwnerDoc(), aBuilder->GetDocument()); + Document* ownerDoc = aParent->OwnerDoc(); + nsHtml5OtherDocUpdate update(ownerDoc, aBuilder->GetDocument()); aParent->AppendChildTo(aNode, false, rv); - if (!rv.Failed()) { + if (!rv.Failed() && !ownerDoc->DOMNotificationsSuspended()) { aNode->SetParserHasNotified(); MutationObservers::NotifyContentAppended(aParent, aNode); } @@ -303,8 +304,10 @@ nsresult nsHtml5TreeOperation::AppendToDocument( return rv.StealNSResult(); } - aNode->SetParserHasNotified(); - MutationObservers::NotifyContentInserted(doc, aNode); + if (!doc->DOMNotificationsSuspended()) { + aNode->SetParserHasNotified(); + MutationObservers::NotifyContentInserted(doc, aNode); + } NS_ASSERTION(!nsContentUtils::IsSafeToRunScript(), "Someone forgot to block scripts");