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

fix($location): parse query string when path is empty in hashbang mode #5977

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/ng/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ function LocationHashbangUrl(appBase, hashPrefix) {
throw $locationMinErr('ihshprfx', 'Invalid url "{0}", missing hash prefix "{1}".', url,
hashPrefix);
}

if (withoutHashUrl === '' && withoutBaseUrl.charAt(0) === '?') {
withoutHashUrl = withoutBaseUrl;
}

parseAppUrl(withoutHashUrl, this, appBase);

this.$$path = removeWindowsDriveName(this.$$path, withoutHashUrl, appBase);
Expand Down Expand Up @@ -228,10 +233,14 @@ function LocationHashbangUrl(appBase, hashPrefix) {
*/
this.$$compose = function() {
var search = toKeyValue(this.$$search),
hash = this.$$hash ? '#' + encodeUriSegment(this.$$hash) : '';
hash = this.$$hash ? '#' + encodeUriSegment(this.$$hash) : '',
url = '';

this.$$url = encodePath(this.$$path) + (search ? '?' + search : '') + hash;
this.$$absUrl = appBase + (this.$$url ? hashPrefix + this.$$url : '');
if (this.$$url) {
url = this.$$path ? hashPrefix + this.$$url : this.$$url;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the weird looking absUrl()... once the change to the way path() works is merged, this would need to be updated to test that path !== '/'

}
this.$$absUrl = appBase + url;
};

this.$$rewrite = function(url) {
Expand Down
24 changes: 24 additions & 0 deletions test/ng/locationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,30 @@ describe('$location', function() {
expect(location.url()).toBe('/not-starting-with-slash');
expect(location.absUrl()).toBe('http://server/pre/index.html#/not-starting-with-slash');
});

describe("search()", function() {
it("should return a populated search object for search query when path is empty", function() {
location = new LocationHashbangUrl('http://server/pre/index.html', '!');

location.$$parse('http://server/pre/?foo=1&bar=2&baz=3');
expect(location.path()).toBe('');
expect(location.absUrl()).toBe('http://server/pre/index.html?foo=1&bar=2&baz=3')
expect(location.search()).toEqual({
foo: '1',
bar: '2',
baz: '3'
});

location.$$parse('http://server/pre/index.html?foo=1&bar=2&baz=3');
expect(location.path()).toBe('');
expect(location.absUrl()).toBe('http://server/pre/index.html?foo=1&bar=2&baz=3')
expect(location.search()).toEqual({
foo: '1',
bar: '2',
baz: '3'
});
});
});
});


Expand Down