Bug 1341588 Part 2 - Add StringifiedSelection() to simplify code. draft
authorTing-Yu Lin <tlin@mozilla.com>
Tue, 07 Mar 2017 17:09:39 +0800
changeset 494567 6b3004979f0f678bc1e611e2c0555e9b4c30ebba
parent 494564 ff0240af4257ee525e827e8c40b764a4cb315465
child 494568 9349122f6d26479f9719215b557a665655abab6b
push id48061
push userbmo:tlin@mozilla.com
push dateTue, 07 Mar 2017 09:39:52 +0000
bugs1341588
milestone55.0a1
Bug 1341588 Part 2 - Add StringifiedSelection() to simplify code. MozReview-Commit-ID: 1tRG8WY13ok
layout/base/AccessibleCaretManager.cpp
layout/base/AccessibleCaretManager.h
--- a/layout/base/AccessibleCaretManager.cpp
+++ b/layout/base/AccessibleCaretManager.cpp
@@ -806,16 +806,24 @@ AccessibleCaretManager::GetFrameSelectio
 
     return fs.forget();
   } else {
     // For non-editable content
     return mPresShell->FrameSelection();
   }
 }
 
+nsAutoString
+AccessibleCaretManager::StringifiedSelection() const
+{
+  nsAutoString str;
+  GetSelection()->Stringify(str);
+  return str;
+}
+
 Element*
 AccessibleCaretManager::GetEditingHostForFrame(nsIFrame* aFrame) const
 {
   if (!aFrame) {
     return nullptr;
   }
 
   auto content = aFrame->GetContent();
@@ -952,18 +960,17 @@ AccessibleCaretManager::ExtendPhoneNumbe
     // Backup the anchor focus range since both anchor node and focus node might
     // be changed after calling Selection::Modify().
     RefPtr<nsRange> oldAnchorFocusRange = anchorFocusRange->CloneRange();
 
     // Save current focus node, focus offset and the selected text so that
     // we can compare them with the modified ones later.
     nsINode* oldFocusNode = selection->GetFocusNode();
     uint32_t oldFocusOffset = selection->FocusOffset();
-    nsAutoString oldSelectedText;
-    selection->Stringify(oldSelectedText);
+    nsAutoString oldSelectedText = StringifiedSelection();
 
     // Extend the selection by one char.
     selection->Modify(NS_LITERAL_STRING("extend"),
                       aDirection,
                       NS_LITERAL_STRING("character"));
     if (IsTerminated()) {
       return;
     }
@@ -974,18 +981,17 @@ AccessibleCaretManager::ExtendPhoneNumbe
       return;
     }
 
     // If the changed selection isn't a valid phone number, we're done.
     // Also, if the selection was extended to a new block node, the string
     // returned by stringify() won't have a new line at the beginning or the
     // end of the string. Therefore, if either focus node or offset is
     // changed, but selected text is not changed, we're done, too.
-    nsAutoString selectedText;
-    selection->Stringify(selectedText);
+    nsAutoString selectedText = StringifiedSelection();
     nsAutoString phoneRegex(NS_LITERAL_STRING("(^\\+)?[0-9 ,\\-.()*#pw]{1,30}$"));
 
     if (!nsContentUtils::IsPatternMatching(selectedText, phoneRegex, doc) ||
         oldSelectedText == selectedText) {
       // Backout the undesired selection extend, restore the old anchor focus
       // range before exit.
       selection->SetAnchorFocusToRange(oldAnchorFocusRange);
       return;
--- a/layout/base/AccessibleCaretManager.h
+++ b/layout/base/AccessibleCaretManager.h
@@ -182,16 +182,17 @@ protected:
 
   // Caller is responsible to use IsTerminated() to check whether PresShell is
   // still valid.
   void FlushLayout() const;
 
   dom::Element* GetEditingHostForFrame(nsIFrame* aFrame) const;
   dom::Selection* GetSelection() const;
   already_AddRefed<nsFrameSelection> GetFrameSelection() const;
+  nsAutoString StringifiedSelection() const;
 
   // Get the union of all the child frame scrollable overflow rects for aFrame,
   // which is used as a helper function to restrict the area where the caret can
   // be dragged. Returns the rect relative to aFrame.
   nsRect GetAllChildFrameRectsUnion(nsIFrame* aFrame) const;
 
   // Restrict the active caret's dragging position based on
   // sCaretsAllowDraggingAcrossOtherCaret. If the active caret is the first