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

Commit 93d1c95

Browse files
caitptbosch
authored andcommittedMar 21, 2014
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
1 parent 01a34f5 commit 93d1c95

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed
 

‎src/ngCookies/cookies.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,10 @@ angular.module('ngCookies', ['ng']).
9595
for(name in cookies) {
9696
value = cookies[name];
9797
if (!angular.isString(value)) {
98-
if (angular.isDefined(lastCookies[name])) {
99-
cookies[name] = lastCookies[name];
100-
} else {
101-
delete cookies[name];
102-
}
103-
} else if (value !== lastCookies[name]) {
98+
value = '' + value;
99+
cookies[name] = value;
100+
}
101+
if (value !== lastCookies[name]) {
104102
$browser.cookies(name, value);
105103
updated = true;
106104
}

‎test/ngCookies/cookiesSpec.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,25 @@ describe('$cookies', function() {
4545
}));
4646

4747

48-
it('should drop or reset any cookie that was set to a non-string value',
48+
it('should convert non-string values to string',
4949
inject(function($cookies, $browser, $rootScope) {
5050
$cookies.nonString = [1, 2, 3];
5151
$cookies.nullVal = null;
5252
$cookies.undefVal = undefined;
53-
$cookies.preexisting = function() {};
53+
var preexisting = $cookies.preexisting = function() {};
5454
$rootScope.$digest();
55-
expect($browser.cookies()).toEqual({'preexisting': 'oldCookie'});
56-
expect($cookies).toEqual({'preexisting': 'oldCookie'});
55+
expect($browser.cookies()).toEqual({
56+
'preexisting': '' + preexisting,
57+
'nonString': '1,2,3',
58+
'nullVal': 'null',
59+
'undefVal': 'undefined'
60+
});
61+
expect($cookies).toEqual({
62+
'preexisting': '' + preexisting,
63+
'nonString': '1,2,3',
64+
'nullVal': 'null',
65+
'undefVal': 'undefined'
66+
});
5767
}));
5868

5969

0 commit comments

Comments
 (0)
This repository has been archived.