Skip to content

Commit 6818b83

Browse files
author
carhartl
committed
introduced "raw" option as to whether encode-/decodeURIComponent cookie value or not... (there was an issues when encoded value is read server-side)
1 parent 593636a commit 6818b83

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

‎jquery.cookie.js‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*jslint browser: true */ /*global jQuery: true */
22

33
/**
4-
* Cookie plugin
4+
* jQuery Cookie plugin
55
*
66
* Copyright (c) 2010 Klaus Hartl (stilbuero.de)
77
* Dual licensed under the MIT and GPL licenses:
@@ -60,7 +60,7 @@
6060
jQuery.cookie = function (key, value, options) {
6161

6262
// key and value given, set cookie...
63-
if (arguments.length > 1) {
63+
if (arguments.length > 1 && (value === null || typeof value !== "object")) {
6464
options = jQuery.extend({}, options);
6565

6666
if (value === null) {
@@ -72,15 +72,18 @@ jQuery.cookie = function (key, value, options) {
7272
t.setDate(t.getDate() + days);
7373
}
7474

75-
return (document.cookie = [key, '=', encodeURIComponent(String(value)),
75+
return (document.cookie = [
76+
encodeURIComponent(key), '=',
77+
options.raw ? String(value) : encodeURIComponent(String(value)),
7678
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
7779
options.path ? '; path=' + options.path : '',
7880
options.domain ? '; domain=' + options.domain : '',
7981
options.secure ? '; secure' : ''
8082
].join(''));
8183
}
8284

83-
// get cookie...
84-
var result;
85-
return (result = new RegExp('(?:^|; )' + key + '=([^;]*)').exec(document.cookie)) ? decodeURIComponent(result[1]) : null;
85+
// key and possibly options given, get cookie...
86+
options = value || {};
87+
var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
88+
return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
8689
};

‎test/test.js‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
11
var before = {
22
setup: function () {
33
document.cookie = 'c=; expires=' + new Date(0).toUTCString();
4+
document.cookie = encodeURIComponent(' foo ') + '=; expires=' + new Date(0).toUTCString();
5+
document.cookie = 'raw=; expires=' + new Date(0).toUTCString();
46
}
57
};
68

79
module('read', before);
810

911
test('Read', function () {
10-
expect(3);
12+
expect(5);
1113

1214
equals($.cookie('c'), null);
1315
document.cookie = 'c=v';
1416
equals($.cookie('c'), 'v', 'Read cookie value');
17+
18+
document.cookie = encodeURIComponent(' foo ') + '=' + encodeURIComponent(' bar ');
19+
equals($.cookie(' foo '), ' bar ', 'Decode key/value');
20+
21+
document.cookie = 'raw=%20whatever';
22+
equals($.cookie('raw', { raw: true }), '%20whatever', 'Do not decode with raw set true');
23+
1524
equals($.cookie('whatever'), null, 'Give null if cookie does not exist');
1625
});
1726

1827
module('Write', before);
1928

2029
test('Write', function () {
21-
expect(3);
30+
expect(4);
2231

2332
equals($.cookie('c'), null);
2433
var cookie = $.cookie('c', 'v');
2534
equals(document.cookie, 'c=v', 'Write cookie value');
26-
equals(cookie, 'c=v', 'Return cookie');
35+
equals(cookie, 'c=v', 'Return cookie string');
36+
equals($.cookie('raw', ' whatever', { raw: true }).split('=')[1], ' whatever', 'Do not encode with raw set true');
2737
});
2838

2939
module('Delete', before);

0 commit comments

Comments
 (0)