Source: misc/jquery.cookie.shim.es6.js

  1. /**
  2. * @file
  3. * Defines a backwards-compatible shim for jquery.cookie.
  4. */
  5. /**
  6. * The core/js-cookie library object.
  7. *
  8. * @global
  9. *
  10. * @var {object} Cookies
  11. */
  12. (($, Drupal, cookies) => {
  13. const deprecatedMessageSuffix = `is deprecated in Drupal 9.0.0 and will be removed in Drupal 10.0.0. Use the core/js-cookie library instead. See https://www.drupal.org/node/3104677`;
  14. /**
  15. * Determines if an object is a function.
  16. *
  17. * @param {Object} obj
  18. * The object to check.
  19. *
  20. * @return {boolean}
  21. * True if the object is a function.
  22. */
  23. const isFunction = (obj) =>
  24. Object.prototype.toString.call(obj) === '[object Function]';
  25. /**
  26. * Decodes cookie value for compatibility with jquery.cookie.
  27. *
  28. * @param {string} value
  29. * The cookie value to parse.
  30. * @param {boolean} parseJson
  31. * Whether cookie value should be parsed from JSON.
  32. *
  33. * @return {string}
  34. * The cookie value for the reader to return.
  35. */
  36. const parseCookieValue = (value, parseJson) => {
  37. if (value.indexOf('"') === 0) {
  38. value = value.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
  39. }
  40. try {
  41. value = decodeURIComponent(value.replace(/\+/g, ' '));
  42. return parseJson ? JSON.parse(value) : value;
  43. } catch (e) {
  44. // Exceptions on JSON parsing should be ignored.
  45. }
  46. };
  47. /**
  48. * Wraps the cookie value to support unsanitized values.
  49. *
  50. * Decoding strings is the job of the converter when using js-cookie, and
  51. * the shim uses the same decode function as that library when the deprecated
  52. * raw option is not used.
  53. *
  54. * @param {string} cookieValue
  55. * The cookie value.
  56. * @param {string} cookieName
  57. * The cookie name.
  58. * @param {reader~converterCallback} converter
  59. * A function that takes the cookie value for further processing.
  60. * @param {boolean} readUnsanitized
  61. * Uses the unsanitized value when set to true.
  62. * @param {boolean} parseJson
  63. * Whether cookie value should be parsed from JSON.
  64. *
  65. * @return {string}
  66. * The cookie value that js-cookie will return.
  67. */
  68. const reader = (
  69. cookieValue,
  70. cookieName,
  71. converter,
  72. readUnsanitized,
  73. parseJson,
  74. ) => {
  75. const value = readUnsanitized
  76. ? cookieValue
  77. : parseCookieValue(cookieValue, parseJson);
  78. if (converter !== undefined && isFunction(converter)) {
  79. return converter(value, cookieName);
  80. }
  81. return value;
  82. };
  83. /**
  84. * Gets or sets a browser cookie.
  85. *
  86. * @example
  87. * // Returns 'myCookie=myCookieValue'.
  88. * $.cookie('myCookie', 'myCookieValue');
  89. * @example
  90. * // Returns 'myCookieValue'.
  91. * $.cookie('myCookie');
  92. *
  93. * @example
  94. * // Returns the literal URI-encoded value of {"key": "value"} as the cookie
  95. * // value along with the path as in the above example.
  96. * $.cookie('myCookie', { key: 'value' });
  97. * @example
  98. * $.cookie.json = true;
  99. * // Returns { key: 'value' }.
  100. * $.cookie('myCookie');
  101. *
  102. * @param {string} key
  103. * The name of the cookie.
  104. * @param {string|Object|Function|undefined} value
  105. * A js-cookie converter callback when used as a getter. This callback must
  106. * be a function when using this shim for backwards-compatibility with
  107. * jquery.cookie. When used as a setter, value is the string or JSON object
  108. * to be used as the cookie value.
  109. * @param {Object|undefined} options
  110. * Overrides the default options when used as a setter. See the js-cookie
  111. * library README.md file for details.
  112. *
  113. * @return {string}
  114. * Returns the cookie name, value, and other properties based on the
  115. * return value of the document.cookie setter.
  116. *
  117. * @deprecated in Drupal 9.0.0 and is removed from Drupal 10.0.0.
  118. * Use the core/js-cookie library instead.
  119. *
  120. * @see https://www.drupal.org/node/3104677
  121. * @see https://github.com/js-cookie/js-cookie/blob/v3.0.0-rc.0/README.md
  122. */
  123. $.cookie = (key, value = undefined, options = undefined) => {
  124. Drupal.deprecationError({
  125. message: `jQuery.cookie() ${deprecatedMessageSuffix}`,
  126. });
  127. // Key should be only encoded if it exists and when not in a raw mode.
  128. key = key && !$.cookie.raw ? encodeURIComponent(key) : key;
  129. if (value !== undefined && !isFunction(value)) {
  130. // The caller is setting a cookie value and not trying to retrieve the
  131. // cookie value using a converter callback.
  132. const attributes = { ...$.cookie.defaults, ...options };
  133. if (typeof attributes.expires === 'string' && attributes.expires !== '') {
  134. attributes.expires = new Date(attributes.expires);
  135. }
  136. const cookieSetter = cookies.withConverter({
  137. write: (cookieValue) => encodeURIComponent(cookieValue),
  138. });
  139. value =
  140. $.cookie.json && !$.cookie.raw ? JSON.stringify(value) : String(value);
  141. return cookieSetter.set(key, value, attributes);
  142. }
  143. // Use either js-cookie or pass in a converter to get the raw cookie value,
  144. // which has security implications, but remains in place for
  145. // backwards-compatibility.
  146. const userProvidedConverter = value;
  147. const cookiesShim = cookies.withConverter({
  148. read: (cookieValue, cookieName) =>
  149. reader(
  150. cookieValue,
  151. cookieName,
  152. userProvidedConverter,
  153. $.cookie.raw,
  154. $.cookie.json,
  155. ),
  156. });
  157. if (key !== undefined) {
  158. return cookiesShim.get(key);
  159. }
  160. const results = cookiesShim.get();
  161. Object.keys(results).forEach((resultKey) => {
  162. if (results[resultKey] === undefined) {
  163. delete results[resultKey];
  164. }
  165. });
  166. return results;
  167. };
  168. /**
  169. * @prop {Object} defaults
  170. * The default options when setting a cookie.
  171. * @prop {string} defaults.path
  172. * The default path for the cookie is ''.
  173. * @prop {undefined} defaults.expires
  174. * There is no default value for the expires option. The default expiration
  175. * is set to an empty string.
  176. */
  177. $.cookie.defaults = { path: '', ...cookies.defaults };
  178. /**
  179. * @prop {boolean} json
  180. * True if the cookie value should be parsed as JSON.
  181. */
  182. $.cookie.json = false;
  183. /**
  184. * @prop {boolean} json
  185. * True if the cookie value should be returned as-is without decoding
  186. * URI entities. In jquery.cookie, this also would not encode the cookie
  187. * name, but js-cookie does not allow this.
  188. */
  189. $.cookie.raw = false;
  190. /**
  191. * Removes a browser cookie.
  192. *
  193. * @param {string} key
  194. * The name of the cookie.
  195. * @param {Object} options
  196. * Optional options. See the js-cookie library README.md for more details.
  197. *
  198. * @return {boolean}
  199. * Returns true when the cookie is successfully removed.
  200. *
  201. * @deprecated in Drupal 9.0.0 and is removed from Drupal 10.0.0.
  202. * Use the core/js-cookie library instead.
  203. *
  204. * @see https://www.drupal.org/node/3104677
  205. * @see https://github.com/js-cookie/js-cookie/blob/v3.0.0-rc.0/README.md
  206. */
  207. $.removeCookie = (key, options) => {
  208. Drupal.deprecationError({
  209. message: `jQuery.removeCookie() ${deprecatedMessageSuffix}`,
  210. });
  211. cookies.remove(key, { ...$.cookie.defaults, ...options });
  212. return !cookies.get(key);
  213. };
  214. })(jQuery, Drupal, window.Cookies);