1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
/**
* Cookie Plugin
* Based on https://github.com/carhartl/jquery-cookie
*
* Copyright 2013 Klaus Hartl
* Released under the MIT license
*
* Now forked by MediaWiki.
*
* @private
* @class mw.cookie.jar
*/
( function () {
var pluses = /\+/g;
var config, cookie;
function raw( s ) {
return s;
}
function decoded( s ) {
try {
return unRfc2068( decodeURIComponent( s.replace( pluses, ' ' ) ) );
} catch ( e ) {
// If the cookie cannot be decoded this should not throw an error.
// See T271838.
return '';
}
}
function unRfc2068( value ) {
if ( value.indexOf( '"' ) === 0 ) {
// This is a quoted cookie as according to RFC2068, unescape
value = value.slice( 1, -1 ).replace( /\\"/g, '"' ).replace( /\\\\/g, '\\' );
}
return value;
}
function fromJSON( value ) {
return config.json ? JSON.parse( value ) : value;
}
/**
* Get, set, or remove a cookie.
*
* @ignore
* @param {string} [key] Cookie name or (when getting) omit to return an object with all
* current cookie keys and values.
* @param {string|null} [value] Cookie value to set. If `null`, this method will remove the cookie.
* If omited, this method will get and return the current value.
* @param {mw.cookie.CookieOptions} [options]
* @return {string|Object} The current value (if getting a cookie), or an internal `document.cookie`
* expression (if setting or removing).
*/
config = cookie = function ( key, value, options ) {
// write
if ( value !== undefined ) {
options = Object.assign( {}, config.defaults, options );
if ( value === null ) {
options.expires = -1;
}
if ( typeof options.expires === 'number' ) {
var days = options.expires, t = options.expires = new Date();
t.setDate( t.getDate() + days );
}
value = config.json ? JSON.stringify( value ) : String( value );
try {
return ( document.cookie = [
encodeURIComponent( key ), '=', config.raw ? value : encodeURIComponent( value ),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : '',
// PATCH: handle SameSite flag --tgr
options.sameSite ? '; samesite=' + options.sameSite : ''
].join( '' ) );
} catch ( e ) {
// Fail silently if the document is not allowed to access cookies.
return '';
}
}
// read
var decode = config.raw ? raw : decoded;
var cookies;
try {
cookies = document.cookie.split( '; ' );
} catch ( e ) {
// Fail silently if the document is not allowed to access cookies.
cookies = [];
}
var result = key ? null : {};
for ( var i = 0, l = cookies.length; i < l; i++ ) {
var parts = cookies[ i ].split( '=' );
var name = decode( parts.shift() );
var s = decode( parts.join( '=' ) );
if ( key && key === name ) {
result = fromJSON( s );
break;
}
if ( !key ) {
result[ name ] = fromJSON( s );
}
}
return result;
};
config.defaults = {};
/**
* Remove a cookie by key.
*
* @ignore
* @param {string} key
* @param {mw.cookie.CookieOptions} options
* @return {boolean} True if the cookie previously existed
*/
function removeCookie( key, options ) {
if ( cookie( key ) !== null ) {
cookie( key, null, options );
return true;
}
return false;
}
module.exports = {
cookie,
removeCookie
};
}() );
|