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

Commit cad717b

Browse files
committed
fix($location): parse query string when path is empty in hashbang mode
Before this fix, search queries in hashbang mode were ignored if the hash was not present in the url. This patch corrects this by ensuring that the search query is available to be parsed by urlResolve when the hashbang is not present. Closes #5964
1 parent c2d447e commit cad717b

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/ng/location.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ function LocationHashbangUrl(appBase, hashPrefix) {
178178
throw $locationMinErr('ihshprfx', 'Invalid url "{0}", missing hash prefix "{1}".', url,
179179
hashPrefix);
180180
}
181+
182+
if (withoutHashUrl === '' && withoutBaseUrl.charAt(0) === '?') {
183+
withoutHashUrl = withoutBaseUrl;
184+
}
185+
181186
parseAppUrl(withoutHashUrl, this, appBase);
182187

183188
this.$$path = removeWindowsDriveName(this.$$path, withoutHashUrl, appBase);
@@ -228,10 +233,14 @@ function LocationHashbangUrl(appBase, hashPrefix) {
228233
*/
229234
this.$$compose = function() {
230235
var search = toKeyValue(this.$$search),
231-
hash = this.$$hash ? '#' + encodeUriSegment(this.$$hash) : '';
236+
hash = this.$$hash ? '#' + encodeUriSegment(this.$$hash) : '',
237+
url = '';
232238

233239
this.$$url = encodePath(this.$$path) + (search ? '?' + search : '') + hash;
234-
this.$$absUrl = appBase + (this.$$url ? hashPrefix + this.$$url : '');
240+
if (this.$$url) {
241+
url = this.$$path ? hashPrefix + this.$$url : this.$$url;
242+
}
243+
this.$$absUrl = appBase + url;
235244
};
236245

237246
this.$$rewrite = function(url) {

test/ng/locationSpec.js

+24
Original file line numberDiff line numberDiff line change
@@ -1487,6 +1487,30 @@ describe('$location', function() {
14871487
expect(location.url()).toBe('/not-starting-with-slash');
14881488
expect(location.absUrl()).toBe('http://server/pre/index.html#/not-starting-with-slash');
14891489
});
1490+
1491+
describe("search()", function() {
1492+
it("should return a populated search object for search query when path is empty", function() {
1493+
location = new LocationHashbangUrl('http://server/pre/index.html', '!');
1494+
1495+
location.$$parse('http://server/pre/?foo=1&bar=2&baz=3');
1496+
expect(location.path()).toBe('');
1497+
expect(location.absUrl()).toBe('http://server/pre/index.html?foo=1&bar=2&baz=3')
1498+
expect(location.search()).toEqual({
1499+
foo: '1',
1500+
bar: '2',
1501+
baz: '3'
1502+
});
1503+
1504+
location.$$parse('http://server/pre/index.html?foo=1&bar=2&baz=3');
1505+
expect(location.path()).toBe('');
1506+
expect(location.absUrl()).toBe('http://server/pre/index.html?foo=1&bar=2&baz=3')
1507+
expect(location.search()).toEqual({
1508+
foo: '1',
1509+
bar: '2',
1510+
baz: '3'
1511+
});
1512+
});
1513+
});
14901514
});
14911515

14921516

0 commit comments

Comments
 (0)