Skip to content

Commit eb39ce5

Browse files
committed
Fixing writing/removing a cookie with a name that contains characters where encodeURIComponent applies, closes #155.
1 parent 8ebbca5 commit eb39ce5

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎jquery.cookie.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@
4949
value = config.json ? JSON.stringify(value) : String(value);
5050

5151
return (document.cookie = [
52-
encodeURIComponent(key), '=', config.raw ? value : encodeURIComponent(value),
52+
config.raw ? key : encodeURIComponent(key),
53+
'=',
54+
config.raw ? value : encodeURIComponent(value),
5355
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
5456
options.path ? '; path=' + options.path : '',
5557
options.domain ? '; domain=' + options.domain : '',

‎test/tests.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ test('defaults', function () {
187187
test('raw = true', function () {
188188
expect(1);
189189
$.cookie.raw = true;
190-
strictEqual($.cookie('c', ' v').split('=')[1], ' v', 'should not encode');
190+
strictEqual($.cookie('c[1]', 'v[1]'), 'c[1]=v[1]', 'should not encode');
191+
$.each($.cookie(), $.removeCookie);
191192
});
192193

193194
test('json = true', function () {
@@ -242,3 +243,11 @@ test('with options', function() {
242243

243244
$.cookie = originalCookie;
244245
});
246+
247+
test('[] used in name', function () {
248+
expect(1);
249+
$.cookie.raw = true;
250+
document.cookie = 'c[1]=foo';
251+
$.removeCookie('c[1]');
252+
strictEqual(document.cookie, '', 'delete the cookie');
253+
});

0 commit comments

Comments
 (0)