Skip to content

Commit 2a4ee3a

Browse files
Fix a bug found when one creates a cookie with a quote
1 parent 9264f2a commit 2a4ee3a

2 files changed

Lines changed: 31 additions & 23 deletions

File tree

‎src/js.cookie.js‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@
2727
.replace(/%22/g, '"');
2828
}
2929

30-
function parseCookieValue(s) {
31-
var value = s;
32-
if (s.indexOf('"') === 0) {
30+
function parseCookieValue(value) {
31+
if (value.indexOf('"') === 0) {
3332
// This is a quoted cookie as according to RFC2068, unescape...
34-
value = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
33+
value = value.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
3534
}
3635

36+
value = decode(value);
37+
3738
try {
38-
return api.json ? JSON.parse(s) : value;
39+
return api.json ? JSON.parse(value) : value;
3940
} catch(e) {}
4041
}
4142

@@ -95,7 +96,7 @@
9596
for (; i < l; i++) {
9697
var parts = cookies[i].split('='),
9798
name = parts.shift(),
98-
cookie = decode(parts.join('='));
99+
cookie = parts.join('=');
99100

100101
if (key === name) {
101102
// If second argument (value) is a function it's a converter...

‎test/tests.js‎

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,26 @@ test('Call to read all with json: true', function () {
172172
deepEqual(Cookies.get(), { c: { foo: 'bar' } }, 'returns JSON parsed cookies');
173173
});
174174

175+
//github.com/carhartl/jquery-cookie/pull/62
176+
test('provide a way for decoding PHP whitespace encoding', function () {
177+
expect(1);
178+
document.cookie = 'c=foo+bar';
179+
var actual = Cookies.get('c', function (value) {
180+
return value.replace(/\+/g, ' ');
181+
});
182+
strictEqual(actual, 'foo bar', 'should convert pluses back to space');
183+
});
184+
185+
// github.com/carhartl/jquery-cookie/pull/166
186+
test('provide a way for decoding chinese characters', function () {
187+
expect(1);
188+
document.cookie = 'c=%u5317%u4eac';
189+
var actual = Cookies.get('c', function (value) {
190+
return unescape(value);
191+
});
192+
strictEqual(actual, '北京', 'should convert chinese characters correctly');
193+
});
194+
175195
module('write', lifecycle);
176196

177197
test('String primitive', function () {
@@ -262,24 +282,11 @@ test('json = true', function () {
262282
}
263283
});
264284

265-
// github.com/carhartl/jquery-cookie/pull/62
266-
test('provide a way for decoding PHP whitespace encoding', function () {
285+
test('Quote as the first character in the cookie value', function () {
267286
expect(1);
268-
document.cookie = 'c=foo+bar';
269-
var actual = Cookies.get('c', function (value) {
270-
return value.replace(/\+/g, ' ');
271-
});
272-
strictEqual(actual, 'foo bar', 'should convert pluses back to space');
273-
});
274-
275-
// github.com/carhartl/jquery-cookie/pull/166
276-
test('provide a way for decoding chinese characters', function () {
277-
expect(1);
278-
document.cookie = 'c=%u5317%u4eac';
279-
var actual = Cookies.get('c', function (value) {
280-
return unescape(value);
281-
});
282-
strictEqual(actual, '北京', 'should convert chinese characters correctly');
287+
Cookies.set('c', '"');
288+
var actual = Cookies.get('c');
289+
strictEqual(actual, '"', 'should read the quote value');
283290
});
284291

285292
module('removeCookie', lifecycle);

0 commit comments

Comments
 (0)