Bug 1190172 part 8 - Clean up nsHTMLEditor::PromoteRangeIfStartsOrEndsInNamedAnchor; r=ehsan
authorAryeh Gregor <ayg@aryeh.name>
Tue, 19 Apr 2016 06:54:00 +0200
changeset 331776 f349334cc957a4187d53a0c30d848574d3c978a3
parent 331775 77393145e701729ddab4ee8178aa20f0c45fcd5b
child 331777 f0c671a3cf1d22e3aa40c2bca92d382a1a16d89e
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 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);