|
17 | 17 | window.Cookies = factory(); |
18 | 18 | } |
19 | 19 | }(function () { |
20 | | - function encode(s) { |
21 | | - return s |
22 | | - .replace(/"/g, '%22'); |
| 20 | + var unallowedChars = { |
| 21 | + ';': '%3B', |
| 22 | + ',': '%2C', |
| 23 | + '"': '%22' |
| 24 | + }; |
| 25 | + var unallowedCharsInName = extend(unallowedChars, { |
| 26 | + '=': '%3D', |
| 27 | + '\t': '%09' |
| 28 | + }); |
| 29 | + var unallowedCharsInValue = extend(unallowedChars, { |
| 30 | + ' ': '%20' |
| 31 | + }); |
| 32 | + |
| 33 | + function encode (value, charmap) { |
| 34 | + for (var character in charmap) { |
| 35 | + value = value |
| 36 | + .replace(new RegExp(character, 'g'), charmap[character]); |
| 37 | + } |
| 38 | + return value; |
| 39 | + } |
| 40 | + |
| 41 | + function decode (value, charmap) { |
| 42 | + for (var character in charmap) { |
| 43 | + value = value |
| 44 | + .replace(new RegExp(charmap[character], 'g'), character); |
| 45 | + } |
| 46 | + return value; |
23 | 47 | } |
24 | 48 |
|
25 | | - function decode(s) { |
26 | | - return s |
27 | | - .replace(/%22/g, '"'); |
| 49 | + function processWrite (value) { |
| 50 | + var stringified; |
| 51 | + try { |
| 52 | + stringified = JSON.stringify(value); |
| 53 | + if (/^(?:\{[\w\W]*\}|\[[\w\W]*\])$/.test(stringified)) { |
| 54 | + value = stringified; |
| 55 | + } |
| 56 | + } catch(e) {} |
| 57 | + return encode(String(value), unallowedCharsInValue); |
28 | 58 | } |
29 | 59 |
|
30 | | - function parseCookieValue(value) { |
| 60 | + function processRead (value, converter, json) { |
31 | 61 | if (value.indexOf('"') === 0) { |
32 | 62 | // This is a quoted cookie as according to RFC2068, unescape... |
33 | 63 | value = value.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\'); |
34 | 64 | } |
35 | 65 |
|
36 | | - value = decode(value); |
| 66 | + value = decode(value, unallowedCharsInValue); |
37 | 67 |
|
38 | | - try { |
39 | | - return api.json ? JSON.parse(value) : value; |
40 | | - } catch(e) {} |
41 | | - } |
| 68 | + if (json) { |
| 69 | + try { |
| 70 | + value = JSON.parse(value); |
| 71 | + } catch(e) {} |
| 72 | + } |
42 | 73 |
|
43 | | - function read(s, converter) { |
44 | | - var value = parseCookieValue(s); |
45 | 74 | return isFunction(converter) ? converter(value) : value; |
46 | 75 | } |
47 | 76 |
|
48 | | - function extend() { |
| 77 | + function extend () { |
49 | 78 | var key, options; |
50 | 79 | var i = 0; |
51 | 80 | var result = {}; |
|
58 | 87 | return result; |
59 | 88 | } |
60 | 89 |
|
61 | | - function isFunction(obj) { |
| 90 | + function isFunction (obj) { |
62 | 91 | return Object.prototype.toString.call(obj) === '[object Function]'; |
63 | 92 | } |
64 | 93 |
|
65 | 94 | var api = function (key, value, options) { |
| 95 | + var converter; |
| 96 | + |
| 97 | + if (isFunction(value)) { |
| 98 | + converter = value; |
| 99 | + value = undefined; |
| 100 | + } |
66 | 101 |
|
67 | 102 | // Write |
68 | 103 |
|
69 | | - if (arguments.length > 1 && !isFunction(value)) { |
| 104 | + if (arguments.length > 1 && !converter) { |
70 | 105 | options = extend(api.defaults, options); |
71 | 106 |
|
72 | 107 | if (typeof options.expires === 'number') { |
|
75 | 110 | } |
76 | 111 |
|
77 | 112 | return (document.cookie = [ |
78 | | - key, '=', encode(api.json ? JSON.stringify(value) : String(value)), |
| 113 | + encode(key, unallowedCharsInName), '=', processWrite(value), |
79 | 114 | options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE |
80 | 115 | options.path ? '; path=' + options.path : '', |
81 | 116 | options.domain ? '; domain=' + options.domain : '', |
|
95 | 130 |
|
96 | 131 | for (; i < l; i++) { |
97 | 132 | var parts = cookies[i].split('='), |
98 | | - name = parts.shift(), |
| 133 | + name = decode(parts.shift(), unallowedCharsInName), |
99 | 134 | cookie = parts.join('='); |
100 | 135 |
|
101 | 136 | if (key === name) { |
102 | | - // If second argument (value) is a function it's a converter... |
103 | | - result = read(cookie, value); |
| 137 | + result = processRead(cookie, converter, this.json); |
104 | 138 | break; |
105 | 139 | } |
106 | 140 |
|
107 | | - // Prevent storing a cookie that we couldn't decode. |
108 | | - if (!key && (cookie = read(cookie)) !== undefined) { |
109 | | - result[name] = cookie; |
| 141 | + if (!key) { |
| 142 | + result[name] = processRead(cookie, converter, this.json); |
110 | 143 | } |
111 | 144 | } |
112 | 145 |
|
113 | 146 | return result; |
114 | 147 | }; |
115 | 148 |
|
116 | 149 | api.get = api.set = api; |
| 150 | + api.getJSON = function() { |
| 151 | + return api.get.apply({ |
| 152 | + json: true |
| 153 | + }, [].slice.call(arguments)); |
| 154 | + }; |
117 | 155 | api.defaults = {}; |
118 | 156 |
|
119 | 157 | api.remove = function (key, options) { |
|
0 commit comments