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

Commit b16d825

Browse files
committed
fix($location): respect absolute paths correctly
Fixes absolute links on browsers with html5Mode enabled. Previously clicking on a link out of the base href would still rewrite the URL.
1 parent f6f0791 commit b16d825

File tree

2 files changed

+56
-21
lines changed

2 files changed

+56
-21
lines changed

src/ng/location.js

+26-19
Original file line numberDiff line numberDiff line change
@@ -675,26 +675,33 @@ function $LocationProvider(){
675675

676676
if (href && href.indexOf('://') < 0) { // Ignore absolute URLs
677677
var prefix = '#' + hashPrefix;
678-
if (href[0] == '/') {
679-
// absolute path - replace old path
680-
absHref = appBase + prefix + href;
681-
} else if (href[0] == '#') {
682-
// local anchor
683-
absHref = appBase + prefix + ($location.path() || '/') + href;
684-
} else {
685-
// relative path - join with current path
686-
var stack = $location.path().split("/"),
687-
parts = href.split("/");
688-
if (stack.length === 2 && !stack[1]) stack.length = 1;
689-
for (var i=0; i<parts.length; i++) {
690-
if (parts[i] == ".")
691-
continue;
692-
else if (parts[i] == "..")
693-
stack.pop();
694-
else if (parts[i].length)
695-
stack.push(parts[i]);
678+
var baseHrefNoFile = stripFile(baseHref);
679+
if (href[0] == '/' && baseHref && beginsWith(baseHrefNoFile, href)) {
680+
// absolute path - within base or when there's no base
681+
var hrefNoBase = href.substr(baseHrefNoFile.length - 1);
682+
absHref = appBase + prefix + hrefNoBase;
683+
} else if (href[0] != '/') { // Ignore absolute path outside of base
684+
if (beginsWith(prefix + '/', href)) {
685+
// local anchor with absolute path
686+
absHref = appBase + href;
687+
} else if (href[0] == '#') {
688+
// local anchor
689+
absHref = appBase + prefix + ($location.path() || '/') + href;
690+
} else {
691+
// relative path - join with current path
692+
var stack = $location.path().split("/"),
693+
parts = href.split("/");
694+
if (stack.length === 2 && !stack[1]) stack.length = 1;
695+
for (var i=0; i<parts.length; i++) {
696+
if (parts[i] == ".")
697+
continue;
698+
else if (parts[i] == "..")
699+
stack.pop();
700+
else if (parts[i].length)
701+
stack.push(parts[i]);
702+
}
703+
absHref = appBase + prefix + stack.join('/');
696704
}
697-
absHref = appBase + prefix + stack.join('/');
698705
}
699706
}
700707
}

test/ng/locationSpec.js

+30-2
Original file line numberDiff line numberDiff line change
@@ -1206,8 +1206,8 @@ describe('$location', function() {
12061206
});
12071207

12081208

1209-
it('should replace current path when link begins with "/" and history disabled', function() {
1210-
configureService('/link', true, false, true);
1209+
it('should replace current path when hashbang url begins with "/" and history enabled on old browser', function() {
1210+
configureService('#!/link', true, false, true);
12111211
inject(
12121212
initBrowser(),
12131213
initLocation(),
@@ -1220,6 +1220,34 @@ describe('$location', function() {
12201220
});
12211221

12221222

1223+
it('should replace current path when relative link begins with "/base/" and history enabled on old browser', function() {
1224+
configureService('/base/link', true, false, true);
1225+
inject(
1226+
initBrowser(),
1227+
initLocation(),
1228+
function($browser, $location) {
1229+
$location.path('/some');
1230+
browserTrigger(link, 'click');
1231+
expectRewriteTo($browser, 'http://host.com/base/index.html#!/link');
1232+
}
1233+
);
1234+
});
1235+
1236+
1237+
it('should not rewrite when relative link begins with "/" and history enabled on old browser', function() {
1238+
configureService('/other_base/link', true, false, true);
1239+
inject(
1240+
initBrowser(),
1241+
initLocation(),
1242+
function($browser, $location) {
1243+
$location.path('/some');
1244+
browserTrigger(link, 'click');
1245+
expectNoRewrite($browser);
1246+
}
1247+
);
1248+
});
1249+
1250+
12231251
it('should replace current hash fragment when link begins with "#" history disabled', function() {
12241252
configureService('#link', true, false, true);
12251253
inject(

0 commit comments

Comments
 (0)