Bug 1341588 Part 2 - Add StringifiedSelection() to simplify code.
MozReview-Commit-ID: 1tRG8WY13ok
--- 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