diff --git a/layout/base/AccessibleCaretManager.cpp b/layout/base/AccessibleCaretManager.cpp index f7d8eff25140..729076db1c8e 100644 --- a/layout/base/AccessibleCaretManager.cpp +++ b/layout/base/AccessibleCaretManager.cpp @@ -953,9 +953,14 @@ AccessibleCaretManager::ExtendPhoneNumberSelection(const nsAString& aDirection) // be changed after calling Selection::Modify(). RefPtr oldAnchorFocusRange = anchorFocusRange->CloneRange(); - // Save current Focus position, and extend the selection one char. - nsINode* focusNode = selection->GetFocusNode(); - uint32_t focusOffset = selection->FocusOffset(); + // 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); + + // Extend the selection by one char. selection->Modify(NS_LITERAL_STRING("extend"), aDirection, NS_LITERAL_STRING("character")); @@ -964,17 +969,22 @@ AccessibleCaretManager::ExtendPhoneNumberSelection(const nsAString& aDirection) } // If the selection didn't change, (can't extend further), we're done. - if (selection->GetFocusNode() == focusNode && - selection->FocusOffset() == focusOffset) { + if (selection->GetFocusNode() == oldFocusNode && + selection->FocusOffset() == oldFocusOffset) { 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 phoneRegex(NS_LITERAL_STRING("(^\\+)?[0-9\\s,\\-.()*#pw]{1,30}$")); + nsAutoString phoneRegex(NS_LITERAL_STRING("(^\\+)?[0-9 ,\\-.()*#pw]{1,30}$")); - if (!nsContentUtils::IsPatternMatching(selectedText, phoneRegex, doc)) { + if (!nsContentUtils::IsPatternMatching(selectedText, phoneRegex, doc) || + oldSelectedText == selectedText) { // Backout the undesired selection extend, restore the old anchor focus // range before exit. selection->SetAnchorFocusToRange(oldAnchorFocusRange); diff --git a/mobile/android/tests/browser/robocop/testAccessibleCarets.html b/mobile/android/tests/browser/robocop/testAccessibleCarets.html index df378ed46a5a..b1ab210257d5 100644 --- a/mobile/android/tests/browser/robocop/testAccessibleCarets.html +++ b/mobile/android/tests/browser/robocop/testAccessibleCarets.html @@ -41,5 +41,10 @@


x3 45 678 90
+


+ cell

012345p

cell +


+

p12

p34

p56

+ diff --git a/mobile/android/tests/browser/robocop/testAccessibleCarets.js b/mobile/android/tests/browser/robocop/testAccessibleCarets.js index 9b0b0596d7b4..b8841d04c5e9 100644 --- a/mobile/android/tests/browser/robocop/testAccessibleCarets.js +++ b/mobile/android/tests/browser/robocop/testAccessibleCarets.js @@ -182,6 +182,8 @@ add_task(function* testAccessibleCarets() { let ip_LTR_elem = doc.getElementById("LTRphone"); let ip_RTL_elem = doc.getElementById("RTLphone"); let bug1265750_elem = doc.getElementById("bug1265750"); + let bug1338445_elem1 = doc.getElementById("bug1338445-1"); + let bug1338445_elem2 = doc.getElementById("bug1338445-2"); // Locate longpress midpoints for test elements, ensure expactations. let ce_LTR_midPoint = getCharPressPoint(doc, ce_LTR_elem, 0, "F"); @@ -197,6 +199,8 @@ add_task(function* testAccessibleCarets() { let ip_LTR_midPoint = getCharPressPoint(doc, ip_LTR_elem, 8, "2"); let ip_RTL_midPoint = getCharPressPoint(doc, ip_RTL_elem, 9, "2"); let bug1265750_midPoint = getCharPressPoint(doc, bug1265750_elem, 2, "7"); + let bug1338445_midPoint1 = getCharPressPoint(doc, bug1338445_elem1, 3, "3"); + let bug1338445_midPoint2 = getCharPressPoint(doc, bug1338445_elem2, 1, "3"); // Longpress various LTR content elements. Test focused element against // expected, and selected text against expected. @@ -228,6 +232,16 @@ add_task(function* testAccessibleCarets() { is(result.text, "3 45 678 90", "Selected phone number should match expected text."); + result = getLongPressResult(browser, bug1338445_midPoint1); + is(result.focusedElement, null, "Focused element should match expected."); + is(result.text, "012345p", + "Selected phone number should match expected text."); + + result = getLongPressResult(browser, bug1338445_midPoint2); + is(result.focusedElement, null, "Focused element should match expected."); + is(result.text, "p34", + "Selected phone number should match expected text."); + // Longpress various RTL content elements. Test focused element against // expected, and selected text against expected. result = getLongPressResult(browser, ce_RTL_midPoint);