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

Commit 74fa65e

Browse files
mheveryIgorMinar
authored andcommitted
fix($location): correctly parse link urls in hashbang mode
This is a fix for a regression that was introduced by 92a2e18 Closes #1037
1 parent ee6014a commit 74fa65e

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/ng/location.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,14 @@ function $LocationProvider(){
476476
this.$get = ['$rootScope', '$browser', '$sniffer', '$rootElement',
477477
function( $rootScope, $browser, $sniffer, $rootElement) {
478478
var $location,
479-
basePath = $browser.baseHref() || '/',
480-
pathPrefix = pathPrefixFromBase(basePath),
479+
basePath,
480+
pathPrefix,
481481
initUrl = $browser.url(),
482482
absUrlPrefix;
483483

484484
if (html5Mode) {
485+
basePath = $browser.baseHref() || '/';
486+
pathPrefix = pathPrefixFromBase(basePath);
485487
if ($sniffer.history) {
486488
$location = new LocationUrl(
487489
convertToHtml5Url(initUrl, basePath, hashPrefix),
@@ -491,14 +493,14 @@ function $LocationProvider(){
491493
convertToHashbangUrl(initUrl, basePath, hashPrefix),
492494
hashPrefix);
493495
}
496+
// link rewriting
497+
absUrlPrefix = composeProtocolHostPort(
498+
$location.protocol(), $location.host(), $location.port()) + pathPrefix;
494499
} else {
495500
$location = new LocationHashbangUrl(initUrl, hashPrefix);
501+
absUrlPrefix = $location.absUrl().split('#')[0];
496502
}
497503

498-
// link rewriting
499-
absUrlPrefix = composeProtocolHostPort(
500-
$location.protocol(), $location.host(), $location.port()) + pathPrefix;
501-
502504
$rootElement.bind('click', function(event) {
503505
// TODO(vojta): rewrite link when opening in new tab/window (in legacy browser)
504506
// currently we open nice url link and redirect then
@@ -512,7 +514,8 @@ function $LocationProvider(){
512514
elm = elm.parent();
513515
}
514516

515-
var absHref = elm.prop('href');
517+
var absHref = elm.prop('href'),
518+
href;
516519

517520
if (!absHref ||
518521
elm.attr('target') ||
@@ -521,7 +524,9 @@ function $LocationProvider(){
521524
}
522525

523526
// update location with href without the prefix
524-
$location.url(absHref.substr(absUrlPrefix.length));
527+
href = absHref.substr(absUrlPrefix.length);
528+
if (href.charAt(0) == '#') href = href.substr(1);
529+
$location.url(href);
525530
$rootScope.$apply();
526531
event.preventDefault();
527532
// hack to work around FF6 bug 684208 when scenario runner clicks on links

test/ng/locationSpec.js

+31
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,37 @@ describe('$location', function() {
968968
);
969969
});
970970
}
971+
972+
973+
it('should not mess up hash urls when clicking on links in hashbang mode', function() {
974+
var base;
975+
module(function() {
976+
return function($browser) {
977+
window.location.hash = 'someHash';
978+
base = window.location.href
979+
$browser.url(base);
980+
base = base.split('#')[0];
981+
}
982+
});
983+
inject(function($rootScope, $compile, $browser, $rootElement, $document, $location) {
984+
// we need to do this otherwise we can't simulate events
985+
$document.find('body').append($rootElement);
986+
987+
var element = $compile('<a href="#/view1">v1</a><a href="#/view2">v2</a>')($rootScope);
988+
$rootElement.append(element);
989+
var av1 = $rootElement.find('a').eq(0);
990+
var av2 = $rootElement.find('a').eq(1);
991+
992+
993+
browserTrigger(av1, 'click');
994+
expect($browser.url()).toEqual(base + '#/view1');
995+
996+
browserTrigger(av2, 'click');
997+
expect($browser.url()).toEqual(base + '#/view2');
998+
999+
$rootElement.remove();
1000+
});
1001+
});
9711002
});
9721003

9731004

0 commit comments

Comments
 (0)