Bug 1190172 part 8 - Clean up nsHTMLEditor::PromoteRangeIfStartsOrEndsInNamedAnchor; r=ehsan
authorAryeh Gregor <ayg@aryeh.name>
Sun, 01 May 2016 16:12:51 +0300
changeset 295634 cc88a585df5d3eb22611d27b887b37154926ffeb
parent 295633 284c3b1d52bfca422dab1ea7324651638a0c4aa1
child 295635 5c3bb21481f7b51779b6f08bc39d6cb70286e457
push id19015
push usercbook@mozilla.com
push dateMon, 02 May 2016 09:39:23 +0000
treeherderfx-team@2080375bc69d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs1190172
milestone49.0a1
Bug 1190172 part 8 - Clean up nsHTMLEditor::PromoteRangeIfStartsOrEndsInNamedAnchor; r=ehsan
editor/libeditor/nsHTMLEditor.h
editor/libeditor/nsHTMLEditorStyle.cpp
--- a/editor/libeditor/nsHTMLEditor.h
+++ b/editor/libeditor/nsHTMLEditor.h
@@ -655,17 +655,17 @@ protected:
                                        const nsAString* aAttribute,
                                        const nsAString& aValue);
   nsresult SetInlinePropertyOnNode(nsIContent& aNode,
                                    nsIAtom& aProperty,
                                    const nsAString* aAttribute,
                                    const nsAString& aValue);
 
   nsresult PromoteInlineRange(nsRange& aRange);
-  nsresult PromoteRangeIfStartsOrEndsInNamedAnchor(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,
                                 nsIContent** aOutRightNode = nullptr);
--- a/editor/libeditor/nsHTMLEditorStyle.cpp
+++ b/editor/libeditor/nsHTMLEditorStyle.cpp
@@ -20,19 +20,17 @@
 #include "nsError.h"
 #include "nsGkAtoms.h"
 #include "nsHTMLCSSUtils.h"
 #include "nsHTMLEditUtils.h"
 #include "nsHTMLEditor.h"
 #include "nsIAtom.h"
 #include "nsIContent.h"
 #include "nsIContentIterator.h"
-#include "nsIDOMCharacterData.h"
 #include "nsIDOMElement.h"
-#include "nsIDOMNode.h"
 #include "nsIEditor.h"
 #include "nsIEditorIMESupport.h"
 #include "nsNameSpaceManager.h"
 #include "nsINode.h"
 #include "nsISupportsImpl.h"
 #include "nsLiteralString.h"
 #include "nsRange.h"
 #include "nsReadableUtils.h"
@@ -706,17 +704,17 @@ nsresult nsHTMLEditor::ApplyDefaultPrope
 }
 
 nsresult
 nsHTMLEditor::RemoveStyleInside(nsIContent& aNode,
                                 nsIAtom* aProperty,
                                 const nsAString* aAttribute,
                                 const bool aChildrenOnly /* = false */)
 {
-  if (aNode.NodeType() == nsIDOMNode::TEXT_NODE) {
+  if (aNode.GetAsText()) {
     return NS_OK;
   }
 
   // first process the children
   RefPtr<nsIContent> child = aNode.GetFirstChild();
   while (child) {
     // cache next sibling since we might remove child
     nsCOMPtr<nsIContent> next = child->GetNextSibling();
@@ -839,68 +837,55 @@ nsHTMLEditor::IsOnlyAttribute(const nsIC
     }
   }
   // if we made it through all of them without finding a real attribute
   // other than aAttribute, then return true
   return true;
 }
 
 nsresult
-nsHTMLEditor::PromoteRangeIfStartsOrEndsInNamedAnchor(nsRange* inRange)
+nsHTMLEditor::PromoteRangeIfStartsOrEndsInNamedAnchor(nsRange& aRange)
 {
-  NS_ENSURE_TRUE(inRange, NS_ERROR_NULL_POINTER);
-  nsresult res;
-  nsCOMPtr<nsIDOMNode> startNode, endNode, parent, tmp;
-  int32_t startOffset, endOffset, tmpOffset;
+  // We assume that <a> is not nested.
+  nsCOMPtr<nsINode> startNode = aRange.GetStartParent();
+  int32_t startOffset = aRange.StartOffset();
+  nsCOMPtr<nsINode> endNode = aRange.GetEndParent();
+  int32_t endOffset = aRange.EndOffset();
+
+  nsCOMPtr<nsINode> parent = startNode;
+
+  while (parent && !parent->IsHTMLElement(nsGkAtoms::body) &&
+         !nsHTMLEditUtils::IsNamedAnchor(parent)) {
+    parent = parent->GetParentNode();
+  }
+  NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
 
-  res = inRange->GetStartContainer(getter_AddRefs(startNode));
-  NS_ENSURE_SUCCESS(res, res);
-  res = inRange->GetStartOffset(&startOffset);
+  if (nsHTMLEditUtils::IsNamedAnchor(parent)) {
+    startNode = parent->GetParentNode();
+    startOffset = startNode ? startNode->IndexOf(parent) : -1;
+  }
+
+  parent = endNode;
+  while (parent && !parent->IsHTMLElement(nsGkAtoms::body) &&
+         !nsHTMLEditUtils::IsNamedAnchor(parent)) {
+    parent = parent->GetParentNode();
+  }
+  NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
+
+  if (nsHTMLEditUtils::IsNamedAnchor(parent)) {
+    endNode = parent->GetParentNode();
+    endOffset = endNode ? endNode->IndexOf(parent) + 1 : 0;
+  }
+
+  nsresult res = aRange.SetStart(startNode, startOffset);
   NS_ENSURE_SUCCESS(res, res);
-  res = inRange->GetEndContainer(getter_AddRefs(endNode));
-  NS_ENSURE_SUCCESS(res, res);
-  res = inRange->GetEndOffset(&endOffset);
+  res = aRange.SetEnd(endNode, endOffset);
   NS_ENSURE_SUCCESS(res, res);
 
-  tmp = startNode;
-  while ( tmp &&
-          !nsTextEditUtils::IsBody(tmp) &&
-          !nsHTMLEditUtils::IsNamedAnchor(tmp))
-  {
-    parent = GetNodeLocation(tmp, &tmpOffset);
-    tmp = parent;
-  }
-  NS_ENSURE_TRUE(tmp, NS_ERROR_NULL_POINTER);
-  if (nsHTMLEditUtils::IsNamedAnchor(tmp))
-  {
-    parent = GetNodeLocation(tmp, &tmpOffset);
-    startNode = parent;
-    startOffset = tmpOffset;
-  }
-
-  tmp = endNode;
-  while ( tmp &&
-          !nsTextEditUtils::IsBody(tmp) &&
-          !nsHTMLEditUtils::IsNamedAnchor(tmp))
-  {
-    parent = GetNodeLocation(tmp, &tmpOffset);
-    tmp = parent;
-  }
-  NS_ENSURE_TRUE(tmp, NS_ERROR_NULL_POINTER);
-  if (nsHTMLEditUtils::IsNamedAnchor(tmp))
-  {
-    parent = GetNodeLocation(tmp, &tmpOffset);
-    endNode = parent;
-    endOffset = tmpOffset + 1;
-  }
-
-  res = inRange->SetStart(startNode, startOffset);
-  NS_ENSURE_SUCCESS(res, res);
-  res = inRange->SetEnd(endNode, endOffset);
-  return res;
+  return NS_OK;
 }
 
 nsresult
 nsHTMLEditor::PromoteInlineRange(nsRange& aRange)
 {
   nsCOMPtr<nsINode> startNode = aRange.GetStartParent();
   int32_t startOffset = aRange.StartOffset();
   nsCOMPtr<nsINode> endNode = aRange.GetEndParent();
@@ -1236,25 +1221,25 @@ nsHTMLEditor::RemoveInlinePropertyImpl(n
   // Protect the edit rules object from dying
   nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
   nsresult res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
   NS_ENSURE_SUCCESS(res, res);
   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);
+      OwningNonNull<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);