Skip to content

Commit

Permalink
Fixing writing/removing a cookie with a name that contains characters…
Browse files Browse the repository at this point in the history
… where encodeURIComponent applies, closes carhartl#155.
  • Loading branch information
carhartl committed Feb 26, 2013
1 parent 8ebbca5 commit eb39ce5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 3 additions & 1 deletion jquery.cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
value = config.json ? JSON.stringify(value) : String(value);

return (document.cookie = [
encodeURIComponent(key), '=', config.raw ? value : encodeURIComponent(value),
config.raw ? key : encodeURIComponent(key),
'=',
config.raw ? value : encodeURIComponent(value),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
Expand Down
11 changes: 10 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ test('defaults', function () {
test('raw = true', function () {
expect(1);
$.cookie.raw = true;
strictEqual($.cookie('c', ' v').split('=')[1], ' v', 'should not encode');
strictEqual($.cookie('c[1]', 'v[1]'), 'c[1]=v[1]', 'should not encode');
$.each($.cookie(), $.removeCookie);
});

test('json = true', function () {
Expand Down Expand Up @@ -242,3 +243,11 @@ test('with options', function() {

$.cookie = originalCookie;
});

test('[] used in name', function () {
expect(1);
$.cookie.raw = true;
document.cookie = 'c[1]=foo';
$.removeCookie('c[1]');
strictEqual(document.cookie, '', 'delete the cookie');
});

0 comments on commit eb39ce5

Please sign in to comment.