Bug 1341588 Part 3 - Extend phone number selection only if it is already a phone number.
MozReview-Commit-ID: D25xl0oAD4o
--- a/layout/base/AccessibleCaretManager.cpp
+++ b/layout/base/AccessibleCaretManager.cpp
@@ -922,37 +922,48 @@ AccessibleCaretManager::SetSelectionDrag
#ifdef MOZ_WIDGET_ANDROID
nsIDocument* doc = mPresShell->GetDocument();
MOZ_ASSERT(doc);
nsIWidget* widget = nsContentUtils::WidgetForDocument(doc);
static_cast<nsWindow*>(widget)->SetSelectionDragState(aState);
#endif
}
+bool
+AccessibleCaretManager::IsPhoneNumber(nsAString& aCandidate) const
+{
+ RefPtr<nsIDocument> doc = mPresShell->GetDocument();
+ nsAutoString phoneNumberRegex(
+ NS_LITERAL_STRING("(^\\+)?[0-9 ,\\-.()*#pw]{1,30}$"));
+ return nsContentUtils::IsPatternMatching(aCandidate, phoneNumberRegex, doc);
+}
+
void
AccessibleCaretManager::SelectMoreIfPhoneNumber() const
{
- SetSelectionDirection(eDirNext);
- ExtendPhoneNumberSelection(NS_LITERAL_STRING("forward"));
+ nsAutoString selectedText = StringifiedSelection();
+
+ if (IsPhoneNumber(selectedText)) {
+ SetSelectionDirection(eDirNext);
+ ExtendPhoneNumberSelection(NS_LITERAL_STRING("forward"));
- SetSelectionDirection(eDirPrevious);
- ExtendPhoneNumberSelection(NS_LITERAL_STRING("backward"));
+ SetSelectionDirection(eDirPrevious);
+ ExtendPhoneNumberSelection(NS_LITERAL_STRING("backward"));
- SetSelectionDirection(eDirNext);
+ SetSelectionDirection(eDirNext);
+ }
}
void
AccessibleCaretManager::ExtendPhoneNumberSelection(const nsAString& aDirection) const
{
if (!mPresShell) {
return;
}
- RefPtr<nsIDocument> doc = mPresShell->GetDocument();
-
// Extend the phone number selection until we find a boundary.
RefPtr<Selection> selection = GetSelection();
while (selection) {
const nsRange* anchorFocusRange = selection->GetAnchorFocusRange();
if (!anchorFocusRange) {
return;
}
@@ -982,20 +993,18 @@ AccessibleCaretManager::ExtendPhoneNumbe
}
// 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 = StringifiedSelection();
- nsAutoString phoneRegex(NS_LITERAL_STRING("(^\\+)?[0-9 ,\\-.()*#pw]{1,30}$"));
- if (!nsContentUtils::IsPatternMatching(selectedText, phoneRegex, doc) ||
- oldSelectedText == selectedText) {
+ if (!IsPhoneNumber(selectedText) || 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
@@ -156,18 +156,23 @@ protected:
// Change focus to aFrame if it isn't nullptr. Otherwise, clear the old focus
// then re-focus the window.
void ChangeFocusToOrClearOldFocus(nsIFrame* aFrame) const;
nsresult SelectWord(nsIFrame* aFrame, const nsPoint& aPoint) const;
void SetSelectionDragState(bool aState) const;
- // Called to extend a selection if possible that it's a phone number.
+ // Return true if the candidate string is a phone number.
+ bool IsPhoneNumber(nsAString& aCandidate) const;
+
+ // Extend the current selection forwards and backwards if it's already a
+ // phone number.
void SelectMoreIfPhoneNumber() const;
+
// Extend the current phone number selection in the requested direction.
void ExtendPhoneNumberSelection(const nsAString& aDirection) const;
void SetSelectionDirection(nsDirection aDir) const;
// If aDirection is eDirNext, get the frame for the range start in the first
// range from the current selection, and return the offset into that frame as
// well as the range start node and the node offset. Otherwise, get the frame