mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-28 21:05:43 +08:00
fix(Angular.js): handle duplicate params in parseKeyValue/toKeyValue
parseKeyValue and toKeyValue can now handle duplicate values in the query.
```
?x=1&x=2 <-> {x:[1,2]}
```
The algorithm looks like:
1)parseKeyValue looks for presence of obj[key]
2)detects and replaces obj[key] with [obj[key],val]
3)then pushes more duplicates if necessary
4)toKeyValue decodes array correctly
5)(not changed)$location.search({param: 'key'}) still replaces if necessary
6)(not changed)$location.search({param: ['key1', 'key2']}) sets the url with duplicates
BREAKING CHANGE: Before this change:
- `parseKeyValue` only took the last key overwriting all the previous keys;
- `toKeyValue` joined the keys together in a comma delimited string.
This was deemed buggy behavior. If your server relied on this behavior
then either the server should be fixed or a simple serialization of
the array should be done on the client before passing it to $location.
This commit is contained in:
committed by
Pete Bacon Darwin
parent
a258817310
commit
807394095b
@@ -850,7 +850,14 @@ function parseKeyValue(/**string*/keyValue) {
|
||||
key_value = keyValue.split('=');
|
||||
key = tryDecodeURIComponent(key_value[0]);
|
||||
if ( isDefined(key) ) {
|
||||
obj[key] = isDefined(key_value[1]) ? tryDecodeURIComponent(key_value[1]) : true;
|
||||
var val = isDefined(key_value[1]) ? tryDecodeURIComponent(key_value[1]) : true;
|
||||
if (!obj[key]) {
|
||||
obj[key] = val;
|
||||
} else if(isArray(obj[key])) {
|
||||
obj[key].push(val);
|
||||
} else {
|
||||
obj[key] = [obj[key],val];
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -860,7 +867,13 @@ function parseKeyValue(/**string*/keyValue) {
|
||||
function toKeyValue(obj) {
|
||||
var parts = [];
|
||||
forEach(obj, function(value, key) {
|
||||
if (isArray(value)) {
|
||||
forEach(value, function(arrayValue) {
|
||||
parts.push(encodeUriQuery(key, true) + (arrayValue === true ? '' : '=' + encodeUriQuery(arrayValue, true)));
|
||||
});
|
||||
} else {
|
||||
parts.push(encodeUriQuery(key, true) + (value === true ? '' : '=' + encodeUriQuery(value, true)));
|
||||
}
|
||||
});
|
||||
return parts.length ? parts.join('&') : '';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user