From c91765edda2b983789c8fda683715bc073ba27cf Mon Sep 17 00:00:00 2001 From: Moonchild Date: Thu, 26 Feb 2026 09:48:23 +0100 Subject: [PATCH] [parser] Account for some corner-case structural document issues. --- parser/html/nsHtml5TreeOperation.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/parser/html/nsHtml5TreeOperation.cpp b/parser/html/nsHtml5TreeOperation.cpp index 5e3f01ef3f..8bd423b901 100644 --- a/parser/html/nsHtml5TreeOperation.cpp +++ b/parser/html/nsHtml5TreeOperation.cpp @@ -200,6 +200,13 @@ nsHtml5TreeOperation::Append(nsIContent* aNode, { MOZ_ASSERT(aBuilder); MOZ_ASSERT(aBuilder->IsInDocUpdate()); + if (MOZ_UNLIKELY(aNode->GetParentNode())) { + Detach(aNode, aBuilder); + if (MOZ_UNLIKELY(aNode->GetParentNode())) { + // Can this happen? If it can, give up. + return NS_OK; + } + } nsresult rv = NS_OK; nsHtml5OtherDocUpdate update(aParent->OwnerDoc(), aBuilder->GetDocument()); @@ -219,6 +226,13 @@ nsHtml5TreeOperation::AppendToDocument(nsIContent* aNode, MOZ_ASSERT(aBuilder); MOZ_ASSERT(aBuilder->GetDocument() == aNode->OwnerDoc()); MOZ_ASSERT(aBuilder->IsInDocUpdate()); + if (MOZ_UNLIKELY(aNode->GetParentNode())) { + Detach(aNode, aBuilder); + if (MOZ_UNLIKELY(aNode->GetParentNode())) { + // Can this happen? If it can, give up. + return NS_OK; + } + } nsresult rv = NS_OK; nsIDocument* doc = aBuilder->GetDocument(); @@ -308,6 +322,14 @@ nsHtml5TreeOperation::FosterParent(nsIContent* aNode, { MOZ_ASSERT(aBuilder); MOZ_ASSERT(aBuilder->IsInDocUpdate()); + if (MOZ_UNLIKELY(aNode->GetParentNode())) { + Detach(aNode, aBuilder); + if (MOZ_UNLIKELY(aNode->GetParentNode())) { + // Can this happen? If it can, give up. + return NS_OK; + } + } + nsIContent* foster = aTable->GetParent(); if (IsElementOrTemplateContent(foster)) {