Skip to content

Commit 94557e8

Browse files
committed
Handle embedded equal signs when reading raw cookies, closes js-cookie#50, closes js-cookie#53.
1 parent e13a9b6 commit 94557e8

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

‎jquery.cookie.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@
3838
options = value || {};
3939
var decode = options.raw ? function(s) { return s; } : decodeURIComponent;
4040

41-
var pairs = document.cookie.split('; ');
42-
for (var i = 0, pair; (pair = pairs[i] && pairs[i].split('=')); i++) {
43-
if (decode(pair[0]) === key) return decode(pair[1] || ''); // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, thus pair[1] may be undefined
41+
var cookies = document.cookie.split('; ');
42+
for (var i = 0, parts; (parts = cookies[i] && cookies[i].split('=')); i++) {
43+
if (decode(parts.shift()) === key) {
44+
return decode(parts.join('='));
45+
}
4446
}
4547
return null;
4648
};

‎test.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ test('simple value', 1, function () {
1616
});
1717

1818
test('empty value', 1, function () {
19+
// IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, which
20+
// resulted in a bug while reading such a cookie.
1921
$.cookie('c', '');
2022
equal($.cookie('c'), '', 'should return value');
2123
});
@@ -34,17 +36,22 @@ test('raw: true', 1, function () {
3436
equal($.cookie('c', { raw: true }), '%20v', 'should not decode');
3537
});
3638

39+
test('embedded equals', 1, function() {
40+
$.cookie('c', 'foo=bar', { raw: true });
41+
equal($.cookie('c', { raw: true }), 'foo=bar', 'should include the entire value');
42+
});
43+
3744

3845
module('write', before);
3946

4047
test('String primitive', 1, function () {
4148
$.cookie('c', 'v');
42-
equal(document.cookie, 'c=v', 'should write value');
49+
equal($.cookie('c'), 'v', 'should write value');
4350
});
4451

4552
test('String object', 1, function () {
4653
$.cookie('c', new String('v'));
47-
equal(document.cookie, 'c=v', 'should write value');
54+
equal($.cookie('c'), 'v', 'should write value');
4855
});
4956

5057
test('value "[object Object]"', 1, function() {

0 commit comments

Comments
 (0)