|
17 | 17 |
|
18 | 18 | var pluses = /\+/g; |
19 | 19 |
|
| 20 | + function encode(s) { |
| 21 | + return config.raw ? s : encodeURIComponent(s); |
| 22 | + } |
| 23 | + |
20 | 24 | function decode(s) { |
21 | | - if (config.raw) { |
22 | | - return s; |
23 | | - } |
24 | | - try { |
25 | | - // If we can't decode the cookie, ignore it, it's unusable. |
26 | | - return decodeURIComponent(s.replace(pluses, ' ')); |
27 | | - } catch(e) {} |
| 25 | + return config.raw ? s : decodeURIComponent(s); |
| 26 | + } |
| 27 | + |
| 28 | + function stringifyCookieValue(value) { |
| 29 | + return encode(config.json ? JSON.stringify(value) : String(value)); |
28 | 30 | } |
29 | 31 |
|
30 | | - function decodeAndParse(s) { |
| 32 | + function parseCookieValue(s) { |
31 | 33 | if (s.indexOf('"') === 0) { |
32 | 34 | // This is a quoted cookie as according to RFC2068, unescape... |
33 | 35 | s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\'); |
34 | 36 | } |
35 | 37 |
|
36 | | - s = decode(s); |
| 38 | + try { |
| 39 | + // Replace server-side written pluses with spaces. |
| 40 | + // If we can't decode the cookie, ignore it, it's unusable. |
| 41 | + s = decodeURIComponent(s.replace(pluses, ' ')); |
| 42 | + } catch(e) { |
| 43 | + return; |
| 44 | + } |
37 | 45 |
|
38 | 46 | try { |
39 | 47 | // If we can't parse the cookie, ignore it, it's unusable. |
40 | 48 | return config.json ? JSON.parse(s) : s; |
41 | 49 | } catch(e) {} |
42 | 50 | } |
43 | 51 |
|
| 52 | + function read(s, converter) { |
| 53 | + var value = config.raw ? s : parseCookieValue(s); |
| 54 | + return $.isFunction(converter) ? converter(value) : value; |
| 55 | + } |
| 56 | + |
44 | 57 | var config = $.cookie = function (key, value, options) { |
45 | 58 |
|
46 | 59 | // Write |
47 | | - if (value !== undefined) { |
| 60 | + if (value !== undefined && !$.isFunction(value)) { |
48 | 61 | options = $.extend({}, config.defaults, options); |
49 | 62 |
|
50 | 63 | if (typeof options.expires === 'number') { |
51 | 64 | var days = options.expires, t = options.expires = new Date(); |
52 | 65 | t.setDate(t.getDate() + days); |
53 | 66 | } |
54 | 67 |
|
55 | | - value = config.json ? JSON.stringify(value) : String(value); |
56 | | - |
57 | 68 | return (document.cookie = [ |
58 | | - config.raw ? key : encodeURIComponent(key), |
59 | | - '=', |
60 | | - config.raw ? value : encodeURIComponent(value), |
| 69 | + encode(key), '=', stringifyCookieValue(value), |
61 | 70 | options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE |
62 | 71 | options.path ? '; path=' + options.path : '', |
63 | 72 | options.domain ? '; domain=' + options.domain : '', |
|
80 | 89 | var cookie = parts.join('='); |
81 | 90 |
|
82 | 91 | if (key && key === name) { |
83 | | - result = decodeAndParse(cookie); |
| 92 | + // If second argument (value) is a function it's a converter... |
| 93 | + result = read(cookie, value); |
84 | 94 | break; |
85 | 95 | } |
86 | 96 |
|
87 | 97 | // Prevent storing a cookie that we couldn't decode. |
88 | | - if (!key && (cookie = decodeAndParse(cookie)) !== undefined) { |
| 98 | + if (!key && (cookie = read(cookie)) !== undefined) { |
89 | 99 | result[name] = cookie; |
90 | 100 | } |
91 | 101 | } |
|
0 commit comments