|
| 1 | +/*global escape: true */ |
1 | 2 | /*! |
2 | 3 | * Javascript Cookie v2.0.0-pre |
3 | 4 | * https://github.com/js-cookie/js-cookie |
|
22 | 23 | }; |
23 | 24 | } |
24 | 25 | }(function () { |
25 | | - var unallowedChars = { |
26 | | - ';': '%3B', |
27 | | - ',': '%2C', |
28 | | - '"': '%22' |
29 | | - }; |
30 | | - var unallowedCharsInName = extend(unallowedChars, { |
31 | | - '=': '%3D', |
32 | | - '\t': '%09' |
33 | | - }); |
34 | | - var unallowedCharsInValue = extend(unallowedChars, { |
35 | | - ' ': '%20' |
36 | | - }); |
37 | | - |
38 | | - function encode (value, charmap) { |
39 | | - for (var character in charmap) { |
40 | | - value = value |
41 | | - .replace(new RegExp(character, 'g'), charmap[character]); |
42 | | - } |
43 | | - return value; |
44 | | - } |
45 | | - |
46 | | - function decode (value, charmap) { |
47 | | - for (var character in charmap) { |
48 | | - value = value |
49 | | - .replace(new RegExp(charmap[character], 'g'), character); |
50 | | - } |
51 | | - return value; |
52 | | - } |
53 | | - |
54 | | - function processRead (value, converter, json) { |
55 | | - if (value.charAt(0) === '"') { |
56 | | - value = value.slice(1, -1); |
57 | | - } |
58 | | - |
59 | | - value = decode(value, unallowedCharsInValue); |
60 | | - |
61 | | - if (json) { |
62 | | - try { |
63 | | - value = JSON.parse(value); |
64 | | - } catch(e) {} |
65 | | - } |
66 | | - |
67 | | - return converter ? converter(value) : value; |
| 26 | + function decode (value) { |
| 27 | + return value.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent); |
68 | 28 | } |
69 | 29 |
|
70 | 30 | function extend () { |
|
79 | 39 | return result; |
80 | 40 | } |
81 | 41 |
|
82 | | - var api = function (key, value, options) { |
83 | | - var converter, result; |
84 | | - var args = [].slice.call(arguments); |
| 42 | + function init(converter) { |
| 43 | + var processRead = function (value, name, json) { |
| 44 | + if (value.charAt(0) === '"') { |
| 45 | + value = value.slice(1, -1); |
| 46 | + } |
85 | 47 |
|
86 | | - if (typeof value === 'function') { |
87 | | - converter = value; |
88 | | - args.length = 1; |
89 | | - } |
| 48 | + value = converter && converter(value, name) || decode(value); |
90 | 49 |
|
91 | | - // Write |
| 50 | + if (json) { |
| 51 | + try { |
| 52 | + value = JSON.parse(value); |
| 53 | + } catch(e) {} |
| 54 | + } |
92 | 55 |
|
93 | | - if (args.length > 1) { |
94 | | - options = extend(api.defaults, options); |
| 56 | + return value; |
| 57 | + }; |
| 58 | + var api = function (key, value, options) { |
| 59 | + var result; |
| 60 | + var args = [].slice.call(arguments); |
95 | 61 |
|
96 | | - if (typeof options.expires === 'number') { |
97 | | - var expires = new Date(); |
98 | | - expires.setMilliseconds(expires.getMilliseconds() + options.expires * 864e+5); |
99 | | - options.expires = expires; |
100 | | - } |
| 62 | + // Write |
| 63 | + |
| 64 | + if (args.length > 1) { |
| 65 | + options = extend(api.defaults, options); |
101 | 66 |
|
102 | | - try { |
103 | | - result = JSON.stringify(value); |
104 | | - if (/^(?:\{[\w\W]*\}|\[[\w\W]*\])$/.test(result)) { |
105 | | - value = result; |
| 67 | + if (typeof options.expires === 'number') { |
| 68 | + var expires = new Date(); |
| 69 | + expires.setMilliseconds(expires.getMilliseconds() + options.expires * 864e+5); |
| 70 | + options.expires = expires; |
106 | 71 | } |
107 | | - } catch(e) {} |
108 | 72 |
|
109 | | - value = encode(String(value), unallowedCharsInValue); |
| 73 | + try { |
| 74 | + result = JSON.stringify(value); |
| 75 | + if (/^(?:\{[\w\W]*\}|\[[\w\W]*\])$/.test(result)) { |
| 76 | + value = result; |
| 77 | + } |
| 78 | + } catch(e) {} |
| 79 | + |
| 80 | + value = encodeURIComponent(String(value)); |
| 81 | + value = value.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent); |
| 82 | + |
| 83 | + key = encodeURIComponent(String(key)); |
| 84 | + key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent); |
| 85 | + key = key.replace(/[\(\)]/g, escape); |
| 86 | + |
| 87 | + return (document.cookie = [ |
| 88 | + key, '=', value, |
| 89 | + options.expires && '; expires=' + options.expires.toUTCString(), // use expires attribute, max-age is not supported by IE |
| 90 | + options.path && '; path=' + options.path, |
| 91 | + options.domain && '; domain=' + options.domain, |
| 92 | + options.secure && '; secure' |
| 93 | + ].join('')); |
| 94 | + } |
110 | 95 |
|
111 | | - return (document.cookie = [ |
112 | | - encode(key, unallowedCharsInName), '=', value, |
113 | | - options.expires && '; expires=' + options.expires.toUTCString(), // use expires attribute, max-age is not supported by IE |
114 | | - options.path && '; path=' + options.path, |
115 | | - options.domain && '; domain=' + options.domain, |
116 | | - options.secure && '; secure' |
117 | | - ].join('')); |
118 | | - } |
| 96 | + // Read |
119 | 97 |
|
120 | | - // Read |
| 98 | + if (!key) { |
| 99 | + result = {}; |
| 100 | + } |
121 | 101 |
|
122 | | - if (!key) { |
123 | | - result = {}; |
124 | | - } |
| 102 | + // To prevent the for loop in the first place assign an empty array |
| 103 | + // in case there are no cookies at all. Also prevents odd result when |
| 104 | + // calling "get()" |
| 105 | + var cookies = document.cookie ? document.cookie.split('; ') : []; |
| 106 | + var i = 0; |
125 | 107 |
|
126 | | - // To prevent the for loop in the first place assign an empty array |
127 | | - // in case there are no cookies at all. Also prevents odd result when |
128 | | - // calling "get()" |
129 | | - var cookies = document.cookie ? document.cookie.split('; ') : []; |
130 | | - var i = 0; |
| 108 | + for (; i < cookies.length; i++) { |
| 109 | + var parts = cookies[i].split('='), |
| 110 | + name = decode(parts.shift()), |
| 111 | + cookie = parts.join('='); |
131 | 112 |
|
132 | | - for (; i < cookies.length; i++) { |
133 | | - var parts = cookies[i].split('='), |
134 | | - name = decode(parts.shift(), unallowedCharsInName), |
135 | | - cookie = parts.join('='); |
| 113 | + if (key === name) { |
| 114 | + result = processRead(cookie, name, this.json); |
| 115 | + break; |
| 116 | + } |
136 | 117 |
|
137 | | - if (key === name) { |
138 | | - result = processRead(cookie, converter, this.json); |
139 | | - break; |
| 118 | + if (!key) { |
| 119 | + result[name] = processRead(cookie, name, this.json); |
| 120 | + } |
140 | 121 | } |
141 | 122 |
|
142 | | - if (!key) { |
143 | | - result[name] = processRead(cookie, converter, this.json); |
144 | | - } |
145 | | - } |
| 123 | + return result; |
| 124 | + }; |
146 | 125 |
|
147 | | - return result; |
148 | | - }; |
149 | | - |
150 | | - api.get = api.set = api; |
151 | | - api.getJSON = function() { |
152 | | - return api.apply({ |
153 | | - json: true |
154 | | - }, [].slice.call(arguments)); |
155 | | - }; |
156 | | - api.defaults = {}; |
157 | | - |
158 | | - api.remove = function (key, options) { |
159 | | - // Must not alter options, thus extending a fresh object... |
160 | | - api(key, '', extend(options, { expires: -1 })); |
161 | | - return !api(key); |
162 | | - }; |
163 | | - |
164 | | - return api; |
| 126 | + api.get = api.set = api; |
| 127 | + api.getJSON = function () { |
| 128 | + return api.apply({ |
| 129 | + json: true |
| 130 | + }, [].slice.call(arguments)); |
| 131 | + }; |
| 132 | + api.defaults = {}; |
| 133 | + |
| 134 | + api.remove = function (key, options) { |
| 135 | + // Must not alter options, thus extending a fresh object... |
| 136 | + api(key, '', extend(options, { expires: -1 })); |
| 137 | + return !api(key); |
| 138 | + }; |
| 139 | + |
| 140 | + api.withConverter = init; |
| 141 | + |
| 142 | + return api; |
| 143 | + } |
| 144 | + |
| 145 | + return init(); |
165 | 146 | })); |
0 commit comments