Bug 1191356 part 8 - Clean up nsHTMLEditRules::WillAbsolutePosition; r=ehsan
☠☠ backed out by c3204b6c4e6c ☠ ☠
authorAryeh Gregor <ayg@aryeh.name>
Sun, 01 May 2016 16:17:50 +0300
changeset 295659 7b3284a488dc93e0b3a94fa4ce1c280460a582dc
parent 295658 aa61db1f8f832ce51b8970c6acf27c9132bde8e7
child 295660 acf185618acd3cbd431605def5677f9fa31bb0cc
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
bugs1191356
milestone49.0a1
Bug 1191356 part 8 - Clean up nsHTMLEditRules::WillAbsolutePosition; r=ehsan
editor/libeditor/nsHTMLEditRules.cpp
editor/libeditor/nsHTMLEditRules.h
--- a/editor/libeditor/nsHTMLEditRules.cpp
+++ b/editor/libeditor/nsHTMLEditRules.cpp
@@ -645,17 +645,17 @@ nsHTMLEditRules::WillDoAction(Selection*
     case EditAction::makeList:
       return WillMakeList(aSelection, info->blockType, info->entireList,
                           info->bulletType, aCancel, aHandled);
     case EditAction::indent:
       return WillIndent(aSelection, aCancel, aHandled);
     case EditAction::outdent:
       return WillOutdent(*aSelection, aCancel, aHandled);
     case EditAction::setAbsolutePosition:
-      return WillAbsolutePosition(aSelection, aCancel, aHandled);
+      return WillAbsolutePosition(*aSelection, aCancel, aHandled);
     case EditAction::removeAbsolutePosition:
       return WillRemoveAbsolutePosition(aSelection, aCancel, aHandled);
     case EditAction::align:
       return WillAlign(*aSelection, *info->alignType, aCancel, aHandled);
     case EditAction::makeBasicBlock:
       return WillMakeBasicBlock(*aSelection, *info->blockType, aCancel,
                                 aHandled);
     case EditAction::removeList:
@@ -3330,31 +3330,25 @@ nsHTMLEditRules::WillMakeDefListItem(Sel
 }
 
 nsresult
 nsHTMLEditRules::WillMakeBasicBlock(Selection& aSelection,
                                     const nsAString& aBlockType,
                                     bool* aCancel,
                                     bool* aHandled)
 {
-<<<<<<< HEAD
-  OwningNonNull<nsIAtom> blockType = NS_Atomize(*aBlockType);
-  if (!aSelection || !aCancel || !aHandled) { return NS_ERROR_NULL_POINTER; }
-  // initialize out param
-=======
   MOZ_ASSERT(aCancel && aHandled);
 
   NS_ENSURE_STATE(mHTMLEditor);
   nsCOMPtr<nsIEditor> kungFuDeathGrip(mHTMLEditor);
 
-  OwningNonNull<nsIAtom> blockType = do_GetAtom(aBlockType);
+  OwningNonNull<nsIAtom> blockType = NS_Atomize(aBlockType);
 
   WillInsert(aSelection, aCancel);
   // We want to ignore result of WillInsert()
->>>>>>> Bug 1191356 part 6 - Clean up nsHTMLEditRules::WillMakeBasicBlock
   *aCancel = false;
   *aHandled = false;
 
   nsresult res = NormalizeSelection(&aSelection);
   NS_ENSURE_SUCCESS(res, res);
   nsAutoSelectionReset selectionResetter(&aSelection, mHTMLEditor);
   nsAutoTxnsConserveSelection dontSpazMySelection(mHTMLEditor);
   *aHandled = true;
@@ -8471,234 +8465,207 @@ nsHTMLEditRules::ChangeIndentation(Eleme
   return NS_OK;
 }
 
 //
 // Support for Absolute Positioning
 //
 
 nsresult
-nsHTMLEditRules::WillAbsolutePosition(Selection* aSelection,
+nsHTMLEditRules::WillAbsolutePosition(Selection& aSelection,
                                       bool* aCancel, bool* aHandled)
 {
-  if (!aSelection || !aCancel || !aHandled) { return NS_ERROR_NULL_POINTER; }
-  WillInsert(*aSelection, aCancel);
-
-  // initialize out param
-  // we want to ignore result of WillInsert()
+  MOZ_ASSERT(aCancel && aHandled);
+  NS_ENSURE_STATE(mHTMLEditor);
+  nsCOMPtr<nsIEditor> kungFuDeathGrip(mHTMLEditor);
+
+  WillInsert(aSelection, aCancel);
+
+  // We want to ignore result of WillInsert()
   *aCancel = false;
   *aHandled = true;
 
-  nsCOMPtr<nsIDOMElement> focusElement;
-  NS_ENSURE_STATE(mHTMLEditor);
-  nsresult res =
-    mHTMLEditor->GetSelectionContainer(getter_AddRefs(focusElement));
-  if (focusElement) {
-    nsCOMPtr<nsIDOMNode> node = do_QueryInterface(focusElement);
-    if (nsHTMLEditUtils::IsImage(node)) {
-      mNewBlock = node;
-      return NS_OK;
-    }
-  }
-
-  res = NormalizeSelection(aSelection);
+  nsCOMPtr<Element> focusElement = mHTMLEditor->GetSelectionContainer();
+  if (focusElement && nsHTMLEditUtils::IsImage(focusElement)) {
+    mNewBlock = focusElement->AsDOMNode();
+    return NS_OK;
+  }
+
+  nsresult res = NormalizeSelection(&aSelection);
   NS_ENSURE_SUCCESS(res, res);
-  NS_ENSURE_STATE(mHTMLEditor);
-  nsAutoSelectionReset selectionResetter(aSelection, mHTMLEditor);
-
-  // convert the selection ranges into "promoted" selection ranges:
-  // this basically just expands the range to include the immediate
-  // block parent, and then further expands to include any ancestors
-  // whose children are all in the range
+  nsAutoSelectionReset selectionResetter(&aSelection, mHTMLEditor);
+
+  // Convert the selection ranges into "promoted" selection ranges: this
+  // basically just expands the range to include the immediate block parent,
+  // and then further expands to include any ancestors whose children are all
+  // in the range.
 
   nsTArray<RefPtr<nsRange>> arrayOfRanges;
-  GetPromotedRanges(*aSelection, arrayOfRanges,
+  GetPromotedRanges(aSelection, arrayOfRanges,
                     EditAction::setAbsolutePosition);
 
-  // use these ranges to contruct a list of nodes to act on.
+  // Use these ranges to contruct a list of nodes to act on.
   nsTArray<OwningNonNull<nsINode>> arrayOfNodes;
   res = GetNodesForOperation(arrayOfRanges, arrayOfNodes,
                              EditAction::setAbsolutePosition);
   NS_ENSURE_SUCCESS(res, res);
 
-  // if nothing visible in list, make an empty block
-  if (ListIsEmptyLine(arrayOfNodes))
-  {
-    // get selection location
-    NS_ENSURE_STATE(aSelection->RangeCount());
-    nsCOMPtr<nsINode> parent = aSelection->GetRangeAt(0)->GetStartParent();
-    int32_t offset = aSelection->GetRangeAt(0)->StartOffset();
-    NS_ENSURE_STATE(parent);
-
-    // make sure we can put a block here
+  // If nothing visible in list, make an empty block
+  if (ListIsEmptyLine(arrayOfNodes)) {
+    // Get selection location
+    NS_ENSURE_STATE(aSelection.GetRangeAt(0) &&
+                    aSelection.GetRangeAt(0)->GetStartParent());
+    OwningNonNull<nsINode> parent =
+      *aSelection.GetRangeAt(0)->GetStartParent();
+    int32_t offset = aSelection.GetRangeAt(0)->StartOffset();
+
+    // Make sure we can put a block here
     res = SplitAsNeeded(*nsGkAtoms::div, parent, offset);
     NS_ENSURE_SUCCESS(res, res);
-    NS_ENSURE_STATE(mHTMLEditor);
-    nsCOMPtr<Element> thePositionedDiv =
+    nsCOMPtr<Element> positionedDiv =
       mHTMLEditor->CreateNode(nsGkAtoms::div, parent, offset);
-    NS_ENSURE_STATE(thePositionedDiv);
-    // remember our new block for postprocessing
-    mNewBlock = thePositionedDiv->AsDOMNode();
-    // delete anything that was in the list of nodes
+    NS_ENSURE_STATE(positionedDiv);
+    // Remember our new block for postprocessing
+    mNewBlock = positionedDiv->AsDOMNode();
+    // Delete anything that was in the list of nodes
     while (!arrayOfNodes.IsEmpty()) {
       OwningNonNull<nsINode> curNode = arrayOfNodes[0];
-      NS_ENSURE_STATE(mHTMLEditor);
       res = mHTMLEditor->DeleteNode(curNode);
       NS_ENSURE_SUCCESS(res, res);
       arrayOfNodes.RemoveElementAt(0);
     }
-    // put selection in new block
-    res = aSelection->Collapse(thePositionedDiv,0);
-    selectionResetter.Abort();  // to prevent selection reseter from overriding us.
+    // Put selection in new block
+    res = aSelection.Collapse(positionedDiv, 0);
+    // Prevent selection resetter from overriding us.
+    selectionResetter.Abort();
     *aHandled = true;
-    return res;
-  }
-
-  // Ok, now go through all the nodes and put them in a blockquote,
-  // or whatever is appropriate.  Wohoo!
-  nsCOMPtr<nsINode> curParent;
-  nsCOMPtr<nsIDOMNode> indentedLI, sibling;
-  nsCOMPtr<Element> curList, curPositionedDiv;
+    NS_ENSURE_SUCCESS(res, res);
+    return NS_OK;
+  }
+
+  // Okay, now go through all the nodes and put them in a blockquote, or
+  // whatever is appropriate.  Woohoo!
+  nsCOMPtr<Element> curList, curPositionedDiv, indentedLI;
   for (uint32_t i = 0; i < arrayOfNodes.Length(); i++) {
-    // here's where we actually figure out what to do
+    // Here's where we actually figure out what to do
     NS_ENSURE_STATE(arrayOfNodes[i]->IsContent());
-    nsCOMPtr<nsIContent> curNode = arrayOfNodes[i]->AsContent();
+    OwningNonNull<nsIContent> curNode = *arrayOfNodes[i]->AsContent();
 
     // Ignore all non-editable nodes.  Leave them be.
-    NS_ENSURE_STATE(mHTMLEditor);
-    if (!mHTMLEditor->IsEditable(curNode)) continue;
-
-    curParent = curNode->GetParentNode();
+    if (!mHTMLEditor->IsEditable(curNode)) {
+      continue;
+    }
+
+    nsCOMPtr<nsIContent> sibling;
+
+    nsCOMPtr<nsINode> curParent = curNode->GetParentNode();
     int32_t offset = curParent ? curParent->IndexOf(curNode) : -1;
 
-    // some logic for putting list items into nested lists...
-    if (nsHTMLEditUtils::IsList(curParent))
-    {
-      // check to see if curList is still appropriate.  Which it is if
-      // curNode is still right after it in the same list.
-      if (curList)
-      {
-        NS_ENSURE_STATE(mHTMLEditor);
-        sibling = GetAsDOMNode(mHTMLEditor->GetPriorHTMLSibling(curNode));
-      }
-
-      if (!curList || (sibling && sibling != GetAsDOMNode(curList))) {
-        // create a new nested list of correct type
+    // Some logic for putting list items into nested lists...
+    if (nsHTMLEditUtils::IsList(curParent)) {
+      // Check to see if curList is still appropriate.  Which it is if curNode
+      // is still right after it in the same list.
+      if (curList) {
+        sibling = mHTMLEditor->GetPriorHTMLSibling(curNode);
+      }
+
+      if (!curList || (sibling && sibling != curList)) {
+        // Create a new nested list of correct type
         res = SplitAsNeeded(*curParent->NodeInfo()->NameAtom(), curParent,
                             offset);
         NS_ENSURE_SUCCESS(res, res);
         if (!curPositionedDiv) {
           nsCOMPtr<nsINode> curParentParent = curParent->GetParentNode();
           int32_t parentOffset = curParentParent
             ? curParentParent->IndexOf(curParent) : -1;
-          NS_ENSURE_STATE(mHTMLEditor);
           curPositionedDiv = mHTMLEditor->CreateNode(nsGkAtoms::div, curParentParent,
                                                      parentOffset);
           mNewBlock = GetAsDOMNode(curPositionedDiv);
         }
-        NS_ENSURE_STATE(mHTMLEditor);
         curList = mHTMLEditor->CreateNode(curParent->NodeInfo()->NameAtom(),
                                           curPositionedDiv, -1);
         NS_ENSURE_STATE(curList);
-        // curList is now the correct thing to put curNode in
-        // remember our new block for postprocessing
-        // mNewBlock = curList;
-      }
-      // tuck the node into the end of the active list
-      NS_ENSURE_STATE(mHTMLEditor);
+        // curList is now the correct thing to put curNode in.  Remember our
+        // new block for postprocessing.
+      }
+      // Tuck the node into the end of the active list
       res = mHTMLEditor->MoveNode(curNode, curList, -1);
       NS_ENSURE_SUCCESS(res, res);
-      // forget curPositionedDiv, if any
-      // curPositionedDiv = nullptr;
-    }
-
-    else // not a list item, use blockquote?
-    {
-      // if we are inside a list item, we don't want to blockquote, we want
-      // to sublist the list item.  We may have several nodes listed in the
-      // array of nodes to act on, that are in the same list item.  Since
-      // we only want to indent that li once, we must keep track of the most
-      // recent indented list item, and not indent it if we find another node
-      // to act on that is still inside the same li.
+    } else {
+      // Not a list item, use blockquote?  If we are inside a list item, we
+      // don't want to blockquote, we want to sublist the list item.  We may
+      // have several nodes listed in the array of nodes to act on, that are in
+      // the same list item.  Since we only want to indent that li once, we
+      // must keep track of the most recent indented list item, and not indent
+      // it if we find another node to act on that is still inside the same li.
       nsCOMPtr<Element> listItem = IsInListItem(curNode);
       if (listItem) {
-        if (indentedLI == GetAsDOMNode(listItem)) {
-          // already indented this list item
+        if (indentedLI == listItem) {
+          // Already indented this list item
           continue;
         }
         curParent = listItem->GetParentNode();
         offset = curParent ? curParent->IndexOf(listItem) : -1;
-        // check to see if curList is still appropriate.  Which it is if
+        // Check to see if curList is still appropriate.  Which it is if
         // curNode is still right after it in the same list.
-        if (curList)
-        {
-          NS_ENSURE_STATE(mHTMLEditor);
-          sibling = GetAsDOMNode(mHTMLEditor->GetPriorHTMLSibling(curNode));
+        if (curList) {
+          sibling = mHTMLEditor->GetPriorHTMLSibling(curNode);
         }
 
-        if (!curList || (sibling && sibling != GetAsDOMNode(curList))) {
-          // create a new nested list of correct type
+        if (!curList || (sibling && sibling != curList)) {
+          // Create a new nested list of correct type
           res = SplitAsNeeded(*curParent->NodeInfo()->NameAtom(), curParent,
                               offset);
           NS_ENSURE_SUCCESS(res, res);
           if (!curPositionedDiv) {
             nsCOMPtr<nsINode> curParentParent = curParent->GetParentNode();
             int32_t parentOffset = curParentParent ?
               curParentParent->IndexOf(curParent) : -1;
-            NS_ENSURE_STATE(mHTMLEditor);
             curPositionedDiv = mHTMLEditor->CreateNode(nsGkAtoms::div,
                                                        curParentParent,
                                                        parentOffset);
             mNewBlock = GetAsDOMNode(curPositionedDiv);
           }
-          NS_ENSURE_STATE(mHTMLEditor);
           curList = mHTMLEditor->CreateNode(curParent->NodeInfo()->NameAtom(),
                                             curPositionedDiv, -1);
           NS_ENSURE_STATE(curList);
         }
-        NS_ENSURE_STATE(mHTMLEditor);
         res = mHTMLEditor->MoveNode(listItem, curList, -1);
         NS_ENSURE_SUCCESS(res, res);
-        // remember we indented this li
-        indentedLI = GetAsDOMNode(listItem);
-      }
-
-      else
-      {
-        // need to make a div to put things in if we haven't already
-
-        if (!curPositionedDiv)
-        {
+        // Remember we indented this li
+        indentedLI = listItem;
+      } else {
+        // Need to make a div to put things in if we haven't already
+
+        if (!curPositionedDiv) {
           if (curNode->IsHTMLElement(nsGkAtoms::div)) {
             curPositionedDiv = curNode->AsElement();
             mNewBlock = GetAsDOMNode(curPositionedDiv);
             curList = nullptr;
             continue;
           }
           res = SplitAsNeeded(*nsGkAtoms::div, curParent, offset);
           NS_ENSURE_SUCCESS(res, res);
-          NS_ENSURE_STATE(mHTMLEditor);
           curPositionedDiv = mHTMLEditor->CreateNode(nsGkAtoms::div, curParent,
                                                      offset);
           NS_ENSURE_STATE(curPositionedDiv);
-          // remember our new block for postprocessing
+          // Remember our new block for postprocessing
           mNewBlock = GetAsDOMNode(curPositionedDiv);
           // curPositionedDiv is now the correct thing to put curNode in
         }
 
-        // tuck the node into the end of the active blockquote
-        NS_ENSURE_STATE(mHTMLEditor);
+        // Tuck the node into the end of the active blockquote
         res = mHTMLEditor->MoveNode(curNode, curPositionedDiv, -1);
         NS_ENSURE_SUCCESS(res, res);
-        // forget curList, if any
+        // Forget curList, if any
         curList = nullptr;
       }
     }
   }
-  return res;
+  return NS_OK;
 }
 
 nsresult
 nsHTMLEditRules::DidAbsolutePosition()
 {
   NS_ENSURE_STATE(mHTMLEditor);
   nsCOMPtr<nsIHTMLAbsPosEditor> absPosHTMLEditor = mHTMLEditor;
   nsCOMPtr<nsIDOMElement> elt = do_QueryInterface(mNewBlock);
--- a/editor/libeditor/nsHTMLEditRules.h
+++ b/editor/libeditor/nsHTMLEditRules.h
@@ -164,17 +164,17 @@ protected:
                           bool* aHandled);
   nsresult WillIndent(Selection* aSelection, bool* aCancel, bool* aHandled);
   nsresult WillCSSIndent(Selection* aSelection, bool* aCancel, bool* aHandled);
   nsresult WillHTMLIndent(Selection* aSelection, bool* aCancel,
                           bool* aHandled);
   nsresult WillOutdent(Selection& aSelection, bool* aCancel, bool* aHandled);
   nsresult WillAlign(Selection& aSelection, const nsAString& aAlignType,
                      bool* aCancel, bool* aHandled);
-  nsresult WillAbsolutePosition(Selection* aSelection, bool* aCancel,
+  nsresult WillAbsolutePosition(Selection& aSelection, bool* aCancel,
                                 bool* aHandled);
   nsresult WillRemoveAbsolutePosition(Selection* aSelection, bool* aCancel,
                                       bool* aHandled);
   nsresult WillRelativeChangeZIndex(Selection* aSelection, int32_t aChange,
                                     bool* aCancel, bool* aHandled);
   nsresult WillMakeDefListItem(Selection* aSelection,
                                const nsAString* aBlockType, bool aEntireList,
                                bool* aCancel, bool* aHandled);