Bug 1149163 part 2 - Make nsDOMIterator infallible; r=froydnj

This commit is contained in:
Aryeh Gregor 2015-04-22 14:26:57 +03:00
parent 458032e3c2
commit 383ea4d52b
4 changed files with 59 additions and 98 deletions

View File

@ -66,91 +66,69 @@ nsAutoSelectionReset::Abort()
* some helper classes for iterating the dom tree
*****************************************************************************/
nsDOMIterator::nsDOMIterator() :
mIter(nullptr)
nsDOMIterator::nsDOMIterator(nsRange& aRange)
{
MOZ_ASSERT(aRange.GetStartParent(), "Invalid range");
mIter = NS_NewContentIterator();
DebugOnly<nsresult> res = mIter->Init(&aRange);
MOZ_ASSERT(NS_SUCCEEDED(res));
}
nsDOMIterator::nsDOMIterator(nsIDOMNode& aNode)
{
mIter = NS_NewContentIterator();
nsCOMPtr<nsINode> node = do_QueryInterface(&aNode);
NS_ENSURE_TRUE(node, );
DebugOnly<nsresult> res = mIter->Init(node);
MOZ_ASSERT(NS_SUCCEEDED(res));
}
nsDOMIterator::nsDOMIterator()
{
}
nsDOMIterator::~nsDOMIterator()
{
}
nsresult
nsDOMIterator::Init(nsRange* aRange)
{
nsresult res;
mIter = do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &res);
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(mIter, NS_ERROR_FAILURE);
return mIter->Init(aRange);
}
nsresult
nsDOMIterator::Init(nsIDOMNode* aNode)
{
nsresult res;
mIter = do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &res);
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(mIter, NS_ERROR_FAILURE);
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
return mIter->Init(content);
}
nsresult
void
nsDOMIterator::AppendList(nsBoolDomIterFunctor& functor,
nsTArray<nsCOMPtr<nsINode>>& arrayOfNodes) const
{
// Iterate through dom and build list
while (!mIter->IsDone()) {
for (; !mIter->IsDone(); mIter->Next()) {
nsCOMPtr<nsINode> node = mIter->GetCurrentNode();
NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
if (functor(node)) {
arrayOfNodes.AppendElement(node);
}
mIter->Next();
}
return NS_OK;
}
nsresult
void
nsDOMIterator::AppendList(nsBoolDomIterFunctor& functor,
nsCOMArray<nsIDOMNode>& arrayOfNodes) const
{
nsCOMPtr<nsIDOMNode> node;
// iterate through dom and build list
while (!mIter->IsDone())
{
node = do_QueryInterface(mIter->GetCurrentNode());
NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
for (; !mIter->IsDone(); mIter->Next()) {
nsCOMPtr<nsIDOMNode> node = mIter->GetCurrentNode()->AsDOMNode();
if (functor(node))
{
if (functor(node)) {
arrayOfNodes.AppendObject(node);
}
mIter->Next();
}
return NS_OK;
}
nsDOMSubtreeIterator::nsDOMSubtreeIterator()
nsDOMSubtreeIterator::nsDOMSubtreeIterator(nsRange& aRange)
{
mIter = NS_NewContentSubtreeIterator();
DebugOnly<nsresult> res = mIter->Init(&aRange);
MOZ_ASSERT(NS_SUCCEEDED(res));
}
nsDOMSubtreeIterator::~nsDOMSubtreeIterator()
{
}
nsresult
nsDOMSubtreeIterator::Init(nsRange* aRange)
{
nsresult res;
mIter = do_CreateInstance("@mozilla.org/content/subtree-content-iterator;1", &res);
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(mIter, NS_ERROR_FAILURE);
return mIter->Init(aRange);
}
/******************************************************************************
* some general purpose editor utils

View File

@ -177,26 +177,26 @@ class nsBoolDomIterFunctor
class MOZ_STACK_CLASS nsDOMIterator
{
public:
nsDOMIterator();
explicit nsDOMIterator(nsRange& aRange);
explicit nsDOMIterator(nsIDOMNode& aNode);
virtual ~nsDOMIterator();
nsresult Init(nsRange* aRange);
nsresult Init(nsIDOMNode* aNode);
nsresult AppendList(nsBoolDomIterFunctor& functor,
nsTArray<nsCOMPtr<nsINode>>& arrayOfNodes) const;
nsresult AppendList(nsBoolDomIterFunctor& functor,
nsCOMArray<nsIDOMNode>& arrayOfNodes) const;
void AppendList(nsBoolDomIterFunctor& functor,
nsTArray<nsCOMPtr<nsINode>>& arrayOfNodes) const;
void AppendList(nsBoolDomIterFunctor& functor,
nsCOMArray<nsIDOMNode>& arrayOfNodes) const;
protected:
nsCOMPtr<nsIContentIterator> mIter;
// For nsDOMSubtreeIterator
nsDOMIterator();
};
class MOZ_STACK_CLASS nsDOMSubtreeIterator : public nsDOMIterator
{
public:
nsDOMSubtreeIterator();
explicit nsDOMSubtreeIterator(nsRange& aRange);
virtual ~nsDOMSubtreeIterator();
nsresult Init(nsRange* aRange);
};
class nsTrivialFunctor : public nsBoolDomIterFunctor

View File

@ -2197,11 +2197,10 @@ nsresult nsHTMLEditor::CreateListOfNodesToPaste(nsIDOMNode *aFragmentAsNode,
// now use a subtree iterator over the range to create a list of nodes
nsTrivialFunctor functor;
nsDOMSubtreeIterator iter;
rv = iter.Init(docFragRange);
NS_ENSURE_SUCCESS(rv, rv);
nsDOMSubtreeIterator iter(*docFragRange);
iter.AppendList(functor, outNodeList);
return iter.AppendList(functor, outNodeList);
return NS_OK;
}
nsresult

View File

@ -2440,11 +2440,8 @@ nsHTMLEditRules::WillDeleteSelection(Selection* aSelection,
// Build a list of nodes in the range
nsTArray<nsCOMPtr<nsINode>> arrayOfNodes;
nsTrivialFunctor functor;
nsDOMSubtreeIterator iter;
res = iter.Init(range);
NS_ENSURE_SUCCESS(res, res);
res = iter.AppendList(functor, arrayOfNodes);
NS_ENSURE_SUCCESS(res, res);
nsDOMSubtreeIterator iter(*range);
iter.AppendList(functor, arrayOfNodes);
// Now that we have the list, delete non-table elements
int32_t listCount = arrayOfNodes.Length();
@ -4933,11 +4930,8 @@ nsHTMLEditRules::AlignInnerBlocks(nsIDOMNode *aNode, const nsAString *alignType)
// gather list of table cells or list items
nsCOMArray<nsIDOMNode> arrayOfNodes;
nsTableCellAndListItemFunctor functor;
nsDOMIterator iter;
res = iter.Init(aNode);
NS_ENSURE_SUCCESS(res, res);
res = iter.AppendList(functor, arrayOfNodes);
NS_ENSURE_SUCCESS(res, res);
nsDOMIterator iter(*aNode);
iter.AppendList(functor, arrayOfNodes);
// now that we have the list, align their contents as requested
int32_t listCount = arrayOfNodes.Count();
@ -5939,13 +5933,10 @@ nsHTMLEditRules::GetNodesForOperation(nsTArray<nsRefPtr<nsRange>>& inArrayOfRang
{
opRange = inArrayOfRanges[i];
nsDOMSubtreeIterator iter;
res = iter.Init(opRange);
NS_ENSURE_SUCCESS(res, res);
nsDOMSubtreeIterator iter(*opRange);
if (outArrayOfNodes.Count() == 0) {
nsTrivialFunctor functor;
res = iter.AppendList(functor, outArrayOfNodes);
NS_ENSURE_SUCCESS(res, res);
iter.AppendList(functor, outArrayOfNodes);
}
else {
// We don't want duplicates in outArrayOfNodes, so we use an
@ -5953,8 +5944,7 @@ nsHTMLEditRules::GetNodesForOperation(nsTArray<nsRefPtr<nsRange>>& inArrayOfRang
// outArrayOfNodes.
nsCOMArray<nsIDOMNode> nodes;
nsUniqueFunctor functor(outArrayOfNodes);
res = iter.AppendList(functor, nodes);
NS_ENSURE_SUCCESS(res, res);
iter.AppendList(functor, nodes);
if (!outArrayOfNodes.AppendObjects(nodes))
return NS_ERROR_OUT_OF_MEMORY;
}
@ -6344,11 +6334,8 @@ nsHTMLEditRules::BustUpInlinesAtBRs(nsIDOMNode *inNode,
// the inline container.
nsCOMArray<nsIDOMNode> arrayOfBreaks;
nsBRNodeFunctor functor;
nsDOMIterator iter;
nsresult res = iter.Init(inNode);
NS_ENSURE_SUCCESS(res, res);
res = iter.AppendList(functor, arrayOfBreaks);
NS_ENSURE_SUCCESS(res, res);
nsDOMIterator iter(*inNode);
iter.AppendList(functor, arrayOfBreaks);
// if there aren't any breaks, just put inNode itself in the array
int32_t listCount = arrayOfBreaks.Count();
@ -6376,7 +6363,7 @@ nsHTMLEditRules::BustUpInlinesAtBRs(nsIDOMNode *inNode,
splitParentNode = GetAsDOMNode(nsEditor::GetNodeLocation(breakNode,
&splitOffset));
NS_ENSURE_STATE(mHTMLEditor);
res = mHTMLEditor->SplitNodeDeep(splitDeepNode, splitParentNode, splitOffset,
nsresult res = mHTMLEditor->SplitNodeDeep(splitDeepNode, splitParentNode, splitOffset,
&resultOffset, false, address_of(leftNode), address_of(rightNode));
NS_ENSURE_SUCCESS(res, res);
// put left node in node list
@ -6404,7 +6391,7 @@ nsHTMLEditRules::BustUpInlinesAtBRs(nsIDOMNode *inNode,
return NS_ERROR_FAILURE;
}
}
return res;
return NS_OK;
}
@ -7622,11 +7609,8 @@ nsHTMLEditRules::AdjustSpecialBreaks(bool aSafeToAskFrames)
// gather list of empty nodes
NS_ENSURE_STATE(mHTMLEditor);
nsEmptyEditableFunctor functor(mHTMLEditor);
nsDOMIterator iter;
nsresult res = iter.Init(mDocChangeRange);
NS_ENSURE_SUCCESS(res, res);
res = iter.AppendList(functor, arrayOfNodes);
NS_ENSURE_SUCCESS(res, res);
nsDOMIterator iter(*mDocChangeRange);
iter.AppendList(functor, arrayOfNodes);
// put moz-br's into these empty li's and td's
nodeCount = arrayOfNodes.Count();
@ -7639,13 +7623,13 @@ nsHTMLEditRules::AdjustSpecialBreaks(bool aSafeToAskFrames)
uint32_t len;
nsCOMPtr<nsIDOMNode> theNode = arrayOfNodes[0];
arrayOfNodes.RemoveObjectAt(0);
res = nsEditor::GetLengthOfDOMNode(theNode, len);
nsresult res = nsEditor::GetLengthOfDOMNode(theNode, len);
NS_ENSURE_SUCCESS(res, res);
res = CreateMozBR(theNode, (int32_t)len);
NS_ENSURE_SUCCESS(res, res);
}
return res;
return NS_OK;
}
nsresult