|
1 /* |
|
2 Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
3 Code licensed under the BSD License: |
|
4 http://developer.yahoo.net/yui/license.txt |
|
5 version: 3.0.0b1 |
|
6 build: 1163 |
|
7 */ |
|
8 YUI.add('cookie', function(Y) { |
|
9 |
|
10 /** |
|
11 * Utilities for cookie management |
|
12 * @module cookie |
|
13 */ |
|
14 |
|
15 //shortcuts |
|
16 var L = Y.Lang, |
|
17 O = Y.Object, |
|
18 NULL = null, |
|
19 |
|
20 //shortcuts to functions |
|
21 isString = L.isString, |
|
22 isObject = L.isObject, |
|
23 isUndefined = L.isUndefined, |
|
24 isFunction = L.isFunction, |
|
25 encode = encodeURIComponent, |
|
26 decode = decodeURIComponent, |
|
27 |
|
28 //shortcut to document |
|
29 doc = Y.config.doc; |
|
30 |
|
31 /* |
|
32 * Throws an error message. |
|
33 */ |
|
34 function error(message){ |
|
35 throw new TypeError(message); |
|
36 } |
|
37 |
|
38 /* |
|
39 * Checks the validity of a cookie name. |
|
40 */ |
|
41 function validateCookieName(name){ |
|
42 if (!isString(name) || name === ""){ |
|
43 error("Cookie name must be a non-empty string."); |
|
44 } |
|
45 } |
|
46 |
|
47 /* |
|
48 * Checks the validity of a subcookie name. |
|
49 */ |
|
50 function validateSubcookieName(subName){ |
|
51 if (!isString(subName) || subName === ""){ |
|
52 error("Subcookie name must be a non-empty string."); |
|
53 } |
|
54 } |
|
55 |
|
56 /** |
|
57 * Cookie utility. |
|
58 * @class Cookie |
|
59 * @static |
|
60 */ |
|
61 Y.Cookie = { |
|
62 |
|
63 //------------------------------------------------------------------------- |
|
64 // Private Methods |
|
65 //------------------------------------------------------------------------- |
|
66 |
|
67 /** |
|
68 * Creates a cookie string that can be assigned into document.cookie. |
|
69 * @param {String} name The name of the cookie. |
|
70 * @param {String} value The value of the cookie. |
|
71 * @param {Boolean} encodeValue True to encode the value, false to leave as-is. |
|
72 * @param {Object} options (Optional) Options for the cookie. |
|
73 * @return {String} The formatted cookie string. |
|
74 * @method _createCookieString |
|
75 * @private |
|
76 * @static |
|
77 */ |
|
78 _createCookieString : function (name /*:String*/, value /*:Variant*/, encodeValue /*:Boolean*/, options /*:Object*/) /*:String*/ { |
|
79 |
|
80 options = options || {}; |
|
81 |
|
82 var text /*:String*/ = encode(name) + "=" + (encodeValue ? encode(value) : value), |
|
83 expires = options.expires, |
|
84 path = options.path, |
|
85 domain = options.domain; |
|
86 |
|
87 |
|
88 if (isObject(options)){ |
|
89 //expiration date |
|
90 if (expires instanceof Date){ |
|
91 text += "; expires=" + expires.toUTCString(); |
|
92 } |
|
93 |
|
94 //path |
|
95 if (isString(path) && path !== ""){ |
|
96 text += "; path=" + path; |
|
97 } |
|
98 |
|
99 //domain |
|
100 if (isString(domain) && domain !== ""){ |
|
101 text += "; domain=" + domain; |
|
102 } |
|
103 |
|
104 //secure |
|
105 if (options.secure === true){ |
|
106 text += "; secure"; |
|
107 } |
|
108 } |
|
109 |
|
110 return text; |
|
111 }, |
|
112 |
|
113 /** |
|
114 * Formats a cookie value for an object containing multiple values. |
|
115 * @param {Object} hash An object of key-value pairs to create a string for. |
|
116 * @return {String} A string suitable for use as a cookie value. |
|
117 * @method _createCookieHashString |
|
118 * @private |
|
119 * @static |
|
120 */ |
|
121 _createCookieHashString : function (hash /*:Object*/) /*:String*/ { |
|
122 if (!isObject(hash)){ |
|
123 error("Cookie._createCookieHashString(): Argument must be an object."); |
|
124 } |
|
125 |
|
126 var text /*:Array*/ = []; |
|
127 |
|
128 O.each(hash, function(value, key){ |
|
129 if (!isFunction(value) && !isUndefined(value)){ |
|
130 text.push(encode(key) + "=" + encode(String(value))); |
|
131 } |
|
132 }); |
|
133 |
|
134 return text.join("&"); |
|
135 }, |
|
136 |
|
137 /** |
|
138 * Parses a cookie hash string into an object. |
|
139 * @param {String} text The cookie hash string to parse (format: n1=v1&n2=v2). |
|
140 * @return {Object} An object containing entries for each cookie value. |
|
141 * @method _parseCookieHash |
|
142 * @private |
|
143 * @static |
|
144 */ |
|
145 _parseCookieHash : function (text) { |
|
146 |
|
147 var hashParts = text.split("&"), |
|
148 hashPart = NULL, |
|
149 hash = {}; |
|
150 |
|
151 if (text.length){ |
|
152 for (var i=0, len=hashParts.length; i < len; i++){ |
|
153 hashPart = hashParts[i].split("="); |
|
154 hash[decode(hashPart[0])] = decode(hashPart[1]); |
|
155 } |
|
156 } |
|
157 |
|
158 return hash; |
|
159 }, |
|
160 |
|
161 /** |
|
162 * Parses a cookie string into an object representing all accessible cookies. |
|
163 * @param {String} text The cookie string to parse. |
|
164 * @param {Boolean} shouldDecode (Optional) Indicates if the cookie values should be decoded or not. Default is true. |
|
165 * @return {Object} An object containing entries for each accessible cookie. |
|
166 * @method _parseCookieString |
|
167 * @private |
|
168 * @static |
|
169 */ |
|
170 _parseCookieString : function (text /*:String*/, shouldDecode /*:Boolean*/) /*:Object*/ { |
|
171 |
|
172 var cookies /*:Object*/ = {}; |
|
173 |
|
174 if (isString(text) && text.length > 0) { |
|
175 |
|
176 var decodeValue = (shouldDecode === false ? function(s){return s;} : decode), |
|
177 cookieParts = text.split(/;\s/g), |
|
178 cookieName = NULL, |
|
179 cookieValue = NULL, |
|
180 cookieNameValue = NULL; |
|
181 |
|
182 for (var i=0, len=cookieParts.length; i < len; i++){ |
|
183 |
|
184 //check for normally-formatted cookie (name-value) |
|
185 cookieNameValue = cookieParts[i].match(/([^=]+)=/i); |
|
186 if (cookieNameValue instanceof Array){ |
|
187 try { |
|
188 cookieName = decode(cookieNameValue[1]); |
|
189 cookieValue = decodeValue(cookieParts[i].substring(cookieNameValue[1].length+1)); |
|
190 } catch (ex){ |
|
191 //intentionally ignore the cookie - the encoding is wrong |
|
192 } |
|
193 } else { |
|
194 //means the cookie does not have an "=", so treat it as a boolean flag |
|
195 cookieName = decode(cookieParts[i]); |
|
196 cookieValue = ""; |
|
197 } |
|
198 cookies[cookieName] = cookieValue; |
|
199 } |
|
200 |
|
201 } |
|
202 |
|
203 return cookies; |
|
204 }, |
|
205 |
|
206 //------------------------------------------------------------------------- |
|
207 // Public Methods |
|
208 //------------------------------------------------------------------------- |
|
209 |
|
210 /** |
|
211 * Determines if the cookie with the given name exists. This is useful for |
|
212 * Boolean cookies (those that do not follow the name=value convention). |
|
213 * @param {String} name The name of the cookie to check. |
|
214 * @return {Boolean} True if the cookie exists, false if not. |
|
215 * @method exists |
|
216 * @static |
|
217 */ |
|
218 exists: function(name) { |
|
219 |
|
220 validateCookieName(name); //throws error |
|
221 |
|
222 var cookies = this._parseCookieString(doc.cookie, true); |
|
223 |
|
224 return cookies.hasOwnProperty(name); |
|
225 }, |
|
226 |
|
227 /** |
|
228 * Returns the cookie value for the given name. |
|
229 * @param {String} name The name of the cookie to retrieve. |
|
230 * @param {Function|Object} options (Optional) An object containing one or more |
|
231 * cookie options: raw (true/false) and converter (a function). |
|
232 * The converter function is run on the value before returning it. The |
|
233 * function is not used if the cookie doesn't exist. The function can be |
|
234 * passed instead of the options object for backwards compatibility. When |
|
235 * raw is set to true, the cookie value is not URI decoded. |
|
236 * @return {Variant} If no converter is specified, returns a string or null if |
|
237 * the cookie doesn't exist. If the converter is specified, returns the value |
|
238 * returned from the converter or null if the cookie doesn't exist. |
|
239 * @method get |
|
240 * @static |
|
241 */ |
|
242 get : function (name, options) { |
|
243 |
|
244 validateCookieName(name); //throws error |
|
245 |
|
246 var cookies, |
|
247 cookie, |
|
248 converter; |
|
249 |
|
250 //if options is a function, then it's the converter |
|
251 if (isFunction(options)) { |
|
252 converter = options; |
|
253 options = {}; |
|
254 } else if (isObject(options)) { |
|
255 converter = options.converter; |
|
256 } else { |
|
257 options = {}; |
|
258 } |
|
259 |
|
260 cookies = this._parseCookieString(doc.cookie, !options.raw); |
|
261 cookie = cookies[name]; |
|
262 |
|
263 //should return null, not undefined if the cookie doesn't exist |
|
264 if (isUndefined(cookie)) { |
|
265 return NULL; |
|
266 } |
|
267 |
|
268 if (!isFunction(converter)){ |
|
269 return cookie; |
|
270 } else { |
|
271 return converter(cookie); |
|
272 } |
|
273 }, |
|
274 |
|
275 /** |
|
276 * Returns the value of a subcookie. |
|
277 * @param {String} name The name of the cookie to retrieve. |
|
278 * @param {String} subName The name of the subcookie to retrieve. |
|
279 * @param {Function} converter (Optional) A function to run on the value before returning |
|
280 * it. The function is not used if the cookie doesn't exist. |
|
281 * @return {Variant} If the cookie doesn't exist, null is returned. If the subcookie |
|
282 * doesn't exist, null if also returned. If no converter is specified and the |
|
283 * subcookie exists, a string is returned. If a converter is specified and the |
|
284 * subcookie exists, the value returned from the converter is returned. |
|
285 * @method getSub |
|
286 * @static |
|
287 */ |
|
288 getSub : function (name /*:String*/, subName /*:String*/, converter /*:Function*/) /*:Variant*/ { |
|
289 |
|
290 var hash /*:Variant*/ = this.getSubs(name); |
|
291 |
|
292 if (hash !== NULL) { |
|
293 |
|
294 validateSubcookieName(subName); //throws error |
|
295 |
|
296 if (isUndefined(hash[subName])){ |
|
297 return NULL; |
|
298 } |
|
299 |
|
300 if (!isFunction(converter)){ |
|
301 return hash[subName]; |
|
302 } else { |
|
303 return converter(hash[subName]); |
|
304 } |
|
305 } else { |
|
306 return NULL; |
|
307 } |
|
308 |
|
309 }, |
|
310 |
|
311 /** |
|
312 * Returns an object containing name-value pairs stored in the cookie with the given name. |
|
313 * @param {String} name The name of the cookie to retrieve. |
|
314 * @return {Object} An object of name-value pairs if the cookie with the given name |
|
315 * exists, null if it does not. |
|
316 * @method getSubs |
|
317 * @static |
|
318 */ |
|
319 getSubs : function (name) { |
|
320 |
|
321 validateCookieName(name); //throws error |
|
322 |
|
323 var cookies = this._parseCookieString(doc.cookie, false); |
|
324 if (isString(cookies[name])){ |
|
325 return this._parseCookieHash(cookies[name]); |
|
326 } |
|
327 return NULL; |
|
328 }, |
|
329 |
|
330 /** |
|
331 * Removes a cookie from the machine by setting its expiration date to |
|
332 * sometime in the past. |
|
333 * @param {String} name The name of the cookie to remove. |
|
334 * @param {Object} options (Optional) An object containing one or more |
|
335 * cookie options: path (a string), domain (a string), |
|
336 * and secure (true/false). The expires option will be overwritten |
|
337 * by the method. |
|
338 * @return {String} The created cookie string. |
|
339 * @method remove |
|
340 * @static |
|
341 */ |
|
342 remove : function (name, options) { |
|
343 |
|
344 validateCookieName(name); //throws error |
|
345 |
|
346 //set options |
|
347 options = Y.merge(options || {}, { |
|
348 expires: new Date(0) |
|
349 }); |
|
350 |
|
351 //set cookie |
|
352 return this.set(name, "", options); |
|
353 }, |
|
354 |
|
355 /** |
|
356 * Removes a sub cookie with a given name. |
|
357 * @param {String} name The name of the cookie in which the subcookie exists. |
|
358 * @param {String} subName The name of the subcookie to remove. |
|
359 * @param {Object} options (Optional) An object containing one or more |
|
360 * cookie options: path (a string), domain (a string), expires (a Date object), |
|
361 * removeIfEmpty (true/false), and secure (true/false). This must be the same |
|
362 * settings as the original subcookie. |
|
363 * @return {String} The created cookie string. |
|
364 * @method removeSub |
|
365 * @static |
|
366 */ |
|
367 removeSub : function(name, subName, options) { |
|
368 |
|
369 validateCookieName(name); //throws error |
|
370 |
|
371 validateSubcookieName(subName); //throws error |
|
372 |
|
373 options = options || {}; |
|
374 |
|
375 //get all subcookies for this cookie |
|
376 var subs = this.getSubs(name); |
|
377 |
|
378 //delete the indicated subcookie |
|
379 if (isObject(subs) && subs.hasOwnProperty(subName)){ |
|
380 delete subs[subName]; |
|
381 |
|
382 if (!options.removeIfEmpty) { |
|
383 //reset the cookie |
|
384 |
|
385 return this.setSubs(name, subs, options); |
|
386 } else { |
|
387 //reset the cookie if there are subcookies left, else remove |
|
388 for (var key in subs){ |
|
389 if (subs.hasOwnProperty(key) && !isFunction(subs[key]) && !isUndefined(subs[key])){ |
|
390 return this.setSubs(name, subs, options); |
|
391 } |
|
392 } |
|
393 |
|
394 return this.remove(name, options); |
|
395 } |
|
396 } else { |
|
397 return ""; |
|
398 } |
|
399 |
|
400 }, |
|
401 |
|
402 /** |
|
403 * Sets a cookie with a given name and value. |
|
404 * @param {String} name The name of the cookie to set. |
|
405 * @param {Variant} value The value to set for the cookie. |
|
406 * @param {Object} options (Optional) An object containing one or more |
|
407 * cookie options: path (a string), domain (a string), expires (a Date object), |
|
408 * secure (true/false), and raw (true/false). Setting raw to true indicates |
|
409 * that the cookie should not be URI encoded before being set. |
|
410 * @return {String} The created cookie string. |
|
411 * @method set |
|
412 * @static |
|
413 */ |
|
414 set : function (name, value, options) { |
|
415 |
|
416 validateCookieName(name); //throws error |
|
417 |
|
418 if (isUndefined(value)){ |
|
419 error("Cookie.set(): Value cannot be undefined."); |
|
420 } |
|
421 |
|
422 options = options || {}; |
|
423 |
|
424 var text = this._createCookieString(name, value, !options.raw, options); |
|
425 doc.cookie = text; |
|
426 return text; |
|
427 }, |
|
428 |
|
429 /** |
|
430 * Sets a sub cookie with a given name to a particular value. |
|
431 * @param {String} name The name of the cookie to set. |
|
432 * @param {String} subName The name of the subcookie to set. |
|
433 * @param {Variant} value The value to set. |
|
434 * @param {Object} options (Optional) An object containing one or more |
|
435 * cookie options: path (a string), domain (a string), expires (a Date object), |
|
436 * and secure (true/false). |
|
437 * @return {String} The created cookie string. |
|
438 * @method setSub |
|
439 * @static |
|
440 */ |
|
441 setSub : function (name, subName, value, options) { |
|
442 |
|
443 validateCookieName(name); //throws error |
|
444 |
|
445 validateSubcookieName(subName); //throws error |
|
446 |
|
447 if (isUndefined(value)){ |
|
448 error("Cookie.setSub(): Subcookie value cannot be undefined."); |
|
449 } |
|
450 |
|
451 var hash = this.getSubs(name); |
|
452 |
|
453 if (!isObject(hash)){ |
|
454 hash = {}; |
|
455 } |
|
456 |
|
457 hash[subName] = value; |
|
458 |
|
459 return this.setSubs(name, hash, options); |
|
460 |
|
461 }, |
|
462 |
|
463 /** |
|
464 * Sets a cookie with a given name to contain a hash of name-value pairs. |
|
465 * @param {String} name The name of the cookie to set. |
|
466 * @param {Object} value An object containing name-value pairs. |
|
467 * @param {Object} options (Optional) An object containing one or more |
|
468 * cookie options: path (a string), domain (a string), expires (a Date object), |
|
469 * and secure (true/false). |
|
470 * @return {String} The created cookie string. |
|
471 * @method setSubs |
|
472 * @static |
|
473 */ |
|
474 setSubs : function (name, value, options) { |
|
475 |
|
476 validateCookieName(name); //throws error |
|
477 |
|
478 if (!isObject(value)){ |
|
479 error("Cookie.setSubs(): Cookie value must be an object."); |
|
480 } |
|
481 |
|
482 var text /*:String*/ = this._createCookieString(name, this._createCookieHashString(value), false, options); |
|
483 doc.cookie = text; |
|
484 return text; |
|
485 } |
|
486 |
|
487 }; |
|
488 |
|
489 |
|
490 |
|
491 }, '3.0.0b1' ,{requires:['yui-base']}); |