Update jQuery Cookie to v1.3.1
[mediawiki.git] / resources / lib / jquery / jquery.cookie.js
blob3fb201c6a01360420488dc83614ff47cced89f35
1 /*!
2  * jQuery Cookie Plugin v1.3.1
3  * https://github.com/carhartl/jquery-cookie
4  *
5  * Copyright 2013 Klaus Hartl
6  * Released under the MIT license
7  */
8 (function ($, document, undefined) {
10         var pluses = /\+/g;
12         function raw(s) {
13                 return s;
14         }
16         function decoded(s) {
17                 return unRfc2068(decodeURIComponent(s.replace(pluses, ' ')));
18         }
20         function unRfc2068(value) {
21                 if (value.indexOf('"') === 0) {
22                         // This is a quoted cookie as according to RFC2068, unescape
23                         value = value.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
24                 }
25                 return value;
26         }
28         function fromJSON(value) {
29                 return config.json ? JSON.parse(value) : value;
30         }
32         var config = $.cookie = function (key, value, options) {
34                 // write
35                 if (value !== undefined) {
36                         options = $.extend({}, config.defaults, options);
38                         if (value === null) {
39                                 options.expires = -1;
40                         }
42                         if (typeof options.expires === 'number') {
43                                 var days = options.expires, t = options.expires = new Date();
44                                 t.setDate(t.getDate() + days);
45                         }
47                         value = config.json ? JSON.stringify(value) : String(value);
49                         return (document.cookie = [
50                                 encodeURIComponent(key), '=', config.raw ? value : encodeURIComponent(value),
51                                 options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
52                                 options.path    ? '; path=' + options.path : '',
53                                 options.domain  ? '; domain=' + options.domain : '',
54                                 options.secure  ? '; secure' : ''
55                         ].join(''));
56                 }
58                 // read
59                 var decode = config.raw ? raw : decoded;
60                 var cookies = document.cookie.split('; ');
61                 var result = key ? null : {};
62                 for (var i = 0, l = cookies.length; i < l; i++) {
63                         var parts = cookies[i].split('=');
64                         var name = decode(parts.shift());
65                         var cookie = decode(parts.join('='));
67                         if (key && key === name) {
68                                 result = fromJSON(cookie);
69                                 break;
70                         }
72                         if (!key) {
73                                 result[name] = fromJSON(cookie);
74                         }
75                 }
77                 return result;
78         };
80         config.defaults = {};
82         $.removeCookie = function (key, options) {
83                 if ($.cookie(key) !== null) {
84                         $.cookie(key, null, options);
85                         return true;
86                 }
87                 return false;
88         };
90 })(jQuery, document);