diff cs/cookies.js @ 113:4d3f845c4ef2

cs: update cookies.js, set domain and secure cookie parameters
author paulo
date Mon, 03 Aug 2020 01:45:23 -0600
parents f833a888c548
children
line diff
     1.1 --- a/cs/cookies.js	Sun Jun 28 15:51:12 2020 -0600
     1.2 +++ b/cs/cookies.js	Mon Aug 03 01:45:23 2020 -0600
     1.3 @@ -1,63 +1,91 @@
     1.4  /*\
     1.5  |*|
     1.6 -|*|  :: cookies.js ::
     1.7 +|*|	:: cookies.js ::
     1.8  |*|
     1.9 -|*|  A complete cookies reader/writer framework with full unicode support.
    1.10 +|*|	A complete cookies reader/writer framework with full unicode support.
    1.11  |*|
    1.12 -|*|  Revision #1 - September 4, 2014
    1.13 +|*|	Revision #8 - February 18th, 2020
    1.14  |*|
    1.15 -|*|  https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
    1.16 -|*|  https://developer.mozilla.org/User:fusionchess
    1.17 +|*|	https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
    1.18 +|*|	https://developer.mozilla.org/User:fusionchess
    1.19 +|*|	https://github.com/madmurphy/cookies.js
    1.20  |*|
    1.21 -|*|  This framework is released under the GNU Public License, version 3 or later.
    1.22 -|*|  http://www.gnu.org/licenses/gpl-3.0-standalone.html
    1.23 +|*|	This framework is released under the GNU Public License, version 3 or later.
    1.24 +|*|	http://www.gnu.org/licenses/gpl-3.0-standalone.html
    1.25  |*|
    1.26 -|*|  Syntaxes:
    1.27 +|*|	Syntaxes:
    1.28  |*|
    1.29 -|*|  * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])
    1.30 -|*|  * docCookies.getItem(name)
    1.31 -|*|  * docCookies.removeItem(name[, path[, domain]])
    1.32 -|*|  * docCookies.hasItem(name)
    1.33 -|*|  * docCookies.keys()
    1.34 +|*|	* docCookies.setItem(name, value[, end[, path[, domain[, secure[, same-site]]]]])
    1.35 +|*|	* docCookies.getItem(name)
    1.36 +|*|	* docCookies.removeItem(name[, path[, domain[, secure[, same-site]]]])
    1.37 +|*|	* docCookies.hasItem(name)
    1.38 +|*|	* docCookies.keys()
    1.39 +|*|	* docCookies.clear([path[, domain[, secure[, same-site]]]])
    1.40  |*|
    1.41  \*/
    1.42  
    1.43 -var docCookies = {
    1.44 -  getItem: function (sKey) {
    1.45 -    if (!sKey) { return null; }
    1.46 -    return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
    1.47 -  },
    1.48 -  setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
    1.49 -    if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
    1.50 -    var sExpires = "";
    1.51 -    if (vEnd) {
    1.52 -      switch (vEnd.constructor) {
    1.53 -        case Number:
    1.54 -          sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
    1.55 -          break;
    1.56 -        case String:
    1.57 -          sExpires = "; expires=" + vEnd;
    1.58 -          break;
    1.59 -        case Date:
    1.60 -          sExpires = "; expires=" + vEnd.toUTCString();
    1.61 -          break;
    1.62 -      }
    1.63 -    }
    1.64 -    document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
    1.65 -    return true;
    1.66 -  },
    1.67 -  removeItem: function (sKey, sPath, sDomain) {
    1.68 -    if (!this.hasItem(sKey)) { return false; }
    1.69 -    document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
    1.70 -    return true;
    1.71 -  },
    1.72 -  hasItem: function (sKey) {
    1.73 -    if (!sKey) { return false; }
    1.74 -    return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
    1.75 -  },
    1.76 -  keys: function () {
    1.77 -    var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
    1.78 -    for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); }
    1.79 -    return aKeys;
    1.80 -  }
    1.81 -};
    1.82 +(function () {
    1.83 +	function makeSetterString (sKey, sValue, vEnd, sPath, sDomain, bSecure, vSameSite) {
    1.84 +		var sExpires = "";
    1.85 +		if (vEnd) {
    1.86 +			switch (vEnd.constructor) {
    1.87 +				case Number:
    1.88 +					sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
    1.89 +					/*
    1.90 +					Note: Despite officially defined in RFC 6265, the use of `max-age` is not compatible with any
    1.91 +					version of Internet Explorer, Edge and some mobile browsers. Therefore passing a number to
    1.92 +					the end parameter might not work as expected. A possible solution might be to convert the the
    1.93 +					relative time to an absolute time. For instance you could replace the previous line with:
    1.94 +					*/
    1.95 +					/*
    1.96 +					sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; expires=" + (new Date(vEnd * 1e3 + Date.now())).toUTCString();
    1.97 +					*/
    1.98 +					break;
    1.99 +				case String:
   1.100 +					sExpires = "; expires=" + vEnd;
   1.101 +					break;
   1.102 +				case Date:
   1.103 +					sExpires = "; expires=" + vEnd.toUTCString();
   1.104 +					break;
   1.105 +			}
   1.106 +		}
   1.107 +		return	encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "") + (!vSameSite || vSameSite.toString().toLowerCase() === "no_restriction" ? "" : vSameSite.toString().toLowerCase() === "lax" || Math.ceil(vSameSite) === 1 || vSameSite === true ? "; samesite=lax" : vSameSite.toString().toLowerCase() === "none" || vSameSite < 0 ? "; samesite=none" : "; samesite=strict");
   1.108 +	}
   1.109 +	var reURIAllowed = /[\-\.\+\*]/g, reCNameAllowed = /^(?:expires|max\-age|path|domain|secure|samesite|httponly)$/i;
   1.110 +	window.docCookies = {
   1.111 +		"getItem": function (sKey) {
   1.112 +			if (!sKey) { return null; }
   1.113 +			return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(reURIAllowed, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
   1.114 +		},
   1.115 +		"setItem": function (sKey, sValue, vEnd, sPath, sDomain, bSecure, vSameSite) {
   1.116 +			if (!sKey || reCNameAllowed.test(sKey)) { return false; }
   1.117 +			document.cookie = makeSetterString(sKey, sValue, vEnd, sPath, sDomain, bSecure, vSameSite);
   1.118 +			return true;
   1.119 +		},
   1.120 +		"removeItem": function (sKey, sPath, sDomain, bSecure, vSameSite) {
   1.121 +			if (!this.hasItem(sKey)) { return false; }
   1.122 +			document.cookie = makeSetterString(sKey, "", "Thu, 01 Jan 1970 00:00:00 GMT", sPath, sDomain, bSecure, vSameSite);
   1.123 +			return true;
   1.124 +		},
   1.125 +		"hasItem": function (sKey) {
   1.126 +			if (!sKey || reCNameAllowed.test(sKey)) { return false; }
   1.127 +			return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(reURIAllowed, "\\$&") + "\\s*\\=")).test(document.cookie);
   1.128 +		},
   1.129 +		"keys": function () {
   1.130 +			var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
   1.131 +			for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) {
   1.132 +				aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]);
   1.133 +			}
   1.134 +			return aKeys;
   1.135 +		},
   1.136 +		"clear": function (sPath, sDomain, bSecure, vSameSite) {
   1.137 +			for (var aKeys = this.keys(), nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) {
   1.138 +				this.removeItem(aKeys[nIdx], sPath, sDomain, bSecure, vSameSite);
   1.139 +			}
   1.140 +		}
   1.141 +	};
   1.142 +})();
   1.143 +
   1.144 +if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
   1.145 +	module.exports = docCookies;
   1.146 +}