Skip to content

Commit f677912

Browse files
committed
AC-2626: Migrate from jquery/jquery-cookie to js-cookie/js-cookie
1 parent ac0c588 commit f677912

File tree

7 files changed

+204
-121
lines changed

7 files changed

+204
-121
lines changed

app/code/Magento/Cookie/view/base/web/js/jquery.storageapi.extended.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
define([
77
'jquery',
8-
'jquery/jquery.cookie',
8+
'js-cookie/cookie-wrapper',
99
'jquery/jquery.storageapi.min'
1010
], function ($) {
1111
'use strict';

app/code/Magento/Theme/view/base/requirejs-config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ var config = {
4747
'jquery-ui-modules/widget': 'jquery/ui-modules/widget',
4848
'jquery-ui-modules/timepicker': 'jquery/timepicker',
4949
'vimeo': 'vimeo/player',
50-
'vimeoWrapper': 'vimeo/vimeo-wrapper'
50+
'vimeoWrapper': 'vimeo/vimeo-wrapper',
51+
'jquery/jquery.cookie': 'js-cookie/cookie-wrapper'
5152
}
5253
},
5354
shim: {

lib/web/jquery/jquery.cookie.js

Lines changed: 0 additions & 118 deletions
This file was deleted.

lib/web/js-cookie/cookie-wrapper.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([
7+
'jquery',
8+
'js-cookie/js.cookie'
9+
], function ($, cookie) {
10+
'use strict';
11+
12+
var config = $.cookie = function (key, value, options) {
13+
if (value !== undefined) {
14+
options = $.extend({}, config.defaults, options);
15+
16+
return cookie.set(key, value, options);
17+
}
18+
19+
var result = key ? undefined : {},
20+
cookies = document.cookie ? document.cookie.split('; ') : [],
21+
i;
22+
23+
for (i = 0; i < cookies.length; i++) {
24+
var parts = cookies[i].split('='),
25+
name = config.raw ? parts.shift() : decodeURIComponent(parts.shift()),
26+
cookieValue = parts.join('=');
27+
28+
if (key && key === name) {
29+
result = decodeURIComponent(cookieValue.replace('/\\+/g', ' '));
30+
break;
31+
}
32+
33+
if (!key && (cookieValue = decodeURIComponent(cookieValue.replace('/\\+/g', ' '))) !== undefined) {
34+
result[name] = cookieValue;
35+
}
36+
}
37+
38+
return result;
39+
}
40+
41+
config.defaults = {};
42+
43+
$.removeCookie = function (key, options) {
44+
if ($.cookie(key) === undefined) {
45+
return false;
46+
}
47+
48+
$.cookie(key, '', $.extend({}, options, { expires: -1 }));
49+
return !$.cookie(key);
50+
};
51+
});

lib/web/js-cookie/js.cookie.js

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*! js-cookie v3.0.1 | MIT */
2+
;
3+
(function (global, factory) {
4+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
5+
typeof define === 'function' && define.amd ? define(factory) :
6+
(global = global || self, (function () {
7+
var current = global.Cookies;
8+
var exports = global.Cookies = factory();
9+
exports.noConflict = function () { global.Cookies = current; return exports; };
10+
}()));
11+
}(this, (function () { 'use strict';
12+
13+
/* eslint-disable no-var */
14+
function assign (target) {
15+
for (var i = 1; i < arguments.length; i++) {
16+
var source = arguments[i];
17+
for (var key in source) {
18+
target[key] = source[key];
19+
}
20+
}
21+
return target
22+
}
23+
/* eslint-enable no-var */
24+
25+
/* eslint-disable no-var */
26+
var defaultConverter = {
27+
read: function (value) {
28+
if (value[0] === '"') {
29+
value = value.slice(1, -1);
30+
}
31+
return value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent)
32+
},
33+
write: function (value) {
34+
return encodeURIComponent(value).replace(
35+
/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,
36+
decodeURIComponent
37+
)
38+
}
39+
};
40+
/* eslint-enable no-var */
41+
42+
/* eslint-disable no-var */
43+
44+
function init (converter, defaultAttributes) {
45+
function set (key, value, attributes) {
46+
if (typeof document === 'undefined') {
47+
return
48+
}
49+
50+
attributes = assign({}, defaultAttributes, attributes);
51+
52+
if (typeof attributes.expires === 'number') {
53+
attributes.expires = new Date(Date.now() + attributes.expires * 864e5);
54+
}
55+
if (attributes.expires) {
56+
attributes.expires = attributes.expires.toUTCString();
57+
}
58+
59+
key = encodeURIComponent(key)
60+
.replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent)
61+
.replace(/[()]/g, escape);
62+
63+
var stringifiedAttributes = '';
64+
for (var attributeName in attributes) {
65+
if (!attributes[attributeName]) {
66+
continue
67+
}
68+
69+
stringifiedAttributes += '; ' + attributeName;
70+
71+
if (attributes[attributeName] === true) {
72+
continue
73+
}
74+
75+
// Considers RFC 6265 section 5.2:
76+
// ...
77+
// 3. If the remaining unparsed-attributes contains a %x3B (";")
78+
// character:
79+
// Consume the characters of the unparsed-attributes up to,
80+
// not including, the first %x3B (";") character.
81+
// ...
82+
stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];
83+
}
84+
85+
return (document.cookie =
86+
key + '=' + converter.write(value, key) + stringifiedAttributes)
87+
}
88+
89+
function get (key) {
90+
if (typeof document === 'undefined' || (arguments.length && !key)) {
91+
return
92+
}
93+
94+
// To prevent the for loop in the first place assign an empty array
95+
// in case there are no cookies at all.
96+
var cookies = document.cookie ? document.cookie.split('; ') : [];
97+
var jar = {};
98+
for (var i = 0; i < cookies.length; i++) {
99+
var parts = cookies[i].split('=');
100+
var value = parts.slice(1).join('=');
101+
102+
try {
103+
var foundKey = decodeURIComponent(parts[0]);
104+
jar[foundKey] = converter.read(value, foundKey);
105+
106+
if (key === foundKey) {
107+
break
108+
}
109+
} catch (e) {}
110+
}
111+
112+
return key ? jar[key] : jar
113+
}
114+
115+
return Object.create(
116+
{
117+
set: set,
118+
get: get,
119+
remove: function (key, attributes) {
120+
set(
121+
key,
122+
'',
123+
assign({}, attributes, {
124+
expires: -1
125+
})
126+
);
127+
},
128+
withAttributes: function (attributes) {
129+
return init(this.converter, assign({}, this.attributes, attributes))
130+
},
131+
withConverter: function (converter) {
132+
return init(assign({}, this.converter, converter), this.attributes)
133+
}
134+
},
135+
{
136+
attributes: { value: Object.freeze(defaultAttributes) },
137+
converter: { value: Object.freeze(converter) }
138+
}
139+
)
140+
}
141+
142+
var api = init(defaultConverter, { path: '/' });
143+
/* eslint-enable no-var */
144+
145+
return api;
146+
147+
})));

lib/web/js-cookie/js.cookie.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/web/mage/cookies.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
define([
77
'jquery',
88
'mage/mage',
9-
'jquery/jquery.cookie'
9+
'js-cookie/cookie-wrapper'
1010
], function ($) {
1111
'use strict';
1212

0 commit comments

Comments
 (0)