diff --git a/src/ng/location.js b/src/ng/location.js index 99474ee7..337681fa 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -183,16 +183,25 @@ function LocationHashbangUrl(appBase, hashPrefix) { */ this.$$parse = function(url) { var withoutBaseUrl = beginsWith(appBase, url) || beginsWith(appBaseNoFile, url); - var withoutHashUrl = withoutBaseUrl.charAt(0) == '#' - ? beginsWith(hashPrefix, withoutBaseUrl) - : (this.$$html5) - ? withoutBaseUrl - : ''; + var withoutHashUrl; - if (!isString(withoutHashUrl)) { - throw $locationMinErr('ihshprfx', 'Invalid url "{0}", missing hash prefix "{1}".', url, - hashPrefix); + if (withoutBaseUrl.charAt(0) === '#') { + + // The rest of the url starts with a hash so we have + // got either a hashbang path or a plain hash fragment + withoutHashUrl = beginsWith(hashPrefix, withoutBaseUrl); + if (isUndefined(withoutHashUrl)) { + // There was no hashbang prefix so we just have a hash fragment + withoutHashUrl = withoutBaseUrl; + } + + } else { + // There was no hashbang path nor hash fragment: + // If we are in HTML5 mode we use what is left as the path; + // Otherwise we ignore what is left + withoutHashUrl = this.$$html5 ? withoutBaseUrl : ''; } + parseAppUrl(withoutHashUrl, this); this.$$path = removeWindowsDriveName(this.$$path, withoutHashUrl, appBase); diff --git a/test/ng/locationSpec.js b/test/ng/locationSpec.js index f99dd399..1ba12190 100644 --- a/test/ng/locationSpec.js +++ b/test/ng/locationSpec.js @@ -554,10 +554,24 @@ describe('$location', function() { }); - it('should throw error when invalid hashbang prefix given', function() { - expect(function() { - url.$$parse('http://www.server.org:1234/base#/path'); - }).toThrowMinErr('$location', 'ihshprfx', 'Invalid url "http://www.server.org:1234/base#/path", missing hash prefix "#!".'); + it('should insert default hashbang if a hash is given with no hashbang prefix', function() { + + url.$$parse('http://www.server.org:1234/base#/path'); + expect(url.absUrl()).toBe('http://www.server.org:1234/base#!#%2Fpath'); + expect(url.hash()).toBe('/path'); + expect(url.path()).toBe(''); + + url.$$parse('http://www.server.org:1234/base#'); + expect(url.absUrl()).toBe('http://www.server.org:1234/base'); + expect(url.hash()).toBe(''); + expect(url.path()).toBe(''); + }); + + it('should ignore extra path segments if no hashbang is given', function() { + url.$$parse('http://www.server.org:1234/base/extra/path'); + expect(url.absUrl()).toBe('http://www.server.org:1234/base'); + expect(url.path()).toBe(''); + expect(url.hash()).toBe(''); });