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

Commit 20a98f2

Browse files
committed
fix($location): respect base href
1 parent 8dc10a4 commit 20a98f2

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

src/ng/location.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -682,13 +682,18 @@ function $LocationProvider(){
682682
var hrefNoBase = baseHrefNoFile ? href.substr(baseHrefNoFile.length - 1) : href;
683683
absHref = appBase + prefix + hrefNoBase;
684684
} else if (href[0] != '/') { // Ignore absolute path outside of base
685+
var beginsWithPrefix = beginsWith(prefix, href);
685686
if (beginsWith(prefix + '/', href)) {
686687
// local anchor with absolute path
687688
absHref = appBase + href;
688-
} else if (href[0] == '#') {
689+
} else if (!beginsWithPrefix && href[0] == '#') {
689690
// local anchor
690691
absHref = appBase + prefix + ($location.path() || '/') + href;
692+
} else if (!beginsWithPrefix && baseHref) {
693+
// local anchor and base[href] exists
694+
absHref = appBase + prefix + '/' + href;
691695
} else {
696+
href = beginsWithPrefix ? href.substr(prefix.length) : href;
692697
// relative path - join with current path
693698
var stack = $location.path().split("/"),
694699
parts = href.split("/");

test/ng/locationSpec.js

+33-8
Original file line numberDiff line numberDiff line change
@@ -879,14 +879,10 @@ describe('$location', function() {
879879
return function($browser){
880880
if (atRoot) {
881881
$browser.url('http://host.com/');
882-
if (!noBase) {
883-
$browser.$$baseHref = '/index.html';
884-
}
882+
$browser.$$baseHref = noBase ? '' : '/index.html';
885883
} else {
886884
$browser.url('http://host.com/base');
887-
if (!noBase) {
888-
$browser.$$baseHref = '/base/index.html';
889-
}
885+
$browser.$$baseHref = noBase ? '' : '/base/index.html';
890886
}
891887
};
892888
}
@@ -1201,15 +1197,30 @@ describe('$location', function() {
12011197
});
12021198

12031199

1204-
it('should rewrite relative links relative to current path when history disabled', function() {
1200+
it('should rewrite relative links relative to current path when no base and history enabled on old browser', function() {
1201+
configureService('link', true, false, true);
1202+
inject(
1203+
initBrowser(false, true),
1204+
initLocation(),
1205+
function($browser, $location) {
1206+
$location.path('/some');
1207+
expect($browser.url(), 'http://host.com/#!/some');
1208+
browserTrigger(link, 'click');
1209+
expectRewriteTo($browser, 'http://host.com/#!/some/link');
1210+
}
1211+
);
1212+
});
1213+
1214+
1215+
it('should rewrite relative links relative to base href when history enabled on old browser', function() {
12051216
configureService('link', true, false, true);
12061217
inject(
12071218
initBrowser(),
12081219
initLocation(),
12091220
function($browser, $location) {
12101221
$location.path('/some');
12111222
browserTrigger(link, 'click');
1212-
expectRewriteTo($browser, 'http://host.com/base/index.html#!/some/link');
1223+
expectRewriteTo($browser, 'http://host.com/base/index.html#!/link');
12131224
}
12141225
);
12151226
});
@@ -1243,6 +1254,20 @@ describe('$location', function() {
12431254
});
12441255

12451256

1257+
it('should rewrite relative hashbang links when history enabled on old browser', function() {
1258+
configureService('#!link', true, false, true);
1259+
inject(
1260+
initBrowser(),
1261+
initLocation(),
1262+
function($browser, $location) {
1263+
$location.path('/some');
1264+
browserTrigger(link, 'click');
1265+
expectRewriteTo($browser, 'http://host.com/base/index.html#!/some/link');
1266+
}
1267+
);
1268+
});
1269+
1270+
12461271
it('should replace current path when link begins with "#!/" and history enabled on old browser', function() {
12471272
configureService('#!/link', true, false, true);
12481273
inject(

0 commit comments

Comments
 (0)