Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 9befe37

Browse files
tleruitteIgorMinar
authored andcommitted
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.
1 parent e88d617 commit 9befe37

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/ng/location.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ function convertToHashbangUrl(url, basePath, hashPrefix) {
7979
var match = matchUrl(url);
8080

8181
// already hashbang url
82-
if (decodeURIComponent(match.path) == basePath) {
82+
if (decodeURIComponent(match.path) == basePath && !isUndefined(match.hash) &&
83+
match.hash.indexOf(hashPrefix) === 0) {
8384
return url;
8485
// convert html5 url -> hashbang url
8586
} else {

test/ng/locationSpec.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,16 @@ describe('$location', function() {
549549
}
550550
);
551551
});
552+
553+
it('should correctly convert html5 url with path matching basepath to hashbang url', function () {
554+
initService(true, '!', false);
555+
inject(
556+
initBrowser('http://domain.com/base/index.html', '/base/index.html'),
557+
function($browser, $location) {
558+
expect($browser.url()).toBe('http://domain.com/base/index.html#!/index.html');
559+
}
560+
);
561+
});
552562
});
553563

554564

@@ -1209,7 +1219,7 @@ describe('$location', function() {
12091219
);
12101220

12111221

1212-
it('should listen on click events on href and prevent browser default in hashbang mode', function() {
1222+
it('should listen on click events on href and prevent browser default in hashbang mode', function() {
12131223
module(function() {
12141224
return function($rootElement, $compile, $rootScope) {
12151225
$rootElement.html('<a href="http://server/#/somePath">link</a>');
@@ -1253,20 +1263,21 @@ describe('$location', function() {
12531263

12541264
inject(function($location, $rootScope, $browser, $rootElement) {
12551265
var log = '',
1256-
link = $rootElement.find('a');
1266+
link = $rootElement.find('a'),
1267+
browserUrlBefore = $browser.url();
12571268

12581269
$rootScope.$on('$locationChangeStart', function(event) {
12591270
event.preventDefault();
12601271
log += '$locationChangeStart';
12611272
});
12621273
$rootScope.$on('$locationChangeSuccess', function() {
1263-
throw new Error('after cancelation in html5 mode');
1274+
throw new Error('after cancellation in html5 mode');
12641275
});
12651276

12661277
browserTrigger(link, 'click');
12671278

12681279
expect(log).toEqual('$locationChangeStart');
1269-
expect($browser.url()).toEqual('http://server/');
1280+
expect($browser.url()).toBe(browserUrlBefore);
12701281

12711282
dealoc($rootElement);
12721283
});

0 commit comments

Comments
 (0)