Skip to content

Commit a0ca45a

Browse files
Remove encoding operations and raw option
Everything is going to be handled by the RFC 6265 implementation.
1 parent e00ffbb commit a0ca45a

2 files changed

Lines changed: 11 additions & 77 deletions

File tree

‎src/js.cookie.js‎

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,20 @@
1717
window.Cookies = factory();
1818
}
1919
}(function () {
20-
21-
var pluses = /\+/g;
22-
23-
function encode(s) {
24-
return api.raw ? s : encodeURIComponent(s);
25-
}
26-
27-
function decode(s) {
28-
return api.raw ? s : decodeURIComponent(s);
29-
}
30-
31-
function stringifyCookieValue(value) {
32-
return encode(api.json ? JSON.stringify(value) : String(value));
33-
}
34-
3520
function parseCookieValue(s) {
21+
var value = s;
3622
if (s.indexOf('"') === 0) {
3723
// This is a quoted cookie as according to RFC2068, unescape...
38-
s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
24+
value = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
3925
}
4026

4127
try {
42-
// Replace server-side written pluses with spaces.
43-
// If we can't decode the cookie, ignore it, it's unusable.
44-
// If we can't parse the cookie, ignore it, it's unusable.
45-
s = decodeURIComponent(s.replace(pluses, ' '));
46-
return api.json ? JSON.parse(s) : s;
28+
return api.json ? JSON.parse(s) : value;
4729
} catch(e) {}
4830
}
4931

5032
function read(s, converter) {
51-
var value = api.raw ? s : parseCookieValue(s);
33+
var value = parseCookieValue(s);
5234
return isFunction(converter) ? converter(value) : value;
5335
}
5436

@@ -82,7 +64,7 @@
8264
}
8365

8466
return (document.cookie = [
85-
encode(key), '=', stringifyCookieValue(value),
67+
key, '=', api.json ? JSON.stringify(value) : String(value),
8668
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
8769
options.path ? '; path=' + options.path : '',
8870
options.domain ? '; domain=' + options.domain : '',
@@ -102,7 +84,7 @@
10284

10385
for (; i < l; i++) {
10486
var parts = cookies[i].split('='),
105-
name = decode(parts.shift()),
87+
name = parts.shift(),
10688
cookie = parts.join('=');
10789

10890
if (key === name) {

‎test/tests.js‎

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
var lifecycle = {
3737
teardown: function () {
3838
Cookies.defaults = {};
39-
delete Cookies.raw;
4039
delete Cookies.json;
4140
Object.keys(Cookies.get()).forEach(Cookies.remove);
4241
}
@@ -70,26 +69,9 @@ test('RFC 2068 quoted string', function () {
7069
strictEqual(Cookies.get('c'), 'v@address.com"\\"', 'should decode RFC 2068 quoted string');
7170
});
7271

73-
test('decode', function () {
72+
// github.com/carhartl/jquery-cookie/issues/50
73+
test('equality sign in cookie value', function () {
7474
expect(1);
75-
document.cookie = encodeURIComponent(' c') + '=' + encodeURIComponent(' v');
76-
strictEqual(Cookies.get(' c'), ' v', 'should decode key and value');
77-
});
78-
79-
test('decode pluses to space for server side written cookie', function () {
80-
expect(1);
81-
document.cookie = 'c=foo+bar';
82-
strictEqual(Cookies.get('c'), 'foo bar', 'should convert pluses back to space');
83-
});
84-
85-
test('raw = true', function () {
86-
expect(2);
87-
Cookies.raw = true;
88-
89-
document.cookie = 'c=%20v';
90-
strictEqual(Cookies.get('c'), '%20v', 'should not decode value');
91-
92-
// see https://github.com/carhartl/jquery-cookie/issues/50
9375
Cookies.set('c', 'foo=bar');
9476
strictEqual(Cookies.get('c'), 'foo=bar', 'should include the entire value');
9577
});
@@ -141,13 +123,10 @@ test('invalid JSON string with json = true', function () {
141123
}
142124
});
143125

144-
test('invalid URL encoding', function () {
126+
test('percent character in cookie value', function () {
145127
expect(1);
146128
document.cookie = 'bad=foo%';
147-
strictEqual(Cookies.get('bad'), undefined, "won't throw exception, returns undefined");
148-
// Delete manually here because it requires raw === true...
149-
Cookies.raw = true;
150-
Cookies.remove('bad');
129+
strictEqual(Cookies.get('bad'), 'foo%', 'should read the percent character');
151130
});
152131

153132
asyncTest('malformed cookie value in IE (#88, #117)', function () {
@@ -193,17 +172,6 @@ test('Call to read all with json: true', function () {
193172
deepEqual(Cookies.get(), { c: { foo: 'bar' } }, 'returns JSON parsed cookies');
194173
});
195174

196-
test('Call to read all with a badly encoded cookie', function () {
197-
expect(1);
198-
document.cookie = 'bad=foo%';
199-
document.cookie = 'good=foo';
200-
deepEqual(Cookies.get(), { good: 'foo' }, 'returns object containing all decodable cookies');
201-
// Delete manually here because it requires raw === true...
202-
Cookies.raw = true;
203-
Cookies.remove('bad');
204-
});
205-
206-
207175
module('write', lifecycle);
208176

209177
test('String primitive', function () {
@@ -282,21 +250,13 @@ test('defaults', function () {
282250
ok(Cookies.set('c', 'v', { path: '/bar' }).match(/path=\/bar/), 'options argument has precedence');
283251
});
284252

285-
test('raw = true', function () {
286-
expect(1);
287-
Cookies.raw = true;
288-
strictEqual(Cookies.set('c[1]', 'v[1]'), 'c[1]=v[1]', 'should not encode');
289-
// Delete manually here because it requires raw === true...
290-
Cookies.remove('c[1]');
291-
});
292-
293253
test('json = true', function () {
294254
expect(1);
295255
Cookies.json = true;
296256

297257
if ('JSON' in window) {
298258
Cookies.set('c', { foo: 'bar' });
299-
strictEqual(document.cookie, 'c=' + encodeURIComponent(JSON.stringify({ foo: 'bar' })), 'should stringify JSON');
259+
strictEqual(document.cookie, 'c=' + JSON.stringify({ foo: 'bar' }), 'should stringify JSON');
300260
} else {
301261
ok(true);
302262
}
@@ -341,7 +301,6 @@ test('passing options reference', function () {
341301

342302
test('[] used in name', function () {
343303
expect(1);
344-
Cookies.raw = true;
345304
document.cookie = 'c[1]=foo';
346305
Cookies.remove('c[1]');
347306
strictEqual(document.cookie, '', 'delete the cookie');
@@ -355,10 +314,3 @@ test('read converter', function() {
355314
Cookies.set('c', '1');
356315
strictEqual(Cookies.get('c', Number), 1, 'converts read value');
357316
});
358-
359-
test('read converter with raw = true', function() {
360-
expect(1);
361-
Cookies.raw = true;
362-
Cookies.set('c', '1');
363-
strictEqual(Cookies.get('c', Number), 1, 'does not decode, but converts read value');
364-
});

0 commit comments

Comments
 (0)