fix($location): correctly rewrite html5 url to hashbang url

In situations where path() matched basepath and we needed to
convert from html5 url to hashbang url, the $location service
considered the url to be already rewritten, which resulted in
an error.
This commit is contained in:
Thibault Leruitte
2013-03-08 14:53:54 +01:00
committed by Igor Minar
parent 2b1f10266a
commit 346e98330c
2 changed files with 17 additions and 5 deletions

View File

@@ -79,7 +79,8 @@ function convertToHashbangUrl(url, basePath, hashPrefix) {
var match = matchUrl(url); var match = matchUrl(url);
// already hashbang url // already hashbang url
if (decodeURIComponent(match.path) == basePath) { if (decodeURIComponent(match.path) == basePath && !isUndefined(match.hash) &&
match.hash.indexOf(hashPrefix) === 0) {
return url; return url;
// convert html5 url -> hashbang url // convert html5 url -> hashbang url
} else { } else {

View File

@@ -549,6 +549,16 @@ describe('$location', function() {
} }
); );
}); });
it('should correctly convert html5 url with path matching basepath to hashbang url', function () {
initService(true, '!', false);
inject(
initBrowser('http://domain.com/base/index.html', '/base/index.html'),
function($browser, $location) {
expect($browser.url()).toBe('http://domain.com/base/index.html#!/index.html');
}
);
});
}); });
@@ -1209,7 +1219,7 @@ describe('$location', function() {
); );
it('should listen on click events on href and prevent browser default in hashbang mode', function() { it('should listen on click events on href and prevent browser default in hashbang mode', function() {
module(function() { module(function() {
return function($rootElement, $compile, $rootScope) { return function($rootElement, $compile, $rootScope) {
$rootElement.html('<a href="http://server/#/somePath">link</a>'); $rootElement.html('<a href="http://server/#/somePath">link</a>');
@@ -1253,20 +1263,21 @@ describe('$location', function() {
inject(function($location, $rootScope, $browser, $rootElement) { inject(function($location, $rootScope, $browser, $rootElement) {
var log = '', var log = '',
link = $rootElement.find('a'); link = $rootElement.find('a'),
browserUrlBefore = $browser.url();
$rootScope.$on('$locationChangeStart', function(event) { $rootScope.$on('$locationChangeStart', function(event) {
event.preventDefault(); event.preventDefault();
log += '$locationChangeStart'; log += '$locationChangeStart';
}); });
$rootScope.$on('$locationChangeSuccess', function() { $rootScope.$on('$locationChangeSuccess', function() {
throw new Error('after cancelation in html5 mode'); throw new Error('after cancellation in html5 mode');
}); });
browserTrigger(link, 'click'); browserTrigger(link, 'click');
expect(log).toEqual('$locationChangeStart'); expect(log).toEqual('$locationChangeStart');
expect($browser.url()).toEqual('http://server/'); expect($browser.url()).toBe(browserUrlBefore);
dealoc($rootElement); dealoc($rootElement);
}); });