js-cookie 2.2.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,20 +1,21 @@
1
- Copyright 2014 Klaus Hartl
1
+ MIT License
2
2
 
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
3
+ Copyright (c) 2018 Copyright 2018 Klaus Hartl, Fagner Brack, GitHub Contributors
10
4
 
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
13
11
 
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -20,7 +20,7 @@ A simple, lightweight JavaScript API for handling cookies
20
20
  **If you're viewing this at https://github.com/js-cookie/js-cookie, you're reading the documentation for the master branch.
21
21
  [View documentation for the latest release.](https://github.com/js-cookie/js-cookie/tree/latest#readme)**
22
22
 
23
- ## Build Status Matrix
23
+ ## Build Status Matrix ([including active Pull Requests](https://github.com/js-cookie/js-cookie/issues/286))
24
24
 
25
25
  [![Selenium Test Status](https://saucelabs.com/browser-matrix/js-cookie.svg)](https://saucelabs.com/u/js-cookie)
26
26
 
@@ -47,9 +47,14 @@ in Internet Explorer on Windows 7 for instance (because of the wrong MIME type).
47
47
 
48
48
  JavaScript Cookie supports [npm](https://www.npmjs.com/package/js-cookie) and [Bower](http://bower.io/search/?q=js-cookie) under the name `js-cookie`.
49
49
 
50
+ #### NPM
51
+ ```
52
+ $ npm install js-cookie --save
53
+ ```
54
+
50
55
  ### Module Loaders
51
56
 
52
- JavaScript Cookie can also be loaded as an AMD, CommonJS or [ES6](https://github.com/js-cookie/js-cookie/issues/233#issuecomment-233187386) module.
57
+ JavaScript Cookie can also be loaded as an AMD or CommonJS module.
53
58
 
54
59
  ## Basic Usage
55
60
 
@@ -84,6 +89,16 @@ Read all visible cookies:
84
89
  Cookies.get(); // => { name: 'value' }
85
90
  ```
86
91
 
92
+ *Note: It is not possible to read a particular cookie by passing one of the cookie attributes (which may or may not
93
+ have been used when writing the cookie in question):*
94
+
95
+ ```javascript
96
+ Cookies.get('foo', { domain: 'sub.example.com' }); // `domain` won't have any effect...!
97
+ ```
98
+
99
+ The cookie with the name `foo` will only be available on `.get()` if it's visible from where the
100
+ code is called; the domain and/or path attribute will not have an effect when reading.
101
+
87
102
  Delete cookie:
88
103
 
89
104
  ```javascript
@@ -98,9 +113,13 @@ Cookies.remove('name'); // fail!
98
113
  Cookies.remove('name', { path: '' }); // removed!
99
114
  ```
100
115
 
101
- *IMPORTANT! when deleting a cookie, you must pass the exact same path and domain attributes that was used to set the cookie, unless you're relying on the [default attributes](#cookie-attributes).*
116
+ *IMPORTANT! When deleting a cookie and you're not relying on the [default attributes](#cookie-attributes), you must pass the exact same path and domain attributes that were used to set the cookie:*
117
+
118
+ ```javascript
119
+ Cookies.remove('name', { path: '', domain: '.yourdomain.com' });
120
+ ```
102
121
 
103
- *Note: Removing unexisting cookie does not raise any exception nor return any value*
122
+ *Note: Removing a nonexistent cookie does not raise any exception nor return any value.*
104
123
 
105
124
  ## Namespace conflicts
106
125
 
@@ -152,6 +171,8 @@ This project is [RFC 6265](http://tools.ietf.org/html/rfc6265#section-4.1.1) com
152
171
  The only character in cookie-name or cookie-value that is allowed and still encoded is the percent `%` character, it is escaped in order to interpret percent input as literal.
153
172
  Please note that the default encoding/decoding strategy is meant to be interoperable [only between cookies that are read/written by js-cookie](https://github.com/js-cookie/js-cookie/pull/200#discussion_r63270778). To override the default encoding/decoding strategy you need to use a [converter](#converters).
154
173
 
174
+ *Note: According to [RFC 6265](https://tools.ietf.org/html/rfc6265#section-6.1), your cookies may get deleted if they are too big or there are too many cookies in the same domain, [more details here](https://github.com/js-cookie/js-cookie/wiki/Frequently-Asked-Questions#why-are-my-cookies-being-deleted).*
175
+
155
176
  ## Cookie Attributes
156
177
 
157
178
  Cookie attributes defaults can be set globally by setting properties of the `Cookies.defaults` object or individually for each call to `Cookies.set(...)` by passing a plain object in the last argument. Per-call attributes override the default attributes.
@@ -192,7 +213,9 @@ Cookies.remove('name', { path: '' });
192
213
 
193
214
  (From [Internet Explorer Cookie Internals (FAQ)](http://blogs.msdn.com/b/ieinternals/archive/2009/08/20/wininet-ie-cookie-internals-faq.aspx))
194
215
 
195
- This means one cannot set a path using `path: window.location.pathname` in case such pathname contains a filename like so: `/check.html` (or at least, such cookie cannot be read correctly).
216
+ This means one cannot set a path using `window.location.pathname` in case such pathname contains a filename like so: `/check.html` (or at least, such cookie cannot be read correctly).
217
+
218
+ In fact, you should never allow untrusted input to set the cookie attributes or you might be exposed to a [XSS attack](https://github.com/js-cookie/js-cookie/issues/396).
196
219
 
197
220
  ### domain
198
221
 
package/SERVER_SIDE.md CHANGED
@@ -32,7 +32,7 @@ var PHPCookies = Cookies.withConverter({
32
32
  write: function (value) {
33
33
  // Encode all characters according to the "encodeURIComponent" spec
34
34
  return encodeURIComponent(value)
35
- // Revert the characters that are unnecessarly encoded but are
35
+ // Revert the characters that are unnecessarily encoded but are
36
36
  // allowed in a cookie value, except for the plus sign (%2B)
37
37
  .replace(/%(23|24|26|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
38
38
  },
@@ -62,7 +62,7 @@ var TomcatCookies = Cookies.withConverter({
62
62
  write: function (value) {
63
63
  // Encode all characters according to the "encodeURIComponent" spec
64
64
  return encodeURIComponent(value)
65
- // Revert the characters that are unnecessarly encoded but are
65
+ // Revert the characters that are unnecessarily encoded but are
66
66
  // allowed in a cookie value
67
67
  .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent)
68
68
  // Encode the parens that are interpreted incorrectly by Tomcat
@@ -95,7 +95,7 @@ var JBossCookies = Cookies.withConverter({
95
95
  write: function (value) {
96
96
  // Encode all characters according to the "encodeURIComponent" spec
97
97
  return encodeURIComponent(value)
98
- // Revert the characters that are unnecessarly encoded but are
98
+ // Revert the characters that are unnecessarily encoded but are
99
99
  // allowed in a cookie value
100
100
  .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent)
101
101
  // Encode again the characters that are not allowed in JBoss 7.1.1, like "[" and "]":
package/package.json CHANGED
@@ -1,13 +1,12 @@
1
1
  {
2
2
  "name": "js-cookie",
3
- "version": "2.2.0",
3
+ "version": "2.2.1",
4
4
  "description": "A simple, lightweight JavaScript API for handling cookies",
5
5
  "main": "src/js.cookie.js",
6
6
  "directories": {
7
7
  "test": "test"
8
8
  },
9
9
  "keywords": [
10
- "jquery-plugin",
11
10
  "cookie",
12
11
  "cookies",
13
12
  "browser",
@@ -32,15 +31,14 @@
32
31
  "author": "Klaus Hartl",
33
32
  "license": "MIT",
34
33
  "devDependencies": {
35
- "grunt": "1.0.1",
34
+ "grunt": "1.0.3",
36
35
  "grunt-compare-size": "0.4.2",
37
- "grunt-contrib-connect": "1.0.2",
38
- "grunt-contrib-jshint": "1.1.0",
39
- "grunt-contrib-nodeunit": "1.0.0",
36
+ "grunt-contrib-connect": "2.0.0",
37
+ "grunt-contrib-nodeunit": "2.0.0",
40
38
  "grunt-contrib-qunit": "2.0.0",
41
39
  "grunt-contrib-uglify": "2.3.0",
42
- "grunt-contrib-watch": "1.0.0",
43
- "grunt-jscs": "3.0.1",
40
+ "grunt-contrib-watch": "1.1.0",
41
+ "grunt-eslint": "21.0.0",
44
42
  "grunt-saucelabs": "9.0.0",
45
43
  "gzip-js": "0.3.2",
46
44
  "qunitjs": "1.23.1",
package/src/js.cookie.js CHANGED
@@ -1,12 +1,12 @@
1
1
  /*!
2
- * JavaScript Cookie v2.2.0
2
+ * JavaScript Cookie v2.2.1
3
3
  * https://github.com/js-cookie/js-cookie
4
4
  *
5
5
  * Copyright 2006, 2015 Klaus Hartl & Fagner Brack
6
6
  * Released under the MIT license
7
7
  */
8
8
  ;(function (factory) {
9
- var registeredInModuleLoader = false;
9
+ var registeredInModuleLoader;
10
10
  if (typeof define === 'function' && define.amd) {
11
11
  define(factory);
12
12
  registeredInModuleLoader = true;
@@ -36,126 +36,124 @@
36
36
  return result;
37
37
  }
38
38
 
39
+ function decode (s) {
40
+ return s.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);
41
+ }
42
+
39
43
  function init (converter) {
40
- function api (key, value, attributes) {
41
- var result;
44
+ function api() {}
45
+
46
+ function set (key, value, attributes) {
42
47
  if (typeof document === 'undefined') {
43
48
  return;
44
49
  }
45
50
 
46
- // Write
47
-
48
- if (arguments.length > 1) {
49
- attributes = extend({
50
- path: '/'
51
- }, api.defaults, attributes);
52
-
53
- if (typeof attributes.expires === 'number') {
54
- var expires = new Date();
55
- expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5);
56
- attributes.expires = expires;
57
- }
51
+ attributes = extend({
52
+ path: '/'
53
+ }, api.defaults, attributes);
58
54
 
59
- // We're using "expires" because "max-age" is not supported by IE
60
- attributes.expires = attributes.expires ? attributes.expires.toUTCString() : '';
55
+ if (typeof attributes.expires === 'number') {
56
+ attributes.expires = new Date(new Date() * 1 + attributes.expires * 864e+5);
57
+ }
61
58
 
62
- try {
63
- result = JSON.stringify(value);
64
- if (/^[\{\[]/.test(result)) {
65
- value = result;
66
- }
67
- } catch (e) {}
59
+ // We're using "expires" because "max-age" is not supported by IE
60
+ attributes.expires = attributes.expires ? attributes.expires.toUTCString() : '';
68
61
 
69
- if (!converter.write) {
70
- value = encodeURIComponent(String(value))
71
- .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
72
- } else {
73
- value = converter.write(value, key);
62
+ try {
63
+ var result = JSON.stringify(value);
64
+ if (/^[\{\[]/.test(result)) {
65
+ value = result;
74
66
  }
67
+ } catch (e) {}
75
68
 
76
- key = encodeURIComponent(String(key));
77
- key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent);
78
- key = key.replace(/[\(\)]/g, escape);
69
+ value = converter.write ?
70
+ converter.write(value, key) :
71
+ encodeURIComponent(String(value))
72
+ .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
79
73
 
80
- var stringifiedAttributes = '';
74
+ key = encodeURIComponent(String(key))
75
+ .replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent)
76
+ .replace(/[\(\)]/g, escape);
81
77
 
82
- for (var attributeName in attributes) {
83
- if (!attributes[attributeName]) {
84
- continue;
85
- }
86
- stringifiedAttributes += '; ' + attributeName;
87
- if (attributes[attributeName] === true) {
88
- continue;
89
- }
90
- stringifiedAttributes += '=' + attributes[attributeName];
78
+ var stringifiedAttributes = '';
79
+ for (var attributeName in attributes) {
80
+ if (!attributes[attributeName]) {
81
+ continue;
91
82
  }
92
- return (document.cookie = key + '=' + value + stringifiedAttributes);
83
+ stringifiedAttributes += '; ' + attributeName;
84
+ if (attributes[attributeName] === true) {
85
+ continue;
86
+ }
87
+
88
+ // Considers RFC 6265 section 5.2:
89
+ // ...
90
+ // 3. If the remaining unparsed-attributes contains a %x3B (";")
91
+ // character:
92
+ // Consume the characters of the unparsed-attributes up to,
93
+ // not including, the first %x3B (";") character.
94
+ // ...
95
+ stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];
93
96
  }
94
97
 
95
- // Read
98
+ return (document.cookie = key + '=' + value + stringifiedAttributes);
99
+ }
96
100
 
97
- if (!key) {
98
- result = {};
101
+ function get (key, json) {
102
+ if (typeof document === 'undefined') {
103
+ return;
99
104
  }
100
105
 
106
+ var jar = {};
101
107
  // To prevent the for loop in the first place assign an empty array
102
- // in case there are no cookies at all. Also prevents odd result when
103
- // calling "get()"
108
+ // in case there are no cookies at all.
104
109
  var cookies = document.cookie ? document.cookie.split('; ') : [];
105
- var rdecode = /(%[0-9A-Z]{2})+/g;
106
110
  var i = 0;
107
111
 
108
112
  for (; i < cookies.length; i++) {
109
113
  var parts = cookies[i].split('=');
110
114
  var cookie = parts.slice(1).join('=');
111
115
 
112
- if (!this.json && cookie.charAt(0) === '"') {
116
+ if (!json && cookie.charAt(0) === '"') {
113
117
  cookie = cookie.slice(1, -1);
114
118
  }
115
119
 
116
120
  try {
117
- var name = parts[0].replace(rdecode, decodeURIComponent);
118
- cookie = converter.read ?
119
- converter.read(cookie, name) : converter(cookie, name) ||
120
- cookie.replace(rdecode, decodeURIComponent);
121
+ var name = decode(parts[0]);
122
+ cookie = (converter.read || converter)(cookie, name) ||
123
+ decode(cookie);
121
124
 
122
- if (this.json) {
125
+ if (json) {
123
126
  try {
124
127
  cookie = JSON.parse(cookie);
125
128
  } catch (e) {}
126
129
  }
127
130
 
131
+ jar[name] = cookie;
132
+
128
133
  if (key === name) {
129
- result = cookie;
130
134
  break;
131
135
  }
132
-
133
- if (!key) {
134
- result[name] = cookie;
135
- }
136
136
  } catch (e) {}
137
137
  }
138
138
 
139
- return result;
139
+ return key ? jar[key] : jar;
140
140
  }
141
141
 
142
- api.set = api;
142
+ api.set = set;
143
143
  api.get = function (key) {
144
- return api.call(api, key);
144
+ return get(key, false /* read as raw */);
145
145
  };
146
- api.getJSON = function () {
147
- return api.apply({
148
- json: true
149
- }, [].slice.call(arguments));
146
+ api.getJSON = function (key) {
147
+ return get(key, true /* read as json */);
150
148
  };
151
- api.defaults = {};
152
-
153
149
  api.remove = function (key, attributes) {
154
- api(key, '', extend(attributes, {
150
+ set(key, '', extend(attributes, {
155
151
  expires: -1
156
152
  }));
157
153
  };
158
154
 
155
+ api.defaults = {};
156
+
159
157
  api.withConverter = init;
160
158
 
161
159
  return api;