From a077967be8daad0863c1b2f8ff00788f8d89d44a Mon Sep 17 00:00:00 2001 From: "jst%netscape.com" Date: Thu, 14 Dec 2000 22:37:22 +0000 Subject: [PATCH] Adding protection for the casae where there's no root element in a document, bug 58477. r=blakeross@telocity.com, sr=scc@mozilla.org git-svn-id: svn://10.0.0.236/trunk@83661 18797224-902f-48f8-a5cc-f745e15eee43 --- .../html/document/src/nsHTMLDocument.cpp | 91 ++++++++++--------- .../html/document/src/nsHTMLDocument.cpp | 91 ++++++++++--------- 2 files changed, 98 insertions(+), 84 deletions(-) diff --git a/mozilla/content/html/document/src/nsHTMLDocument.cpp b/mozilla/content/html/document/src/nsHTMLDocument.cpp index 1839bc85ebf..83be6fcf746 100644 --- a/mozilla/content/html/document/src/nsHTMLDocument.cpp +++ b/mozilla/content/html/document/src/nsHTMLDocument.cpp @@ -1854,42 +1854,49 @@ nsHTMLDocument::GetBody(nsIDOMHTMLElement** aBody) NS_IMETHODIMP nsHTMLDocument::SetBody(nsIDOMHTMLElement* aBody) { - nsresult result = NS_OK; - nsIDOMElement * root = nsnull; - result = GetDocumentElement(&root); - if (NS_OK != result) { - return result; + nsCOMPtr bodyElement(do_QueryInterface(aBody)); + + // The body element must be of type nsIDOMHTMLBodyElement. + if (!bodyElement) { + return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; } - nsAutoString bodyStr; bodyStr.AssignWithConversion("BODY"); - nsIDOMNode * child; - root->GetFirstChild(&child); + nsCOMPtr root; + GetDocumentElement(getter_AddRefs(root)); + + if (!root) { + return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; + } + + nsAutoString bodyStr; + bodyStr.AssignWithConversion("BODY"); + + nsCOMPtr child; + root->GetFirstChild(getter_AddRefs(child)); + + while (child) { + nsCOMPtr domElement(do_QueryInterface(child)); + + if (domElement) { + nsAutoString tagName; - while (child != nsnull) { - nsIDOMElement* domElement; - result = child->QueryInterface(NS_GET_IID(nsIDOMElement),(void **)&domElement); - if (NS_OK == result) { - nsString tagName; domElement->GetTagName(tagName); + if (bodyStr.EqualsIgnoreCase(tagName)) { - nsIDOMNode* ret; - - result = root->ReplaceChild(aBody, child, &ret); - NS_IF_RELEASE(ret); + nsCOMPtr ret; + + nsresult rv = root->ReplaceChild(aBody, child, getter_AddRefs(ret)); + NS_IF_RELEASE(mBodyContent); - NS_RELEASE(child); - NS_RELEASE(root); - NS_RELEASE(domElement); - return result; + return rv; } - NS_RELEASE(domElement); } - nsIDOMNode * node = child; - NS_RELEASE(child); - node->GetNextSibling(&child); + + nsIDOMNode *tmpNode = child; + tmpNode->GetNextSibling(getter_AddRefs(child)); } - NS_RELEASE(root); + return PR_FALSE; } @@ -4090,34 +4097,34 @@ NS_IMETHODIMP nsHTMLDocument::FindNext(const nsAReadableString &aSearchStr, PRBool nsHTMLDocument::GetBodyContent() { - nsIDOMElement * root = nsnull; - if (NS_OK != GetDocumentElement(&root)) { + nsCOMPtr root; + + GetDocumentElement(getter_AddRefs(root)); + + if (!root) { return PR_FALSE; } nsAutoString bodyStr; bodyStr.AssignWithConversion("BODY"); - nsIDOMNode * child; - root->GetFirstChild(&child); + nsCOMPtr child; + root->GetFirstChild(getter_AddRefs(child)); - while (child != nsnull) { - nsIDOMElement* domElement; - nsresult rv = child->QueryInterface(NS_GET_IID(nsIDOMElement),(void **)&domElement); - if (NS_OK == rv) { - nsString tagName; + while (child) { + nsCOMPtr domElement(do_QueryInterface(child)); + + if (domElement) { + nsAutoString tagName; domElement->GetTagName(tagName); + if (bodyStr.EqualsIgnoreCase(tagName)) { mBodyContent = child; - NS_RELEASE(root); - NS_RELEASE(domElement); return PR_TRUE; } - NS_RELEASE(domElement); } - nsIDOMNode * node = child; - NS_RELEASE(child); - node->GetNextSibling(&child); + nsIDOMNode *tmpNode = child; + tmpNode->GetNextSibling(getter_AddRefs(child)); } - NS_RELEASE(root); + return PR_FALSE; } diff --git a/mozilla/layout/html/document/src/nsHTMLDocument.cpp b/mozilla/layout/html/document/src/nsHTMLDocument.cpp index 1839bc85ebf..83be6fcf746 100644 --- a/mozilla/layout/html/document/src/nsHTMLDocument.cpp +++ b/mozilla/layout/html/document/src/nsHTMLDocument.cpp @@ -1854,42 +1854,49 @@ nsHTMLDocument::GetBody(nsIDOMHTMLElement** aBody) NS_IMETHODIMP nsHTMLDocument::SetBody(nsIDOMHTMLElement* aBody) { - nsresult result = NS_OK; - nsIDOMElement * root = nsnull; - result = GetDocumentElement(&root); - if (NS_OK != result) { - return result; + nsCOMPtr bodyElement(do_QueryInterface(aBody)); + + // The body element must be of type nsIDOMHTMLBodyElement. + if (!bodyElement) { + return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; } - nsAutoString bodyStr; bodyStr.AssignWithConversion("BODY"); - nsIDOMNode * child; - root->GetFirstChild(&child); + nsCOMPtr root; + GetDocumentElement(getter_AddRefs(root)); + + if (!root) { + return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; + } + + nsAutoString bodyStr; + bodyStr.AssignWithConversion("BODY"); + + nsCOMPtr child; + root->GetFirstChild(getter_AddRefs(child)); + + while (child) { + nsCOMPtr domElement(do_QueryInterface(child)); + + if (domElement) { + nsAutoString tagName; - while (child != nsnull) { - nsIDOMElement* domElement; - result = child->QueryInterface(NS_GET_IID(nsIDOMElement),(void **)&domElement); - if (NS_OK == result) { - nsString tagName; domElement->GetTagName(tagName); + if (bodyStr.EqualsIgnoreCase(tagName)) { - nsIDOMNode* ret; - - result = root->ReplaceChild(aBody, child, &ret); - NS_IF_RELEASE(ret); + nsCOMPtr ret; + + nsresult rv = root->ReplaceChild(aBody, child, getter_AddRefs(ret)); + NS_IF_RELEASE(mBodyContent); - NS_RELEASE(child); - NS_RELEASE(root); - NS_RELEASE(domElement); - return result; + return rv; } - NS_RELEASE(domElement); } - nsIDOMNode * node = child; - NS_RELEASE(child); - node->GetNextSibling(&child); + + nsIDOMNode *tmpNode = child; + tmpNode->GetNextSibling(getter_AddRefs(child)); } - NS_RELEASE(root); + return PR_FALSE; } @@ -4090,34 +4097,34 @@ NS_IMETHODIMP nsHTMLDocument::FindNext(const nsAReadableString &aSearchStr, PRBool nsHTMLDocument::GetBodyContent() { - nsIDOMElement * root = nsnull; - if (NS_OK != GetDocumentElement(&root)) { + nsCOMPtr root; + + GetDocumentElement(getter_AddRefs(root)); + + if (!root) { return PR_FALSE; } nsAutoString bodyStr; bodyStr.AssignWithConversion("BODY"); - nsIDOMNode * child; - root->GetFirstChild(&child); + nsCOMPtr child; + root->GetFirstChild(getter_AddRefs(child)); - while (child != nsnull) { - nsIDOMElement* domElement; - nsresult rv = child->QueryInterface(NS_GET_IID(nsIDOMElement),(void **)&domElement); - if (NS_OK == rv) { - nsString tagName; + while (child) { + nsCOMPtr domElement(do_QueryInterface(child)); + + if (domElement) { + nsAutoString tagName; domElement->GetTagName(tagName); + if (bodyStr.EqualsIgnoreCase(tagName)) { mBodyContent = child; - NS_RELEASE(root); - NS_RELEASE(domElement); return PR_TRUE; } - NS_RELEASE(domElement); } - nsIDOMNode * node = child; - NS_RELEASE(child); - node->GetNextSibling(&child); + nsIDOMNode *tmpNode = child; + tmpNode->GetNextSibling(getter_AddRefs(child)); } - NS_RELEASE(root); + return PR_FALSE; }