Bug 1190172 part 7 - Clean up nsHTMLEditor::PromoteInlineRange; r=ehsan
authorAryeh Gregor <ayg@aryeh.name>
Tue, 19 Apr 2016 06:52:00 +0200
changeset 331775 77393145e701729ddab4ee8178aa20f0c45fcd5b
parent 331774 e5e7a733fdbc1f1c8daf814137701225152985b7
child 331776 f349334cc957a4187d53a0c30d848574d3c978a3
push id6048
push userkmoir@mozilla.com
push dateMon, 06 Jun 2016 19:02:08 +0000
treeherdermozilla-beta@46d72a56c57d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs1190172
milestone48.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1190172 part 7 - Clean up nsHTMLEditor::PromoteInlineRange; r=ehsan
editor/libeditor/nsHTMLEditor.h
editor/libeditor/nsHTMLEditorStyle.cpp
--- a/editor/libeditor/nsHTMLEditor.h
+++ b/editor/libeditor/nsHTMLEditor.h
@@ -654,17 +654,17 @@ protected:
                                        nsIAtom& aProperty,
                                        const nsAString* aAttribute,
                                        const nsAString& aValue);
   nsresult SetInlinePropertyOnNode(nsIContent& aNode,
                                    nsIAtom& aProperty,
                                    const nsAString* aAttribute,
                                    const nsAString& aValue);
 
-  nsresult PromoteInlineRange(nsRange* aRange);
+  nsresult PromoteInlineRange(nsRange& aRange);
   nsresult PromoteRangeIfStartsOrEndsInNamedAnchor(nsRange* aRange);
   nsresult SplitStyleAboveRange(nsRange* aRange,
                                 nsIAtom *aProperty,
                                 const nsAString *aAttribute);
   nsresult SplitStyleAbovePoint(nsCOMPtr<nsINode>* aNode, int32_t* aOffset,
                                 nsIAtom* aProperty,
                                 const nsAString* aAttribute,
                                 nsIContent** aOutLeftNode = nullptr,
@@ -673,18 +673,18 @@ protected:
   nsresult RemoveStyleInside(nsIContent& aNode,
                              nsIAtom* aProperty,
                              const nsAString* aAttribute,
                              const bool aChildrenOnly = false);
   nsresult RemoveInlinePropertyImpl(nsIAtom* aProperty,
                                     const nsAString* aAttribute);
 
   bool NodeIsProperty(nsINode& aNode);
-  bool IsAtFrontOfNode(nsIDOMNode *aNode, int32_t aOffset);
-  bool IsAtEndOfNode(nsIDOMNode *aNode, int32_t aOffset);
+  bool IsAtFrontOfNode(nsINode& aNode, int32_t aOffset);
+  bool IsAtEndOfNode(nsINode& aNode, int32_t aOffset);
   bool IsOnlyAttribute(const nsIContent* aElement, const nsAString& aAttribute);
 
   nsresult RemoveBlockContainer(nsIDOMNode *inNode);
 
   nsIContent* GetPriorHTMLSibling(nsINode* aNode);
   nsresult GetPriorHTMLSibling(nsIDOMNode *inNode, nsCOMPtr<nsIDOMNode> *outNode);
   nsIContent* GetPriorHTMLSibling(nsINode* aParent, int32_t aOffset);
   nsresult GetPriorHTMLSibling(nsIDOMNode *inParent, int32_t inOffset, nsCOMPtr<nsIDOMNode> *outNode);
--- a/editor/libeditor/nsHTMLEditorStyle.cpp
+++ b/editor/libeditor/nsHTMLEditorStyle.cpp
@@ -140,17 +140,17 @@ nsHTMLEditor::SetInlineProperty(nsIAtom*
   if (!cancel && !handled) {
     // Loop through the ranges in the selection
     uint32_t rangeCount = selection->RangeCount();
     for (uint32_t rangeIdx = 0; rangeIdx < rangeCount; rangeIdx++) {
       RefPtr<nsRange> range = selection->GetRangeAt(rangeIdx);
 
       // Adjust range to include any ancestors whose children are entirely
       // selected
-      res = PromoteInlineRange(range);
+      res = PromoteInlineRange(*range);
       NS_ENSURE_SUCCESS(res, res);
 
       // Check for easy case: both range endpoints in same text node
       nsCOMPtr<nsINode> startNode = range->GetStartParent();
       nsCOMPtr<nsINode> endNode = range->GetEndParent();
       if (startNode && startNode == endNode && startNode->GetAsText()) {
         res = SetInlinePropertyOnTextNode(*startNode->GetAsText(),
                                           range->StartOffset(),
@@ -894,100 +894,84 @@ nsHTMLEditor::PromoteRangeIfStartsOrEnds
 
   res = inRange->SetStart(startNode, startOffset);
   NS_ENSURE_SUCCESS(res, res);
   res = inRange->SetEnd(endNode, endOffset);
   return res;
 }
 
 nsresult
-nsHTMLEditor::PromoteInlineRange(nsRange* inRange)
+nsHTMLEditor::PromoteInlineRange(nsRange& aRange)
 {
-  NS_ENSURE_TRUE(inRange, NS_ERROR_NULL_POINTER);
-  nsresult res;
-  nsCOMPtr<nsIDOMNode> startNode, endNode, parent;
-  int32_t startOffset, endOffset;
+  nsCOMPtr<nsINode> startNode = aRange.GetStartParent();
+  int32_t startOffset = aRange.StartOffset();
+  nsCOMPtr<nsINode> endNode = aRange.GetEndParent();
+  int32_t endOffset = aRange.EndOffset();
 
-  res = inRange->GetStartContainer(getter_AddRefs(startNode));
-  NS_ENSURE_SUCCESS(res, res);
-  res = inRange->GetStartOffset(&startOffset);
-  NS_ENSURE_SUCCESS(res, res);
-  res = inRange->GetEndContainer(getter_AddRefs(endNode));
-  NS_ENSURE_SUCCESS(res, res);
-  res = inRange->GetEndOffset(&endOffset);
-  NS_ENSURE_SUCCESS(res, res);
-
-  while ( startNode &&
-          !nsTextEditUtils::IsBody(startNode) &&
-          IsEditable(startNode) &&
-          IsAtFrontOfNode(startNode, startOffset) )
-  {
-    parent = GetNodeLocation(startNode, &startOffset);
+  while (startNode && !startNode->IsHTMLElement(nsGkAtoms::body) &&
+         IsEditable(startNode) && IsAtFrontOfNode(*startNode, startOffset)) {
+    nsCOMPtr<nsINode> parent = startNode->GetParentNode();
+    NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
+    startOffset = parent->IndexOf(startNode);
     startNode = parent;
   }
-  NS_ENSURE_TRUE(startNode, NS_ERROR_NULL_POINTER);
 
-  while ( endNode &&
-          !nsTextEditUtils::IsBody(endNode) &&
-          IsEditable(endNode) &&
-          IsAtEndOfNode(endNode, endOffset) )
-  {
-    parent = GetNodeLocation(endNode, &endOffset);
+  while (endNode && !endNode->IsHTMLElement(nsGkAtoms::body) &&
+         IsEditable(endNode) && IsAtEndOfNode(*endNode, endOffset)) {
+    nsCOMPtr<nsINode> parent = endNode->GetParentNode();
+    NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
+    // We are AFTER this node
+    endOffset = 1 + parent->IndexOf(endNode);
     endNode = parent;
-    endOffset++;  // we are AFTER this node
   }
-  NS_ENSURE_TRUE(endNode, NS_ERROR_NULL_POINTER);
 
-  res = inRange->SetStart(startNode, startOffset);
+  nsresult res = aRange.SetStart(startNode, startOffset);
   NS_ENSURE_SUCCESS(res, res);
-  res = inRange->SetEnd(endNode, endOffset);
-  return res;
+  res = aRange.SetEnd(endNode, endOffset);
+  NS_ENSURE_SUCCESS(res, res);
+
+  return NS_OK;
 }
 
-bool nsHTMLEditor::IsAtFrontOfNode(nsIDOMNode *aNode, int32_t aOffset)
+bool
+nsHTMLEditor::IsAtFrontOfNode(nsINode& aNode, int32_t aOffset)
 {
-  nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
-  NS_ENSURE_TRUE(node, false);
   if (!aOffset) {
     return true;
   }
 
-  if (IsTextNode(aNode))
-  {
+  if (IsTextNode(&aNode)) {
     return false;
   }
-  else
-  {
-    nsCOMPtr<nsIContent> firstNode = GetFirstEditableChild(*node);
-    NS_ENSURE_TRUE(firstNode, true);
-    int32_t offset = node->IndexOf(firstNode);
-    if (offset < aOffset) return false;
-    return true;
+
+  nsCOMPtr<nsIContent> firstNode = GetFirstEditableChild(aNode);
+  NS_ENSURE_TRUE(firstNode, true);
+  if (aNode.IndexOf(firstNode) < aOffset) {
+    return false;
   }
+  return true;
 }
 
-bool nsHTMLEditor::IsAtEndOfNode(nsIDOMNode *aNode, int32_t aOffset)
+bool
+nsHTMLEditor::IsAtEndOfNode(nsINode& aNode, int32_t aOffset)
 {
-  nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
-  NS_ENSURE_TRUE(node, false);
-  uint32_t len = node->Length();
-  if (aOffset == (int32_t)len) return true;
+  if (aOffset == (int32_t)aNode.Length()) {
+    return true;
+  }
 
-  if (IsTextNode(aNode))
-  {
+  if (IsTextNode(&aNode)) {
     return false;
   }
-  else
-  {
-    nsCOMPtr<nsIContent> lastNode = GetLastEditableChild(*node);
-    NS_ENSURE_TRUE(lastNode, true);
-    int32_t offset = node->IndexOf(lastNode);
-    if (offset < aOffset) return true;
-    return false;
+
+  nsCOMPtr<nsIContent> lastNode = GetLastEditableChild(aNode);
+  NS_ENSURE_TRUE(lastNode, true);
+  if (aNode.IndexOf(lastNode) < aOffset) {
+    return true;
   }
+  return false;
 }
 
 
 nsresult
 nsHTMLEditor::GetInlinePropertyBase(nsIAtom& aProperty,
                                     const nsAString* aAttribute,
                                     const nsAString* aValue,
                                     bool* aFirst,
@@ -1260,17 +1244,17 @@ nsHTMLEditor::RemoveInlinePropertyImpl(n
       RefPtr<nsRange> range = selection->GetRangeAt(rangeIdx);
       if (aProperty == nsGkAtoms::name) {
         // Promote range if it starts or end in a named anchor and we want to
         // remove named anchors
         res = PromoteRangeIfStartsOrEndsInNamedAnchor(range);
       } else {
         // Adjust range to include any ancestors whose children are entirely
         // selected
-        res = PromoteInlineRange(range);
+        res = PromoteInlineRange(*range);
       }
       NS_ENSURE_SUCCESS(res, res);
 
       // Remove this style from ancestors of our range endpoints, splitting
       // them as appropriate
       res = SplitStyleAboveRange(range, aProperty, aAttribute);
       NS_ENSURE_SUCCESS(res, res);
 
@@ -1397,17 +1381,17 @@ nsHTMLEditor::RelativeFontChange(FontSiz
   nsAutoTxnsConserveSelection dontSpazMySelection(this);
 
   // Loop through the ranges in the selection
   uint32_t rangeCount = selection->RangeCount();
   for (uint32_t rangeIdx = 0; rangeIdx < rangeCount; ++rangeIdx) {
     RefPtr<nsRange> range = selection->GetRangeAt(rangeIdx);
 
     // Adjust range to include any ancestors with entirely selected children
-    nsresult res = PromoteInlineRange(range);
+    nsresult res = PromoteInlineRange(*range);
     NS_ENSURE_SUCCESS(res, res);
 
     // Check for easy case: both range endpoints in same text node
     nsCOMPtr<nsINode> startNode = range->GetStartParent();
     nsCOMPtr<nsINode> endNode = range->GetEndParent();
     if (startNode == endNode && IsTextNode(startNode)) {
       res = RelativeFontChangeOnTextNode(aDir, *startNode->GetAsText(),
                                          range->StartOffset(),