Bug 1265750 - Some word cannot be selected via long pressing, r=TYLin, mats
MozReview-Commit-ID: 46VNaTwZG3n
--- a/layout/base/AccessibleCaretManager.cpp
+++ b/layout/base/AccessibleCaretManager.cpp
@@ -853,27 +853,34 @@ AccessibleCaretManager::SetSelectionDrag
void
AccessibleCaretManager::SelectMoreIfPhoneNumber() const
{
SetSelectionDirection(eDirNext);
ExtendPhoneNumberSelection(NS_LITERAL_STRING("forward"));
SetSelectionDirection(eDirPrevious);
ExtendPhoneNumberSelection(NS_LITERAL_STRING("backward"));
+
+ SetSelectionDirection(eDirNext);
}
void
AccessibleCaretManager::ExtendPhoneNumberSelection(const nsAString& aDirection) const
{
nsIDocument* doc = mPresShell->GetDocument();
// Extend the phone number selection until we find a boundary.
Selection* selection = GetSelection();
while (selection) {
+ // Backup the anchor focus range since both anchor node and focus node might
+ // be changed after calling Selection::Modify().
+ RefPtr<nsRange> oldAnchorFocusRange =
+ selection->GetAnchorFocusRange()->CloneRange();
+
// Save current Focus position, and extend the selection one char.
nsINode* focusNode = selection->GetFocusNode();
uint32_t focusOffset = selection->FocusOffset();
selection->Modify(NS_LITERAL_STRING("extend"),
aDirection,
NS_LITERAL_STRING("character"));
// If the selection didn't change, (can't extend further), we're done.
@@ -883,20 +890,19 @@ AccessibleCaretManager::ExtendPhoneNumbe
}
// If the changed selection isn't a valid phone number, we're done.
nsAutoString selectedText;
selection->Stringify(selectedText);
nsAutoString phoneRegex(NS_LITERAL_STRING("(^\\+)?[0-9\\s,\\-.()*#pw]{1,30}$"));
if (!nsContentUtils::IsPatternMatching(selectedText, phoneRegex, doc)) {
- // Backout the undesired selection extend, (collapse to original
- // Anchor, extend to original Focus), before exit.
- selection->Collapse(selection->GetAnchorNode(), selection->AnchorOffset());
- selection->Extend(focusNode, focusOffset);
+ // Backout the undesired selection extend, restore the old anchor focus
+ // range before exit.
+ selection->SetAnchorFocusToRange(oldAnchorFocusRange);
return;
}
}
}
void
AccessibleCaretManager::SetSelectionDirection(nsDirection aDir) const
{
--- a/mobile/android/tests/browser/robocop/testAccessibleCarets.html
+++ b/mobile/android/tests/browser/robocop/testAccessibleCarets.html
@@ -33,11 +33,13 @@
rows="3" cols="8">הספר הוא טוב</textarea>
<br>
<input id="LTRphone" style="direction: ltr;" size="40"
value="09876543210 .-.)(wp#*1034103410341034X">
<br>
<input id="RTLphone" style="direction: rtl;" size="40"
value="התקשר +972 3 7347514 במשך זמן טוב">
+ <br>
+ <div><input value="DDs12">3 45<em id="bug1265750"> 678</em> 90</div>
</body>
</html>
--- a/mobile/android/tests/browser/robocop/testAccessibleCarets.js
+++ b/mobile/android/tests/browser/robocop/testAccessibleCarets.js
@@ -159,30 +159,32 @@ add_task(function* testAccessibleCarets(
let ce_RTL_elem = doc.getElementById("RTLcontenteditable");
let tc_RTL_elem = doc.getElementById("RTLtextContent");
let i_RTL_elem = doc.getElementById("RTLinput");
let ta_RTL_elem = doc.getElementById("RTLtextarea");
let ip_LTR_elem = doc.getElementById("LTRphone");
let ip_RTL_elem = doc.getElementById("RTLphone");
+ let bug1265750_elem = doc.getElementById("bug1265750");
// Locate longpress midpoints for test elements, ensure expactations.
let ce_LTR_midPoint = getCharPressPoint(doc, ce_LTR_elem, 0, "F");
let tc_LTR_midPoint = getCharPressPoint(doc, tc_LTR_elem, 0, "O");
let i_LTR_midPoint = getCharPressPoint(doc, i_LTR_elem, 0, "T");
let ta_LTR_midPoint = getCharPressPoint(doc, ta_LTR_elem, 0, "W");
let ce_RTL_midPoint = getCharPressPoint(doc, ce_RTL_elem, 0, "א");
let tc_RTL_midPoint = getCharPressPoint(doc, tc_RTL_elem, 0, "ת");
let i_RTL_midPoint = getCharPressPoint(doc, i_RTL_elem, 0, "ל");
let ta_RTL_midPoint = getCharPressPoint(doc, ta_RTL_elem, 0, "ה");
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");
// Longpress various LTR content elements. Test focused element against
// expected, and selected text against expected.
let result = getLongPressResult(browser, ce_LTR_midPoint);
is(result.focusedElement, ce_LTR_elem, "Focused element should match expected.");
is(result.text, "Find", "Selected text should match expected text.");
result = getLongPressResult(browser, tc_LTR_midPoint);
@@ -199,16 +201,21 @@ add_task(function* testAccessibleCarets(
result = getLongPressResult(browser, ip_LTR_midPoint);
is(result.focusedElement, ip_LTR_elem, "Focused element should match expected.");
is(result.text, "09876543210 .-.)(wp#*103410341",
"Selected phone number should match expected text.");
is(result.text.length, 30,
"Selected phone number length should match expected maximum.");
+ result = getLongPressResult(browser, bug1265750_midPoint);
+ is(result.focusedElement, null, "Focused element should match expected.");
+ is(result.text, "3 45 678 90",
+ "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);
is(result.focusedElement, ce_RTL_elem, "Focused element should match expected.");
is(result.text, "איפה", "Selected text should match expected text.");
result = getLongPressResult(browser, tc_RTL_midPoint);
is(result.focusedElement, null, "No focused element is expected.");