Bug 857102 part 4 - Make NS_NewCommentNode and nsIDocument::CreateCommentNode infallible; r=bz

This commit is contained in:
Aryeh Gregor 2013-04-04 15:02:22 +03:00
Родитель b26de96c21
Коммит 337756f144
10 изменённых файлов: 37 добавлений и 59 удалений

Просмотреть файл

@ -30,12 +30,6 @@ NS_NewElement(nsIContent** aResult,
nsresult
NS_NewXMLElement(nsIContent** aResult, already_AddRefed<nsINodeInfo> aNodeInfo);
/**
* aNodeInfoManager must not be null.
*/
nsresult
NS_NewCommentNode(nsIContent **aResult, nsNodeInfoManager *aNodeInfoManager);
nsresult
NS_NewHTMLElement(nsIContent** aResult, already_AddRefed<nsINodeInfo> aNodeInfo,
mozilla::dom::FromParser aFromParser);

Просмотреть файл

@ -1954,7 +1954,7 @@ public:
CreateDocumentFragment(mozilla::ErrorResult& rv) const;
already_AddRefed<nsTextNode> CreateTextNode(const nsAString& aData) const;
already_AddRefed<mozilla::dom::Comment>
CreateComment(const nsAString& aData, mozilla::ErrorResult& rv) const;
CreateComment(const nsAString& aData) const;
already_AddRefed<mozilla::dom::ProcessingInstruction>
CreateProcessingInstruction(const nsAString& target, const nsAString& data,
mozilla::ErrorResult& rv) const;

Просмотреть файл

@ -15,27 +15,6 @@
using namespace mozilla;
using namespace dom;
nsresult
NS_NewCommentNode(nsIContent** aInstancePtrResult,
nsNodeInfoManager *aNodeInfoManager)
{
NS_PRECONDITION(aNodeInfoManager, "Missing nodeinfo manager");
*aInstancePtrResult = nullptr;
nsCOMPtr<nsINodeInfo> ni = aNodeInfoManager->GetCommentNodeInfo();
NS_ENSURE_TRUE(ni, NS_ERROR_OUT_OF_MEMORY);
Comment *instance = new Comment(ni.forget());
if (!instance) {
return NS_ERROR_OUT_OF_MEMORY;
}
NS_ADDREF(*aInstancePtrResult = instance);
return NS_OK;
}
namespace mozilla {
namespace dom {

Просмотреть файл

@ -15,14 +15,27 @@ namespace dom {
class Comment : public nsGenericDOMDataNode,
public nsIDOMComment
{
public:
Comment(already_AddRefed<nsINodeInfo> aNodeInfo)
: nsGenericDOMDataNode(aNodeInfo)
private:
void Init()
{
NS_ABORT_IF_FALSE(mNodeInfo->NodeType() == nsIDOMNode::COMMENT_NODE,
"Bad NodeType in aNodeInfo");
SetIsDOMBinding();
}
public:
Comment(already_AddRefed<nsINodeInfo> aNodeInfo)
: nsGenericDOMDataNode(aNodeInfo)
{
Init();
}
Comment(nsNodeInfoManager* aNodeInfoManager)
: nsGenericDOMDataNode(aNodeInfoManager->GetCommentNodeInfo())
{
Init();
}
virtual ~Comment();
// nsISupports

Просмотреть файл

@ -4811,25 +4811,19 @@ nsIDocument::CreateDocumentFragment(ErrorResult& rv) const
NS_IMETHODIMP
nsDocument::CreateComment(const nsAString& aData, nsIDOMComment** aReturn)
{
ErrorResult rv;
*aReturn = nsIDocument::CreateComment(aData, rv).get();
return rv.ErrorCode();
*aReturn = nsIDocument::CreateComment(aData).get();
return NS_OK;
}
// Unfortunately, bareword "Comment" is ambiguous with some Mac system headers.
already_AddRefed<dom::Comment>
nsIDocument::CreateComment(const nsAString& aData, ErrorResult& rv) const
nsIDocument::CreateComment(const nsAString& aData) const
{
nsCOMPtr<nsIContent> comment;
nsresult res = NS_NewCommentNode(getter_AddRefs(comment), mNodeInfoManager);
if (NS_FAILED(res)) {
rv.Throw(res);
return nullptr;
}
nsRefPtr<dom::Comment> comment = new dom::Comment(mNodeInfoManager);
// Don't notify; this node is still being created.
comment->SetText(aData, false);
return static_cast<dom::Comment*>(comment.forget().get());
return comment.forget();
}
NS_IMETHODIMP

Просмотреть файл

@ -43,6 +43,7 @@
#include "nsIStyleRuleProcessor.h"
#include "nsXBLResourceLoader.h"
#include "mozilla/dom/CDATASection.h"
#include "mozilla/dom/Comment.h"
#include "mozilla/dom/Element.h"
#ifdef MOZ_XUL
@ -1689,12 +1690,11 @@ nsXBLPrototypeBinding::ReadContentNode(nsIObjectInputStream* aStream,
content = new CDATASection(aNim);
break;
case XBLBinding_Serialize_CommentNode:
rv = NS_NewCommentNode(getter_AddRefs(content), aNim);
content = new Comment(aNim);
break;
default:
break;
}
NS_ENSURE_SUCCESS(rv, rv);
nsAutoString text;
rv = aStream->ReadString(text);

Просмотреть файл

@ -58,6 +58,7 @@
#include "nsHtml5SVGLoadDispatcher.h"
#include "nsTextNode.h"
#include "mozilla/dom/CDATASection.h"
#include "mozilla/dom/Comment.h"
#include "mozilla/dom/ProcessingInstruction.h"
using namespace mozilla::dom;
@ -1134,13 +1135,10 @@ nsXMLContentSink::HandleComment(const PRUnichar *aName)
{
FlushText();
nsCOMPtr<nsIContent> comment;
nsresult rv = NS_NewCommentNode(getter_AddRefs(comment), mNodeInfoManager);
if (comment) {
comment->SetText(nsDependentString(aName), false);
rv = AddContentAsLeaf(comment);
DidAddContent();
}
nsRefPtr<Comment> comment = new Comment(mNodeInfoManager);
comment->SetText(nsDependentString(aName), false);
nsresult rv = AddContentAsLeaf(comment);
DidAddContent();
return NS_SUCCEEDED(rv) ? DidProcessATokenImpl() : rv;
}

Просмотреть файл

@ -41,6 +41,7 @@
#include "nsIFrame.h"
#include <algorithm>
#include "nsTextNode.h"
#include "mozilla/dom/Comment.h"
#include "mozilla/dom/ProcessingInstruction.h"
using namespace mozilla::dom;
@ -194,9 +195,7 @@ txMozillaXMLOutput::comment(const nsString& aData)
TX_ENSURE_CURRENTNODE;
nsCOMPtr<nsIContent> comment;
rv = NS_NewCommentNode(getter_AddRefs(comment), mNodeInfoManager);
NS_ENSURE_SUCCESS(rv, rv);
nsRefPtr<Comment> comment = new Comment(mNodeInfoManager);
rv = comment->SetText(aData, false);
NS_ENSURE_SUCCESS(rv, rv);

Просмотреть файл

@ -50,7 +50,7 @@ interface Document : Node {
DocumentFragment createDocumentFragment();
[Creator]
Text createTextNode(DOMString data);
[Creator, Throws]
[Creator]
Comment createComment(DOMString data);
[Creator, Throws]
ProcessingInstruction createProcessingInstruction(DOMString target, DOMString data);

Просмотреть файл

@ -30,6 +30,7 @@
#include "nsIFormProcessor.h"
#include "nsIServiceManager.h"
#include "nsEscape.h"
#include "mozilla/dom/Comment.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/HTMLTemplateElement.h"
#include "nsHtml5SVGLoadDispatcher.h"
@ -516,8 +517,8 @@ nsHtml5TreeOperation::Perform(nsHtml5TreeOpExecutor* aBuilder,
PRUnichar* buffer = mTwo.unicharPtr;
int32_t length = mFour.integer;
nsCOMPtr<nsIContent> comment;
NS_NewCommentNode(getter_AddRefs(comment), aBuilder->GetNodeInfoManager());
nsRefPtr<dom::Comment> comment =
new dom::Comment(aBuilder->GetNodeInfoManager());
NS_ASSERTION(comment, "Infallible malloc failed?");
rv = comment->SetText(buffer, length, false);
NS_ENSURE_SUCCESS(rv, rv);
@ -528,8 +529,8 @@ nsHtml5TreeOperation::Perform(nsHtml5TreeOpExecutor* aBuilder,
PRUnichar* buffer = mTwo.unicharPtr;
int32_t length = mFour.integer;
nsCOMPtr<nsIContent> comment;
NS_NewCommentNode(getter_AddRefs(comment), aBuilder->GetNodeInfoManager());
nsRefPtr<dom::Comment> comment =
new dom::Comment(aBuilder->GetNodeInfoManager());
NS_ASSERTION(comment, "Infallible malloc failed?");
rv = comment->SetText(buffer, length, false);
NS_ENSURE_SUCCESS(rv, rv);