Skip to content

Commit fd254dd

Browse files
committed
Fixing IE issue, where cookie values were not read correctly because
of duplicate occurrences of "; ", fixes carhartl#88, fixes carhartl#117.
1 parent c2b8055 commit fd254dd

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

jquery.cookie.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
// read
4949
var decode = config.raw ? raw : decoded;
5050
var cookies = document.cookie.split('; ');
51-
for (var i = 0, parts; (parts = cookies[i] && cookies[i].split('=')); i++) {
51+
for (var i = 0, l = cookies.length; i < l; i++) {
52+
var parts = cookies[i].split('=');
5253
if (decode(parts.shift()) === key) {
5354
var cookie = decode(parts.join('='));
5455
return config.json ? JSON.parse(cookie) : cookie;

sandbox.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title></title>
5+
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
6+
<script src="jquery.cookie.js"></script>
7+
<script>
8+
try {
9+
Object.defineProperty(document, "cookie", { get: function() { return "first=one; ; second=two"; } });
10+
window.testValue = $.cookie("second");
11+
window.ok = true;
12+
} catch (er) {
13+
}
14+
</script>
15+
</head>
16+
<body>
17+
</body>
18+
</html>

test.js

+18
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@ test('json: true', 1, function () {
6767
}
6868
});
6969

70+
asyncTest('malformed cookie value in IE (#88, #117)', 1, function() {
71+
// Sandbox in an iframe so that we can poke around with document.cookie.
72+
var iframe = document.createElement('iframe');
73+
iframe.onload = function() {
74+
start();
75+
if (iframe.contentWindow.ok) {
76+
equal(iframe.contentWindow.testValue, 'two', 'reads all cookie values, skipping duplicate occurences of "; "');
77+
} else {
78+
// Skip the test where we can't stub document.cookie using
79+
// Object.defineProperty. Seems to work fine in
80+
// Chrome, Firefox and IE 8+.
81+
ok(true, 'N/A');
82+
}
83+
};
84+
iframe.src = '/sandbox.html';
85+
document.body.appendChild(iframe)
86+
});
87+
7088

7189
module('write', before);
7290

0 commit comments

Comments
 (0)