diff --git a/content/html/document/src/nsHTMLDocument.cpp b/content/html/document/src/nsHTMLDocument.cpp
index d63a624901c5..3299b66daf43 100644
--- a/content/html/document/src/nsHTMLDocument.cpp
+++ b/content/html/document/src/nsHTMLDocument.cpp
@@ -1126,12 +1126,12 @@ nsHTMLDocument::SetBody(nsGenericHTMLElement* newBody, ErrorResult& rv)
}
// Use DOM methods so that we pass through the appropriate security checks.
- Element* currentBody = GetBodyElement();
+ nsCOMPtr currentBody = GetBodyElement();
if (currentBody) {
root->ReplaceChild(*newBody, *currentBody, rv);
+ } else {
+ root->AppendChild(*newBody, rv);
}
-
- root->AppendChild(*newBody, rv);
}
NS_IMETHODIMP