-
Notifications
You must be signed in to change notification settings - Fork 27.4k
fix($location): consider baseHref in relative link for legacy browsers #8233
Changes from 4 commits
25da859
df05668
dfd2959
b6d00f7
5b99a00
b65fd3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -668,6 +668,13 @@ function $LocationProvider(){ | |
if (href.indexOf('://') < 0) { // Ignore absolute URLs | ||
var prefix = '#' + hashPrefix; | ||
if (href[0] == '/') { | ||
// Account for base href already present in appBase | ||
if (baseHref && href.indexOf(baseHref) === 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is quite right. Assuming we have a base tag like
The issues are, 1) we're not really correctly resolving relative to the baseURI for absolute references, and 2) we're not resolving relative URIs correctly this way at all, because this branch is only ever evaluated for absolute uris (uris beginning with a We need to fix this up |
||
href = href.substr(baseHref.length); | ||
if (!href || href[0] != '/') { | ||
href = '/' + href; | ||
} | ||
} | ||
// absolute path - replace old path | ||
absHref = appBase + prefix + href; | ||
} else if (href[0] == '#') { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1330,6 +1330,36 @@ describe('$location', function() { | |
}).not.toThrow(); | ||
}); | ||
}); | ||
|
||
|
||
it('should transform the url correctly when a base path is present and html5mode is enabled but not supported', function() { | ||
var base; | ||
module(function() { | ||
return function($browser) { | ||
base = 'http://server/foo'; | ||
$browser.url(base); | ||
$browser.$$baseHref = '/foo'; | ||
}; | ||
}); | ||
inject(initService(true, '', false), 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="/foo/view1">v1</a><a href="/foo/bar/view2">v2</a>')($rootScope); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: these don't need to be compiled, just append them to the root element |
||
$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 + '#/bar/view2'); | ||
|
||
$rootElement.remove(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: $rootElement gets dealocd after each spec anyways, this isn't really needed |
||
}); | ||
}); | ||
}); | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// use baseHref variable is fine
if (!baseHref && href.indexOf(baseHref) === 0) {
href = href.substr(baseHref.length);
if (!href || href[0] != '/') {
href = '/' + href;
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks! I've updated accordingly