Bug 1627175 - part 62: Get rid of `HTMLEditor::IsPrevCharInNodeWhiteSpace()` r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Mon, 17 May 2021 22:25:30 +0000
changeset 579825 6a05527852751ddd4ce377226fe5968663c90bc7
parent 579824 51c404b61e6c0c770d0cb8bb200398c2eb9bbed9
child 579826 78d4066a51a17f375fedc64f090a3d0a60f4ba1c
push id38469
push userncsoregi@mozilla.com
push dateTue, 18 May 2021 09:45:31 +0000
treeherdermozilla-central@4f930697652e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1627175
milestone90.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 1627175 - part 62: Get rid of `HTMLEditor::IsPrevCharInNodeWhiteSpace()` r=m_kato It's used only by `HTMLEditor::GetWhiteSpaceEndPoint()` and it just skips white-spaces and returns first white-space position if and only if the given point container is a text node and its previous character is a white-space. Therefore, it can be rewritten with `EditorDOMPoint` simply in the caller. Differential Revision: https://phabricator.services.mozilla.com/D115171
editor/libeditor/HTMLEditSubActionHandler.cpp
editor/libeditor/HTMLEditor.cpp
editor/libeditor/HTMLEditor.h
--- a/editor/libeditor/HTMLEditSubActionHandler.cpp
+++ b/editor/libeditor/HTMLEditSubActionHandler.cpp
@@ -5479,37 +5479,44 @@ nsresult HTMLEditor::MaybeExtendSelectio
   }
   return error.StealNSResult();
 }
 
 // static
 template <typename PT, typename RT>
 EditorDOMPoint HTMLEditor::GetWhiteSpaceEndPoint(
     const RangeBoundaryBase<PT, RT>& aPoint, ScanDirection aScanDirection) {
-  if (NS_WARN_IF(!aPoint.IsSet()) ||
+  if (NS_WARN_IF(!aPoint.IsSetAndValid()) ||
       NS_WARN_IF(!aPoint.Container()->IsContent())) {
     return EditorDOMPoint();
   }
 
+  if (aScanDirection == ScanDirection::Backward) {
+    EditorDOMPoint point(aPoint);
+    if (point.IsInTextNode()) {
+      while (!point.IsStartOfContainer()) {
+        if (!point.IsPreviousCharASCIISpaceOrNBSP()) {
+          break;
+        }
+        MOZ_ALWAYS_TRUE(point.RewindOffset());
+      }
+    }
+    return point;
+  }
+
   bool isSpace = false, isNBSP = false;
   nsIContent* newContent = aPoint.Container()->AsContent();
   int32_t newOffset = *aPoint.Offset(
       RangeBoundaryBase<PT, RT>::OffsetFilter::kValidOrInvalidOffsets);
   while (newContent) {
     int32_t offset = -1;
     nsCOMPtr<nsIContent> content;
-    if (aScanDirection == ScanDirection::Backward) {
-      HTMLEditor::IsPrevCharInNodeWhiteSpace(newContent, newOffset, &isSpace,
-                                             &isNBSP, getter_AddRefs(content),
-                                             &offset);
-    } else {
-      HTMLEditor::IsNextCharInNodeWhiteSpace(newContent, newOffset, &isSpace,
-                                             &isNBSP, getter_AddRefs(content),
-                                             &offset);
-    }
+    HTMLEditor::IsNextCharInNodeWhiteSpace(newContent, newOffset, &isSpace,
+                                           &isNBSP, getter_AddRefs(content),
+                                           &offset);
     if (!isSpace && !isNBSP) {
       break;
     }
     newContent = content;
     newOffset = offset;
   }
   return EditorDOMPoint(newContent, newOffset);
 }
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -924,45 +924,16 @@ void HTMLEditor::IsNextCharInNodeWhiteSp
     if (outNode && outOffset) {
       NS_IF_ADDREF(*outNode = aContent);
       // yes, this is _past_ the character
       *outOffset = aOffset + 1;
     }
   }
 }
 
-/**
- * IsPrevCharInNodeWhiteSpace() checks the adjacent content in the same node to
- * see if following selection is white-space.
- */
-void HTMLEditor::IsPrevCharInNodeWhiteSpace(nsIContent* aContent,
-                                            int32_t aOffset, bool* outIsSpace,
-                                            bool* outIsNBSP,
-                                            nsIContent** outNode,
-                                            int32_t* outOffset) {
-  MOZ_ASSERT(aContent && outIsSpace && outIsNBSP);
-  MOZ_ASSERT((outNode && outOffset) || (!outNode && !outOffset));
-  *outIsSpace = false;
-  *outIsNBSP = false;
-  if (outNode && outOffset) {
-    *outNode = nullptr;
-    *outOffset = -1;
-  }
-
-  if (aContent->IsText() && aOffset > 0) {
-    char16_t ch = aContent->GetText()->CharAt(aOffset - 1);
-    *outIsSpace = nsCRT::IsAsciiSpace(ch);
-    *outIsNBSP = (ch == kNBSP);
-    if (outNode && outOffset) {
-      NS_IF_ADDREF(*outNode = aContent);
-      *outOffset = aOffset - 1;
-    }
-  }
-}
-
 NS_IMETHODIMP HTMLEditor::UpdateBaseURL() {
   RefPtr<Document> document = GetDocument();
   if (NS_WARN_IF(!document)) {
     return NS_ERROR_FAILURE;
   }
 
   // Look for an HTML <base> tag
   RefPtr<nsContentList> baseElementList =
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -798,20 +798,16 @@ class HTMLEditor final : public TextEdit
    * error even if selection is not in cell element, just does nothing.
    */
   MOZ_CAN_RUN_SCRIPT nsresult DeleteTableCellContentsWithTransaction();
 
   static void IsNextCharInNodeWhiteSpace(nsIContent* aContent, int32_t aOffset,
                                          bool* outIsSpace, bool* outIsNBSP,
                                          nsIContent** outNode = nullptr,
                                          int32_t* outOffset = 0);
-  static void IsPrevCharInNodeWhiteSpace(nsIContent* aContent, int32_t aOffset,
-                                         bool* outIsSpace, bool* outIsNBSP,
-                                         nsIContent** outNode = nullptr,
-                                         int32_t* outOffset = 0);
 
   /**
    * extracts an element from the normal flow of the document and
    * positions it, and puts it back in the normal flow.
    * @param aElement [IN] the element
    * @param aEnabled [IN] true to absolutely position the element,
    *                      false to put it back in the normal flow
    */