Bug 1416099 - part 3: Rename aPara of HTMLEditRules::ReturnInParagraph() to aParentDivOrP and make it and aSelection as references r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Fri, 10 Nov 2017 14:58:53 +0900
changeset 696346 277a25c64a93c8e1191f3e7225538e955d94d0d1
parent 696345 0268dc157c2ab48aef6a0b7da932962b092a4897
child 696347 ae662cdef973787d17907c4eafd6d431ceeb170d
push id88684
push usermasayuki@d-toybox.com
push dateFri, 10 Nov 2017 13:42:43 +0000
reviewersm_kato
bugs1416099
milestone58.0a1
Bug 1416099 - part 3: Rename aPara of HTMLEditRules::ReturnInParagraph() to aParentDivOrP and make it and aSelection as references r?m_kato This cleans up |aSelection| and |aPara| of HTMLEditRules::ReturnInParagraph(). |aSelection| should be reference for avoiding nullptr check. |aPara| is so too. Additionally, the name is not clear. We should rename it to |aParentDivOrP| because it's a block parent of the point and has to be <div> or <p> element. MozReview-Commit-ID: 8LbKGlrvaIj
editor/libeditor/HTMLEditRules.cpp
editor/libeditor/HTMLEditRules.h
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -1784,17 +1784,17 @@ HTMLEditRules::WillInsertBreak(Selection
   //     paragraph separator is <br>.  Otherwise, take consistent behavior
   //     between <p> container and <div> container.
   else if ((separator == ParagraphSeparator::br &&
             blockParent->IsHTMLElement(nsGkAtoms::p)) ||
            (separator != ParagraphSeparator::br &&
             blockParent->IsAnyOfHTMLElements(nsGkAtoms::p, nsGkAtoms::div))) {
     // Paragraphs: special rules to look for <br>s
     EditActionResult result =
-      ReturnInParagraph(&aSelection, blockParent, node, offset, child);
+      ReturnInParagraph(aSelection, *blockParent, node, offset, child);
     if (NS_WARN_IF(result.Failed())) {
       return result.Rv();
     }
     *aHandled = result.Handled();
     *aCancel = result.Canceled();
     // Fall through, we may not have handled it in ReturnInParagraph()
   }
 
@@ -6709,52 +6709,46 @@ HTMLEditRules::ReturnInHeader(Selection&
   } else {
     // Put selection at front of righthand heading
     rv = aSelection.Collapse(&aHeader, 0);
     NS_ENSURE_SUCCESS(rv, rv);
   }
   return NS_OK;
 }
 
-/**
- * ReturnInParagraph() does the right thing for returns pressed in paragraphs.
- * For our purposes, this means either <p> or <div>, which is not in keeping
- * with the semantics of <div>, but is necessary for compatibility with other
- * browsers.
- */
 EditActionResult
-HTMLEditRules::ReturnInParagraph(Selection* aSelection,
-                                 nsINode* aPara,
+HTMLEditRules::ReturnInParagraph(Selection& aSelection,
+                                 nsINode& aParentDivOrP,
                                  nsINode* aNode,
                                  int32_t aOffset,
                                  nsIContent* aChildAtOffset)
 {
   if (NS_WARN_IF(!mHTMLEditor)) {
     return EditActionResult(NS_ERROR_NOT_AVAILABLE);
   }
 
   nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
-  if (!aSelection || !aPara || !node) {
+  if (!node) {
     return EditActionResult(NS_ERROR_NULL_POINTER);
   }
 
   RefPtr<HTMLEditor> htmlEditor = mHTMLEditor;
 
   int32_t offset;
   nsCOMPtr<nsINode> parent = EditorBase::GetNodeLocation(node, &offset);
 
   bool doesCRCreateNewP = htmlEditor->GetReturnInParagraphCreatesNewParagraph();
 
   bool newBRneeded = false;
   bool newSelNode = false;
   nsCOMPtr<nsIContent> brNode;
   nsCOMPtr<nsIDOMNode> selNode = GetAsDOMNode(aNode);
   int32_t selOffset = aOffset;
 
-  if (aNode == aPara && doesCRCreateNewP) {
+  if (aNode == &aParentDivOrP && doesCRCreateNewP) {
     // we are at the edges of the block, newBRneeded not needed!
     brNode = nullptr;
   } else if (EditorBase::IsTextNode(aNode)) {
     // at beginning of text node?
     if (!aOffset) {
       // is there a BR prior to it?
       brNode = htmlEditor->GetPriorHTMLSibling(node);
       if (!brNode ||
@@ -6826,17 +6820,17 @@ HTMLEditRules::ReturnInParagraph(Selecti
     brNode = htmlEditor->CreateBR(parent, offset);
     if (newSelNode) {
       // We split the parent after the br we've just inserted.
       selNode = GetAsDOMNode(parent);
       selOffset = offset + 1;
     }
   }
   EditActionResult result(
-    SplitParagraph(GetAsDOMNode(aPara), brNode, aSelection,
+    SplitParagraph(GetAsDOMNode(&aParentDivOrP), brNode, &aSelection,
                    address_of(selNode), &selOffset));
   result.MarkAsHandled();
   if (NS_WARN_IF(result.Failed())) {
     return result;
   }
   return result;
 }
 
--- a/editor/libeditor/HTMLEditRules.h
+++ b/editor/libeditor/HTMLEditRules.h
@@ -289,17 +289,33 @@ protected:
   void GetInnerContent(nsINode& aNode,
                        nsTArray<OwningNonNull<nsINode>>& aOutArrayOfNodes,
                        int32_t* aIndex, Lists aLists = Lists::yes,
                        Tables aTables = Tables::yes);
   Element* IsInListItem(nsINode* aNode);
   nsAtom& DefaultParagraphSeparator();
   nsresult ReturnInHeader(Selection& aSelection, Element& aHeader,
                           nsINode& aNode, int32_t aOffset);
-  EditActionResult ReturnInParagraph(Selection* aSelection, nsINode* aHeader,
+
+  /**
+   * ReturnInParagraph() does the right thing for Enter key press or
+   * 'insertParagraph' command in aParentDivOrP.
+   *
+   * @param aSelection      The selection
+   * @param aParentDivOrP   The parent block.  This must be <p> or <div>
+   *                        element.
+   * @param aTextNode
+   * @param aOffset
+   * @param aChildAtOffset
+   * @return                Returns with NS_OK if this doesn't meat any
+   *                        unexpected situation.  If this method tries to
+   *                        split the paragraph, marked as handled.
+   */
+  EditActionResult ReturnInParagraph(Selection& aSelection,
+                                     nsINode& aParentDivOrP,
                                      nsINode* aTextNode, int32_t aOffset,
                                      nsIContent* aChildAtOffset);
 
   /**
    * SplitParagraph() splits the parent block, aPara, at aSelNode - aOffset.
    *
    * @param aPara       The parent block to be split.
    * @param aBRNode     Next <br> node if there is.  Otherwise, nullptr.