Skip to content

Commit dbce759

Browse files
icamysFagnerMartinsBrack
authored andcommitted
Add semicolon to the begining of the library source
This considers when the library is included after a script that uses an IIFE but doesn't end with a semicolon. Closes js-cookiegh-171.
1 parent 4a4122e commit dbce759

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

src/js.cookie.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Copyright 2006, 2015 Klaus Hartl & Fagner Brack
66
* Released under the MIT license
77
*/
8-
(function (factory) {
8+
;(function (factory) {
99
if (typeof define === 'function' && define.amd) {
1010
define(factory);
1111
} else if (typeof exports === 'object') {

test/.jshintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"unescape": true,
1111
"lifecycle": true,
1212
"using": true,
13-
"addEvent": true
13+
"addEvent": true,
14+
"loadFileSync": true
1415
}
1516
}

test/missing_semicolon.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
<link href="../node_modules/qunitjs/qunit/qunit.css" rel="stylesheet">
7+
<script src="../node_modules/qunitjs/qunit/qunit.js"></script>
8+
<script src="utils.js"></script>
9+
<script>
10+
(function() {
11+
var contents = window.loadFileSync('../src/js.cookie.js');
12+
13+
if (contents !== null) {
14+
var script = document.createElement('script');
15+
script.innerHTML = '(function (){ return {}; })() ' + contents;
16+
document.getElementsByTagName('head')[0].appendChild(script);
17+
}
18+
})();
19+
</script>
20+
</head>
21+
<body>
22+
23+
</body>
24+
</html>

test/tests.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,25 @@ QUnit.test('malformed cookie value in IE', function (assert) {
6363
document.body.appendChild(iframe);
6464
});
6565

66+
// github.com/js-cookie/js-cookie/pull/171
67+
QUnit.test('missing leading semicolon', function (assert) {
68+
assert.expect(1);
69+
var done = assert.async();
70+
// Sandbox in an iframe so that we can poke around with document.cookie.
71+
var iframe = document.createElement('iframe');
72+
var loadedSuccessfully = true;
73+
iframe.src = 'missing_semicolon.html';
74+
75+
addEvent(iframe, 'load', function () {
76+
iframe.contentWindow.onerror = function () {
77+
loadedSuccessfully = false;
78+
};
79+
assert.strictEqual(loadedSuccessfully, true, 'can\'t throw Object is not a function error');
80+
done();
81+
});
82+
document.body.appendChild(iframe);
83+
});
84+
6685
QUnit.test('Call to read all when there are cookies', function (assert) {
6786
Cookies.set('c', 'v');
6887
Cookies.set('foo', 'bar');

test/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,12 @@
111111
setCookie: setCookie
112112
};
113113
};
114+
115+
window.loadFileSync = function (path) {
116+
var xhr = new XMLHttpRequest();
117+
xhr.open('GET', path, false);
118+
xhr.send(null);
119+
return xhr.status === 200 ? xhr.responseText : null;
120+
};
121+
114122
}());

0 commit comments

Comments
 (0)