Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/ng/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,13 @@ function Browser(window, document, $log, $sniffer) {
* - cookies() -> hash of all cookies, this is NOT a copy of the internal state, so do not modify
* it
* - cookies(name, value) -> set name to value, if value is undefined delete the cookie
* - cookies(name, value, expires) -> set name to value, and the expire date to expires
* - cookies(name) -> the same as (name, undefined) == DELETES (no one calls it right now that
* way)
*
* @returns {Object} Hash of all cookies (if called without any parameter)
*/
self.cookies = function(name, value) {
self.cookies = function(name, value, expires ) {
/* global escape: false, unescape: false */
var cookieLength, cookieArray, cookie, i, index;

Expand All @@ -293,8 +294,14 @@ function Browser(window, document, $log, $sniffer) {
";expires=Thu, 01 Jan 1970 00:00:00 GMT";
} else {
if (isString(value)) {
cookieLength = (rawDocument.cookie = escape(name) + '=' + escape(value) +
';path=' + cookiePath).length + 1;
var rawCookie = escape(name) + '=' + escape(value) +
';path=' + cookiePath;

if (expires !== undefined) {
rawCookie = rawCookie + ";expires=" + expires;
}

cookieLength = (rawDocument.cookie = rawCookie).length + 1;

// per http://www.ietf.org/rfc/rfc2109.txt browser must allow at minimum:
// - 300 cookies
Expand Down
6 changes: 5 additions & 1 deletion src/ngCookies/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ angular.module('ngCookies', ['ng']).
function push() {
var name,
value,
expires,
browserCookies,
updated;

Expand All @@ -95,14 +96,17 @@ angular.module('ngCookies', ['ng']).
//update all cookies updated in $cookies
for(name in cookies) {
value = cookies[name];
if(value !== null && value !== undefined){
expires = value.expires;
}
if (!angular.isString(value)) {
if (angular.isDefined(lastCookies[name])) {
cookies[name] = lastCookies[name];
} else {
delete cookies[name];
}
} else if (value !== lastCookies[name]) {
$browser.cookies(name, value);
$browser.cookies(name, value, expires);
updated = true;
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/ng/browserSpecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,24 @@ describe('browser', function() {
});
});

describe('put via cookies(cookieName, string, expires)', function() {

it('when expire is set to tomorrow the cookie is valid', function() {
var expireDate = Date.now() + 1;
browser.cookies('foo', 'bar', expireDate);
expect(document.cookie).toMatch(/foo=bar;? ?/);
expect(browser.cookies()).toEqual({'foo':'bar'});
});

it('when expire is set to 1st jan 1970 the cookie is deleted ', function() {
document.cookie = 'foo=bar;path=/';
browser.cookies('foo', 'bar', "Thu, 01 Jan 1970 00:00:00 GMT");
expect(document.cookie).toEqual('');
expect(browser.cookies()).toEqual({});
});

});

describe('put via cookies(cookieName, string), if no <base href> ', function () {
beforeEach(function () {
fakeDocument.basePath = undefined;
Expand Down