Bug 1025195 - switchToTabHavingURI should have an option to ignore URL hashes when looking for already opened tabs. r=dao

--HG--
extra : rebase_source : 02cf11785573131ac8d0b267512e4cf93c43eb31
This commit is contained in:
Jared Wein 2014-06-17 10:01:41 -04:00
parent 1c230bfb39
commit dad32a197c
4 changed files with 55 additions and 3 deletions

View File

@ -6902,15 +6902,23 @@ let gRemoteTabsUI = {
* If switching to this URI results in us opening a tab, aOpenParams
* will be the parameter object that gets passed to openUILinkIn. Please
* see the documentation for openUILinkIn to see what parameters can be
* passed via this object.
* passed via this object. This object also allows the 'ignoreFragment'
* property to be set to true to exclude fragment-portion matching when
* comparing URIs.
* @return True if an existing tab was found, false otherwise
*/
function switchToTabHavingURI(aURI, aOpenNew, aOpenParams) {
function switchToTabHavingURI(aURI, aOpenNew, aOpenParams={}) {
// Certain URLs can be switched to irrespective of the source or destination
// window being in private browsing mode:
const kPrivateBrowsingWhitelist = new Set([
"about:customizing",
]);
let ignoreFragment = aOpenParams.ignoreFragment;
// This property is only used by switchToTabHavingURI and should
// not be used as a parameter for the new load.
delete aOpenParams.ignoreFragment;
// This will switch to the tab in aWindow having aURI, if present.
function switchIfURIInWindow(aWindow) {
// Only switch to the tab if neither the source nor the destination window
@ -6925,10 +6933,17 @@ function switchToTabHavingURI(aURI, aOpenNew, aOpenParams) {
let browsers = aWindow.gBrowser.browsers;
for (let i = 0; i < browsers.length; i++) {
let browser = browsers[i];
if (browser.currentURI.equals(aURI)) {
if (ignoreFragment ? browser.currentURI.equalsExceptRef(aURI) :
browser.currentURI.equals(aURI)) {
// Focus the matching window & tab
aWindow.focus();
aWindow.gBrowser.tabContainer.selectedIndex = i;
if (ignoreFragment) {
let spec = aURI.spec;
if (!aURI.ref)
spec += "#";
browser.loadURI(spec);
}
return true;
}
}

View File

@ -428,3 +428,4 @@ skip-if = e10s # Bug 940206 - nsIWebContentHandlerRegistrar::registerProtocolHan
[browser_no_mcb_on_http_site.js]
skip-if = e10s # Bug 516755 - SessionStore disabled for e10s
[browser_bug1003461-switchtab-override.js]
[browser_bug1025195_switchToTabHavingURI_ignoreFragment.js]

View File

@ -0,0 +1,35 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
add_task(function() {
registerCleanupFunction(function() {
while (gBrowser.tabs.length > 1)
gBrowser.removeCurrentTab();
});
let tabRefAboutHome = gBrowser.addTab("about:home#1");
yield promiseTabLoaded(tabRefAboutHome);
let tabRefAboutMozilla = gBrowser.addTab("about:mozilla");
yield promiseTabLoaded(tabRefAboutMozilla);
gBrowser.selectedTab = tabRefAboutMozilla;
let numTabsAtStart = gBrowser.tabs.length;
switchTab("about:home#1", false, true);
switchTab("about:mozilla", false, true);
switchTab("about:home#2", true, true);
is(tabRefAboutHome, gBrowser.selectedTab, "The same about:home tab should be switched to");
is(gBrowser.currentURI.ref, "2", "The ref should be updated to the new ref");
switchTab("about:mozilla", false, true);
switchTab("about:home#1", false, false);
isnot(tabRefAboutHome, gBrowser.selectedTab, "Selected tab should not be initial about:blank tab");
is(gBrowser.tabs.length, numTabsAtStart + 1, "Should have one new tab opened");
switchTab("about:about", true, false);
});
function switchTab(aURI, aIgnoreFragment, aShouldFindExistingTab) {
let tabFound = switchToTabHavingURI(aURI, true, {ignoreFragment: aIgnoreFragment});
is(tabFound, aShouldFindExistingTab,
"Should switch to existing " + aURI + " tab if one existed, " +
(aIgnoreFragment ? "ignoring" : "including") + " fragment portion");
}

View File

@ -471,3 +471,4 @@ let FullZoomHelper = {
};
},
};