3 * Based on https://github.com/carhartl/jquery-cookie
5 * Copyright 2013 Klaus Hartl
6 * Released under the MIT license
8 * Now forked by MediaWiki.
11 * @class mw.cookie.jar
16 let config = null, cookie;
22 function decoded( s ) {
24 return unRfc2068( decodeURIComponent( s.replace( pluses, ' ' ) ) );
26 // If the cookie cannot be decoded this should not throw an error.
32 function unRfc2068( value ) {
33 if ( value.indexOf( '"' ) === 0 ) {
34 // This is a quoted cookie as according to RFC2068, unescape
35 value = value.slice( 1, -1 ).replace( /\\"/g, '"' ).replace( /\\\\/g, '\\' );
40 function fromJSON( value ) {
41 return config.json ? JSON.parse( value ) : value;
45 * Get, set, or remove a cookie.
48 * @param {string} [key] Cookie name or (when getting) omit to return an object with all
49 * current cookie keys and values.
50 * @param {string|null} [value] Cookie value to set. If `null`, this method will remove the cookie.
51 * If omited, this method will get and return the current value.
52 * @param {mw.cookie.CookieOptions} [options]
53 * @return {string|Object} The current value (if getting a cookie), or an internal `document.cookie`
54 * expression (if setting or removing).
56 config = cookie = function ( key, value, options ) {
59 if ( value !== undefined ) {
60 options = Object.assign( {}, config.defaults, options );
62 if ( value === null ) {
66 if ( typeof options.expires === 'number' ) {
67 const days = options.expires, t = options.expires = new Date();
68 t.setDate( t.getDate() + days );
71 value = config.json ? JSON.stringify( value ) : String( value );
74 return ( document.cookie = [
75 encodeURIComponent( key ), '=', config.raw ? value : encodeURIComponent( value ),
76 options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
77 options.path ? '; path=' + options.path : '',
78 options.domain ? '; domain=' + options.domain : '',
79 options.secure ? '; secure' : '',
80 // PATCH: handle SameSite flag --tgr
81 options.sameSite ? '; samesite=' + options.sameSite : ''
84 // Fail silently if the document is not allowed to access cookies.
90 const decode = config.raw ? raw : decoded;
93 cookies = document.cookie.split( '; ' );
95 // Fail silently if the document is not allowed to access cookies.
98 let result = key ? null : {};
99 for ( let i = 0, l = cookies.length; i < l; i++ ) {
100 const parts = cookies[ i ].split( '=' );
101 const name = decode( parts.shift() );
102 const s = decode( parts.join( '=' ) );
104 if ( key && key === name ) {
105 result = fromJSON( s );
110 result[ name ] = fromJSON( s );
117 config.defaults = {};
120 * Remove a cookie by key.
123 * @param {string} key
124 * @param {mw.cookie.CookieOptions} options
125 * @return {boolean} True if the cookie previously existed
127 function removeCookie( key, options ) {
128 if ( cookie( key ) !== null ) {
129 cookie( key, null, options );