Bug 1190172 part 1 - Clean up nsHTMLEditor::SplitStyleAbovePoint; r=ehsan
authorAryeh Gregor <ayg@aryeh.name>
Sun, 01 May 2016 16:11:28 +0300
changeset 295627 9c056080ee98e46887513b654745816e3abd2a2d
parent 295626 99ff6e7956f75aa8541e7e5c4a118b8cd7e9cdfc
child 295628 09235b9b9c4cabf6d00add930dcf54acd4b485b5
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 1 - Clean up nsHTMLEditor::SplitStyleAbovePoint; r=ehsan
editor/libeditor/nsHTMLEditRules.cpp
editor/libeditor/nsHTMLEditor.h
editor/libeditor/nsHTMLEditorStyle.cpp
--- a/editor/libeditor/nsHTMLEditRules.cpp
+++ b/editor/libeditor/nsHTMLEditRules.cpp
@@ -2742,32 +2742,24 @@ nsHTMLEditRules::JoinBlocks(nsIContent& 
         previousContentOffset++;
       }
 
       // Because we don't want the moving content to receive the style of the
       // previous content, we split the previous content's style.
 
       nsCOMPtr<Element> editorRoot = mHTMLEditor->GetEditorRoot();
       if (!editorRoot || &aLeftNode != editorRoot) {
-        nsCOMPtr<nsIDOMNode> previousContentParentDOM =
-          GetAsDOMNode(previousContentParent);
-        nsCOMPtr<nsIDOMNode> splittedPreviousContentDOM;
-        res = mHTMLEditor->SplitStyleAbovePoint(address_of(previousContentParentDOM),
+        nsCOMPtr<nsIContent> splittedPreviousContent;
+        res = mHTMLEditor->SplitStyleAbovePoint(address_of(previousContentParent),
                                                 &previousContentOffset,
                                                 nullptr, nullptr, nullptr,
-                                                address_of(splittedPreviousContentDOM));
+                                                getter_AddRefs(splittedPreviousContent));
         NS_ENSURE_SUCCESS(res, res);
-        previousContentParent = do_QueryInterface(previousContentParentDOM);
-        NS_ENSURE_STATE(previousContentParent || !previousContentParentDOM);
-
-        if (splittedPreviousContentDOM) {
-          nsCOMPtr<nsINode> splittedPreviousContent =
-            do_QueryInterface(splittedPreviousContentDOM);
-          NS_ENSURE_STATE(splittedPreviousContent ||
-                          !splittedPreviousContentDOM);
+
+        if (splittedPreviousContent) {
           previousContentParent = splittedPreviousContent->GetParentNode();
           previousContentOffset = previousContentParent ?
             previousContentParent->IndexOf(splittedPreviousContent) : -1;
         }
       }
 
       res = MoveBlock(GetAsDOMNode(previousContentParent),
                       GetAsDOMNode(rightBlock),
--- a/editor/libeditor/nsHTMLEditor.h
+++ b/editor/libeditor/nsHTMLEditor.h
@@ -659,22 +659,21 @@ protected:
                                    const nsAString* aAttribute,
                                    const nsAString& aValue);
 
   nsresult PromoteInlineRange(nsRange* aRange);
   nsresult PromoteRangeIfStartsOrEndsInNamedAnchor(nsRange* aRange);
   nsresult SplitStyleAboveRange(nsRange* aRange,
                                 nsIAtom *aProperty,
                                 const nsAString *aAttribute);
-  nsresult SplitStyleAbovePoint(nsCOMPtr<nsIDOMNode> *aNode,
-                                int32_t *aOffset,
-                                nsIAtom *aProperty,
-                                const nsAString *aAttribute,
-                                nsCOMPtr<nsIDOMNode> *outLeftNode = nullptr,
-                                nsCOMPtr<nsIDOMNode> *outRightNode = nullptr);
+  nsresult SplitStyleAbovePoint(nsCOMPtr<nsINode>* aNode, int32_t* aOffset,
+                                nsIAtom* aProperty,
+                                const nsAString* aAttribute,
+                                nsIContent** aOutLeftNode = nullptr,
+                                nsIContent** aOutRightNode = nullptr);
   nsresult ApplyDefaultProperties();
   nsresult RemoveStyleInside(nsIDOMNode *aNode,
                              nsIAtom *aProperty,
                              const nsAString *aAttribute,
                              const bool aChildrenOnly = false);
   nsresult RemoveStyleInside(nsIContent& aNode,
                              nsIAtom* aProperty,
                              const nsAString* aAttribute,
--- a/editor/libeditor/nsHTMLEditorStyle.cpp
+++ b/editor/libeditor/nsHTMLEditorStyle.cpp
@@ -518,156 +518,143 @@ nsHTMLEditor::SetInlinePropertyOnNode(ns
 
 
 nsresult
 nsHTMLEditor::SplitStyleAboveRange(nsRange* inRange, nsIAtom* aProperty,
                                    const nsAString* aAttribute)
 {
   NS_ENSURE_TRUE(inRange, NS_ERROR_NULL_POINTER);
   nsresult res;
-  nsCOMPtr<nsIDOMNode> startNode, endNode, origStartNode;
-  int32_t startOffset, 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);
+  nsCOMPtr<nsINode> startNode = inRange->GetStartParent();
+  int32_t startOffset = inRange->StartOffset();
+  nsCOMPtr<nsINode> endNode = inRange->GetEndParent();
+  int32_t endOffset = inRange->EndOffset();
 
-  origStartNode = startNode;
+  nsCOMPtr<nsINode> origStartNode = startNode;
 
   // split any matching style nodes above the start of range
   {
     nsAutoTrackDOMPoint tracker(mRangeUpdater, address_of(endNode), &endOffset);
-    res = SplitStyleAbovePoint(address_of(startNode), &startOffset, aProperty, aAttribute);
+    res = SplitStyleAbovePoint(address_of(startNode), &startOffset, aProperty,
+                               aAttribute);
     NS_ENSURE_SUCCESS(res, res);
   }
 
   // second verse, same as the first...
-  res = SplitStyleAbovePoint(address_of(endNode), &endOffset, aProperty, aAttribute);
+  res = SplitStyleAbovePoint(address_of(endNode), &endOffset, aProperty,
+                             aAttribute);
   NS_ENSURE_SUCCESS(res, res);
 
   // reset the range
   res = inRange->SetStart(startNode, startOffset);
   NS_ENSURE_SUCCESS(res, res);
   res = inRange->SetEnd(endNode, endOffset);
   return res;
 }
 
-nsresult nsHTMLEditor::SplitStyleAbovePoint(nsCOMPtr<nsIDOMNode> *aNode,
-                                           int32_t *aOffset,
-                                           nsIAtom *aProperty,          // null here means we split all properties
-                                           const nsAString *aAttribute,
-                                           nsCOMPtr<nsIDOMNode> *outLeftNode,
-                                           nsCOMPtr<nsIDOMNode> *outRightNode)
+nsresult
+nsHTMLEditor::SplitStyleAbovePoint(nsCOMPtr<nsINode>* aNode,
+                                   int32_t* aOffset,
+                                   // null here means we split all properties
+                                   nsIAtom* aProperty,
+                                   const nsAString* aAttribute,
+                                   nsIContent** aOutLeftNode,
+                                   nsIContent** aOutRightNode)
 {
   NS_ENSURE_TRUE(aNode && *aNode && aOffset, NS_ERROR_NULL_POINTER);
-  if (outLeftNode)  *outLeftNode  = nullptr;
-  if (outRightNode) *outRightNode = nullptr;
-  // split any matching style nodes above the node/offset
-  nsCOMPtr<nsIContent> node = do_QueryInterface(*aNode);
-  NS_ENSURE_STATE(node);
-  int32_t offset;
+  NS_ENSURE_TRUE((*aNode)->IsContent(), NS_OK);
+
+  // Split any matching style nodes above the node/offset
+  OwningNonNull<nsIContent> node = *(*aNode)->AsContent();
 
   bool useCSS = IsCSSEnabled();
 
   bool isSet;
-  while (node && !IsBlockNode(node) && node->GetParentNode() &&
-         IsEditable(node->GetParentNode())) {
+  while (!IsBlockNode(node) && node->GetParent() &&
+         IsEditable(node->GetParent())) {
     isSet = false;
-    if (useCSS && mHTMLCSSUtils->IsCSSEditableProperty(node, aProperty, aAttribute)) {
-      // the HTML style defined by aProperty/aAttribute has a CSS equivalence
-      // in this implementation for the node; let's check if it carries those css styles
+    if (useCSS && mHTMLCSSUtils->IsCSSEditableProperty(node, aProperty,
+                                                       aAttribute)) {
+      // The HTML style defined by aProperty/aAttribute has a CSS equivalence
+      // in this implementation for the node; let's check if it carries those
+      // CSS styles
       nsAutoString firstValue;
       mHTMLCSSUtils->IsCSSEquivalentToHTMLInlineStyleSet(GetAsDOMNode(node),
         aProperty, aAttribute, isSet, firstValue, nsHTMLCSSUtils::eSpecified);
     }
     if (// node is the correct inline prop
         (aProperty && node->IsHTMLElement(aProperty)) ||
         // node is href - test if really <a href=...
         (aProperty == nsGkAtoms::href && nsHTMLEditUtils::IsLink(node)) ||
         // or node is any prop, and we asked to split them all
         (!aProperty && NodeIsProperty(GetAsDOMNode(node))) ||
         // or the style is specified in the style attribute
         isSet) {
-      // found a style node we need to split
-      nsCOMPtr<nsIContent> outLeftContent, outRightContent;
-      nsCOMPtr<nsIContent> nodeParam = do_QueryInterface(*aNode);
-      NS_ENSURE_STATE(nodeParam || !*aNode);
-      offset = SplitNodeDeep(*node, *nodeParam, *aOffset, EmptyContainers::yes,
-                             getter_AddRefs(outLeftContent),
-                             getter_AddRefs(outRightContent));
+      // Found a style node we need to split
+      int32_t offset = SplitNodeDeep(*node, *(*aNode)->AsContent(), *aOffset,
+                                     EmptyContainers::yes, aOutLeftNode,
+                                     aOutRightNode);
       NS_ENSURE_TRUE(offset != -1, NS_ERROR_FAILURE);
       // reset startNode/startOffset
-      *aNode = GetAsDOMNode(node->GetParent());
+      *aNode = node->GetParent();
       *aOffset = offset;
-      if (outLeftNode) {
-        *outLeftNode = GetAsDOMNode(outLeftContent);
-      }
-      if (outRightNode) {
-        *outRightNode = GetAsDOMNode(outRightContent);
-      }
     }
     node = node->GetParent();
   }
+
   return NS_OK;
 }
 
 nsresult
 nsHTMLEditor::ClearStyle(nsCOMPtr<nsIDOMNode>* aNode, int32_t* aOffset,
                          nsIAtom* aProperty, const nsAString* aAttribute)
 {
-  nsCOMPtr<nsIDOMNode> leftNode, rightNode, tmp;
-  nsresult res = SplitStyleAbovePoint(aNode, aOffset, aProperty, aAttribute,
-                                      address_of(leftNode),
-                                      address_of(rightNode));
+  nsCOMPtr<nsINode> node = do_QueryInterface(*aNode);
+  nsCOMPtr<nsIContent> leftNode, rightNode;
+  nsresult res = SplitStyleAbovePoint(address_of(node), aOffset, aProperty,
+                                      aAttribute, getter_AddRefs(leftNode),
+                                      getter_AddRefs(rightNode));
+  *aNode = GetAsDOMNode(node);
   NS_ENSURE_SUCCESS(res, res);
+
   if (leftNode) {
     bool bIsEmptyNode;
     IsEmptyNode(leftNode, &bIsEmptyNode, false, true);
     if (bIsEmptyNode) {
       // delete leftNode if it became empty
       res = DeleteNode(leftNode);
       NS_ENSURE_SUCCESS(res, res);
     }
   }
   if (rightNode) {
-    nsCOMPtr<nsINode> rightNode_ = do_QueryInterface(rightNode);
-    NS_ENSURE_STATE(rightNode_);
-    nsCOMPtr<nsIDOMNode> secondSplitParent =
-      GetAsDOMNode(GetLeftmostChild(rightNode_));
+    nsCOMPtr<nsINode> secondSplitParent = GetLeftmostChild(rightNode);
     // don't try to split non-containers (br's, images, hr's, etc)
     if (!secondSplitParent) {
       secondSplitParent = rightNode;
     }
     nsCOMPtr<Element> savedBR;
     if (!IsContainer(secondSplitParent)) {
       if (nsTextEditUtils::IsBreak(secondSplitParent)) {
         savedBR = do_QueryInterface(secondSplitParent);
         NS_ENSURE_STATE(savedBR);
       }
 
-      secondSplitParent->GetParentNode(getter_AddRefs(tmp));
-      secondSplitParent = tmp;
+      secondSplitParent = secondSplitParent->GetParentNode();
     }
     *aOffset = 0;
     res = SplitStyleAbovePoint(address_of(secondSplitParent),
                                aOffset, aProperty, aAttribute,
-                               address_of(leftNode), address_of(rightNode));
+                               getter_AddRefs(leftNode),
+                               getter_AddRefs(rightNode));
     NS_ENSURE_SUCCESS(res, res);
     // should be impossible to not get a new leftnode here
-    nsCOMPtr<nsINode> leftNode_ = do_QueryInterface(leftNode);
-    NS_ENSURE_TRUE(leftNode_, NS_ERROR_FAILURE);
-    nsCOMPtr<nsINode> newSelParent = GetLeftmostChild(leftNode_);
+    nsCOMPtr<nsINode> newSelParent = GetLeftmostChild(leftNode);
     if (!newSelParent) {
-      newSelParent = do_QueryInterface(leftNode);
-      NS_ENSURE_STATE(newSelParent);
+      newSelParent = leftNode;
     }
     // If rightNode starts with a br, suck it out of right node and into
     // leftNode.  This is so we you don't revert back to the previous style
     // if you happen to click at the end of a line.
     if (savedBR) {
       res = MoveNode(savedBR, newSelParent, 0);
       NS_ENSURE_SUCCESS(res, res);
     }
@@ -683,17 +670,17 @@ nsHTMLEditor::ClearStyle(nsCOMPtr<nsIDOM
     {
       // Track the point at the new hierarchy.  This is so we can know where
       // to put the selection after we call RemoveStyleInside().
       // RemoveStyleInside() could remove any and all of those nodes, so I
       // have to use the range tracking system to find the right spot to put
       // selection.
       nsAutoTrackDOMPoint tracker(mRangeUpdater,
                                   address_of(newSelParent), &newSelOffset);
-      res = RemoveStyleInside(leftNode, aProperty, aAttribute);
+      res = RemoveStyleInside(GetAsDOMNode(leftNode), aProperty, aAttribute);
       NS_ENSURE_SUCCESS(res, res);
     }
     // reset our node offset values to the resulting new sel point
     *aNode = GetAsDOMNode(newSelParent);
     *aOffset = newSelOffset;
   }
 
   return NS_OK;