Skip to content

Commit 2754526

Browse files
Faraz SherwaniFagnerMartinsBrack
Faraz Sherwani
authored andcommitted
Do not remove quotes for the getJSON() API
Closes js-cookiegh-364. Closes js-cookiegh-363.
1 parent 976c5ce commit 2754526

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/js.cookie.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
var parts = cookies[i].split('=');
110110
var cookie = parts.slice(1).join('=');
111111

112-
if (cookie.charAt(0) === '"') {
112+
if (!this.json && cookie.charAt(0) === '"') {
113113
cookie = cookie.slice(1, -1);
114114
}
115115

test/tests.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,19 @@ QUnit.test('Call to read all cookies with mixed json', function (assert) {
451451
assert.deepEqual(Cookies.get(), { c: '{"foo":"bar"}', c2: 'v' }, 'returns unparsed cookies');
452452
});
453453

454+
QUnit.test('Cookies with escaped quotes in json using raw converters', function (assert) {
455+
Cookies.withConverter({
456+
read: function (value) {
457+
return value;
458+
},
459+
write: function (value) {
460+
return value;
461+
}
462+
}).set('c', '"{ \\"foo\\": \\"bar\\" }"');
463+
assert.strictEqual(Cookies.getJSON('c'), '{ "foo": "bar" }', 'returns JSON parsed cookie');
464+
assert.strictEqual(Cookies.get('c'), '{ \\"foo\\": \\"bar\\" }', 'returns unparsed cookie with enclosing quotes removed');
465+
});
466+
454467
QUnit.module('noConflict', lifecycle);
455468

456469
QUnit.test('do not conflict with existent globals', function (assert) {

0 commit comments

Comments
 (0)