Bug 717370 - Cleanup range-using and nearby code in editor; r=smaug+ehsan
authorMs2ger <ms2ger@gmail.com>
Sun, 15 Jan 2012 09:13:12 +0100
changeset 85709 fb524533a4777d964ce81dc5bb19c2038152ec73
parent 85708 2b995693ad47281738e0df71443025ea50f67afc
child 85710 fb8cb96c2282a2299a761a5f8d8c8f49357178cd
push id805
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 18:17:35 +0000
treeherdermozilla-aurora@6fb3bf232436 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs717370
milestone12.0a1
Bug 717370 - Cleanup range-using and nearby code in editor; r=smaug+ehsan
content/base/src/nsRange.cpp
content/base/src/nsRange.h
editor/libeditor/base/nsEditor.cpp
editor/libeditor/base/nsSelectionState.cpp
editor/txtsvc/src/nsTextServicesDocument.cpp
--- a/content/base/src/nsRange.cpp
+++ b/content/base/src/nsRange.cpp
@@ -287,16 +287,50 @@ nsRangeUtils::CompareNodeToRange(nsICont
 nsRange::~nsRange() 
 {
   NS_ASSERTION(!IsInSelection(), "deleting nsRange that is in use");
 
   // we want the side effects (releases and list removals)
   DoSetRange(nsnull, 0, nsnull, 0, nsnull);
 } 
 
+/* static */
+nsresult
+nsRange::CreateRange(nsIDOMNode* aStartParent, PRInt32 aStartOffset,
+                     nsIDOMNode* aEndParent, PRInt32 aEndOffset,
+                     nsRange** aRange)
+{
+  MOZ_ASSERT(aRange);
+  *aRange = NULL;
+
+  nsRefPtr<nsRange> range = new nsRange();
+
+  nsresult rv = range->SetStart(aStartParent, aStartOffset);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  rv = range->SetEnd(aEndParent, aEndOffset);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  range.forget(aRange);
+  return NS_OK;
+}
+
+/* static */
+nsresult
+nsRange::CreateRange(nsIDOMNode* aStartParent, PRInt32 aStartOffset,
+                     nsIDOMNode* aEndParent, PRInt32 aEndOffset,
+                     nsIDOMRange** aRange)
+{
+  nsRefPtr<nsRange> range;
+  nsresult rv = nsRange::CreateRange(aStartParent, aStartOffset, aEndParent,
+                                     aEndOffset, getter_AddRefs(range));
+  range.forget(aRange);
+  return rv;
+}
+
 /******************************************************
  * nsISupports
  ******************************************************/
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(nsRange)
 
 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsRange)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsRange)
--- a/content/base/src/nsRange.h
+++ b/content/base/src/nsRange.h
@@ -61,16 +61,23 @@ public:
     , mEndOffset(0)
     , mIsPositioned(false)
     , mIsDetached(false)
     , mMaySpanAnonymousSubtrees(false)
     , mInSelection(false)
   {}
   virtual ~nsRange();
 
+  static nsresult CreateRange(nsIDOMNode* aStartParent, PRInt32 aStartOffset,
+                              nsIDOMNode* aEndParent, PRInt32 aEndOffset,
+                              nsRange** aRange);
+  static nsresult CreateRange(nsIDOMNode* aStartParent, PRInt32 aStartOffset,
+                              nsIDOMNode* aEndParent, PRInt32 aEndOffset,
+                              nsIDOMRange** aRange);
+
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsRange, nsIDOMRange)
 
   // nsIDOMRange interface
   NS_DECL_NSIDOMRANGE
   
   nsINode* GetRoot() const
   {
--- a/editor/libeditor/base/nsEditor.cpp
+++ b/editor/libeditor/base/nsEditor.cpp
@@ -4975,29 +4975,18 @@ nsEditor::CreateTxnForDeleteInsertionPoi
   return result;
 }
 
 nsresult 
 nsEditor::CreateRange(nsIDOMNode *aStartParent, PRInt32 aStartOffset,
                       nsIDOMNode *aEndParent, PRInt32 aEndOffset,
                       nsIDOMRange **aRange)
 {
-  NS_ADDREF(*aRange = new nsRange());
-
-  nsresult result = (*aRange)->SetStart(aStartParent, aStartOffset);
-
-  if (NS_SUCCEEDED(result))
-    result = (*aRange)->SetEnd(aEndParent, aEndOffset);
-
-  if (NS_FAILED(result))
-  {
-    NS_RELEASE((*aRange));
-    *aRange = 0;
-  }
-  return result;
+  return nsRange::CreateRange(aStartParent, aStartOffset, aEndParent,
+                              aEndOffset, aRange);
 }
 
 nsresult 
 nsEditor::AppendNodeToSelectionAsRange(nsIDOMNode *aNode)
 {
   NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER);
   nsCOMPtr<nsISelection> selection;
   nsresult res = GetSelection(getter_AddRefs(selection));
--- a/editor/libeditor/base/nsSelectionState.cpp
+++ b/editor/libeditor/base/nsSelectionState.cpp
@@ -697,18 +697,11 @@ nsresult nsRangeStore::StoreRange(nsIDOM
   aRange->GetEndContainer(getter_AddRefs(endNode));
   aRange->GetStartOffset(&startOffset);
   aRange->GetEndOffset(&endOffset);
   return NS_OK;
 }
 
 nsresult nsRangeStore::GetRange(nsRange** outRange)
 {
-  NS_ENSURE_TRUE(outRange, NS_ERROR_NULL_POINTER);
-  *outRange = new nsRange();
-  NS_ADDREF((*outRange));
-
-  nsresult res = (*outRange)->SetStart(startNode, startOffset);
-  if(NS_FAILED(res)) return res;
-
-  res = (*outRange)->SetEnd(endNode, endOffset);
-  return res;
+  return nsRange::CreateRange(startNode, startOffset, endNode, endOffset,
+                              outRange);
 }
--- a/editor/txtsvc/src/nsTextServicesDocument.cpp
+++ b/editor/txtsvc/src/nsTextServicesDocument.cpp
@@ -2103,125 +2103,86 @@ nsTextServicesDocument::GetDocumentConte
   }
 
   return result;
 }
 
 nsresult
 nsTextServicesDocument::CreateDocumentContentRange(nsIDOMRange **aRange)
 {
-  nsresult result;
-
-  NS_ENSURE_TRUE(aRange, NS_ERROR_NULL_POINTER);
-
-  *aRange = 0;
-
-  nsCOMPtr<nsIDOMNode>node;
-
-  result = GetDocumentContentRootNode(getter_AddRefs(node));
-
-  NS_ENSURE_SUCCESS(result, result);
-
+  *aRange = NULL;
+
+  nsCOMPtr<nsIDOMNode> node;
+  nsresult rv = GetDocumentContentRootNode(getter_AddRefs(node));
+  NS_ENSURE_SUCCESS(rv, rv);
   NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
 
-  *aRange = new nsRange();
-  NS_ADDREF(*aRange);
-
-  result = (*aRange)->SelectNodeContents(node);
-
-  if (NS_FAILED(result))
-  {
-    NS_RELEASE((*aRange));
-    *aRange = 0;
-    return result;
-  }
-
+  nsRefPtr<nsRange> range = new nsRange();
+
+  rv = range->SelectNodeContents(node);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  range.forget(aRange);
   return NS_OK;
 }
 
 nsresult
 nsTextServicesDocument::CreateDocumentContentRootToNodeOffsetRange(nsIDOMNode *aParent, PRInt32 aOffset, bool aToStart, nsIDOMRange **aRange)
 {
-  nsresult result;
-
   NS_ENSURE_TRUE(aParent && aRange, NS_ERROR_NULL_POINTER);
 
   *aRange = 0;
 
   NS_ASSERTION(aOffset >= 0, "Invalid offset!");
 
   if (aOffset < 0)
     return NS_ERROR_FAILURE;
 
   nsCOMPtr<nsIDOMNode> bodyNode; 
-
-  result = GetDocumentContentRootNode(getter_AddRefs(bodyNode));
-
-  NS_ENSURE_SUCCESS(result, result);
-
+  nsresult rv = GetDocumentContentRootNode(getter_AddRefs(bodyNode));
+  NS_ENSURE_SUCCESS(rv, rv);
   NS_ENSURE_TRUE(bodyNode, NS_ERROR_NULL_POINTER);
 
   nsCOMPtr<nsIDOMNode> startNode;
   nsCOMPtr<nsIDOMNode> endNode;
   PRInt32 startOffset, endOffset;
 
-  if (aToStart)
-  {
+  if (aToStart) {
     // The range should begin at the start of the document
     // and extend up until (aParent, aOffset).
 
     startNode   = bodyNode;
     startOffset = 0;
-    endNode     = do_QueryInterface(aParent);
+    endNode     = aParent;
     endOffset   = aOffset;
-  }
-  else
-  {
+  } else {
     // The range should begin at (aParent, aOffset) and
     // extend to the end of the document.
 
     nsCOMPtr<nsIDOMNodeList> nodeList;
     PRUint32 nodeListLength;
 
-    startNode   = do_QueryInterface(aParent);
+    startNode   = aParent;
     startOffset = aOffset;
     endNode     = bodyNode;
     endOffset   = 0;
 
-    result = bodyNode->GetChildNodes(getter_AddRefs(nodeList));
-
-    NS_ENSURE_SUCCESS(result, NS_ERROR_FAILURE);
-
-    if (nodeList)
-    {
-      result = nodeList->GetLength(&nodeListLength);
-
-      NS_ENSURE_SUCCESS(result, NS_ERROR_FAILURE);
+    rv = bodyNode->GetChildNodes(getter_AddRefs(nodeList));
+    NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
+
+    if (nodeList) {
+      rv = nodeList->GetLength(&nodeListLength);
+      NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
 
       endOffset = (PRInt32)nodeListLength;
     }
   }
 
-  *aRange = new nsRange();
-  NS_ADDREF((*aRange));
-
-  NS_ENSURE_TRUE(*aRange, NS_ERROR_NULL_POINTER);
-
-  result = (*aRange)->SetStart(startNode, startOffset);
-
-  if (NS_SUCCEEDED(result))
-    result = (*aRange)->SetEnd(endNode, endOffset);
-
-  if (NS_FAILED(result))
-  {
-    NS_RELEASE((*aRange));
-    *aRange = 0;
-  }
-
-  return result;
+  return nsRange::CreateRange(startNode, startOffset, endNode, endOffset,
+                              aRange);
 }
 
 nsresult
 nsTextServicesDocument::CreateDocumentContentIterator(nsIContentIterator **aIterator)
 {
   nsresult result;
 
   NS_ENSURE_TRUE(aIterator, NS_ERROR_NULL_POINTER);
@@ -2435,34 +2396,26 @@ nsTextServicesDocument::HasSameBlockNode
 
   return p1 == p2;
 }
 
 bool
 nsTextServicesDocument::IsTextNode(nsIContent *aContent)
 {
   NS_ENSURE_TRUE(aContent, false);
-
-  nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aContent);
-
-  return IsTextNode(node);
+  return nsIDOMNode::TEXT_NODE == aContent->NodeType();
 }
 
 bool
 nsTextServicesDocument::IsTextNode(nsIDOMNode *aNode)
 {
   NS_ENSURE_TRUE(aNode, false);
 
-  PRUint16 type;
-
-  nsresult result = aNode->GetNodeType(&type);
-
-  NS_ENSURE_SUCCESS(result, false);
-
-  return nsIDOMNode::TEXT_NODE == type;
+  nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
+  return IsTextNode(content);
 }
 
 nsresult
 nsTextServicesDocument::SetSelectionInternal(PRInt32 aOffset, PRInt32 aLength, bool aDoUpdate)
 {
   nsresult result = NS_OK;
 
   NS_ENSURE_TRUE(mSelCon && aOffset >= 0 && aLength >= 0, NS_ERROR_FAILURE);
@@ -3307,47 +3260,31 @@ nsTextServicesDocument::GetRangeEndPoint
 }
 
 
 nsresult
 nsTextServicesDocument::CreateRange(nsIDOMNode *aStartParent, PRInt32 aStartOffset,
                                     nsIDOMNode *aEndParent, PRInt32 aEndOffset,
                                     nsIDOMRange **aRange)
 {
-  NS_ADDREF(*aRange = new nsRange());
-
-  nsresult result = (*aRange)->SetStart(aStartParent, aStartOffset);
-
-  if (NS_SUCCEEDED(result))
-    result = (*aRange)->SetEnd(aEndParent, aEndOffset);
-
-  if (NS_FAILED(result))
-  {
-    NS_RELEASE((*aRange));
-    *aRange = 0;
-  }
-
-  return result;
+  return nsRange::CreateRange(aStartParent, aStartOffset, aEndParent,
+                              aEndOffset, aRange);
 }
 
 nsresult
 nsTextServicesDocument::FirstTextNode(nsIContentIterator *aIterator,
                                       TSDIteratorStatus *aIteratorStatus)
 {
   if (aIteratorStatus)
     *aIteratorStatus = nsTextServicesDocument::eIsDone;
 
   aIterator->First();
 
-  while (!aIterator->IsDone())
-  {
-    nsCOMPtr<nsIContent> content = do_QueryInterface(aIterator->GetCurrentNode());
-
-    if (IsTextNode(content))
-    {
+  while (!aIterator->IsDone()) {
+    if (aIterator->GetCurrentNode()->NodeType() == nsIDOMNode::TEXT_NODE) {
       if (aIteratorStatus)
         *aIteratorStatus = nsTextServicesDocument::eValid;
       break;
     }
 
     aIterator->Next();
   }
 
@@ -3358,22 +3295,18 @@ nsresult
 nsTextServicesDocument::LastTextNode(nsIContentIterator *aIterator,
                                      TSDIteratorStatus *aIteratorStatus)
 {
   if (aIteratorStatus)
     *aIteratorStatus = nsTextServicesDocument::eIsDone;
 
   aIterator->Last();
 
-  while (!aIterator->IsDone())
-  {
-    nsCOMPtr<nsIContent> content = do_QueryInterface(aIterator->GetCurrentNode());
-
-    if (IsTextNode(content))
-    {
+  while (!aIterator->IsDone()) {
+    if (aIterator->GetCurrentNode()->NodeType() == nsIDOMNode::TEXT_NODE) {
       if (aIteratorStatus)
         *aIteratorStatus = nsTextServicesDocument::eValid;
       break;
     }
 
     aIterator->Prev();
   }
 
@@ -3507,17 +3440,17 @@ nsTextServicesDocument::GetFirstTextNode
     // Try to restore the iterator before returning.
     mIterator->PositionAt(node);
     return result;
   }
 
   if (!mIterator->IsDone())
   {
     nsCOMPtr<nsIContent> current = do_QueryInterface(mIterator->GetCurrentNode());
-    current.swap(*aContent);
+    current.forget(aContent);
   }
 
   // Restore the iterator:
 
   return mIterator->PositionAt(node);
 }
 
 nsresult
@@ -3541,17 +3474,17 @@ nsTextServicesDocument::GetFirstTextNode
     // Try to restore the iterator before returning.
     mIterator->PositionAt(node);
     return result;
   }
 
   if (!mIterator->IsDone())
   {
     nsCOMPtr<nsIContent> current = do_QueryInterface(mIterator->GetCurrentNode());
-    current.swap(*aContent);
+    current.forget(aContent);
   }
 
   // Restore the iterator:
   return mIterator->PositionAt(node);
 }
 
 nsresult
 nsTextServicesDocument::CreateOffsetTable(nsTArray<OffsetEntry*> *aOffsetTable,
@@ -3619,19 +3552,16 @@ nsTextServicesDocument::CreateOffsetTabl
 
           result = node->GetNodeValue(str);
 
           NS_ENSURE_SUCCESS(result, result);
 
           // Add an entry for this text node into the offset table:
 
           OffsetEntry *entry = new OffsetEntry(node, offset, str.Length());
-
-          NS_ENSURE_TRUE(entry, NS_ERROR_OUT_OF_MEMORY);
-
           aOffsetTable->AppendElement(entry);
 
           // If one or both of the endpoints of the iteration range
           // are in the text node for this entry, make sure the entry
           // only accounts for the portion of the text node that is
           // in the range.
 
           PRInt32 startOffset = 0;
@@ -3766,18 +3696,16 @@ nsTextServicesDocument::SplitOffsetEntry
     return NS_ERROR_FAILURE;
 
   PRInt32 oldLength = entry->mLength - aNewEntryLength;
 
   OffsetEntry *newEntry = new OffsetEntry(entry->mNode,
                                           entry->mStrOffset + oldLength,
                                           aNewEntryLength);
 
-  NS_ENSURE_TRUE(newEntry, NS_ERROR_OUT_OF_MEMORY);
-
   if (!mOffsetTable.InsertElementAt(aTableIndex + 1, newEntry))
   {
     delete newEntry;
     return NS_ERROR_FAILURE;
   }
 
    // Adjust entry fields: