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

fix(ngCookie): convert non-string values to string #6220

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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