aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCaitlin Potter2014-03-18 13:13:08 -0400
committerTobias Bosch2014-03-21 13:05:29 -0700
commit93d1c95c61dbfa565333bb64527a103242175af7 (patch)
tree45e6792dd6aa79dfedfbac89a5cc81189e357b5b
parent01a34f513bb567ed6d4c81d00d7c2a777c0dae01 (diff)
downloadangular.js-93d1c95c61dbfa565333bb64527a103242175af7.tar.bz2
fix(ngCookie): convert non-string values to string
Previously, non-string values stored in $cookies would be removed, without warning the user, and causing difficulty debugging. Now, the value is converted to string before being stored, and the value is not dropped. Serialization may be customized using the toString() method of an object's prototype. Closes #6151 Closes #6220
-rw-r--r--src/ngCookies/cookies.js10
-rw-r--r--test/ngCookies/cookiesSpec.js18
2 files changed, 18 insertions, 10 deletions
diff --git a/src/ngCookies/cookies.js b/src/ngCookies/cookies.js
index ccfae23b..4f97c8dd 100644
--- a/src/ngCookies/cookies.js
+++ b/src/ngCookies/cookies.js
@@ -95,12 +95,10 @@ angular.module('ngCookies', ['ng']).
for(name in cookies) {
value = cookies[name];
if (!angular.isString(value)) {
- if (angular.isDefined(lastCookies[name])) {
- cookies[name] = lastCookies[name];
- } else {
- delete cookies[name];
- }
- } else if (value !== lastCookies[name]) {
+ value = '' + value;
+ cookies[name] = value;
+ }
+ if (value !== lastCookies[name]) {
$browser.cookies(name, value);
updated = true;
}
diff --git a/test/ngCookies/cookiesSpec.js b/test/ngCookies/cookiesSpec.js
index 674c2774..1d669c1c 100644
--- a/test/ngCookies/cookiesSpec.js
+++ b/test/ngCookies/cookiesSpec.js
@@ -45,15 +45,25 @@ describe('$cookies', function() {
}));
- it('should drop or reset any cookie that was set to a non-string value',
+ it('should convert non-string values to string',
inject(function($cookies, $browser, $rootScope) {
$cookies.nonString = [1, 2, 3];
$cookies.nullVal = null;
$cookies.undefVal = undefined;
- $cookies.preexisting = function() {};
+ var preexisting = $cookies.preexisting = function() {};
$rootScope.$digest();
- expect($browser.cookies()).toEqual({'preexisting': 'oldCookie'});
- expect($cookies).toEqual({'preexisting': 'oldCookie'});
+ expect($browser.cookies()).toEqual({
+ 'preexisting': '' + preexisting,
+ 'nonString': '1,2,3',
+ 'nullVal': 'null',
+ 'undefVal': 'undefined'
+ });
+ expect($cookies).toEqual({
+ 'preexisting': '' + preexisting,
+ 'nonString': '1,2,3',
+ 'nullVal': 'null',
+ 'undefVal': 'undefined'
+ });
}));