Bug 1416099 - part 2: Rename |sibling| in HTMLEditRules::ReturnInParagraph() to |brNode| r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Fri, 10 Nov 2017 14:43:13 +0900
changeset 696345 0268dc157c2ab48aef6a0b7da932962b092a4897
parent 696344 48a99c120c9995fee123428cb1721b9f15993c72
child 696346 277a25c64a93c8e1191f3e7225538e955d94d0d1
push id88684
push usermasayuki@d-toybox.com
push dateFri, 10 Nov 2017 13:42:43 +0000
reviewersm_kato
bugs1416099
milestone58.0a1
Bug 1416099 - part 2: Rename |sibling| in HTMLEditRules::ReturnInParagraph() to |brNode| r?m_kato The local variable |sibling| in HTMLEditRules::ReturnInParagraph() is set to aBRNode of SplitParagraph(). So, the name should be renamed to |brNode|. Then, the |brNode| should be cleared if it's not a <br> node actually. However, SplitParagraph() doesn't allow aBRNode to be nullptr. aBRNode is only used to remove it from the document if it's not necessary. So, it should be able to be nullptr. Therefore, this patch changes SplitParagraph() too. Note that SplitParagrah() is called only by ReturnInParagraph(). So, we don't need to worry about other callers. MozReview-Commit-ID: 7Ynk9m5F8Mi
editor/libeditor/HTMLEditRules.cpp
editor/libeditor/HTMLEditRules.h
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -6740,41 +6740,43 @@ HTMLEditRules::ReturnInParagraph(Selecti
 
   int32_t offset;
   nsCOMPtr<nsINode> parent = EditorBase::GetNodeLocation(node, &offset);
 
   bool doesCRCreateNewP = htmlEditor->GetReturnInParagraphCreatesNewParagraph();
 
   bool newBRneeded = false;
   bool newSelNode = false;
-  nsCOMPtr<nsIContent> sibling;
+  nsCOMPtr<nsIContent> brNode;
   nsCOMPtr<nsIDOMNode> selNode = GetAsDOMNode(aNode);
   int32_t selOffset = aOffset;
 
   if (aNode == aPara && doesCRCreateNewP) {
     // we are at the edges of the block, newBRneeded not needed!
-    sibling = node->AsContent();
+    brNode = nullptr;
   } else if (EditorBase::IsTextNode(aNode)) {
     // at beginning of text node?
     if (!aOffset) {
       // is there a BR prior to it?
-      sibling = htmlEditor->GetPriorHTMLSibling(node);
-      if (!sibling ||
-          !htmlEditor->IsVisibleBRElement(sibling) ||
-          TextEditUtils::HasMozAttr(GetAsDOMNode(sibling))) {
+      brNode = htmlEditor->GetPriorHTMLSibling(node);
+      if (!brNode ||
+          !htmlEditor->IsVisibleBRElement(brNode) ||
+          TextEditUtils::HasMozAttr(GetAsDOMNode(brNode))) {
         newBRneeded = true;
+        brNode = nullptr;
       }
     } else if (aOffset == static_cast<int32_t>(node->Length())) {
       // we're at the end of text node...
       // is there a BR after to it?
-      sibling = htmlEditor->GetNextHTMLSibling(node);
-      if (!sibling ||
-          !htmlEditor->IsVisibleBRElement(sibling) ||
-          TextEditUtils::HasMozAttr(GetAsDOMNode(sibling))) {
+      brNode = htmlEditor->GetNextHTMLSibling(node);
+      if (!brNode ||
+          !htmlEditor->IsVisibleBRElement(brNode) ||
+          TextEditUtils::HasMozAttr(GetAsDOMNode(brNode))) {
         newBRneeded = true;
+        brNode = nullptr;
         offset++;
       }
     } else {
       if (doesCRCreateNewP) {
         nsCOMPtr<nsIDOMNode> tmp;
         nsresult rv =
           htmlEditor->SplitNode(selNode, aOffset, getter_AddRefs(tmp));
         if (NS_WARN_IF(NS_FAILED(rv))) {
@@ -6801,61 +6803,57 @@ HTMLEditRules::ReturnInParagraph(Selecti
       if (!nearNode || !htmlEditor->IsVisibleBRElement(nearNode) ||
           TextEditUtils::HasMozAttr(GetAsDOMNode(nearNode))) {
         newBRneeded = true;
         parent = node;
         offset = aOffset;
         newSelNode = true;
       }
     }
-    if (!newBRneeded) {
-      sibling = nearNode;
+    if (!newBRneeded && TextEditUtils::IsBreak(nearNode)) {
+      brNode = nearNode;
     }
   }
   if (newBRneeded) {
     // Don't modify the DOM tree if mHTMLEditor disappeared.
     if (NS_WARN_IF(!mHTMLEditor)) {
       return EditActionResult(NS_ERROR_NOT_AVAILABLE);
     }
 
     // if CR does not create a new P, default to BR creation
     if (NS_WARN_IF(!doesCRCreateNewP)) {
       return EditActionResult(NS_OK);
     }
 
-    sibling = htmlEditor->CreateBR(parent, offset);
+    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), sibling, aSelection,
+    SplitParagraph(GetAsDOMNode(aPara), brNode, aSelection,
                    address_of(selNode), &selOffset));
   result.MarkAsHandled();
   if (NS_WARN_IF(result.Failed())) {
     return result;
   }
   return result;
 }
 
-/**
- * SplitParagraph() splits a paragraph at selection point, possibly deleting a
- * br.
- */
 nsresult
 HTMLEditRules::SplitParagraph(nsIDOMNode *aPara,
                               nsIContent* aBRNode,
                               Selection* aSelection,
                               nsCOMPtr<nsIDOMNode>* aSelNode,
                               int32_t* aOffset)
 {
   nsCOMPtr<Element> para = do_QueryInterface(aPara);
-  NS_ENSURE_TRUE(para && aBRNode && aSelNode && *aSelNode && aOffset &&
+  NS_ENSURE_TRUE(para && aSelNode && *aSelNode && aOffset &&
                  aSelection, NS_ERROR_NULL_POINTER);
 
   // split para
   // get ws code to adjust any ws
   nsCOMPtr<nsIContent> leftPara, rightPara;
   NS_ENSURE_STATE(mHTMLEditor);
   nsCOMPtr<nsINode> selNode(do_QueryInterface(*aSelNode));
   nsresult rv =
@@ -6873,17 +6871,18 @@ HTMLEditRules::SplitParagraph(nsIDOMNode
                                HTMLEditor::EmptyContainers::yes,
                                getter_AddRefs(leftPara),
                                getter_AddRefs(rightPara));
   if (NS_WARN_IF(offset == -1)) {
     return NS_ERROR_FAILURE;
   }
   // get rid of the break, if it is visible (otherwise it may be needed to prevent an empty p)
   NS_ENSURE_STATE(mHTMLEditor);
-  if (mHTMLEditor->IsVisibleBRElement(aBRNode)) {
+  if (aBRNode &&
+      mHTMLEditor->IsVisibleBRElement(aBRNode)) {
     NS_ENSURE_STATE(mHTMLEditor);
     rv = mHTMLEditor->DeleteNode(aBRNode);
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   // remove ID attribute on the paragraph we just created
   RefPtr<Element> rightElt = rightPara->AsElement();
   NS_ENSURE_STATE(mHTMLEditor);
--- a/editor/libeditor/HTMLEditRules.h
+++ b/editor/libeditor/HTMLEditRules.h
@@ -292,21 +292,39 @@ protected:
                        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,
                                      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.
+   *                    If this is not nullptr, the <br> node may be removed.
+   * @param aSelection  The selection.
+   * @param aSelNode    Set the selection container to split aPara at.
+   *                    Actual container node will be set by this method.
+   *                    XXX: The only caller ReturnInParagraph() doesn't need
+   *                         this result.
+   * @param aOffset     Set the offset in the container.
+   *                    Actual offset will be set by this method.
+   *                    XXX: The only caller ReturnInParagraph() doesn't need
+   *                         this result.
+   */
   nsresult SplitParagraph(nsIDOMNode* aPara,
                           nsIContent* aBRNode,
                           Selection* aSelection,
                           nsCOMPtr<nsIDOMNode>* aSelNode,
                           int32_t* aOffset);
+
   nsresult ReturnInListItem(Selection& aSelection, Element& aHeader,
                             nsINode& aNode, int32_t aOffset);
   nsresult AfterEditInner(EditAction action,
                           nsIEditor::EDirection aDirection);
   nsresult RemovePartOfBlock(Element& aBlock, nsIContent& aStartChild,
                              nsIContent& aEndChild);
   void SplitBlock(Element& aBlock,
                   nsIContent& aStartChild,