Bug 1434210 - [1.1] Expose text selection trimming as a utility function. r=mconley, a=lizzard
authorEugen Sawin <esawin@mozilla.com>
Fri, 09 Feb 2018 21:22:31 +0100
changeset 454974 4442114ced5703d0d322f3e6db9de51f1a6a02ab
parent 454973 4c672d9530bf8a488ac28e4cf137a067de4e6bb6
child 454975 7a29ddefeab9bf4ad5d798a60b17957ea6cee7d3
push id1648
push usermtabara@mozilla.com
push dateThu, 01 Mar 2018 12:45:47 +0000
treeherdermozilla-release@cbb9688c2eeb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmconley, lizzard
bugs1434210
milestone59.0
Bug 1434210 - [1.1] Expose text selection trimming as a utility function. r=mconley, a=lizzard
toolkit/modules/BrowserUtils.jsm
--- a/toolkit/modules/BrowserUtils.jsm
+++ b/toolkit/modules/BrowserUtils.jsm
@@ -472,23 +472,59 @@ this.BrowserUtils = {
    * @return {nsIDOMWindow}
    */
   getRootWindow(docShell) {
     return docShell.QueryInterface(Ci.nsIDocShellTreeItem)
       .sameTypeRootTreeItem.QueryInterface(Ci.nsIInterfaceRequestor)
       .getInterface(Ci.nsIDOMWindow);
   },
 
-  getSelectionDetails(topWindow, aCharLen) {
-    // selections of more than 150 characters aren't useful
-    const kMaxSelectionLen = 150;
-    const charLen = Math.min(aCharLen || kMaxSelectionLen, kMaxSelectionLen);
+  /**
+   * Trim the selection text to a reasonable size and sanitize it to make it
+   * safe for search query input.
+   *
+   * @param aSelection
+   *        The selection text to trim.
+   * @param aMaxLen
+   *        The maximum string length, defaults to a reasonable size if undefined.
+   * @return The trimmed selection text.
+   */
+  trimSelection(aSelection, aMaxLen) {
+    // Selections of more than 150 characters aren't useful.
+    const maxLen = Math.min(aMaxLen || 150, aSelection.length);
+
+    if (aSelection.length > maxLen) {
+      // only use the first maxLen important chars. see bug 221361
+      let pattern = new RegExp("^(?:\\s*.){0," + maxLen + "}");
+      pattern.test(aSelection);
+      aSelection = RegExp.lastMatch;
+    }
 
+    aSelection = aSelection.trim().replace(/\s+/g, " ");
+
+    if (aSelection.length > maxLen) {
+      aSelection = aSelection.substr(0, maxLen);
+    }
+
+    return aSelection;
+  },
+
+  /**
+   * Retrieve the text selection details for the given window.
+   *
+   * @param  aTopWindow
+   *         The top window of the element containing the selection.
+   * @param  aCharLen
+   *         The maximum string length for the selection text.
+   * @return The selection details containing the full and trimmed selection text
+   *         and link details for link selections.
+   */
+  getSelectionDetails(aTopWindow, aCharLen) {
     let focusedWindow = {};
-    let focusedElement = Services.focus.getFocusedElementForWindow(topWindow, true, focusedWindow);
+    let focusedElement = Services.focus.getFocusedElementForWindow(aTopWindow, true, focusedWindow);
     focusedWindow = focusedWindow.value;
 
     let selection = focusedWindow.getSelection();
     let selectionStr = selection.toString();
     let fullText;
 
     let url;
     let linkText;
@@ -555,29 +591,17 @@ this.BrowserUtils = {
         }
       }
     }
 
     if (selectionStr) {
       // Pass up to 16K through unmolested.  If an add-on needs more, they will
       // have to use a content script.
       fullText = selectionStr.substr(0, 16384);
-
-      if (selectionStr.length > charLen) {
-        // only use the first charLen important chars. see bug 221361
-        var pattern = new RegExp("^(?:\\s*.){0," + charLen + "}");
-        pattern.test(selectionStr);
-        selectionStr = RegExp.lastMatch;
-      }
-
-      selectionStr = selectionStr.trim().replace(/\s+/g, " ");
-
-      if (selectionStr.length > charLen) {
-        selectionStr = selectionStr.substr(0, charLen);
-      }
+      selectionStr = this.trimSelection(selectionStr, aCharLen);
     }
 
     if (url && !url.host) {
       url = null;
     }
 
     return { text: selectionStr, docSelectionIsCollapsed: collapsed, fullText,
              linkURL: url ? url.spec : null, linkText: url ? linkText : "" };