Bug 1386538 - Set initial tab title as new URL's hostname (without protocol or "www."). r?dao
We currently show the full URL without protocol or "www.", but this is noisy technical information and can be truncated because long URLs overflowsthe tab width. For URLs without a path, we currently show "mozilla.org/" with a slash, which is a little confusing.
With this patch, loading "https://www.mozilla.org/en-US/firefox/" would show just "mozilla.org" instead of "mozilla.org/en-US/firefox/".
MozReview-Commit-ID: DsGtzNUSScs
--- a/browser/base/content/tabbrowser.xml
+++ b/browser/base/content/tabbrowser.xml
@@ -1561,21 +1561,20 @@
if (!aLabel) {
return false;
}
aTab._fullLabel = aLabel;
aOptions = aOptions || {};
if (!aOptions.isContentTitle) {
- // Remove protocol and "www."
- if (!("_regex_shortenURLForTabLabel" in this)) {
- this._regex_shortenURLForTabLabel = /^[^:]+:\/\/(?:www\.)?/;
- }
- aLabel = aLabel.replace(this._regex_shortenURLForTabLabel, "");
+ const hostname = getHostnameWithoutWWW(aLabel);
+ if (hostname) {
+ aLabel = hostname;
+ }
}
aTab._labelIsContentTitle = aOptions.isContentTitle;
if (aTab.getAttribute("label") == aLabel) {
return false;
}
@@ -1592,16 +1591,25 @@
this._tabAttrModified(aTab, ["label"]);
}
if (aTab.selected) {
this.updateTitlebar();
}
return true;
+
+ function getHostnameWithoutWWW(label) {
+ // Extract hostname without protocol or "www.".
+ if (!("_regex_getHostnameWithoutWWW" in this)) {
+ this._regex_getHostnameWithoutWWW = /^[^:]+:\/\/(?:www\.)?([^/]+)/;
+ }
+ const matchAndHostname = this._regex_getHostnameWithoutWWW.exec(aLabel);
+ return matchAndHostname ? matchAndHostname[1] : null;
+ }
]]>
</body>
</method>
<method name="loadOneTab">
<parameter name="aURI"/>
<parameter name="aReferrerURI"/>
<parameter name="aCharset"/>
--- a/browser/components/sessionstore/test/browser_tab_label_during_restore.js
+++ b/browser/components/sessionstore/test/browser_tab_label_during_restore.js
@@ -71,17 +71,17 @@ add_task(async function() {
browserLoadedPromise = BrowserTestUtils.browserLoaded(tab2.linkedBrowser, false, ABOUT_ROBOTS_URI);
gBrowser.selectedTab = tab2;
await browserLoadedPromise;
ok(!tab2.hasAttribute("pending"), "second tab isn't pending anymore");
ok(document.title.startsWith(ABOUT_ROBOTS_TITLE), "title bar displays content title");
finishObservingLabelChanges();
info("selecting the third tab");
- finishObservingLabelChanges = observeLabelChanges(tab3, ["example.com/", REMOTE_TITLE]);
+ finishObservingLabelChanges = observeLabelChanges(tab3, ["example.com", REMOTE_TITLE]);
browserLoadedPromise = BrowserTestUtils.browserLoaded(tab3.linkedBrowser, false, REMOTE_URL);
gBrowser.selectedTab = tab3;
await browserLoadedPromise;
ok(!tab3.hasAttribute("pending"), "third tab isn't pending anymore");
ok(document.title.startsWith(REMOTE_TITLE), "title bar displays content title");
finishObservingLabelChanges();
info("selecting the fourth tab");