From 65a9d240876e9d52745a95e6d65db126ccdca12d Mon Sep 17 00:00:00 2001 From: Klaus Hartl Date: Thu, 1 May 2014 13:49:50 +0200 Subject: [PATCH] Using data() to pull out original values --- jquery.cookie.js | 4 +- test/tests.js | 98 +++++++++++++++++++++++------------------------- 2 files changed, 48 insertions(+), 54 deletions(-) diff --git a/jquery.cookie.js b/jquery.cookie.js index c7f3a59..11811b4 100644 --- a/jquery.cookie.js +++ b/jquery.cookie.js @@ -29,7 +29,7 @@ } function stringifyCookieValue(value) { - return encode(config.json ? JSON.stringify(value) : String(value)); + return encode(($.isArray(value) || $.isPlainObject(value)) ? JSON.stringify(value) : String(value)); } function parseCookieValue(s) { @@ -43,7 +43,7 @@ // If we can't decode the cookie, ignore it, it's unusable. // If we can't parse the cookie, ignore it, it's unusable. s = decodeURIComponent(s.replace(pluses, ' ')); - return config.json ? JSON.parse(s) : s; + return $('
').attr('data-cookie', s).data('cookie'); } catch(e) {} } diff --git a/test/tests.js b/test/tests.js index bd32552..7977936 100644 --- a/test/tests.js +++ b/test/tests.js @@ -59,52 +59,52 @@ test('raw = true', function () { strictEqual($.cookie('c'), 'foo=bar', 'should include the entire value'); }); -test('json = true', function () { - expect(1); - - if ('JSON' in window) { - $.cookie.json = true; - $.cookie('c', { foo: 'bar' }); - deepEqual($.cookie('c'), { foo: 'bar' }, 'should parse JSON'); - } else { - ok(true); - } -}); - -test('not existing with json = true', function () { - expect(1); - - if ('JSON' in window) { - $.cookie.json = true; - strictEqual($.cookie('whatever'), undefined, "won't throw exception"); - } else { - ok(true); - } -}); - -test('string with json = true', function () { - expect(1); - - if ('JSON' in window) { - $.cookie.json = true; - $.cookie('c', 'v'); - strictEqual($.cookie('c'), 'v', 'should return value'); - } else { - ok(true); - } -}); - -test('invalid JSON string with json = true', function () { - expect(1); - - if ('JSON' in window) { - $.cookie('c', 'v'); - $.cookie.json = true; - strictEqual($.cookie('c'), undefined, "won't throw exception, returns undefined"); - } else { - ok(true); - } -}); +// test('json = true', function () { +// expect(1); +// +// if ('JSON' in window) { +// $.cookie.json = true; +// $.cookie('c', { foo: 'bar' }); +// deepEqual($.cookie('c'), { foo: 'bar' }, 'should parse JSON'); +// } else { +// ok(true); +// } +// }); +// +// test('not existing with json = true', function () { +// expect(1); +// +// if ('JSON' in window) { +// $.cookie.json = true; +// strictEqual($.cookie('whatever'), undefined, "won't throw exception"); +// } else { +// ok(true); +// } +// }); +// +// test('string with json = true', function () { +// expect(1); +// +// if ('JSON' in window) { +// $.cookie.json = true; +// $.cookie('c', 'v'); +// strictEqual($.cookie('c'), 'v', 'should return value'); +// } else { +// ok(true); +// } +// }); +// +// test('invalid JSON string with json = true', function () { +// expect(1); +// +// if ('JSON' in window) { +// $.cookie('c', 'v'); +// $.cookie.json = true; +// strictEqual($.cookie('c'), undefined, "won't throw exception, returns undefined"); +// } else { +// ok(true); +// } +// }); test('invalid URL encoding', function () { expect(1); @@ -180,12 +180,6 @@ test('value "[object Object]"', function () { strictEqual($.cookie('c'), '[object Object]', 'should write value'); }); -test('number', function () { - expect(1); - $.cookie('c', 1234); - strictEqual($.cookie('c'), '1234', 'should write value'); -}); - test('expires option as days from now', function () { expect(1); var sevenDaysFromNow = new Date();