diff options
| author | Caitlin Potter | 2014-03-18 13:13:08 -0400 |
|---|---|---|
| committer | Caitlin Potter | 2014-03-18 22:50:17 -0400 |
| commit | 3652831084c3788f786046b907a7361d2e89c520 (patch) | |
| tree | 0f40157a58949b07715ee1bc2a0706bd9d0c0d57 | |
| parent | bc42950b514b60f319812eeb87aae2915e394237 (diff) | |
| download | angular.js-3652831084c3788f786046b907a7361d2e89c520.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.js | 10 | ||||
| -rw-r--r-- | test/ngCookies/cookiesSpec.js | 18 |
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' + }); })); |
