Bug 665580 - Hide http:// and single trailing slashes in the location bar. ui-r=faaborg r=sdwilsh

This commit is contained in:
Dão Gottwald 2011-06-23 10:13:41 +02:00
parent 6905c5d4c5
commit 5bb3d596f5
10 changed files with 155 additions and 36 deletions

View File

@ -297,6 +297,7 @@ pref("browser.urlbar.match.url", "@");
pref("browser.urlbar.default.behavior", 0);
pref("browser.urlbar.formatting.enabled", true);
pref("browser.urlbar.trimURLs", true);
// Number of milliseconds to wait for the http headers (and thus
// the Content-Disposition filename) before giving up and falling back to

View File

@ -226,6 +226,7 @@ _BROWSER_FILES = \
browser_tabfocus.js \
browser_tabs_isActive.js \
browser_tabs_owner.js \
browser_urlbarTrimURLs.js \
browser_urlHighlight.js \
browser_visibleFindSelection.js \
browser_visibleTabs.js \

View File

@ -46,8 +46,6 @@ function test() {
partialURLTab = gBrowser.addTab();
testURL = "http://example.org/browser/browser/base/content/test/dummy_page.html";
testPartialURL = testURL.substr(0, (testURL.length - charsToDelete));
function cleanUp() {
gBrowser.removeTab(fullURLTab);
gBrowser.removeTab(partialURLTab);
@ -135,6 +133,9 @@ function test() {
}
function runTests() {
testURL = gURLBar.trimValue(testURL);
testPartialURL = testURL.substr(0, (testURL.length - charsToDelete));
// prepare the three tabs required by this test
prepareFullURLTab(function () {
preparePartialURLTab(function () {

View File

@ -37,6 +37,7 @@
let testURL = "http://example.org/browser/browser/base/content/test/dummy_page.html";
let testActionURL = "moz-action:switchtab," + testURL;
testURL = gURLBar.trimValue(testURL);
let testTab;
function runNextTest() {

View File

@ -27,34 +27,41 @@ function test() {
gURLBar.focus();
testVal("http://mozilla.org/");
testVal("https://mozilla.org");
gBrowser.selectedBrowser.focus();
testVal("<http://>mozilla.org");
testVal("<http://>mozilla.org</>");
testVal("<http://>mözilla.org</>");
testVal("<http://>mozilla.imaginatory</>");
testVal("<http://www.>mozilla.org</>");
testVal("<http://sub.>mozilla.org</>");
testVal("<http://sub1.sub2.sub3.>mozilla.org</>");
testVal("<http://ftp.>mozilla.org</>");
testVal("<ftp://ftp.>mozilla.org</>");
testVal("<https://>mozilla.org");
testVal("<https://>mözilla.org");
testVal("<https://>mozilla.imaginatory");
testVal("<https://sub.>mozilla.org</>");
testVal("<https://sub1.sub2.sub3.>mozilla.org</>");
testVal("<https://user:pass@sub1.sub2.sub3.>mozilla.org</>");
testVal("<https://user:pass@>mozilla.org</>");
testVal("<https://www.>mozilla.org");
testVal("<https://sub.>mozilla.org");
testVal("<https://sub1.sub2.sub3.>mozilla.org");
testVal("<www.>mozilla.org");
testVal("<sub.>mozilla.org");
testVal("<sub1.sub2.sub3.>mozilla.org");
testVal("<http://>mozilla.org</file.ext>");
testVal("<http://>mozilla.org</sub/file.ext>");
testVal("<http://>mozilla.org</sub/file.ext?foo>");
testVal("<http://>mozilla.org</sub/file.ext?foo&bar>");
testVal("<http://>mozilla.org</sub/file.ext?foo&bar#top>");
testVal("<http://ftp.>mozilla.org");
testVal("<ftp://ftp.>mozilla.org");
testVal("<http://sub.>mozilla.org<:666/file.ext>");
testVal("<https://sub.>mozilla.org");
testVal("<https://sub1.sub2.sub3.>mozilla.org");
testVal("<https://user:pass@sub1.sub2.sub3.>mozilla.org");
testVal("<https://user:pass@>mozilla.org");
testVal("<http://>[fe80::222:19ff:fe11:8c76]</file.ext>");
testVal("<https://>mozilla.org</file.ext>");
testVal("<https://>mozilla.org</sub/file.ext>");
testVal("<https://>mozilla.org</sub/file.ext?foo>");
testVal("<https://>mozilla.org</sub/file.ext?foo&bar>");
testVal("<https://>mozilla.org</sub/file.ext?foo&bar#top>");
testVal("<https://>mozilla.org</sub/file.ext?foo&bar#top>");
testVal("<https://sub.>mozilla.org<:666/file.ext>");
testVal("<https://>[fe80::222:19ff:fe11:8c76]</file.ext>");
testVal("[fe80::222:19ff:fe11:8c76]</file.ext>");
testVal("<https://user:pass@>[fe80::222:19ff:fe11:8c76]<:666/file.ext>");
testVal("<http://user:pass@>[fe80::222:19ff:fe11:8c76]<:666/file.ext>");
testVal("mailto:admin@mozilla.org");
@ -63,5 +70,5 @@ function test() {
Services.prefs.setBoolPref(prefname, false);
testVal("http://mozilla.org/");
testVal("https://mozilla.org");
}

View File

@ -0,0 +1,75 @@
function testVal(originalValue, tragetValue) {
gURLBar.value = originalValue;
is(gURLBar.value, tragetValue || originalValue, "original value: " + originalValue);
}
function test() {
const prefname = "browser.urlbar.trimURLs";
gBrowser.selectedTab = gBrowser.addTab();
registerCleanupFunction(function () {
gBrowser.removeCurrentTab();
Services.prefs.clearUserPref(prefname);
URLBarSetURI();
});
Services.prefs.setBoolPref(prefname, true);
testVal("http://mozilla.org/", "mozilla.org");
testVal("https://mozilla.org/", "https://mozilla.org");
testVal("http://mözilla.org/", "mözilla.org");
testVal("http://mozilla.imaginatory/", "mozilla.imaginatory");
testVal("http://www.mozilla.org/", "www.mozilla.org");
testVal("http://sub.mozilla.org/", "sub.mozilla.org");
testVal("http://sub1.sub2.sub3.mozilla.org/", "sub1.sub2.sub3.mozilla.org");
testVal("http://mozilla.org/file.ext", "mozilla.org/file.ext");
testVal("http://mozilla.org/sub/", "mozilla.org/sub/");
testVal("http://ftp.mozilla.org/", "http://ftp.mozilla.org");
testVal("ftp://ftp.mozilla.org/", "ftp://ftp.mozilla.org");
testVal("https://user:pass@mozilla.org/", "https://user:pass@mozilla.org");
testVal("http://user:pass@mozilla.org/", "http://user:pass@mozilla.org");
testVal("http://sub.mozilla.org:666/", "sub.mozilla.org:666");
testVal("https://[fe80::222:19ff:fe11:8c76]/file.ext");
testVal("http://[fe80::222:19ff:fe11:8c76]/", "[fe80::222:19ff:fe11:8c76]");
testVal("https://user:pass@[fe80::222:19ff:fe11:8c76]:666/file.ext");
testVal("http://user:pass@[fe80::222:19ff:fe11:8c76]:666/file.ext");
testVal("mailto:admin@mozilla.org");
testVal("gopher://mozilla.org/");
testVal("about:config");
Services.prefs.setBoolPref(prefname, false);
testVal("http://mozilla.org/");
Services.prefs.setBoolPref(prefname, true);
waitForExplicitFinish();
gBrowser.selectedBrowser.addEventListener("load", function () {
gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true);
is(gBrowser.currentURI.spec, "http://example.com/", "expected page should have loaded");
testCopy("example.com", "http://example.com/", function () {
SetPageProxyState("invalid");
testCopy("example.com", "example.com", finish);
});
}, true);
gBrowser.loadURI("http://example.com/");
}
function testCopy(originalValue, targetValue, cb) {
waitForClipboard(targetValue, function () {
is(gURLBar.value, originalValue);
gURLBar.focus();
gURLBar.select();
goDoCommand("cmd_copy");
}, cb, cb);
}

View File

@ -91,6 +91,7 @@
this.completeDefaultIndex = this._prefs.getBoolPref("autoFill");
this.timeout = this._prefs.getIntPref("delay");
this._formattingEnabled = this._prefs.getBoolPref("formatting.enabled");
this._mayTrimURLs = this._prefs.getBoolPref("trimURLs");
this._urlTooltip = document.getElementById("urlTooltip");
@ -180,6 +181,14 @@
]]></body>
</method>
<field name="_mayTrimURLs">true</field>
<method name="trimValue">
<parameter name="aURL"/>
<body><![CDATA[
return this._mayTrimURLs ? trimURL(aURL) : aURL;
]]></body>
</method>
<field name="_formattingEnabled">true</field>
<method name="formatValue">
<body><![CDATA[
@ -192,7 +201,12 @@
let textNode = this.editor.rootElement.firstChild;
let value = textNode.textContent;
let matchedURL = value.match(/^((?:http|https|ftp):\/\/(?:[^\/]+@)?)(.+?)(?::\d+)?(?:\/|$)/);
let protocol = value.match(/^[a-z]+:/);
if (protocol &&
["http:", "https:", "ftp:"].indexOf(protocol[0]) == -1)
return;
let matchedURL = value.match(/^((?:[a-z]+:\/\/)?(?:[^\/]+@)?)(.+?)(?::\d+)?(?:\/|$)/);
if (!matchedURL)
return;
@ -205,14 +219,17 @@
let subSegments = segments(domain) - segments(baseDomain);
let subDomain = domain.match(new RegExp("(?:[^.]*\.){" + subSegments + "}"))[0];
let range = document.createRange();
range.setStart(textNode, 0);
range.setEnd(textNode, preDomain.length + subDomain.length);
selection.addRange(range);
let rangeLength = preDomain.length + subDomain.length;
if (rangeLength) {
let range = document.createRange();
range.setStart(textNode, 0);
range.setEnd(textNode, rangeLength);
selection.addRange(range);
}
let startRest = preDomain.length + domain.length;
if (startRest < value.length) {
range = document.createRange();
let range = document.createRange();
range.setStart(textNode, startRest);
range.setEnd(textNode, value.length);
selection.addRange(range);
@ -467,7 +484,7 @@
SetPageProxyState("invalid");
this.focus();
try {
urlSecurityCheck(this.value,
urlSecurityCheck(url,
gBrowser.contentPrincipal,
Ci.nsIScriptSecurityManager.DISALLOW_INHERIT_PRINCIPAL);
} catch (ex) {
@ -488,10 +505,7 @@
// non-data URI, encode it.
if (val == inputVal &&
this.getAttribute("pageproxystate") == "valid") {
let uri;
try {
uri = makeURI(val);
} catch (e) {}
let uri = gBrowser.currentURI;
if (uri && !uri.schemeIs("javascript") && !uri.schemeIs("data")) {
val = uri.spec;
@ -565,6 +579,9 @@
case "formatting.enabled":
this._formattingEnabled = this._prefs.getBoolPref(aData);
break;
case "trimURLs":
this._mayTrimURLs = this._prefs.getBoolPref(aData);
break;
}
}
]]></body>

View File

@ -23,6 +23,7 @@
# Alec Flett <alecf@netscape.com>
# Ehsan Akhgari <ehsan.akhgari@gmail.com>
# Gavin Sharp <gavin@gavinsharp.com>
# Dão Gottwald <dao@design-noir.de>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
@ -617,3 +618,10 @@ function openPrefsHelp() {
var helpTopic = document.getElementsByTagName("prefwindow")[0].currentPane.helpTopic;
openHelpLink(helpTopic, !instantApply);
}
function trimURL(aURL) {
return aURL /* remove single trailing slash for http/https/ftp URLs */
.replace(/^((?:http|https|ftp):\/\/[^/]+)\/$/, "$1")
/* remove http:// unless the host starts with "ftp." or contains "@" */
.replace(/^http:\/\/((?!ftp\.)[^\/@]+(?:\/|$))/, "$1");
}

View File

@ -282,7 +282,7 @@ function test() {
"userTypedValue was null after loading a URI");
is(browser.userTypedClear, 0,
"userTypeClear reset to 0");
is(gURLBar.value, "http://example.com/",
is(gURLBar.value, gURLBar.trimValue("http://example.com/"),
"Address bar's value set after loading URI");
runNextTest();
});

View File

@ -280,9 +280,14 @@
if (typeof this.onBeforeValueSet == "function")
val = this.onBeforeValueSet(val);
if (typeof this.trimValue == "function")
val = this.trimValue(val);
this.inputField.value = val;
if (typeof this.formatValue == "function")
this.formatValue();
this.mIgnoreInput = false;
var event = document.createEvent('Events');
event.initEvent('ValueChange', true, true);
@ -1091,6 +1096,9 @@
getService(Components.interfaces.nsITextToSubURI).
unEscapeURIForUI("UTF-8", controller.getValueAt(this._currentIndex));
if (typeof this.input.trimValue == "function")
url = this.input.trimValue(url);
if (this._currentIndex < existingItemsCount) {
// re-use the existing item
item = this.richlistbox.childNodes[this._currentIndex];