Debian Codegraph @

resources/src/mediawiki.cookie/jar.js

raw symbols SCIP
1 /**
2 * Cookie Plugin
3 * Based on https://github.com/carhartl/jquery-cookie
4 *
5 * Copyright 2013 Klaus Hartl
6 * Released under the MIT license
7 *
8 * Now forked by MediaWiki.
9 *
10 * @private
11 * @class mw.cookie.jar
12 */
13 ( function () {
14
15 const pluses = /\+/g;
16 let config = null, cookie;
17
18 function raw( s ) {
19 return s;
20 }
21
22 function decoded( s ) {
23 try {
24 return unRfc2068( decodeURIComponent( s.replace( pluses, ' ' ) ) );
25 } catch ( e ) {
26 // If the cookie cannot be decoded this should not throw an error.
27 // See T271838.
28 return '';
29 }
30 }
31
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, '\\' );
36 }
37 return value;
38 }
39
40 function fromJSON( value ) {
41 return config.json ? JSON.parse( value ) : value;
42 }
43
44 /**
45 * Get, set, or remove a cookie.
46 *
47 * @ignore
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).
55 */
56 config = cookie = function ( key, value, options ) {
57
58 // write
59 if ( value !== undefined ) {
60 options = Object.assign( {}, config.defaults, options );
61
62 if ( value === null ) {
63 options.expires = -1;
64 }
65
66 if ( typeof options.expires === 'number' ) {
67 const days = options.expires, t = options.expires = new Date();
68 t.setDate( t.getDate() + days );
69 }
70
71 value = config.json ? JSON.stringify( value ) : String( value );
72
73 try {
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 : ''
82 ].join( '' ) );
83 } catch ( e ) {
84 // Fail silently if the document is not allowed to access cookies.
85 return '';
86 }
87 }
88
89 // read
90 const decode = config.raw ? raw : decoded;
91 let cookies;
92 try {
93 cookies = document.cookie.split( '; ' );
94 } catch ( e ) {
95 // Fail silently if the document is not allowed to access cookies.
96 cookies = [];
97 }
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( '=' ) );
103
104 if ( key && key === name ) {
105 result = fromJSON( s );
106 break;
107 }
108
109 if ( !key ) {
110 result[ name ] = fromJSON( s );
111 }
112 }
113
114 return result;
115 };
116
117 config.defaults = {};
118
119 /**
120 * Remove a cookie by key.
121 *
122 * @ignore
123 * @param {string} key
124 * @param {mw.cookie.CookieOptions} options
125 * @return {boolean} True if the cookie previously existed
126 */
127 function removeCookie( key, options ) {
128 if ( cookie( key ) !== null ) {
129 cookie( key, null, options );
130 return true;
131 }
132 return false;
133 }
134
135 module.exports = {
136 cookie,
137 removeCookie
138 };
139 }() );