Skip to content

Commit

Permalink
fix($location): parse query parameters which precede hash in hashbang…
Browse files Browse the repository at this point in the history
… mode

This fix causes LocationHashbangUrl#$$rewrite() to move a pre-hash query string
to after the hash.

This, in turn, causes urlResolve() to correctly parse the query string, and
report the correct dictionary.

Closes angular#7239
  • Loading branch information
caitp committed Apr 25, 2014
1 parent 737ef25 commit 24c46c5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/ng/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@ function LocationHashbangUrl(appBase, hashPrefix) {
*/
this.$$parse = function(url) {
var withoutBaseUrl = beginsWith(appBase, url) || beginsWith(appBaseNoFile, url);
var tmp = beginsWith(hashPrefix, withoutBaseUrl);
var withoutHashUrl = withoutBaseUrl.charAt(0) == '#'
? beginsWith(hashPrefix, withoutBaseUrl)
? tmp || (isUndefined(tmp) ? (withoutBaseUrl[1] === '?' && withoutBaseUrl.slice(1)) : '')
: (this.$$html5)
? withoutBaseUrl
: '';
Expand Down Expand Up @@ -235,7 +236,26 @@ function LocationHashbangUrl(appBase, hashPrefix) {
};

this.$$rewrite = function(url) {
if(stripHash(appBase) == stripHash(url)) {
var stripHashBase = stripHash(appBase);

// In HashbangMode, occasionally a query can be found preceeding the hash,
// however urlResolve only parses it as a query if it occurs after the hash.
// So, during the rewrite, it gets repositioned after the hash, if necessary.
var indexOfQuery = url.indexOf('?', stripHashBase.length),
indexOfHash = url.indexOf('#', stripHashBase.length);

if (indexOfQuery >= 0 && indexOfHash >= 0 && indexOfQuery < indexOfHash) {
var preQuery = url.substr(indexOfQuery, indexOfHash - indexOfQuery);
var postQuery = url.indexOf('?', indexOfHash);
url = url.replace(preQuery, '');
if (postQuery >= 0) {
url += '&' + preQuery.slice(1);
} else {
url += preQuery;
}
}

if(stripHashBase == stripHash(url)) {
return url;
}
};
Expand Down
20 changes: 20 additions & 0 deletions test/ng/locationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,26 @@ describe('$location', function() {
});


it('search() should parse query parameters preceding hash prefix', function() {
url = new LocationHashbangUrl('http://server/base', '#!');
url.$$parse(url.$$rewrite('http://server/base?a=b#!?c=d'));
expect(url.search()).toEqual({
'a': 'b',
'c': 'd'
});
});


it('search() should parse query parameters preceeding hash fragment', function() {
url = new LocationHashbangUrl('http://server/base', '#!');
url.$$parse(url.$$rewrite('http://server/base?a=b#?c=d'));
expect(url.search()).toEqual({
'a': 'b',
'c': 'd'
});
});


describe('encoding', function() {

it('should encode special characters', function() {
Expand Down

0 comments on commit 24c46c5

Please sign in to comment.