Update Cookies.js

pull/630/head
Thibaut Courouble 8 years ago
parent b626a9d574
commit 1d792ff7cd

@ -1,141 +1,172 @@
/*! /*
* Cookies.js - 0.3.1 * Cookies.js - 1.2.3
* Wednesday, April 24 2013 @ 2:28 AM EST * https://github.com/ScottHamper/Cookies
* *
* Copyright (c) 2013, Scott Hamper * This is free and unencumbered software released into the public domain.
* Licensed under the MIT license,
* http://www.opensource.org/licenses/MIT
*/ */
(function (undefined) { (function (global, undefined) {
'use strict'; 'use strict';
var Cookies = function (key, value, options) { var factory = function (window) {
return arguments.length === 1 ? if (typeof window.document !== 'object') {
Cookies.get(key) : Cookies.set(key, value, options); throw new Error('Cookies.js requires a `window` with a `document` object');
}; }
// Allows for setter injection in unit tests var Cookies = function (key, value, options) {
Cookies._document = document; return arguments.length === 1 ?
Cookies._navigator = navigator; Cookies.get(key) : Cookies.set(key, value, options);
};
Cookies.defaults = { // Allows for setter injection in unit tests
path: '/' Cookies._document = window.document;
};
Cookies.get = function (key) { // Used to ensure cookie keys do not collide with
if (Cookies._cachedDocumentCookie !== Cookies._document.cookie) { // built-in `Object` properties
Cookies._renewCache(); Cookies._cacheKeyPrefix = 'cookey.'; // Hurr hurr, :)
}
return Cookies._cache[key]; Cookies._maxExpireDate = new Date('Fri, 31 Dec 9999 23:59:59 UTC');
};
Cookies.set = function (key, value, options) { Cookies.defaults = {
options = Cookies._getExtendedOptions(options); path: '/',
options.expires = Cookies._getExpiresDate(value === undefined ? -1 : options.expires); secure: false
};
Cookies._document.cookie = Cookies._generateCookieString(key, value, options); Cookies.get = function (key) {
if (Cookies._cachedDocumentCookie !== Cookies._document.cookie) {
Cookies._renewCache();
}
return Cookies; var value = Cookies._cache[Cookies._cacheKeyPrefix + key];
};
Cookies.expire = function (key, options) { return value === undefined ? undefined : decodeURIComponent(value);
return Cookies.set(key, undefined, options); };
};
Cookies.set = function (key, value, options) {
options = Cookies._getExtendedOptions(options);
options.expires = Cookies._getExpiresDate(value === undefined ? -1 : options.expires);
Cookies._document.cookie = Cookies._generateCookieString(key, value, options);
Cookies._getExtendedOptions = function (options) { return Cookies;
return {
path: options && options.path || Cookies.defaults.path,
domain: options && options.domain || Cookies.defaults.domain,
expires: options && options.expires || Cookies.defaults.expires,
secure: options && options.secure !== undefined ? options.secure : Cookies.defaults.secure
}; };
};
Cookies._isValidDate = function (date) { Cookies.expire = function (key, options) {
return Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime()); return Cookies.set(key, undefined, options);
}; };
Cookies._getExpiresDate = function (expires, now) { Cookies._getExtendedOptions = function (options) {
now = now || new Date(); return {
switch (typeof expires) { path: options && options.path || Cookies.defaults.path,
case 'number': expires = new Date(now.getTime() + expires * 1000); break; domain: options && options.domain || Cookies.defaults.domain,
case 'string': expires = new Date(expires); break; expires: options && options.expires || Cookies.defaults.expires,
} secure: options && options.secure !== undefined ? options.secure : Cookies.defaults.secure
};
};
if (expires && !Cookies._isValidDate(expires)) { Cookies._isValidDate = function (date) {
throw new Error('`expires` parameter cannot be converted to a valid Date instance'); return Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime());
} };
return expires; Cookies._getExpiresDate = function (expires, now) {
}; now = now || new Date();
Cookies._generateCookieString = function (key, value, options) { if (typeof expires === 'number') {
key = encodeURIComponent(key); expires = expires === Infinity ?
value = (value + '').replace(/[^!#$&-+\--:<-\[\]-~]/g, encodeURIComponent); Cookies._maxExpireDate : new Date(now.getTime() + expires * 1000);
options = options || {}; } else if (typeof expires === 'string') {
expires = new Date(expires);
}
var cookieString = key + '=' + value; if (expires && !Cookies._isValidDate(expires)) {
cookieString += options.path ? ';path=' + options.path : ''; throw new Error('`expires` parameter cannot be converted to a valid Date instance');
cookieString += options.domain ? ';domain=' + options.domain : ''; }
cookieString += options.expires ? ';expires=' + options.expires.toGMTString() : '';
cookieString += options.secure ? ';secure' : '';
return cookieString; return expires;
}; };
Cookies._getCookieObjectFromString = function (documentCookie) { Cookies._generateCookieString = function (key, value, options) {
var cookieObject = {}; key = key.replace(/[^#$&+\^`|]/g, encodeURIComponent);
var cookiesArray = documentCookie ? documentCookie.split('; ') : []; key = key.replace(/\(/g, '%28').replace(/\)/g, '%29');
value = (value + '').replace(/[^!#$&-+\--:<-\[\]-~]/g, encodeURIComponent);
options = options || {};
for (var i = 0; i < cookiesArray.length; i++) { var cookieString = key + '=' + value;
var cookieKvp = Cookies._getKeyValuePairFromCookieString(cookiesArray[i]); cookieString += options.path ? ';path=' + options.path : '';
cookieString += options.domain ? ';domain=' + options.domain : '';
cookieString += options.expires ? ';expires=' + options.expires.toUTCString() : '';
cookieString += options.secure ? ';secure' : '';
if (cookieObject[cookieKvp.key] === undefined) { return cookieString;
cookieObject[cookieKvp.key] = cookieKvp.value; };
Cookies._getCacheFromString = function (documentCookie) {
var cookieCache = {};
var cookiesArray = documentCookie ? documentCookie.split('; ') : [];
for (var i = 0; i < cookiesArray.length; i++) {
var cookieKvp = Cookies._getKeyValuePairFromCookieString(cookiesArray[i]);
if (cookieCache[Cookies._cacheKeyPrefix + cookieKvp.key] === undefined) {
cookieCache[Cookies._cacheKeyPrefix + cookieKvp.key] = cookieKvp.value;
}
} }
}
return cookieObject; return cookieCache;
}; };
Cookies._getKeyValuePairFromCookieString = function (cookieString) { Cookies._getKeyValuePairFromCookieString = function (cookieString) {
// "=" is a valid character in a cookie value according to RFC6265, so cannot `split('=')` // "=" is a valid character in a cookie value according to RFC6265, so cannot `split('=')`
var separatorIndex = cookieString.indexOf('='); var separatorIndex = cookieString.indexOf('=');
// IE omits the "=" when the cookie value is an empty string
separatorIndex = separatorIndex < 0 ? cookieString.length : separatorIndex;
var key = cookieString.substr(0, separatorIndex);
var decodedKey;
try {
decodedKey = decodeURIComponent(key);
} catch (e) {
if (console && typeof console.error === 'function') {
console.error('Could not decode cookie with key "' + key + '"', e);
}
}
// IE omits the "=" when the cookie value is an empty string return {
separatorIndex = separatorIndex < 0 ? cookieString.length : separatorIndex; key: decodedKey,
value: cookieString.substr(separatorIndex + 1) // Defer decoding value until accessed
};
};
return { Cookies._renewCache = function () {
key: decodeURIComponent(cookieString.substr(0, separatorIndex)), Cookies._cache = Cookies._getCacheFromString(Cookies._document.cookie);
value: decodeURIComponent(cookieString.substr(separatorIndex + 1)) Cookies._cachedDocumentCookie = Cookies._document.cookie;
}; };
};
Cookies._renewCache = function () { Cookies._areEnabled = function () {
Cookies._cache = Cookies._getCookieObjectFromString(Cookies._document.cookie); var testKey = 'cookies.js';
Cookies._cachedDocumentCookie = Cookies._document.cookie; var areEnabled = Cookies.set(testKey, 1).get(testKey) === '1';
}; Cookies.expire(testKey);
return areEnabled;
};
Cookies._areEnabled = function () { Cookies.enabled = Cookies._areEnabled();
return Cookies._navigator.cookieEnabled ||
Cookies.set('cookies.js', 1).get('cookies.js') === '1';
};
Cookies.enabled = Cookies._areEnabled(); return Cookies;
};
var cookiesExport = (global && typeof global.document === 'object') ? factory(global) : factory;
// AMD support // AMD support
if (typeof define === 'function' && define.amd) { if (typeof define === 'function' && define.amd) {
define(function () { return Cookies; }); define(function () { return cookiesExport; });
// CommonJS and Node.js module support. // CommonJS/Node.js support
} else if (typeof exports !== 'undefined') { } else if (typeof exports === 'object') {
// Support Node.js specific `module.exports` (which can be a function) // Support Node.js specific `module.exports` (which can be a function)
if (typeof module !== 'undefined' && module.exports) { if (typeof module === 'object' && typeof module.exports === 'object') {
exports = module.exports = Cookies; exports = module.exports = cookiesExport;
} }
// But always support CommonJS module 1.1.1 spec (`exports` cannot be a function) // But always support CommonJS module 1.1.1 spec (`exports` cannot be a function)
exports.Cookies = Cookies; exports.Cookies = cookiesExport;
} else { } else {
window.Cookies = Cookies; global.Cookies = cookiesExport;
} }
})(); })(typeof window === 'undefined' ? this : window);

Loading…
Cancel
Save