2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 * Utilities for cookie management
9 * @namespace YAHOO.util
12 YAHOO.namespace("util");
21 //-------------------------------------------------------------------------
23 //-------------------------------------------------------------------------
26 * Creates a cookie string that can be assigned into document.cookie.
27 * @param {String} name The name of the cookie.
28 * @param {String} value The value of the cookie.
29 * @param {encodeValue} encodeValue True to encode the value, false to leave as-is.
30 * @param {Object} options (Optional) Options for the cookie.
31 * @return {String} The formatted cookie string.
32 * @method _createCookieString
36 _createCookieString : function (name /*:String*/, value /*:Variant*/, encodeValue /*:Boolean*/, options /*:Object*/) /*:String*/ {
39 var lang = YAHOO.lang;
41 var text /*:String*/ = encodeURIComponent(name) + "=" + (encodeValue ? encodeURIComponent(value) : value);
44 if (lang.isObject(options)){
46 if (options.expires instanceof Date){
47 text += "; expires=" + options.expires.toGMTString();
51 if (lang.isString(options.path) && options.path != ""){
52 text += "; path=" + options.path;
56 if (lang.isString(options.domain) && options.domain != ""){
57 text += "; domain=" + options.domain;
61 if (options.secure === true){
70 * Formats a cookie value for an object containing multiple values.
71 * @param {Object} hash An object of key-value pairs to create a string for.
72 * @return {String} A string suitable for use as a cookie value.
73 * @method _createCookieHash
77 _createCookieHashString : function (hash /*:Object*/) /*:String*/ {
80 var lang = YAHOO.lang;
82 if (!lang.isObject(hash)){
83 throw new TypeError("Cookie._createCookieHashString(): Argument must be an object.");
86 var text /*:Array*/ = new Array();
88 for (var key in hash){
89 if (lang.hasOwnProperty(hash, key) && !lang.isFunction(hash[key]) && !lang.isUndefined(hash[key])){
90 text.push(encodeURIComponent(key) + "=" + encodeURIComponent(String(hash[key])));
94 return text.join("&");
98 * Parses a cookie hash string into an object.
99 * @param {String} text The cookie hash string to parse. The string should already be URL-decoded.
100 * @return {Object} An object containing entries for each cookie value.
101 * @method _parseCookieHash
105 _parseCookieHash : function (text /*:String*/) /*:Object*/ {
107 var hashParts /*:Array*/ = text.split("&"),
108 hashPart /*:Array*/ = null,
109 hash /*:Object*/ = new Object();
111 if (text.length > 0){
112 for (var i=0, len=hashParts.length; i < len; i++){
113 hashPart = hashParts[i].split("=");
114 hash[decodeURIComponent(hashPart[0])] = decodeURIComponent(hashPart[1]);
122 * Parses a cookie string into an object representing all accessible cookies.
123 * @param {String} text The cookie string to parse.
124 * @param {Boolean} decode (Optional) Indicates if the cookie values should be decoded or not. Default is true.
125 * @return {Object} An object containing entries for each accessible cookie.
126 * @method _parseCookieString
130 _parseCookieString : function (text /*:String*/, decode /*:Boolean*/) /*:Object*/ {
132 var cookies /*:Object*/ = new Object();
134 if (YAHOO.lang.isString(text) && text.length > 0) {
136 var decodeValue = (decode === false ? function(s){return s;} : decodeURIComponent);
138 if (/[^=]+=[^=;]?(?:; [^=]+=[^=]?)?/.test(text)){
139 var cookieParts /*:Array*/ = text.split(/;\s/g),
140 cookieName /*:String*/ = null,
141 cookieValue /*:String*/ = null,
142 cookieNameValue /*:Array*/ = null;
144 for (var i=0, len=cookieParts.length; i < len; i++){
146 //check for normally-formatted cookie (name-value)
147 cookieNameValue = cookieParts[i].match(/([^=]+)=/i);
148 if (cookieNameValue instanceof Array){
150 cookieName = decodeURIComponent(cookieNameValue[1]);
151 cookieValue = decodeValue(cookieParts[i].substring(cookieNameValue[1].length+1));
153 //ignore the entire cookie - encoding is likely invalid
156 //means the cookie does not have an "=", so treat it as a boolean flag
157 cookieName = decodeURIComponent(cookieParts[i]);
158 cookieValue = cookieName;
160 cookies[cookieName] = cookieValue;
168 //-------------------------------------------------------------------------
170 //-------------------------------------------------------------------------
173 * Returns the cookie value for the given name.
174 * @param {String} name The name of the cookie to retrieve.
175 * @param {Function} converter (Optional) A function to run on the value before returning
176 * it. The function is not used if the cookie doesn't exist.
177 * @return {Variant} If no converter is specified, returns a string or null if
178 * the cookie doesn't exist. If the converter is specified, returns the value
179 * returned from the converter or null if the cookie doesn't exist.
183 get : function (name /*:String*/, converter /*:Function*/) /*:Variant*/{
185 var lang = YAHOO.lang;
186 var cookies /*:Object*/ = this._parseCookieString(document.cookie);
188 if (!lang.isString(name) || name === ""){
189 throw new TypeError("Cookie.get(): Cookie name must be a non-empty string.");
192 if (lang.isUndefined(cookies[name])) {
196 if (!lang.isFunction(converter)){
197 return cookies[name];
199 return converter(cookies[name]);
204 * Returns the value of a subcookie.
205 * @param {String} name The name of the cookie to retrieve.
206 * @param {String} subName The name of the subcookie to retrieve.
207 * @param {Function} converter (Optional) A function to run on the value before returning
208 * it. The function is not used if the cookie doesn't exist.
209 * @return {Variant} If the cookie doesn't exist, null is returned. If the subcookie
210 * doesn't exist, null if also returned. If no converter is specified and the
211 * subcookie exists, a string is returned. If a converter is specified and the
212 * subcookie exists, the value returned from the converter is returned.
216 getSub : function (name /*:String*/, subName /*:String*/, converter /*:Function*/) /*:Variant*/ {
218 var lang = YAHOO.lang;
219 var hash /*:Variant*/ = this.getSubs(name);
223 if (!lang.isString(subName) || subName === ""){
224 throw new TypeError("Cookie.getSub(): Subcookie name must be a non-empty string.");
227 if (lang.isUndefined(hash[subName])){
231 if (!lang.isFunction(converter)){
232 return hash[subName];
234 return converter(hash[subName]);
243 * Returns an object containing name-value pairs stored in the cookie with the given name.
244 * @param {String} name The name of the cookie to retrieve.
245 * @return {Object} An object of name-value pairs if the cookie with the given name
246 * exists, null if it does not.
250 getSubs : function (name /*:String*/) /*:Object*/ {
253 if (!YAHOO.lang.isString(name) || name === ""){
254 throw new TypeError("Cookie.getSubs(): Cookie name must be a non-empty string.");
257 var cookies = this._parseCookieString(document.cookie, false);
258 if (YAHOO.lang.isString(cookies[name])){
259 return this._parseCookieHash(cookies[name]);
265 * Removes a cookie from the machine by setting its expiration date to
266 * sometime in the past.
267 * @param {String} name The name of the cookie to remove.
268 * @param {Object} options (Optional) An object containing one or more
269 * cookie options: path (a string), domain (a string),
270 * and secure (true/false). The expires option will be overwritten
272 * @return {String} The created cookie string.
276 remove : function (name /*:String*/, options /*:Object*/) /*:String*/ {
279 if (!YAHOO.lang.isString(name) || name === ""){
280 throw new TypeError("Cookie.remove(): Cookie name must be a non-empty string.");
284 options = options || {};
285 options.expires = new Date(0);
288 return this.set(name, "", options);
292 * Removes a sub cookie with a given name.
293 * @param {String} name The name of the cookie in which the subcookie exists.
294 * @param {String} subName The name of the subcookie to remove.
295 * @param {Object} options (Optional) An object containing one or more
296 * cookie options: path (a string), domain (a string), expires (a Date object),
297 * and secure (true/false). This must be the same settings as the original
299 * @return {String} The created cookie string.
303 removeSub : function(name /*:String*/, subName /*:String*/, options /*:Object*/) /*:String*/ {
306 if (!YAHOO.lang.isString(name) || name === ""){
307 throw new TypeError("Cookie.removeSub(): Cookie name must be a non-empty string.");
310 //check subcookie name
311 if (!YAHOO.lang.isString(subName) || subName === ""){
312 throw new TypeError("Cookie.removeSub(): Subcookie name must be a non-empty string.");
315 //get all subcookies for this cookie
316 var subs = this.getSubs(name);
318 //delete the indicated subcookie
319 if (YAHOO.lang.isObject(subs) && YAHOO.lang.hasOwnProperty(subs, subName)){
320 delete subs[subName];
323 return this.setSubs(name, subs, options);
331 * Sets a cookie with a given name and value.
332 * @param {String} name The name of the cookie to set.
333 * @param {Variant} value The value to set for the cookie.
334 * @param {Object} options (Optional) An object containing one or more
335 * cookie options: path (a string), domain (a string), expires (a Date object),
336 * and secure (true/false).
337 * @return {String} The created cookie string.
341 set : function (name /*:String*/, value /*:Variant*/, options /*:Object*/) /*:String*/ {
343 var lang = YAHOO.lang;
345 if (!lang.isString(name)){
346 throw new TypeError("Cookie.set(): Cookie name must be a string.");
349 if (lang.isUndefined(value)){
350 throw new TypeError("Cookie.set(): Value cannot be undefined.");
354 var text /*:String*/ = this._createCookieString(name, value, true, options);
355 document.cookie = text;
360 * Sets a sub cookie with a given name to a particular value.
361 * @param {String} name The name of the cookie to set.
362 * @param {String} subName The name of the subcookie to set.
363 * @param {Variant} value The value to set.
364 * @param {Object} options (Optional) An object containing one or more
365 * cookie options: path (a string), domain (a string), expires (a Date object),
366 * and secure (true/false).
367 * @return {String} The created cookie string.
371 setSub : function (name /*:String*/, subName /*:String*/, value /*:Variant*/, options /*:Object*/) /*:String*/ {
373 var lang = YAHOO.lang;
375 if (!lang.isString(name) || name === ""){
376 throw new TypeError("Cookie.setSub(): Cookie name must be a non-empty string.");
379 if (!lang.isString(subName) || subName === ""){
380 throw new TypeError("Cookie.setSub(): Subcookie name must be a non-empty string.");
383 if (lang.isUndefined(value)){
384 throw new TypeError("Cookie.setSub(): Subcookie value cannot be undefined.");
387 var hash /*:Object*/ = this.getSubs(name);
389 if (!lang.isObject(hash)){
393 hash[subName] = value;
395 return this.setSubs(name, hash, options);
400 * Sets a cookie with a given name to contain a hash of name-value pairs.
401 * @param {String} name The name of the cookie to set.
402 * @param {Object} value An object containing name-value pairs.
403 * @param {Object} options (Optional) An object containing one or more
404 * cookie options: path (a string), domain (a string), expires (a Date object),
405 * and secure (true/false).
406 * @return {String} The created cookie string.
410 setSubs : function (name /*:String*/, value /*:Object*/, options /*:Object*/) /*:String*/ {
412 var lang = YAHOO.lang;
414 if (!lang.isString(name)){
415 throw new TypeError("Cookie.setSubs(): Cookie name must be a string.");
418 if (!lang.isObject(value)){
419 throw new TypeError("Cookie.setSubs(): Cookie value must be an object.");
422 var text /*:String*/ = this._createCookieString(name, this._createCookieHashString(value), false, options);
423 document.cookie = text;
429 YAHOO.register("cookie", YAHOO.util.Cookie, {version: "2.7.0", build: "1799"});