bug 980197 - on long press of a phone number, select the whole phone number r=mfinkle
authorBrad Lassey <blassey@mozilla.com>
Wed, 12 Mar 2014 21:16:33 -0400
changeset 173992 a0e6d0b027dc
parent 173991 71734f89f0a1
child 173993 6dd68299e842
push id26439
push userphilringnalda@gmail.com
push date2014-03-18 05:42 +0000
treeherdermozilla-central@082761b7bc54 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs980197
milestone31.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
bug 980197 - on long press of a phone number, select the whole phone number r=mfinkle
mobile/android/chrome/content/SelectionHandler.js
--- a/mobile/android/chrome/content/SelectionHandler.js
+++ b/mobile/android/chrome/content/SelectionHandler.js
@@ -271,16 +271,52 @@ var SelectionHandler = {
 
     // Double check results of successful selection operation
     let selection = this._getSelection();
     if (!selection || selection.rangeCount == 0 || selection.getRangeAt(0).collapsed) {
       this._deactivate();
       return false;
     }
 
+    if (this._isPhoneNumber(selection.toString())) {
+      let anchorNode = selection.anchorNode;
+      let anchorOffset = selection.anchorOffset;
+      let focusNode = null;
+      let focusOffset = null;
+      while (this._isPhoneNumber(selection.toString().trim())) {
+        focusNode = selection.focusNode;
+        focusOffset = selection.focusOffset;
+        selection.modify("extend", "forward", "word");
+        // if we hit the end of the text on the page, we can't advance the selection
+        if (focusNode == selection.focusNode && focusOffset == selection.focusOffset) {
+          break;
+        }
+      }
+
+      // reverse selection
+      selection.collapse(focusNode, focusOffset);
+      selection.extend(anchorNode, anchorOffset);
+
+      anchorNode = focusNode;
+      anchorOffset = focusOffset
+
+      while (this._isPhoneNumber(selection.toString().trim())) {
+        focusNode = selection.focusNode;
+        focusOffset = selection.focusOffset;
+        selection.modify("extend", "backward", "word");
+        // if we hit the end of the text on the page, we can't advance the selection
+        if (focusNode == selection.focusNode && focusOffset == selection.focusOffset) {
+          break;
+        }
+      }
+
+      selection.collapse(focusNode, focusOffset);
+      selection.extend(anchorNode, anchorOffset);
+    }
+
     // Add a listener to end the selection if it's removed programatically
     selection.QueryInterface(Ci.nsISelectionPrivate).addSelectionListener(this);
     this._activeType = this.TYPE_SELECTION;
 
     // Initialize the cache
     this._cache = { start: {}, end: {}};
     this._updateCacheForSelection();
 
@@ -754,22 +790,24 @@ var SelectionHandler = {
       // Set current tab as parent of new tab, and set new tab as private if the parent is.
       BrowserApp.addTab(req.uri.spec, {parentId: parent.id,
                                        selected: true,
                                        isPrivate: isPrivate});
     }
     this._closeSelection();
   },
 
-  _phoneRegex: /(?:\s|^)[\+]?(\(?\d{1,8}\)?)?([- \.]+\(?\d{1,8}\)?)+( ?(x|ext) ?\d{1,3})?(?:\s|$)/,
+  _phoneRegex: /^\+?[0-9\s,-.\(\)*#pw]{1,30}$/,
 
-  _getSelectedPhoneNumber: function sh_isPhoneNumber() {
-    let selectedText = this._getSelectedText();
-    return (selectedText.length && this._phoneRegex.test(selectedText) ?
-            selectedText : null);
+  _getSelectedPhoneNumber: function sh_getSelectedPhoneNumber() {
+    return this._isPhoneNumber(this._getSelectedText().trim());
+  },
+
+  _isPhoneNumber: function sh_isPhoneNumber(selectedText) {
+    return (this._phoneRegex.test(selectedText) ? selectedText : null);
   },
 
   callSelection: function sh_callSelection() {
     let selectedText = this._getSelectedPhoneNumber();
     if (selectedText) {
       BrowserApp.loadURI("tel:" + selectedText);
     }
     this._closeSelection();