|
|
@ -1,25 +1,34 @@
|
|
|
|
/*!
|
|
|
|
/*
|
|
|
|
* 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 factory = function (window) {
|
|
|
|
|
|
|
|
if (typeof window.document !== 'object') {
|
|
|
|
|
|
|
|
throw new Error('Cookies.js requires a `window` with a `document` object');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var Cookies = function (key, value, options) {
|
|
|
|
var Cookies = function (key, value, options) {
|
|
|
|
return arguments.length === 1 ?
|
|
|
|
return arguments.length === 1 ?
|
|
|
|
Cookies.get(key) : Cookies.set(key, value, options);
|
|
|
|
Cookies.get(key) : Cookies.set(key, value, options);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Allows for setter injection in unit tests
|
|
|
|
// Allows for setter injection in unit tests
|
|
|
|
Cookies._document = document;
|
|
|
|
Cookies._document = window.document;
|
|
|
|
Cookies._navigator = navigator;
|
|
|
|
|
|
|
|
|
|
|
|
// Used to ensure cookie keys do not collide with
|
|
|
|
|
|
|
|
// built-in `Object` properties
|
|
|
|
|
|
|
|
Cookies._cacheKeyPrefix = 'cookey.'; // Hurr hurr, :)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Cookies._maxExpireDate = new Date('Fri, 31 Dec 9999 23:59:59 UTC');
|
|
|
|
|
|
|
|
|
|
|
|
Cookies.defaults = {
|
|
|
|
Cookies.defaults = {
|
|
|
|
path: '/'
|
|
|
|
path: '/',
|
|
|
|
|
|
|
|
secure: false
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Cookies.get = function (key) {
|
|
|
|
Cookies.get = function (key) {
|
|
|
@ -27,7 +36,9 @@
|
|
|
|
Cookies._renewCache();
|
|
|
|
Cookies._renewCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Cookies._cache[key];
|
|
|
|
var value = Cookies._cache[Cookies._cacheKeyPrefix + key];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return value === undefined ? undefined : decodeURIComponent(value);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Cookies.set = function (key, value, options) {
|
|
|
|
Cookies.set = function (key, value, options) {
|
|
|
@ -58,9 +69,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
Cookies._getExpiresDate = function (expires, now) {
|
|
|
|
Cookies._getExpiresDate = function (expires, now) {
|
|
|
|
now = now || new Date();
|
|
|
|
now = now || new Date();
|
|
|
|
switch (typeof expires) {
|
|
|
|
|
|
|
|
case 'number': expires = new Date(now.getTime() + expires * 1000); break;
|
|
|
|
if (typeof expires === 'number') {
|
|
|
|
case 'string': expires = new Date(expires); break;
|
|
|
|
expires = expires === Infinity ?
|
|
|
|
|
|
|
|
Cookies._maxExpireDate : new Date(now.getTime() + expires * 1000);
|
|
|
|
|
|
|
|
} else if (typeof expires === 'string') {
|
|
|
|
|
|
|
|
expires = new Date(expires);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (expires && !Cookies._isValidDate(expires)) {
|
|
|
|
if (expires && !Cookies._isValidDate(expires)) {
|
|
|
@ -71,32 +85,33 @@
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Cookies._generateCookieString = function (key, value, options) {
|
|
|
|
Cookies._generateCookieString = function (key, value, options) {
|
|
|
|
key = encodeURIComponent(key);
|
|
|
|
key = key.replace(/[^#$&+\^`|]/g, encodeURIComponent);
|
|
|
|
|
|
|
|
key = key.replace(/\(/g, '%28').replace(/\)/g, '%29');
|
|
|
|
value = (value + '').replace(/[^!#$&-+\--:<-\[\]-~]/g, encodeURIComponent);
|
|
|
|
value = (value + '').replace(/[^!#$&-+\--:<-\[\]-~]/g, encodeURIComponent);
|
|
|
|
options = options || {};
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
|
|
|
|
var cookieString = key + '=' + value;
|
|
|
|
var cookieString = key + '=' + value;
|
|
|
|
cookieString += options.path ? ';path=' + options.path : '';
|
|
|
|
cookieString += options.path ? ';path=' + options.path : '';
|
|
|
|
cookieString += options.domain ? ';domain=' + options.domain : '';
|
|
|
|
cookieString += options.domain ? ';domain=' + options.domain : '';
|
|
|
|
cookieString += options.expires ? ';expires=' + options.expires.toGMTString() : '';
|
|
|
|
cookieString += options.expires ? ';expires=' + options.expires.toUTCString() : '';
|
|
|
|
cookieString += options.secure ? ';secure' : '';
|
|
|
|
cookieString += options.secure ? ';secure' : '';
|
|
|
|
|
|
|
|
|
|
|
|
return cookieString;
|
|
|
|
return cookieString;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Cookies._getCookieObjectFromString = function (documentCookie) {
|
|
|
|
Cookies._getCacheFromString = function (documentCookie) {
|
|
|
|
var cookieObject = {};
|
|
|
|
var cookieCache = {};
|
|
|
|
var cookiesArray = documentCookie ? documentCookie.split('; ') : [];
|
|
|
|
var cookiesArray = documentCookie ? documentCookie.split('; ') : [];
|
|
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < cookiesArray.length; i++) {
|
|
|
|
for (var i = 0; i < cookiesArray.length; i++) {
|
|
|
|
var cookieKvp = Cookies._getKeyValuePairFromCookieString(cookiesArray[i]);
|
|
|
|
var cookieKvp = Cookies._getKeyValuePairFromCookieString(cookiesArray[i]);
|
|
|
|
|
|
|
|
|
|
|
|
if (cookieObject[cookieKvp.key] === undefined) {
|
|
|
|
if (cookieCache[Cookies._cacheKeyPrefix + cookieKvp.key] === undefined) {
|
|
|
|
cookieObject[cookieKvp.key] = cookieKvp.value;
|
|
|
|
cookieCache[Cookies._cacheKeyPrefix + cookieKvp.key] = cookieKvp.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return cookieObject;
|
|
|
|
return cookieCache;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Cookies._getKeyValuePairFromCookieString = function (cookieString) {
|
|
|
|
Cookies._getKeyValuePairFromCookieString = function (cookieString) {
|
|
|
@ -106,36 +121,52 @@
|
|
|
|
// IE omits the "=" when the cookie value is an empty string
|
|
|
|
// IE omits the "=" when the cookie value is an empty string
|
|
|
|
separatorIndex = separatorIndex < 0 ? cookieString.length : separatorIndex;
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
key: decodeURIComponent(cookieString.substr(0, separatorIndex)),
|
|
|
|
key: decodedKey,
|
|
|
|
value: decodeURIComponent(cookieString.substr(separatorIndex + 1))
|
|
|
|
value: cookieString.substr(separatorIndex + 1) // Defer decoding value until accessed
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Cookies._renewCache = function () {
|
|
|
|
Cookies._renewCache = function () {
|
|
|
|
Cookies._cache = Cookies._getCookieObjectFromString(Cookies._document.cookie);
|
|
|
|
Cookies._cache = Cookies._getCacheFromString(Cookies._document.cookie);
|
|
|
|
Cookies._cachedDocumentCookie = Cookies._document.cookie;
|
|
|
|
Cookies._cachedDocumentCookie = Cookies._document.cookie;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Cookies._areEnabled = function () {
|
|
|
|
Cookies._areEnabled = function () {
|
|
|
|
return Cookies._navigator.cookieEnabled ||
|
|
|
|
var testKey = 'cookies.js';
|
|
|
|
Cookies.set('cookies.js', 1).get('cookies.js') === '1';
|
|
|
|
var areEnabled = Cookies.set(testKey, 1).get(testKey) === '1';
|
|
|
|
|
|
|
|
Cookies.expire(testKey);
|
|
|
|
|
|
|
|
return areEnabled;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Cookies.enabled = Cookies._areEnabled();
|
|
|
|
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);
|
|
|
|