Skip to content

Commit 996cb0d

Browse files
author
Ed Epstein
committed
GitHub Issue angular#15856 -- Fix query portion parsing when not in html5 mode.
1 parent b9d2b30 commit 996cb0d

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/ng/location.js

+25
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ function stripBaseUrl(base, url) {
7070
}
7171
}
7272

73+
function findSearch(url) {
74+
var index = url.indexOf('?');
75+
return index === -1 ? false : url.substr(index + 1, url.indexOf('#') === -1 ? url.length : url.indexOf('#') - index - 1);
76+
}
7377

7478
function stripHash(url) {
7579
var index = url.indexOf('#');
@@ -206,6 +210,27 @@ function LocationHashbangUrl(appBase, appBaseNoFile, hashPrefix) {
206210
withoutHashUrl = withoutBaseUrl;
207211
}
208212

213+
// apply any search params from the actual url to the withoutHashUrl
214+
// appBase will already have the hash taken off at this point.
215+
var paramRewrittenUrl = withoutHashUrl;
216+
var search = findSearch(appBase);
217+
if (search !== false) {
218+
var indexOfAppFragment = withoutHashUrl.indexOf('#');
219+
if (indexOfAppFragment !== -1) {
220+
paramRewrittenUrl = withoutHashUrl.substr(0, indexOfAppFragment) + '?' + search + withoutHashUrl.substr(indexOfAppFragment, withoutHashUrl.length);
221+
} else {
222+
paramRewrittenUrl = withoutHashUrl + '?' + search;
223+
}
224+
// we know that there is a query param before the hash, as in the RFC.
225+
if (findSearch(withoutHashUrl) !== false) {
226+
var indexOfAppPath = withoutHashUrl.indexOf('?');
227+
if (indexOfAppPath !== -1) {
228+
paramRewrittenUrl = withoutHashUrl.substr(0, indexOfAppPath + 1) + search + '&' + withoutHashUrl.substr(indexOfAppPath + 1, withoutHashUrl.length);
229+
}
230+
}
231+
}
232+
withoutHashUrl = paramRewrittenUrl;
233+
209234
} else {
210235
// There was no hashbang path nor hash fragment:
211236
// If we are in HTML5 mode we use what is left as the path;

test/ng/locationSpec.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -527,14 +527,39 @@ describe('$location', function() {
527527
it('should preserve query params in base', function() {
528528
var locationUrl = new LocationHashbangUrl('http://www.server.org:1234/base?base=param', 'http://www.server.org:1234/', '#');
529529
locationUrl.$$parse('http://www.server.org:1234/base?base=param#/path?a=b&c#hash');
530-
expect(locationUrl.absUrl()).toBe('http://www.server.org:1234/base?base=param#/path?a=b&c#hash');
530+
expect(locationUrl.absUrl()).toBe('http://www.server.org:1234/base?base=param#/path?base=param&a=b&c#hash');
531531

532+
expect(locationUrl.search()).toEqual({base: 'param', a: 'b', c: true});
532533
locationUrl.path('/new/path');
533534
locationUrl.search({one: 1});
534535
locationUrl.hash('hhh');
535536
expect(locationUrl.absUrl()).toBe('http://www.server.org:1234/base?base=param#/new/path?one=1#hhh');
536537
});
537538

539+
it('should copy query params to fragment', function() {
540+
var locationUrl = new LocationHashbangUrl('http://www.server.org:1234/base?base=param', 'http://www.server.org:1234/', '#');
541+
locationUrl.$$parse('http://www.server.org:1234/base?base=param#/path');
542+
543+
expect(locationUrl.absUrl()).toBe('http://www.server.org:1234/base?base=param#/path?base=param');
544+
545+
expect(locationUrl.search()).toEqual({base: 'param'});
546+
locationUrl.path('/new/path');
547+
locationUrl.search({one: 1});
548+
locationUrl.hash('hhh');
549+
expect(locationUrl.absUrl()).toBe('http://www.server.org:1234/base?base=param#/new/path?one=1#hhh');
550+
551+
locationUrl.$$parse('http://www.server.org:1234/base?base=param#/path#trailingHash');
552+
553+
expect(locationUrl.absUrl()).toBe('http://www.server.org:1234/base?base=param#/path?base=param#trailingHash');
554+
555+
expect(locationUrl.search()).toEqual({base: 'param'});
556+
locationUrl.path('/new/path');
557+
locationUrl.search({one: 1});
558+
locationUrl.hash('hhh');
559+
expect(locationUrl.absUrl()).toBe('http://www.server.org:1234/base?base=param#/new/path?one=1#hhh');
560+
561+
});
562+
538563

539564
it('should prefix path with forward-slash', function() {
540565
var locationUrl = new LocationHashbangUrl('http://host.com/base', 'http://host.com/', '#');

0 commit comments

Comments
 (0)