Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit e4626b0

Browse files
committed
add expire to the cookies() method
1 parent 334303e commit e4626b0

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

src/ng/browser.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,14 @@ function Browser(window, document, $log, $sniffer) {
278278
* - cookies() -> hash of all cookies, this is NOT a copy of the internal state, so do not modify
279279
* it
280280
* - cookies(name, value) -> set name to value, if value is undefined delete the cookie
281+
* - cookies(name, value, expires) -> set name to value, if value is undefined delete the cookie,
282+
* the cookie will expire in the days specified in expires
281283
* - cookies(name) -> the same as (name, undefined) == DELETES (no one calls it right now that
282284
* way)
283285
*
284286
* @returns {Object} Hash of all cookies (if called without any parameter)
285287
*/
286-
self.cookies = function(name, value) {
288+
self.cookies = function(name, value, expires ) {
287289
/* global escape: false, unescape: false */
288290
var cookieLength, cookieArray, cookie, i, index;
289291

@@ -293,8 +295,15 @@ function Browser(window, document, $log, $sniffer) {
293295
";expires=Thu, 01 Jan 1970 00:00:00 GMT";
294296
} else {
295297
if (isString(value)) {
296-
cookieLength = (rawDocument.cookie = escape(name) + '=' + escape(value) +
297-
';path=' + cookiePath).length + 1;
298+
var rawCookie
299+
if (expires == undefined) {
300+
rawCookie = escape(name) + '=' + escape(value) +
301+
';path=' + cookiePath;
302+
}else{
303+
rawCookie = escape(name) + '=' + escape(value) +
304+
';path=' + cookiePath + ";expires=" + expires;
305+
}
306+
cookieLength = (rawDocument.cookie = rawCookie).length + 1;
298307

299308
// per http://www.ietf.org/rfc/rfc2109.txt browser must allow at minimum:
300309
// - 300 cookies

src/ngCookies/cookies.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ angular.module('ngCookies', ['ng']).
8282
function push() {
8383
var name,
8484
value,
85+
expires,
8586
browserCookies,
8687
updated;
8788

@@ -95,14 +96,17 @@ angular.module('ngCookies', ['ng']).
9596
//update all cookies updated in $cookies
9697
for(name in cookies) {
9798
value = cookies[name];
99+
if(value != undefined){
100+
expires = value.expires;
101+
}
98102
if (!angular.isString(value)) {
99103
if (angular.isDefined(lastCookies[name])) {
100104
cookies[name] = lastCookies[name];
101105
} else {
102106
delete cookies[name];
103107
}
104108
} else if (value !== lastCookies[name]) {
105-
$browser.cookies(name, value);
109+
$browser.cookies(name, value, expires);
106110
updated = true;
107111
}
108112
}

test/ng/browserSpecs.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,25 @@ describe('browser', function() {
279279
});
280280
});
281281

282+
describe('put via cookies(cookieName, string, expires)', function() {
283+
284+
it('when expire is set to tomorrow the cookie is valid', function() {
285+
var expireDate = Date.now() + 1;
286+
browser.cookies('foo', 'bar', expireDate);
287+
expect(document.cookie).toMatch(/foo=bar;? ?/);
288+
expect(browser.cookies()).toEqual({'foo':'bar'});
289+
});
290+
291+
it('when expire is set to 1st jan 1970 the cookie is deleted ', function() {
292+
var expireDate = "Thu, 01 Jan 1970 00:00:00 GMT";
293+
document.cookie = 'foo=bar;path=/';
294+
browser.cookies('foo', 'bar', expireDate);
295+
expect(document.cookie).toEqual('');
296+
expect(browser.cookies()).toEqual({});
297+
});
298+
299+
});
300+
282301
describe('put via cookies(cookieName, string), if no <base href> ', function () {
283302
beforeEach(function () {
284303
fakeDocument.basePath = undefined;

0 commit comments

Comments
 (0)