1 /**
  2  * @fileOverview cookie
  3  * @author lifesinger@gmail.com
  4  */
  5 KISSY.add('cookie', function (S) {
  6 
  7     /**
  8      * @name Cookie
  9      * @namespace Provide Cookie utilities.
 10      */
 11 
 12     var doc = S.Env.host.document,
 13         MILLISECONDS_OF_DAY = 24 * 60 * 60 * 1000,
 14         encode = encodeURIComponent,
 15         decode = decodeURIComponent;
 16 
 17 
 18     function isNotEmptyString(val) {
 19         return S.isString(val) && val !== '';
 20     }
 21 
 22     return S.Cookie =
 23     /**
 24      * @lends Cookie
 25      */
 26     {
 27 
 28         /**
 29          * Returns the cookie value for given name
 30          * @return {String} name The name of the cookie to retrieve
 31          */
 32         get:function (name) {
 33             var ret, m;
 34 
 35             if (isNotEmptyString(name)) {
 36                 if ((m = String(doc.cookie).match(
 37                     new RegExp('(?:^| )' + name + '(?:(?:=([^;]*))|;|$)')))) {
 38                     ret = m[1] ? decode(m[1]) : '';
 39                 }
 40             }
 41             return ret;
 42         },
 43 
 44         /**
 45          * Set a cookie with a given name and value
 46          * @param {String} name The name of the cookie to set
 47          * @param {String} val The value to set for cookie
 48          * @param {Number|Date} expires
 49          * if Number secified how many days this cookie will expire
 50          * @param {String} domain set cookie's domain
 51          * @param {String} path set cookie's path
 52          * @param {Boolean} secure whether this cookie can only be sent to server on https
 53          */
 54         set:function (name, val, expires, domain, path, secure) {
 55             var text = String(encode(val)), date = expires;
 56 
 57             // 从当前时间开始,多少天后过期
 58             if (typeof date === 'number') {
 59                 date = new Date();
 60                 date.setTime(date.getTime() + expires * MILLISECONDS_OF_DAY);
 61             }
 62             // expiration date
 63             if (date instanceof Date) {
 64                 text += '; expires=' + date.toUTCString();
 65             }
 66 
 67             // domain
 68             if (isNotEmptyString(domain)) {
 69                 text += '; domain=' + domain;
 70             }
 71 
 72             // path
 73             if (isNotEmptyString(path)) {
 74                 text += '; path=' + path;
 75             }
 76 
 77             // secure
 78             if (secure) {
 79                 text += '; secure';
 80             }
 81 
 82             doc.cookie = name + '=' + text;
 83         },
 84 
 85         /**
 86          * Remove a cookie from the machine by setting its expiration date to sometime in the past
 87          * @param {String} name The name of the cookie to remove.
 88          * @param {String} domain The cookie's domain
 89          * @param {String} path The cookie's path
 90          * @param {String} secure The cookie's secure option
 91          */
 92         remove:function (name, domain, path, secure) {
 93             this.set(name, '', -1, domain, path, secure);
 94         }
 95     };
 96 
 97 });
 98 
 99 /**
100  *  2012.02.14 yiminghe@gmail.com
101  *   - jsdoc added
102  *
103  *  2010.04
104  *   - get 方法要考虑 ie 下,
105  *     值为空的 cookie 为 'test3; test3=3; test3tt=2; test1=t1test3; test3', 没有等于号。
106  *     除了正则获取,还可以 split 字符串的方式来获取。
107  *   - api 设计上,原本想借鉴 jQuery 的简明风格:S.cookie(name, ...), 但考虑到可扩展性,目前
108  *     独立成静态工具类的方式更优。
109  */
110