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

Commit 2dc34a9

Browse files
fix($location): allow hash fragments with hashPrefix in hash-bang location urls
Previously if there was a hash fragment but no hashPrefix we would throw an error. Now we assume that the hash-bang path is empty and that the hash is a valid fragment. This prevents unnecessary exceptions where we clear the hashBang path, say by navigating back to the base url, where the $browser leaves an empty hash symbol on the URL to ensure there is no browser reload. BREAKING CHANGE: We no longer throw an `ihshprfx` error if the URL after the base path contains only a hash fragment. Previously, if the base URL was `http://abc.com/base/` and the hashPrefix is `!` then trying to parse `http://abc.com/base/#some-fragment` would have thrown an error. Now we simply assume it is a normal fragment and that the path is empty, resulting `$location.absUrl() === "http://abc.com/base/#!/#some-fragment"`. This should not break any applications, but you can no longer rely on receiving the `ihshprfx` error for paths that have the syntax above. It is actually more similar to what currently happens for invalid extra paths anyway: If the base URL and hashPrfix are set up as above, then `http://abc.com/base/other/path` does not throw an error but just ignores the extra path: `http://abc.com/base`. Closes #9629 Closes #9635 Closes #10228 Closes #10308
1 parent 10ac594 commit 2dc34a9

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

src/ng/location.js

+18-9
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,25 @@ function LocationHashbangUrl(appBase, hashPrefix) {
183183
*/
184184
this.$$parse = function(url) {
185185
var withoutBaseUrl = beginsWith(appBase, url) || beginsWith(appBaseNoFile, url);
186-
var withoutHashUrl = withoutBaseUrl.charAt(0) == '#'
187-
? beginsWith(hashPrefix, withoutBaseUrl)
188-
: (this.$$html5)
189-
? withoutBaseUrl
190-
: '';
191-
192-
if (!isString(withoutHashUrl)) {
193-
throw $locationMinErr('ihshprfx', 'Invalid url "{0}", missing hash prefix "{1}".', url,
194-
hashPrefix);
186+
var withoutHashUrl;
187+
188+
if (withoutBaseUrl.charAt(0) === '#') {
189+
190+
// The rest of the url starts with a hash so we have
191+
// got either a hashbang path or a plain hash fragment
192+
withoutHashUrl = beginsWith(hashPrefix, withoutBaseUrl);
193+
if (isUndefined(withoutHashUrl)) {
194+
// There was no hashbang prefix so we just have a hash fragment
195+
withoutHashUrl = withoutBaseUrl;
196+
}
197+
198+
} else {
199+
// There was no hashbang path nor hash fragment:
200+
// If we are in HTML5 mode we use what is left as the path;
201+
// Otherwise we ignore what is left
202+
withoutHashUrl = this.$$html5 ? withoutBaseUrl : '';
195203
}
204+
196205
parseAppUrl(withoutHashUrl, this);
197206

198207
this.$$path = removeWindowsDriveName(this.$$path, withoutHashUrl, appBase);

test/ng/locationSpec.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -554,10 +554,24 @@ describe('$location', function() {
554554
});
555555

556556

557-
it('should throw error when invalid hashbang prefix given', function() {
558-
expect(function() {
559-
url.$$parse('http://www.server.org:1234/base#/path');
560-
}).toThrowMinErr('$location', 'ihshprfx', 'Invalid url "http://www.server.org:1234/base#/path", missing hash prefix "#!".');
557+
it('should insert default hashbang if a hash is given with no hashbang prefix', function() {
558+
559+
url.$$parse('http://www.server.org:1234/base#/path');
560+
expect(url.absUrl()).toBe('http://www.server.org:1234/base#!#%2Fpath');
561+
expect(url.hash()).toBe('/path');
562+
expect(url.path()).toBe('');
563+
564+
url.$$parse('http://www.server.org:1234/base#');
565+
expect(url.absUrl()).toBe('http://www.server.org:1234/base');
566+
expect(url.hash()).toBe('');
567+
expect(url.path()).toBe('');
568+
});
569+
570+
it('should ignore extra path segments if no hashbang is given', function() {
571+
url.$$parse('http://www.server.org:1234/base/extra/path');
572+
expect(url.absUrl()).toBe('http://www.server.org:1234/base');
573+
expect(url.path()).toBe('');
574+
expect(url.hash()).toBe('');
561575
});
562576

563577

0 commit comments

Comments
 (0)