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

Commit

Permalink
fix(ngCookie): convert non-string values to string
Browse files Browse the repository at this point in the history
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
  • Loading branch information
caitp committed Mar 19, 2014
1 parent bc42950 commit 3652831
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
10 changes: 4 additions & 6 deletions src/ngCookies/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
18 changes: 14 additions & 4 deletions test/ngCookies/cookiesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
}));


Expand Down

0 comments on commit 3652831

Please sign in to comment.