Bug 1314790 - Part 1. GetGoodSelPointForNode doesn't work with ePrevousWord action. r?masayuki draft
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Sat, 12 Nov 2016 21:59:04 +0900
changeset 438166 b22cd532b6072e19fe1bff7f90a0e4a4ae4236e1
parent 438165 71825cbd0e2549d813d8279dcdb19fb357f8ab3f
child 438167 6adb8338981c7654c3b9af2ab0f0ac46a9effb84
push id35638
push userm_kato@ga2.so-net.ne.jp
push dateMon, 14 Nov 2016 00:57:17 +0000
reviewersmasayuki
bugs1314790
milestone52.0a1
Bug 1314790 - Part 1. GetGoodSelPointForNode doesn't work with ePrevousWord action. r?masayuki Although GetGoodSelPointForNode only supports ePrevious or eNext as action, we use other action. So we should add aseetion for it. MozReview-Commit-ID: 3gLFFTAdNxU
editor/libeditor/HTMLEditRules.cpp
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -2506,37 +2506,47 @@ HTMLEditRules::InsertBRIfNeeded(Selectio
 }
 
 /**
  * GetGoodSelPointForNode() finds where at a node you would want to set the
  * selection if you were trying to have a caret next to it.  Always returns a
  * valid value (unless mHTMLEditor has gone away).
  *
  * @param aNode         The node
- * @param aAction       Which edge to find: eNext indicates beginning,
- *                      ePrevious ending.
+ * @param aAction       Which edge to find:
+ *                        eNext/eNextWord/eToEndOfLine indicates beginning,
+ *                        ePrevious/PreviousWord/eToBeginningOfLine ending.
  */
 EditorDOMPoint
 HTMLEditRules::GetGoodSelPointForNode(nsINode& aNode,
                                       nsIEditor::EDirection aAction)
 {
+  MOZ_ASSERT(aAction == nsIEditor::eNext ||
+             aAction == nsIEditor::eNextWord ||
+             aAction == nsIEditor::ePrevious ||
+             aAction == nsIEditor::ePreviousWord ||
+             aAction == nsIEditor::eToBeginningOfLine ||
+             aAction == nsIEditor::eToEndOfLine);
+
+  bool isPreviousAction = (aAction == nsIEditor::ePrevious ||
+                           aAction == nsIEditor::ePreviousWord ||
+                           aAction == nsIEditor::eToBeginningOfLine);
+
   NS_ENSURE_TRUE(mHTMLEditor, EditorDOMPoint());
   if (aNode.GetAsText() || mHTMLEditor->IsContainer(&aNode) ||
       NS_WARN_IF(!aNode.GetParentNode())) {
-    return EditorDOMPoint(&aNode,
-                          aAction == nsIEditor::ePrevious ? aNode.Length() : 0);
+    return EditorDOMPoint(&aNode, isPreviousAction ? aNode.Length() : 0);
   }
 
   EditorDOMPoint ret;
   ret.node = aNode.GetParentNode();
   ret.offset = ret.node ? ret.node->IndexOf(&aNode) : -1;
   NS_ENSURE_TRUE(mHTMLEditor, EditorDOMPoint());
   if ((!aNode.IsHTMLElement(nsGkAtoms::br) ||
-       mHTMLEditor->IsVisBreak(&aNode)) &&
-      aAction == nsIEditor::ePrevious) {
+       mHTMLEditor->IsVisBreak(&aNode)) && isPreviousAction) {
     ret.offset++;
   }
   return ret;
 }
 
 
 /**
  * This method is used to join two block elements.  The right element is always
@@ -4837,42 +4847,47 @@ HTMLEditRules::CheckForEmptyBlock(nsINod
           // Adjust selection to be right before it
           rv = aSelection->Collapse(listParent, listOffset);
           NS_ENSURE_SUCCESS(rv, rv);
         }
         // Else just let selection percolate up.  We'll adjust it in
         // AfterEdit()
       }
     } else {
-      if (aAction == nsIEditor::eNext) {
+      if (aAction == nsIEditor::eNext || aAction == nsIEditor::eNextWord ||
+          aAction == nsIEditor::eToEndOfLine) {
         // Move to the start of the next node, if any
         nsCOMPtr<nsIContent> nextNode = htmlEditor->GetNextNode(blockParent,
                                                                 offset + 1, true);
         if (nextNode) {
           EditorDOMPoint pt = GetGoodSelPointForNode(*nextNode, aAction);
           nsresult rv = aSelection->Collapse(pt.node, pt.offset);
           NS_ENSURE_SUCCESS(rv, rv);
         } else {
           // Adjust selection to be right after it.
           nsresult rv = aSelection->Collapse(blockParent, offset + 1);
           NS_ENSURE_SUCCESS(rv, rv);
         }
-      } else {
+      } else if (aAction == nsIEditor::ePrevious ||
+                 aAction == nsIEditor::ePreviousWord ||
+                 aAction == nsIEditor::eToBeginningOfLine) {
         // Move to the end of the previous node
         nsCOMPtr<nsIContent> priorNode = htmlEditor->GetPriorNode(blockParent,
                                                                   offset,
                                                                   true);
         if (priorNode) {
           EditorDOMPoint pt = GetGoodSelPointForNode(*priorNode, aAction);
           nsresult rv = aSelection->Collapse(pt.node, pt.offset);
           NS_ENSURE_SUCCESS(rv, rv);
         } else {
           nsresult rv = aSelection->Collapse(blockParent, offset + 1);
           NS_ENSURE_SUCCESS(rv, rv);
         }
+      } else {
+        NS_RUNTIMEABORT("CheckForEmptyBlock doesn't support this action yet");
       }
     }
     NS_ENSURE_STATE(htmlEditor);
     *aHandled = true;
     nsresult rv = htmlEditor->DeleteNode(emptyBlock);
     NS_ENSURE_SUCCESS(rv, rv);
   }
   return NS_OK;