Bug 30063: Fix QA issues
[koha.git] / koha-tmpl / intranet-tmpl / lib / js-cookie / js.cookie-2.2.1.js
1 /*!
2  * JavaScript Cookie v2.2.1
3  * https://github.com/js-cookie/js-cookie
4  *
5  * Copyright 2006, 2015 Klaus Hartl & Fagner Brack
6  * Released under the MIT license
7  */
8 ;(function (factory) {
9         var registeredInModuleLoader;
10         if (typeof define === 'function' && define.amd) {
11                 define(factory);
12                 registeredInModuleLoader = true;
13         }
14         if (typeof exports === 'object') {
15                 module.exports = factory();
16                 registeredInModuleLoader = true;
17         }
18         if (!registeredInModuleLoader) {
19                 var OldCookies = window.Cookies;
20                 var api = window.Cookies = factory();
21                 api.noConflict = function () {
22                         window.Cookies = OldCookies;
23                         return api;
24                 };
25         }
26 }(function () {
27         function extend () {
28                 var i = 0;
29                 var result = {};
30                 for (; i < arguments.length; i++) {
31                         var attributes = arguments[ i ];
32                         for (var key in attributes) {
33                                 result[key] = attributes[key];
34                         }
35                 }
36                 return result;
37         }
38
39         function decode (s) {
40                 return s.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);
41         }
42
43         function init (converter) {
44                 function api() {}
45
46                 function set (key, value, attributes) {
47                         if (typeof document === 'undefined') {
48                                 return;
49                         }
50
51                         attributes = extend({
52                                 path: '/'
53                         }, api.defaults, attributes);
54
55                         if (typeof attributes.expires === 'number') {
56                                 attributes.expires = new Date(new Date() * 1 + attributes.expires * 864e+5);
57                         }
58
59                         // We're using "expires" because "max-age" is not supported by IE
60                         attributes.expires = attributes.expires ? attributes.expires.toUTCString() : '';
61
62                         try {
63                                 var result = JSON.stringify(value);
64                                 if (/^[\{\[]/.test(result)) {
65                                         value = result;
66                                 }
67                         } catch (e) {}
68
69                         value = converter.write ?
70                                 converter.write(value, key) :
71                                 encodeURIComponent(String(value))
72                                         .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
73
74                         key = encodeURIComponent(String(key))
75                                 .replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent)
76                                 .replace(/[\(\)]/g, escape);
77
78                         var stringifiedAttributes = '';
79                         for (var attributeName in attributes) {
80                                 if (!attributes[attributeName]) {
81                                         continue;
82                                 }
83                                 stringifiedAttributes += '; ' + attributeName;
84                                 if (attributes[attributeName] === true) {
85                                         continue;
86                                 }
87
88                                 // Considers RFC 6265 section 5.2:
89                                 // ...
90                                 // 3.  If the remaining unparsed-attributes contains a %x3B (";")
91                                 //     character:
92                                 // Consume the characters of the unparsed-attributes up to,
93                                 // not including, the first %x3B (";") character.
94                                 // ...
95                                 stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];
96                         }
97
98                         return (document.cookie = key + '=' + value + stringifiedAttributes);
99                 }
100
101                 function get (key, json) {
102                         if (typeof document === 'undefined') {
103                                 return;
104                         }
105
106                         var jar = {};
107                         // To prevent the for loop in the first place assign an empty array
108                         // in case there are no cookies at all.
109                         var cookies = document.cookie ? document.cookie.split('; ') : [];
110                         var i = 0;
111
112                         for (; i < cookies.length; i++) {
113                                 var parts = cookies[i].split('=');
114                                 var cookie = parts.slice(1).join('=');
115
116                                 if (!json && cookie.charAt(0) === '"') {
117                                         cookie = cookie.slice(1, -1);
118                                 }
119
120                                 try {
121                                         var name = decode(parts[0]);
122                                         cookie = (converter.read || converter)(cookie, name) ||
123                                                 decode(cookie);
124
125                                         if (json) {
126                                                 try {
127                                                         cookie = JSON.parse(cookie);
128                                                 } catch (e) {}
129                                         }
130
131                                         jar[name] = cookie;
132
133                                         if (key === name) {
134                                                 break;
135                                         }
136                                 } catch (e) {}
137                         }
138
139                         return key ? jar[key] : jar;
140                 }
141
142                 api.set = set;
143                 api.get = function (key) {
144                         return get(key, false /* read as raw */);
145                 };
146                 api.getJSON = function (key) {
147                         return get(key, true /* read as json */);
148                 };
149                 api.remove = function (key, attributes) {
150                         set(key, '', extend(attributes, {
151                                 expires: -1
152                         }));
153                 };
154
155                 api.defaults = {};
156
157                 api.withConverter = init;
158
159                 return api;
160         }
161
162         return init(function () {});
163 }));