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

Commit 0bb57d5

Browse files
tsuyoshizawapetebacondarwin
authored andcommitted
fix($location): allow navigating outside the original base URL
Previously, if you navigated outside of the current base URL angular crashed with a `Cannot call method 'charAt' of undefined` error. Closes #11302 Closes #4776
1 parent 61a3fb6 commit 0bb57d5

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/ng/location.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ function LocationHashbangUrl(appBase, hashPrefix) {
185185
var withoutBaseUrl = beginsWith(appBase, url) || beginsWith(appBaseNoFile, url);
186186
var withoutHashUrl;
187187

188-
if (withoutBaseUrl.charAt(0) === '#') {
188+
if (!isUndefined(withoutBaseUrl) && withoutBaseUrl.charAt(0) === '#') {
189189

190190
// The rest of the url starts with a hash so we have
191191
// got either a hashbang path or a plain hash fragment
@@ -199,7 +199,15 @@ function LocationHashbangUrl(appBase, hashPrefix) {
199199
// There was no hashbang path nor hash fragment:
200200
// If we are in HTML5 mode we use what is left as the path;
201201
// Otherwise we ignore what is left
202-
withoutHashUrl = this.$$html5 ? withoutBaseUrl : '';
202+
if (this.$$html5) {
203+
withoutHashUrl = withoutBaseUrl;
204+
} else {
205+
withoutHashUrl = '';
206+
if (isUndefined(withoutBaseUrl)) {
207+
appBase = url;
208+
this.replace();
209+
}
210+
}
203211
}
204212

205213
parseAppUrl(withoutHashUrl, this);

test/ng/locationSpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -2462,6 +2462,14 @@ describe('$location', function() {
24622462
it('should throw on url(urlString, stateObject)', function() {
24632463
expectThrowOnStateChange(locationUrl);
24642464
});
2465+
2466+
it('should allow navigating outside the original base URL', function() {
2467+
locationUrl = new LocationHashbangUrl('http://server/pre/index.html', '#');
2468+
2469+
locationUrl.$$parse('http://server/next/index.html');
2470+
expect(locationUrl.url()).toBe('');
2471+
expect(locationUrl.absUrl()).toBe('http://server/next/index.html');
2472+
});
24652473
});
24662474

24672475

0 commit comments

Comments
 (0)