From 047d82fda5eac06b53c89efc94fdd44aec180a67 Mon Sep 17 00:00:00 2001 From: Klaus Hartl Date: Fri, 29 Mar 2013 18:07:28 +0100 Subject: [PATCH] Do not alter options object in $.removeCookie (consider that a reference may have been passed), closes #175 --- jquery.cookie.js | 3 ++- test/tests.js | 13 +++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/jquery.cookie.js b/jquery.cookie.js index aebaa84..c4f99af 100644 --- a/jquery.cookie.js +++ b/jquery.cookie.js @@ -85,7 +85,8 @@ $.removeCookie = function (key, options) { if ($.cookie(key) !== undefined) { - $.cookie(key, '', $.extend(options, { expires: -1 })); + // Must not alter options, thus extending a fresh object... + $.cookie(key, '', $.extend({}, options, { expires: -1 })); return true; } return false; diff --git a/test/tests.js b/test/tests.js index 0757ae6..f83e192 100644 --- a/test/tests.js +++ b/test/tests.js @@ -84,7 +84,7 @@ test('not existing with json = true', function () { test('invalid JSON string with json = true', function () { expect(1); - + if ('JSON' in window) { $.cookie.json = true; @@ -121,7 +121,7 @@ test('call without arguments', function() { foo: 'bar' }, 'should return all cookies'); $.each($.cookie(), $.removeCookie); - + $.cookie.json = true; $.cookie('c', { foo: 'bar' }); deepEqual($.cookie(), { @@ -244,6 +244,15 @@ test('with options', function() { $.cookie = originalCookie; }); +test('passing options reference', function() { + expect(1); + var options = { path: '/' }; + $.cookie('c', 'v', options); + + $.removeCookie('c', options); + deepEqual(options, { path: '/' }, "won't alter options object"); +}); + test('[] used in name', function () { expect(1); $.cookie.raw = true;