Bug 1482017 - part 1: Create HTMLEditor::CollapseSelectionAfter() for internal use of nsIHTMLEditor::SetCaretAfterElement() r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Tue, 14 Aug 2018 06:13:54 +0000
changeset 829434 581fb40d63fb10e1ea8b29ca2b52deb12b914bc3
parent 829433 b6f1266dd6c5bf04a6dc68a5e7a19dd5f5230f51
child 829435 d0054c7c0ed9abd6182f9369fbd64c4bd48a2f04
push id118781
push userktomlinson@mozilla.com
push dateThu, 16 Aug 2018 02:46:06 +0000
reviewersm_kato
bugs1482017
milestone63.0a1
Bug 1482017 - part 1: Create HTMLEditor::CollapseSelectionAfter() for internal use of nsIHTMLEditor::SetCaretAfterElement() r=m_kato We need to make it possible nsIHTMLEditor::SetCaretAfterElement() to distinguish if it's called by outer class or editor itself. Therefore, this patch creates CollapseSelectionAfter() for internal use. Differential Revision: https://phabricator.services.mozilla.com/D3188
editor/libeditor/HTMLEditor.cpp
editor/libeditor/HTMLEditor.h
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -1632,18 +1632,20 @@ HTMLEditor::InsertElementAtSelection(Ele
         InsertNodeIntoProperAncestorWithTransaction(
           *aElement, pointToInsert, SplitAtEdges::eAllowToCreateEmptyContainer);
       if (NS_WARN_IF(!insertedPoint.IsSet())) {
         return NS_ERROR_FAILURE;
       }
       // Set caret after element, but check for special case
       //  of inserting table-related elements: set in first cell instead
       if (!SetCaretInTableCell(aElement)) {
-        rv = SetCaretAfterElement(aElement);
-        NS_ENSURE_SUCCESS(rv, rv);
+        rv = CollapseSelectionAfter(*selection, *aElement);
+        if (NS_WARN_IF(NS_FAILED(rv))) {
+          return rv;
+        }
       }
       // check for inserting a whole table at the end of a block. If so insert
       // a br after it.
       if (HTMLEditUtils::IsTable(aElement) &&
           IsLastEditableChild(aElement)) {
         DebugOnly<bool> advanced = insertedPoint.AdvanceOffset();
         NS_WARNING_ASSERTION(advanced,
           "Failed to advance offset from inserted point");
@@ -1776,31 +1778,53 @@ HTMLEditor::SelectContentInternal(Select
     return rv;
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP
 HTMLEditor::SetCaretAfterElement(Element* aElement)
 {
-  // Be sure the element is contained in the document body
-  if (!aElement || !IsDescendantOfEditorRoot(aElement)) {
-    return NS_ERROR_NULL_POINTER;
-  }
-
+  if (NS_WARN_IF(!aElement)) {
+    return NS_ERROR_INVALID_ARG;
+  }
   RefPtr<Selection> selection = GetSelection();
-  NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
-  nsCOMPtr<nsINode> parent = aElement->GetParentNode();
-  NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
+  if (NS_WARN_IF(!selection)) {
+    return NS_ERROR_FAILURE;
+  }
+  nsresult rv = CollapseSelectionAfter(*selection, *aElement);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+  return NS_OK;
+}
+
+nsresult
+HTMLEditor::CollapseSelectionAfter(Selection& aSelection,
+                                   Element& aElement)
+{
+  // Be sure the element is contained in the document body
+  if (NS_WARN_IF(!IsDescendantOfEditorRoot(&aElement))) {
+    return NS_ERROR_INVALID_ARG;
+  }
+  nsINode* parent = aElement.GetParentNode();
+  if (NS_WARN_IF(!parent)) {
+    return NS_ERROR_FAILURE;
+  }
   // Collapse selection to just after desired element,
-  EditorRawDOMPoint afterElement(aElement);
+  EditorRawDOMPoint afterElement(&aElement);
   if (NS_WARN_IF(!afterElement.AdvanceOffset())) {
     return NS_ERROR_FAILURE;
   }
-  return selection->Collapse(afterElement);
+  ErrorResult error;
+  aSelection.Collapse(afterElement, error);
+  if (NS_WARN_IF(error.Failed())) {
+    return error.StealNSResult();
+  }
+  return NS_OK;
 }
 
 NS_IMETHODIMP
 HTMLEditor::SetParagraphFormat(const nsAString& aParagraphFormat)
 {
   nsAutoString lowerCaseTagName(aParagraphFormat);
   ToLowerCase(lowerCaseTagName);
   RefPtr<nsAtom> tagName = NS_Atomize(lowerCaseTagName);
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -778,16 +778,23 @@ protected: // Shouldn't be used by frien
    * @param aSelection          The Selection, callers have to guarantee the
    *                            lifetime.
    * @param aContentToSelect    The content which should be selected.
    */
   nsresult SelectContentInternal(Selection& aSelection,
                                  nsIContent& aContentToSelect);
 
   /**
+   * CollapseSelectionAfter() collapses Selection after aElement.
+   * If aElement is an orphan node or not in editing host, returns error.
+   */
+  nsresult CollapseSelectionAfter(Selection& aSelection,
+                                  Element& aElement);
+
+  /**
    * PasteInternal() pasts text with replacing selected content.
    * This tries to dispatch ePaste event first.  If its defaultPrevent() is
    * called, this does nothing but returns NS_OK.
    *
    * @param aClipboardType  nsIClipboard::kGlobalClipboard or
    *                        nsIClipboard::kSelectionClipboard.
    */
   nsresult PasteInternal(int32_t aClipboardType);