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

Commit 0f44964

Browse files
mheveryIgorMinar
authored andcommitted
fix($location): correctly parse link urls in hashbang mode with prefix
This is a second fix for a regression that was introduced by 92a2e18. The fix addresses scenarios when the $location service is configured with a hash prefix. Closes #1037
1 parent f6b09b9 commit 0f44964

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/ng/location.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ function $LocationProvider(){
525525

526526
// update location with href without the prefix
527527
href = absHref.substr(absUrlPrefix.length);
528-
if (href.charAt(0) == '#') href = href.substr(1);
528+
if (href.indexOf('#' + hashPrefix) == 0) href = href.substr(hashPrefix.length + 1);
529529
$location.url(href);
530530
$rootScope.$apply();
531531
event.preventDefault();

test/ng/locationSpec.js

+30
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,36 @@ describe('$location', function() {
999999
$rootElement.remove();
10001000
});
10011001
});
1002+
1003+
1004+
it('should not mess up hash urls when clicking on links in hashbang mode with a prefix',
1005+
function() {
1006+
var base;
1007+
module(function($locationProvider) {
1008+
return function($browser) {
1009+
window.location.hash = '!someHash';
1010+
$browser.url(base = window.location.href);
1011+
base = base.split('#')[0];
1012+
$locationProvider.hashPrefix('!');
1013+
}
1014+
});
1015+
inject(function($rootScope, $compile, $browser, $rootElement, $document, $location) {
1016+
// we need to do this otherwise we can't simulate events
1017+
$document.find('body').append($rootElement);
1018+
1019+
var element = $compile('<a href="#!/view1">v1</a><a href="#!/view2">v2</a>')($rootScope);
1020+
$rootElement.append(element);
1021+
var av1 = $rootElement.find('a').eq(0);
1022+
var av2 = $rootElement.find('a').eq(1);
1023+
1024+
1025+
browserTrigger(av1, 'click');
1026+
expect($browser.url()).toEqual(base + '#!/view1');
1027+
1028+
browserTrigger(av2, 'click');
1029+
expect($browser.url()).toEqual(base + '#!/view2');
1030+
});
1031+
});
10021032
});
10031033

10041034

0 commit comments

Comments
 (0)