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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Mark Banner 2019-03-26 09:15:50 +00:00
parent 1a70ced2d9
commit ce267bd0ae
6 changed files with 40 additions and 32 deletions

View File

@ -4823,7 +4823,7 @@ var XULBrowserWindow = {
encodeURIComponent);
if (UrlbarPrefs.get("trimURLs")) {
url = trimURL(url);
url = BrowserUtils.trimURL(url);
}
}

View File

@ -570,7 +570,7 @@ file, You can obtain one at http://mozilla.org/MPL/2.0/.
<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>

View File

@ -1023,34 +1023,6 @@ function openPrefsHelp(aEvent) {
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.

View File

@ -196,7 +196,7 @@ class UrlbarInput {
* The trimmed string
*/
trimValue(val) {
return UrlbarPrefs.get("trimURLs") ? this.window.trimURL(val) : val;
return UrlbarPrefs.get("trimURLs") ? BrowserUtils.trimURL(val) : val;
}
/**

View File

@ -8,6 +8,7 @@ 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",
@ -454,7 +455,7 @@ class UrlbarView {
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 || []);
};

View File

@ -711,4 +711,39 @@ var BrowserUtils = {
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;
},
};