mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-20 16:55:40 +00:00
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:
parent
1a70ced2d9
commit
ce267bd0ae
@ -4823,7 +4823,7 @@ var XULBrowserWindow = {
|
||||
encodeURIComponent);
|
||||
|
||||
if (UrlbarPrefs.get("trimURLs")) {
|
||||
url = trimURL(url);
|
||||
url = BrowserUtils.trimURL(url);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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>
|
||||
|
||||
|
@ -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.
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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 || []);
|
||||
};
|
||||
|
@ -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;
|
||||
},
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user