Bug 1538771 - Move trimURL to BrowserUtils.jsm so it can be more easily accessed in non-window contexts. r=adw
authorMark Banner <standard8@mozilla.com>
Tue, 26 Mar 2019 09:15:50 +0000
changeset 466068 6bd1f37e726c23b0f8c694dce5e46a0a86af0ead
parent 466067 8df98cd4690728964b11966931a7a690fb17dd56
child 466069 63ecd27aa7a463af72fd6d9d670d56f981802efe
push id81418
push usermbanner@mozilla.com
push dateTue, 26 Mar 2019 09:18:14 +0000
treeherderautoland@63ecd27aa7a4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersadw
bugs1538771
milestone68.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 1538771 - Move trimURL to BrowserUtils.jsm so it can be more easily accessed in non-window contexts. r=adw Differential Revision: https://phabricator.services.mozilla.com/D24761
browser/base/content/browser.js
browser/base/content/urlbarBindings.xml
browser/base/content/utilityOverlay.js
browser/components/urlbar/UrlbarInput.jsm
browser/components/urlbar/UrlbarView.jsm
toolkit/modules/BrowserUtils.jsm
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -4818,17 +4818,17 @@ var XULBrowserWindow = {
       url = Services.textToSubURI.unEscapeURIForUI("UTF-8", url);
 
       // Encode bidirectional formatting characters.
       // (RFC 3987 sections 3.2 and 4.1 paragraph 6)
       url = url.replace(/[\u200e\u200f\u202a\u202b\u202c\u202d\u202e]/g,
                         encodeURIComponent);
 
       if (UrlbarPrefs.get("trimURLs")) {
-        url = trimURL(url);
+        url = BrowserUtils.trimURL(url);
       }
     }
 
     this.overLink = url;
     LinkTargetDisplay.update();
   },
 
   showTooltip(x, y, tooltip, direction, browser) {
--- a/browser/base/content/urlbarBindings.xml
+++ b/browser/base/content/urlbarBindings.xml
@@ -565,17 +565,17 @@ file, You can obtain one at http://mozil
       </method>
 
       <field name="_mayTrimURLs">true</field>
       <method name="trimValue">
         <parameter name="aURL"/>
         <body><![CDATA[
           // This method must not modify the given URL such that calling
           // nsIURIFixup::createFixupURI with the result will produce a different URI.
-          return this._mayTrimURLs ? trimURL(aURL) : aURL;
+          return this._mayTrimURLs ? BrowserUtils.trimURL(aURL) : aURL;
         ]]></body>
       </method>
 
       <!--
         This method tries to apply styling to the text in the input, depending
         on the text.  See the _format* methods.
       -->
       <method name="formatValue">
--- a/browser/base/content/utilityOverlay.js
+++ b/browser/base/content/utilityOverlay.js
@@ -1018,44 +1018,16 @@ function openHelpLink(aHelpTopic, aCalle
   openTrustedLinkIn(url, where);
 }
 
 function openPrefsHelp(aEvent) {
   let helpTopic = aEvent.target.getAttribute("helpTopic");
   openHelpLink(helpTopic);
 }
 
-function trimURL(aURL) {
-  // This function must not modify the given URL such that calling
-  // nsIURIFixup::createFixupURI with the result will produce a different URI.
-
-  // remove single trailing slash for http/https/ftp URLs
-  let url = aURL.replace(/^((?:http|https|ftp):\/\/[^/]+)\/$/, "$1");
-
-  // remove http://
-  if (!url.startsWith("http://")) {
-    return url;
-  }
-  let urlWithoutProtocol = url.substring(7);
-
-  let flags = Services.uriFixup.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP |
-              Services.uriFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS;
-  let fixedUpURL, expectedURLSpec;
-  try {
-    fixedUpURL = Services.uriFixup.createFixupURI(urlWithoutProtocol, flags);
-    expectedURLSpec = makeURI(aURL).displaySpec;
-  } catch (ex) {
-    return url;
-  }
-  if (fixedUpURL.displaySpec == expectedURLSpec) {
-    return urlWithoutProtocol;
-  }
-  return url;
-}
-
 /**
  * Updates visibility of "Import From Another Browser" command depending on
  * the DisableProfileImport policy.
  */
 function updateFileMenuImportUIVisibility(id) {
   if (!Services.policies.isAllowed("profileImport")) {
     let command = document.getElementById(id);
     command.setAttribute("disabled", "true");
--- a/browser/components/urlbar/UrlbarInput.jsm
+++ b/browser/components/urlbar/UrlbarInput.jsm
@@ -191,17 +191,17 @@ class UrlbarInput {
    * the same URI.
    *
    * @param {string} val
    *   The string to be trimmed if it appears to be URI
    * @returns {string}
    *   The trimmed string
    */
   trimValue(val) {
-    return UrlbarPrefs.get("trimURLs") ? this.window.trimURL(val) : val;
+    return UrlbarPrefs.get("trimURLs") ? BrowserUtils.trimURL(val) : val;
   }
 
   /**
    * Applies styling to the text in the urlbar input, depending on the text.
    */
   formatValue() {
     this.valueFormatter.update();
   }
--- a/browser/components/urlbar/UrlbarView.jsm
+++ b/browser/components/urlbar/UrlbarView.jsm
@@ -3,16 +3,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict";
 
 var EXPORTED_SYMBOLS = ["UrlbarView"];
 
 const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 XPCOMUtils.defineLazyModuleGetters(this, {
+  BrowserUtils: "resource://gre/modules/BrowserUtils.jsm",
   Services: "resource://gre/modules/Services.jsm",
   UrlbarPrefs: "resource:///modules/UrlbarPrefs.jsm",
   UrlbarTokenizer: "resource:///modules/UrlbarTokenizer.jsm",
   UrlbarUtils: "resource:///modules/UrlbarUtils.jsm",
 });
 
 XPCOMUtils.defineLazyGetter(this, "bundle", function() {
   return Services.strings.createBundle("chrome://global/locale/autocomplete.properties");
@@ -449,17 +450,17 @@ class UrlbarView {
     let setAction = text => {
       action = this._createElement("span");
       action.className = "urlbarView-secondary urlbarView-action";
       action.textContent = text;
     };
     let setURL = () => {
       url = this._createElement("span");
       url.className = "urlbarView-secondary urlbarView-url";
-      let val = this.window.trimURL(result.payload.displayUrl || "");
+      let val = BrowserUtils.trimURL(result.payload.displayUrl || "");
       this._addTextContentWithHighlights(url, val,
                                          result.payloadHighlights.displayUrl || []);
     };
     switch (result.type) {
       case UrlbarUtils.RESULT_TYPE.TAB_SWITCH:
         setAction(bundle.GetStringFromName("switchToTab2"));
         setURL();
         break;
--- a/toolkit/modules/BrowserUtils.jsm
+++ b/toolkit/modules/BrowserUtils.jsm
@@ -706,9 +706,44 @@ var BrowserUtils = {
         if (test(subject, data)) {
           Services.obs.removeObserver(observer, topic);
           resolve({subject, data});
         }
       };
       Services.obs.addObserver(observer, topic);
     });
   },
+
+  /**
+   * Returns a URL which has been trimmed by removing 'http://' and any
+   * trailing slash (in http/https/ftp urls).
+   *
+   * @param {string} aURL The URL to trim.
+   * @returns {string} The trimmed string.
+   */
+  trimURL(aURL) {
+    // This function must not modify the given URL such that calling
+    // nsIURIFixup::createFixupURI with the result will produce a different URI.
+
+    // remove single trailing slash for http/https/ftp URLs
+    let url = aURL.replace(/^((?:http|https|ftp):\/\/[^/]+)\/$/, "$1");
+
+    // remove http://
+    if (!url.startsWith("http://")) {
+      return url;
+    }
+    let urlWithoutProtocol = url.substring(7);
+
+    let flags = Services.uriFixup.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP |
+                Services.uriFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS;
+    let fixedUpURL, expectedURLSpec;
+    try {
+      fixedUpURL = Services.uriFixup.createFixupURI(urlWithoutProtocol, flags);
+      expectedURLSpec = Services.io.newURI(aURL).displaySpec;
+    } catch (ex) {
+      return url;
+    }
+    if (fixedUpURL.displaySpec == expectedURLSpec) {
+      return urlWithoutProtocol;
+    }
+    return url;
+  },
 };