Skip to content
This repository has been archived by the owner on Nov 15, 2017. It is now read-only.

Commit

Permalink
Really remove cookie whether it's created with an default domain or not
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoqun committed Mar 2, 2015
1 parent aeb793d commit 1e246c0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/jquery.cookie.js
Expand Up @@ -110,8 +110,26 @@
}

// Must not alter options, thus extending a fresh object...
$.cookie(key, '', $.extend({}, options, { expires: -1 }));
return !$.cookie(key);
};
options = $.extend({}, options, { expires: -1 });

$.cookie(key, '', options);
var succeeded = !$.cookie(key);

// Browsers distinguish whether the default (=current) domain
// is explicitly passed or not, so try hard to remove by the
// opposite approach.
if (!succeeded) {
if (options.domain && options.domain === location.hostname) {
delete options.domain;
$.cookie(key, '', options);
succeeded = !$.cookie(key);
} else if (!options.domain) {
options.domain = location.hostname;
$.cookie(key, '', options);
succeeded = !$.cookie(key);
}
}

return succeeded;
};
}));
26 changes: 26 additions & 0 deletions test/tests.js
Expand Up @@ -328,6 +328,32 @@ test('[] used in name', function () {
strictEqual(document.cookie, '', 'delete the cookie');
});

test('with no domain options for set and remove', function() {
expect(1);
$.cookie('c1', 'v');
strictEqual($.removeCookie('c1'), true, 'returns true');
});

test('with domain option for set and none for remove', function() {
expect(1);
var options = {domain: window.location.hostname};
$.cookie('c2', 'v', options);
strictEqual($.removeCookie('c2'), true, 'returns true');
});

test('with no domain option for set and one for remove', function() {
expect(1);
var options = {domain: window.location.hostname};
$.cookie('c3', 'v');
strictEqual($.removeCookie('c3', options), true, 'returns true');
});

test('with domain options for set and remove', function() {
expect(1);
var options = {domain: window.location.host}
$.cookie('c4', 'v', options);
strictEqual($.removeCookie('c4', options), true, 'returns true');
});

module('conversion', lifecycle);

Expand Down

0 comments on commit 1e246c0

Please sign in to comment.