Skip to content

Commit

Permalink
Issue #3118726 by AndyF, lauriii, jungle, xjm, bnjmnm, catch, mradcli…
Browse files Browse the repository at this point in the history
…ffe, effulgentsia: Upgrade to js.cookie 3
  • Loading branch information
xjm committed Apr 30, 2020
1 parent ab20a22 commit 214f9a4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 37 deletions.
5 changes: 2 additions & 3 deletions assets/vendor/js-cookie/js.cookie.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions core.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1027,10 +1027,10 @@ drupal.dialog.off_canvas:

js-cookie:
remote: https://github.com/js-cookie/js-cookie
version: "v2.2.1"
version: "v3.0.0-rc0"
license:
name: MIT
url: https://github.com/js-cookie/js-cookie/blob/v2.2.1/MIT-LICENSE.txt
url: https://github.com/js-cookie/js-cookie/blob/v3.0.0-rc.0/LICENSE
gpl-compatible: true
js:
assets/vendor/js-cookie/js.cookie.min.js: {}
73 changes: 52 additions & 21 deletions misc/jquery.cookie.shim.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,26 @@
*
* @param {string} value
* The cookie value to parse.
* @param {boolean} parseJson
* Whether cookie value should be parsed from JSON.
*
* @return {string}
* The cookie value for the reader to return.
*/
const parseCookieValue = value => {
const parseCookieValue = (value, parseJson) => {
if (value.indexOf('"') === 0) {
value = value
.slice(1, -1)
.replace(/\\"/g, '"')
.replace(/\\\\/g, '\\');
}
return decodeURIComponent(value.replace(/\+/g, ' '));

try {
value = decodeURIComponent(value.replace(/\+/g, ' '));
return parseJson ? JSON.parse(value) : value;
} catch (e) {
// Exceptions on JSON parsing should be ignored.
}
};

/**
Expand All @@ -58,16 +66,27 @@
* A function that takes the cookie value for further processing.
* @param {boolean} readUnsanitized
* Uses the unsanitized value when set to true.
* @param {boolean} parseJson
* Whether cookie value should be parsed from JSON.
*
* @return {string}
* The cookie value that js-cookie will return.
*/
const reader = (cookieValue, cookieName, converter, readUnsanitized) => {
const value = readUnsanitized ? cookieValue : parseCookieValue(cookieValue);
const reader = (
cookieValue,
cookieName,
converter,
readUnsanitized,
parseJson,
) => {
const value = readUnsanitized
? cookieValue
: parseCookieValue(cookieValue, parseJson);

if (converter !== undefined && isFunction(converter)) {
return converter(value, cookieName);
}

return value;
};

Expand Down Expand Up @@ -106,23 +125,16 @@
* return value of the document.cookie setter.
*
* @see https://www.drupal.org/node/3104677
* @see https://github.com/js-cookie/js-cookie/blob/v2.2.1/README.md
* @see https://github.com/js-cookie/js-cookie/blob/v3.0.0-rc.0/README.md
*/
$.cookie = (key, value = undefined, options = undefined) => {
// Key should be only encoded if it exists and when not in a raw mode.
key = key && !$.cookie.raw ? encodeURIComponent(key) : key;
if (value !== undefined && !isFunction(value)) {
// The caller is setting a cookie value and not trying to retrieve the
// cookie value using a converter callback.
const attributes = Object.assign({}, $.cookie.defaults, options);

if (!$.cookie.json) {
// An object that is passed in must be typecast to a string when the
// "json" option is not set because js-cookie will always stringify
// JSON cookie values.
value = String(value);
}

// If the expires value is a non-empty string, it needs to be converted
// to a Date() object before being sent to js-cookie.
if (typeof attributes.expires === 'string' && attributes.expires !== '') {
attributes.expires = new Date(attributes.expires);
}
Expand All @@ -131,20 +143,39 @@
write: cookieValue => encodeURIComponent(cookieValue),
});

value =
$.cookie.json && !$.cookie.raw ? JSON.stringify(value) : String(value);

return cookieSetter.set(key, value, attributes);
}

// Use either js-cookie or pass in a converter to get the raw cookie value,
// which has security implications, but remains in place for
// backwards-compatibility.
const userProvidedConverter = value;
const cookiesShim = cookies.withConverter((cookieValue, cookieName) =>
reader(cookieValue, cookieName, userProvidedConverter, $.cookie.raw),
);
const cookiesShim = cookies.withConverter({
read: (cookieValue, cookieName) =>
reader(
cookieValue,
cookieName,
userProvidedConverter,
$.cookie.raw,
$.cookie.json,
),
});

if (key !== undefined) {
return cookiesShim.get(key);
}

const results = cookiesShim.get();
Object.keys(results).forEach(resultKey => {
if (results[resultKey] === undefined) {
delete results[resultKey];
}
});

return $.cookie.json === true
? cookiesShim.getJSON(key)
: cookiesShim.get(key);
return results;
};

/**
Expand Down Expand Up @@ -184,7 +215,7 @@
* Returns true when the cookie is successfully removed.
*
* @see https://www.drupal.org/node/3104677
* @see https://github.com/js-cookie/js-cookie/blob/v2.2.1/README.md
* @see https://github.com/js-cookie/js-cookie/blob/v3.0.0-rc.0/README.md
*/
$.removeCookie = (key, options) => {
cookies.remove(key, Object.assign({}, $.cookie.defaults, options));
Expand Down
39 changes: 28 additions & 11 deletions misc/jquery.cookie.shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,35 @@
return Object.prototype.toString.call(obj) === '[object Function]';
};

var parseCookieValue = function parseCookieValue(value) {
var parseCookieValue = function parseCookieValue(value, parseJson) {
if (value.indexOf('"') === 0) {
value = value.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
}
return decodeURIComponent(value.replace(/\+/g, ' '));

try {
value = decodeURIComponent(value.replace(/\+/g, ' '));
return parseJson ? JSON.parse(value) : value;
} catch (e) {}
};

var reader = function reader(cookieValue, cookieName, converter, readUnsanitized) {
var value = readUnsanitized ? cookieValue : parseCookieValue(cookieValue);
var reader = function reader(cookieValue, cookieName, converter, readUnsanitized, parseJson) {
var value = readUnsanitized ? cookieValue : parseCookieValue(cookieValue, parseJson);

if (converter !== undefined && isFunction(converter)) {
return converter(value, cookieName);
}

return value;
};

$.cookie = function (key) {
var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;

key = key && !$.cookie.raw ? encodeURIComponent(key) : key;
if (value !== undefined && !isFunction(value)) {
var attributes = Object.assign({}, $.cookie.defaults, options);

if (!$.cookie.json) {
value = String(value);
}

if (typeof attributes.expires === 'string' && attributes.expires !== '') {
attributes.expires = new Date(attributes.expires);
}
Expand All @@ -47,15 +49,30 @@
}
});

value = $.cookie.json && !$.cookie.raw ? JSON.stringify(value) : String(value);

return cookieSetter.set(key, value, attributes);
}

var userProvidedConverter = value;
var cookiesShim = cookies.withConverter(function (cookieValue, cookieName) {
return reader(cookieValue, cookieName, userProvidedConverter, $.cookie.raw);
var cookiesShim = cookies.withConverter({
read: function read(cookieValue, cookieName) {
return reader(cookieValue, cookieName, userProvidedConverter, $.cookie.raw, $.cookie.json);
}
});

if (key !== undefined) {
return cookiesShim.get(key);
}

var results = cookiesShim.get();
Object.keys(results).forEach(function (resultKey) {
if (results[resultKey] === undefined) {
delete results[resultKey];
}
});

return $.cookie.json === true ? cookiesShim.getJSON(key) : cookiesShim.get(key);
return results;
};

$.cookie.defaults = Object.assign({ path: '' }, cookies.defaults);
Expand Down

0 comments on commit 214f9a4

Please sign in to comment.