mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-04 13:14:49 +00:00
fix($location): correctly parse link urls in hashbang mode
This is a fix for a regression that was introduced by 92a2e180
Closes #1037
This commit is contained in:
parent
ee6014a3aa
commit
74fa65ecb7
2 changed files with 44 additions and 8 deletions
|
|
@ -476,12 +476,14 @@ function $LocationProvider(){
|
||||||
this.$get = ['$rootScope', '$browser', '$sniffer', '$rootElement',
|
this.$get = ['$rootScope', '$browser', '$sniffer', '$rootElement',
|
||||||
function( $rootScope, $browser, $sniffer, $rootElement) {
|
function( $rootScope, $browser, $sniffer, $rootElement) {
|
||||||
var $location,
|
var $location,
|
||||||
basePath = $browser.baseHref() || '/',
|
basePath,
|
||||||
pathPrefix = pathPrefixFromBase(basePath),
|
pathPrefix,
|
||||||
initUrl = $browser.url(),
|
initUrl = $browser.url(),
|
||||||
absUrlPrefix;
|
absUrlPrefix;
|
||||||
|
|
||||||
if (html5Mode) {
|
if (html5Mode) {
|
||||||
|
basePath = $browser.baseHref() || '/';
|
||||||
|
pathPrefix = pathPrefixFromBase(basePath);
|
||||||
if ($sniffer.history) {
|
if ($sniffer.history) {
|
||||||
$location = new LocationUrl(
|
$location = new LocationUrl(
|
||||||
convertToHtml5Url(initUrl, basePath, hashPrefix),
|
convertToHtml5Url(initUrl, basePath, hashPrefix),
|
||||||
|
|
@ -491,14 +493,14 @@ function $LocationProvider(){
|
||||||
convertToHashbangUrl(initUrl, basePath, hashPrefix),
|
convertToHashbangUrl(initUrl, basePath, hashPrefix),
|
||||||
hashPrefix);
|
hashPrefix);
|
||||||
}
|
}
|
||||||
|
// link rewriting
|
||||||
|
absUrlPrefix = composeProtocolHostPort(
|
||||||
|
$location.protocol(), $location.host(), $location.port()) + pathPrefix;
|
||||||
} else {
|
} else {
|
||||||
$location = new LocationHashbangUrl(initUrl, hashPrefix);
|
$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) {
|
$rootElement.bind('click', function(event) {
|
||||||
// TODO(vojta): rewrite link when opening in new tab/window (in legacy browser)
|
// TODO(vojta): rewrite link when opening in new tab/window (in legacy browser)
|
||||||
// currently we open nice url link and redirect then
|
// currently we open nice url link and redirect then
|
||||||
|
|
@ -512,7 +514,8 @@ function $LocationProvider(){
|
||||||
elm = elm.parent();
|
elm = elm.parent();
|
||||||
}
|
}
|
||||||
|
|
||||||
var absHref = elm.prop('href');
|
var absHref = elm.prop('href'),
|
||||||
|
href;
|
||||||
|
|
||||||
if (!absHref ||
|
if (!absHref ||
|
||||||
elm.attr('target') ||
|
elm.attr('target') ||
|
||||||
|
|
@ -521,7 +524,9 @@ function $LocationProvider(){
|
||||||
}
|
}
|
||||||
|
|
||||||
// update location with href without the prefix
|
// 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();
|
$rootScope.$apply();
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
// hack to work around FF6 bug 684208 when scenario runner clicks on links
|
// hack to work around FF6 bug 684208 when scenario runner clicks on links
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue