-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathiron-cookie.js
177 lines (164 loc) · 4.81 KB
/
iron-cookie.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* @license
iron-cookie, a Polymer element for managing cookies.
Copyright (C) 2016 Kevin Boxhoorn
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';
/**
* Polymer element for managing cookies.
* @customElement
* @polymer
* @demo demo/index.html
*/
class IronCookieElement extends PolymerElement {
static get properties() {
return {
/**
* Name of the cookie to manipulate.
*/
name: {
type: String,
observer: "_nameChanged"
},
/**
* Value of the cookie.
*/
value: {
type: String,
notify: true,
observer: "_setCookieWrapper"
},
/**
* Length of time to keep the cookie before expiring it. Can be a
* `Number` of days, UTC time `String`, or `Object` with values
* corresponding to the arguments of the JavaScript `Date`
* contructor (e.g. `{year: 2018, month: 11, day: 31 }`).
* See [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) for more info.
* @type {String | Number | Object}
*/
expires: {
type: Object,
observer: "_setCookieWrapper"
},
/**
* URL path that the cookie is available to.
*/
path: {
type: String,
value: "/"
},
/**
* Toggles the `secure` flag of the cookie. This makes it only get
* sent with HTTPS requests.
*/
secure: {
type: Boolean,
observer: "_setCookieWrapper",
value: false
},
/**
* Whether to encode and decode the cookie using `encodeURIComponent`
* and `decodeURIComponent`. This allows values such as `;` and `=`
* to be a part of the cookie without it being corrupted.
* Recommended if data bound to user input.
*/
uriSafe: {
type: Boolean,
value: false
},
/**
* Get the cookie manually using the `getCookie` method. This
* disables automatically loading the cookie when the element is
* initialized.
*/
manualGet: {
type: Boolean,
value: false
},
/**
* Set the cookie manually using the `setCookie` method. This
* disables automatically setting the cookie when `value` changes.
*/
manualSet: {
type: Boolean,
value: false
}
};
}
_nameChanged() {
this.value = this._getCookie();
}
_getCookie(force = false) {
if (this.manualGet && !force)
return "";
var name = this.name + "=";
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
while (cookie.charAt(0) == " ")
cookie = cookie.substring(1);
if (cookie.indexOf(name) == 0) {
cookie = cookie.substring(name.length, cookie.length);
return this.uriSafe ? decodeURIComponent(cookie) : cookie;
}
}
return "";
}
_setCookie(force = false) {
if (this.manualSet && !force)
return;
if (this.name === undefined || this.value === undefined)
return;
var date = new Date();
var expiresTime;
if (typeof this.expires == "number")
expiresTime = this.expires * 24 * 60 * 60 * 1000;
else if (typeof this.expires == "string")
expiresTime = new Date(this.expires).getTime();
else if (typeof this.expires == "object")
expiresTime = new Date(this.expires.year, this.expires.month, this.expires.day, this.expires.hour, this.expires.minutes, this.expires.seconds, this.expires.milliseconds).getTime();
date.setTime(date.getTime() + (expiresTime || 0));
var value = this.value;
if (this.uriSafe)
value = encodeURIComponent(this.value);
if (typeof value != "string")
return;
else if (value.indexOf(";") > 0 || value.indexOf("=") > 0)
throw new Error("`value` cannot contain ';' or '=' characters when `uriSafe` is not true");
var cookie = this.name + "=" + value;
if (expiresTime != undefined)
cookie += "; expires=" + date.toUTCString();
cookie += "; path=" + this.path;
if (this.secure)
cookie += "; secure";
document.cookie = cookie;
}
/**
* Stops arguments being passed from observers.
*/
_setCookieWrapper() {
this._setCookie();
}
/**
* Sets the cookie. Only useful when manualSet is true.
*/
setCookie() {
this._setCookie(true);
}
/**
* Gets the cookie. Only useful when manualGet is true.
*/
getCookie() {
this.value = this._getCookie(true);
}
}
window.customElements.define('iron-cookie', IronCookieElement);