From 0f44964e5e0f7e37d7fa3216bb10fd61fbf52ae2 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Tue, 12 Jun 2012 13:33:57 -0700 Subject: fix($location): correctly parse link urls in hashbang mode with prefix This is a second fix for a regression that was introduced by 92a2e180. The fix addresses scenarios when the $location service is configured with a hash prefix. Closes #1037 --- src/ng/location.js | 2 +- test/ng/locationSpec.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/ng/location.js b/src/ng/location.js index 7576015c..b5f10293 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -525,7 +525,7 @@ function $LocationProvider(){ // update location with href without the prefix href = absHref.substr(absUrlPrefix.length); - if (href.charAt(0) == '#') href = href.substr(1); + if (href.indexOf('#' + hashPrefix) == 0) href = href.substr(hashPrefix.length + 1); $location.url(href); $rootScope.$apply(); event.preventDefault(); diff --git a/test/ng/locationSpec.js b/test/ng/locationSpec.js index 543232c3..91f3688c 100644 --- a/test/ng/locationSpec.js +++ b/test/ng/locationSpec.js @@ -999,6 +999,36 @@ describe('$location', function() { $rootElement.remove(); }); }); + + + it('should not mess up hash urls when clicking on links in hashbang mode with a prefix', + function() { + var base; + module(function($locationProvider) { + return function($browser) { + window.location.hash = '!someHash'; + $browser.url(base = window.location.href); + base = base.split('#')[0]; + $locationProvider.hashPrefix('!'); + } + }); + inject(function($rootScope, $compile, $browser, $rootElement, $document, $location) { + // we need to do this otherwise we can't simulate events + $document.find('body').append($rootElement); + + var element = $compile('v1v2')($rootScope); + $rootElement.append(element); + var av1 = $rootElement.find('a').eq(0); + var av2 = $rootElement.find('a').eq(1); + + + browserTrigger(av1, 'click'); + expect($browser.url()).toEqual(base + '#!/view1'); + + browserTrigger(av2, 'click'); + expect($browser.url()).toEqual(base + '#!/view2'); + }); + }); }); -- cgit v1.2.3