Bug 1482016 - part 1: Create HTMLEditor::SelectContentInternal() for internal use of HTMLEditor::SelectElement() r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Tue, 14 Aug 2018 08:36:59 +0000
changeset 486465 419fd4edef4fb7057f7a168ee03242df759f6fac
parent 486464 7fa9542af27771814577a9825c3c378841979476
child 486466 0af5942407da25f69e19a5533e921c5f6722fbf8
push id9719
push userffxbld-merge
push dateFri, 24 Aug 2018 17:49:46 +0000
treeherdermozilla-beta@719ec98fba77 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1482016
milestone63.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1482016 - part 1: Create HTMLEditor::SelectContentInternal() for internal use of HTMLEditor::SelectElement() r=m_kato For making it possible HTMLEditor::SelectElement() to distinguish if it's called by outer class or editor itself, this patch creates HTMLEditor::SelectContentInternal(). Differential Revision: https://phabricator.services.mozilla.com/D3001
editor/libeditor/HTMLEditor.cpp
editor/libeditor/HTMLEditor.h
editor/libeditor/HTMLTableEditor.cpp
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -1726,37 +1726,61 @@ HTMLEditor::InsertNodeIntoProperAncestor
     }
   }
   return pointToInsert;
 }
 
 NS_IMETHODIMP
 HTMLEditor::SelectElement(Element* aElement)
 {
+  if (NS_WARN_IF(!aElement)) {
+    return NS_ERROR_INVALID_ARG;
+  }
+  RefPtr<Selection> selection = GetSelection();
+  if (NS_WARN_IF(!selection)) {
+    return NS_ERROR_FAILURE;
+  }
+  nsresult rv = SelectContentInternal(*selection, *aElement);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+  return NS_OK;
+}
+
+nsresult
+HTMLEditor::SelectContentInternal(Selection& aSelection,
+                                  nsIContent& aContentToSelect)
+{
   // Must be sure that element is contained in the document body
-  if (!IsDescendantOfEditorRoot(aElement)) {
-    return NS_ERROR_NULL_POINTER;
-  }
-
-  RefPtr<Selection> selection = GetSelection();
-  NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
-  nsINode* parent = aElement->GetParentNode();
+  if (!IsDescendantOfEditorRoot(&aContentToSelect)) {
+    return NS_ERROR_FAILURE;
+  }
+
+  nsINode* parent = aContentToSelect.GetParentNode();
   if (NS_WARN_IF(!parent)) {
     return NS_ERROR_FAILURE;
   }
 
-  int32_t offsetInParent = parent->ComputeIndexOf(aElement);
+  // Don't notify selection change at collapse.
+  AutoUpdateViewBatch notifySelectionChangeOnce(this);
+
+  // XXX Perhaps, Selection should have SelectNode(nsIContent&).
+  int32_t offsetInParent = parent->ComputeIndexOf(&aContentToSelect);
 
   // Collapse selection to just before desired element,
-  nsresult rv = selection->Collapse(parent, offsetInParent);
-  if (NS_SUCCEEDED(rv)) {
-    // then extend it to just after
-    rv = selection->Extend(parent, offsetInParent + 1);
-  }
-  return rv;
+  nsresult rv = aSelection.Collapse(parent, offsetInParent);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+  // then extend it to just after
+  rv = aSelection.Extend(parent, offsetInParent + 1);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    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;
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -767,16 +767,27 @@ protected: // Called by helper classes.
   virtual void OnEndHandlingTopLevelEditSubAction() override;
 
 protected: // Shouldn't be used by friend classes
   virtual ~HTMLEditor();
 
   virtual nsresult SelectAllInternal() override;
 
   /**
+   * SelectContentInternal() sets Selection to aContentToSelect to
+   * aContentToSelect + 1 in parent of aContentToSelect.
+   *
+   * @param aSelection          The Selection, callers have to guarantee the
+   *                            lifetime.
+   * @param aContentToSelect    The content which should be selected.
+   */
+  nsresult SelectContentInternal(Selection& aSelection,
+                                 nsIContent& aContentToSelect);
+
+  /**
    * 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);
--- a/editor/libeditor/HTMLTableEditor.cpp
+++ b/editor/libeditor/HTMLTableEditor.cpp
@@ -3070,17 +3070,19 @@ HTMLEditor::SetSelectionAfterTableEdit(E
     nsresult rv = GetCellAt(aTable, aRow, aCol, getter_AddRefs(cell));
     if (NS_FAILED(rv)) {
       break;
     }
 
     if (cell) {
       if (aSelected) {
         // Reselect the cell
-        SelectElement(cell);
+        DebugOnly<nsresult> rv = SelectContentInternal(*selection, *cell);
+        NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
+          "Failed to select the cell");
         return;
       }
 
       // Set the caret to deepest first child
       //   but don't go into nested tables
       // TODO: Should we really be placing the caret at the END
       //  of the cell content?
       CollapseSelectionToDeepestNonTableFirstChild(selection, cell);