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

fix($location): search should expand array values properly #3010

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
25 changes: 21 additions & 4 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,21 +826,38 @@ function startingTag(element) {
* @returns Object.<(string|boolean)>
*/
function parseKeyValue(/**string*/keyValue) {
var obj = {}, key_value, key;
var obj = {}, key_value, key, value;
forEach((keyValue || "").split('&'), function(keyValue){
if (keyValue) {
key_value = keyValue.split('=');
key = decodeURIComponent(key_value[0]);
obj[key] = isDefined(key_value[1]) ? decodeURIComponent(key_value[1]) : true;
value = isDefined(key_value[1]) ? decodeURIComponent(key_value[1]) : true;
if (isUndefined(obj[key])) {
obj[key] = value;
}
else if (isArray(obj[key])) {
obj[key].push(value);
}
else {
obj[key] = [obj[key], value];
}
}
});
return obj;
}

function toKeyValue(obj) {
var parts = [];
var parts = [], key;
forEach(obj, function(value, key) {
parts.push(encodeUriQuery(key, true) + (value === true ? '' : '=' + encodeUriQuery(value, true)));
key = encodeUriQuery(key, true);
if (isArray(value)) {
forEach(value, function(v) {
parts.push(key + (v === true ? '' : '=' + encodeUriQuery(v, true)));
});
}
else {
parts.push(key + (value === true ? '' : '=' + encodeUriQuery(value, true)));
}
});
return parts.length ? parts.join('&') : '';
}
Expand Down
24 changes: 24 additions & 0 deletions test/ng/locationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ describe('$location', function() {
});


it('search() should accept array of params in string', function () {
url.search('x=a&x=b&z');

expect(url.search()).toEqual({x: ['a', 'b'], z: true});
expect(url.absUrl()).toBe('http://www.domain.com:9877/path/b?x=a&x=b&z#hash');
});


it('search() should accept array of params in object', function () {
url.search({one: ['foo', 'bar'], two: true});

expect(url.search()).toEqual({one: ['foo', 'bar'], two: true});
expect(url.absUrl()).toBe('http://www.domain.com:9877/path/b?one=foo&one=bar&two#hash');
});


it('search() should change single parameter to array of params', function() {
url.search({id: 'old', preserved: true});
url.search('id', ['foo', 'bar']);

expect(url.search()).toEqual({id: ['foo', 'bar'], preserved: true});
});


it('hash() should change hash fragment', function() {
url.hash('new-hash');
expect(url.hash()).toBe('new-hash');
Expand Down