Bug 1338445 - Restrict whitespace match in phone number regex for AccessibleCaret. r=mtseng

In phoneRegex, replace '\\s ' (matching a whitespace character) with ' '
since phone number won't contain something like new line or tab.

Also, consider it done if selected text is not changed after calling
Modify().

MozReview-Commit-ID: 2lB9w2gYCOD

--HG--
extra : rebase_source : f2ea498bbd17c1876a9b7f769cbe93cef84520bb
This commit is contained in:
Ting-Yu Lin 2017-02-17 22:38:08 +08:00
Родитель cd38af3c5c
Коммит 1578a6f939
3 изменённых файлов: 36 добавлений и 7 удалений

Просмотреть файл

@ -953,9 +953,14 @@ AccessibleCaretManager::ExtendPhoneNumberSelection(const nsAString& aDirection)
// be changed after calling Selection::Modify().
RefPtr<nsRange> 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);

Просмотреть файл

@ -41,5 +41,10 @@
<br><br><br>
<div>x<input value="DDs12">3 45<em id="bug1265750"> 678</em> 90</div>
<br><br><br>
<tr><td>cell</td><td><p id="bug1338445-1">012345p</p></td><td>cell</td></tr>
<br><br><br>
<div><p>p12</p><p id="bug1338445-2">p34</p><p>p56</p></div>
</tr>
</body>
</html>

Просмотреть файл

@ -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);