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

Commit

Permalink
fix($location): correctly parse link urls in hashbang mode
Browse files Browse the repository at this point in the history
This is a fix for a regression that was introduced by 92a2e18

Closes #1037
  • Loading branch information
mhevery authored and IgorMinar committed Jun 12, 2012
1 parent ee6014a commit 74fa65e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/ng/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,14 @@ function $LocationProvider(){
this.$get = ['$rootScope', '$browser', '$sniffer', '$rootElement',
function( $rootScope, $browser, $sniffer, $rootElement) {
var $location,
basePath = $browser.baseHref() || '/',
pathPrefix = pathPrefixFromBase(basePath),
basePath,
pathPrefix,
initUrl = $browser.url(),
absUrlPrefix;

if (html5Mode) {
basePath = $browser.baseHref() || '/';
pathPrefix = pathPrefixFromBase(basePath);
if ($sniffer.history) {
$location = new LocationUrl(
convertToHtml5Url(initUrl, basePath, hashPrefix),
Expand All @@ -491,14 +493,14 @@ function $LocationProvider(){
convertToHashbangUrl(initUrl, basePath, hashPrefix),
hashPrefix);
}
// link rewriting
absUrlPrefix = composeProtocolHostPort(
$location.protocol(), $location.host(), $location.port()) + pathPrefix;
} else {
$location = new LocationHashbangUrl(initUrl, hashPrefix);
absUrlPrefix = $location.absUrl().split('#')[0];
}

// link rewriting
absUrlPrefix = composeProtocolHostPort(
$location.protocol(), $location.host(), $location.port()) + pathPrefix;

$rootElement.bind('click', function(event) {
// TODO(vojta): rewrite link when opening in new tab/window (in legacy browser)
// currently we open nice url link and redirect then
Expand All @@ -512,7 +514,8 @@ function $LocationProvider(){
elm = elm.parent();
}

var absHref = elm.prop('href');
var absHref = elm.prop('href'),
href;

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

// update location with href without the prefix
$location.url(absHref.substr(absUrlPrefix.length));
href = absHref.substr(absUrlPrefix.length);
if (href.charAt(0) == '#') href = href.substr(1);
$location.url(href);
$rootScope.$apply();
event.preventDefault();
// hack to work around FF6 bug 684208 when scenario runner clicks on links
Expand Down
31 changes: 31 additions & 0 deletions test/ng/locationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,37 @@ describe('$location', function() {
);
});
}


it('should not mess up hash urls when clicking on links in hashbang mode', function() {
var base;
module(function() {
return function($browser) {
window.location.hash = 'someHash';
base = window.location.href
$browser.url(base);
base = base.split('#')[0];
}
});
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('<a href="#/view1">v1</a><a href="#/view2">v2</a>')($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');

$rootElement.remove();
});
});
});


Expand Down

0 comments on commit 74fa65e

Please sign in to comment.