source: Dev/trunk/node_modules/grunt-curl/test/expected/cookiejar.js

Last change on this file was 484, checked in by hendrikvanantwerpen, 11 years ago

Commit node_modules, to make checkouts and builds more deterministic.

File size: 3.9 KB
Line 
1/**
2 * Javascript code to store data as JSON strings in cookies.
3 * It uses prototype.js 1.5.1 (http://www.prototypejs.org)
4 *
5 * Author : Lalit Patel
6 * Website: http://www.lalit.org/lab/jsoncookies
7 * License: Apache Software License 2
8 *          http://www.apache.org/licenses/LICENSE-2.0
9 * Version: 0.5
10 * Updated: Jan 26, 2009
11 *
12 * Chnage Log:
13 *   v 0.5
14 *   -  Changed License from CC to Apache 2
15 *   v 0.4
16 *   -  Removed a extra comma in options (was breaking in IE and Opera). (Thanks Jason)
17 *   -  Removed the parameter name from the initialize function
18 *   -  Changed the way expires date was being calculated. (Thanks David)
19 *   v 0.3
20 *   -  Removed dependancy on json.js (http://www.json.org/json.js)
21 *   -  empty() function only deletes the cookies set by CookieJar
22 */
23
24var CookieJar = Class.create();
25
26CookieJar.prototype = {
27
28        /**
29         * Append before all cookie names to differntiate them.
30         */
31        appendString: "__CJ_",
32
33        /**
34         * Initializes the cookie jar with the options.
35         */
36        initialize: function(options) {
37                this.options = {
38                        expires: 3600,          // seconds (1 hr)
39                        path: '',                       // cookie path
40                        domain: '',                     // cookie domain
41                        secure: ''                      // secure ?
42                };
43                Object.extend(this.options, options || {});
44
45                if (this.options.expires != '') {
46                        var date = new Date();
47                        date = new Date(date.getTime() + (this.options.expires * 1000));
48                        this.options.expires = '; expires=' + date.toGMTString();
49                }
50                if (this.options.path != '') {
51                        this.options.path = '; path=' + escape(this.options.path);
52                }
53                if (this.options.domain != '') {
54                        this.options.domain = '; domain=' + escape(this.options.domain);
55                }
56                if (this.options.secure == 'secure') {
57                        this.options.secure = '; secure';
58                } else {
59                        this.options.secure = '';
60                }
61        },
62
63        /**
64         * Adds a name values pair.
65         */
66        put: function(name, value) {
67                name = this.appendString + name;
68                cookie = this.options;
69                var type = typeof value;
70                switch(type) {
71                  case 'undefined':
72                  case 'function' :
73                  case 'unknown'  : return false;
74                  case 'boolean'  :
75                  case 'string'   :
76                  case 'number'   : value = String(value.toString());
77                }
78                var cookie_str = name + "=" + escape(Object.toJSON(value));
79                try {
80                        document.cookie = cookie_str + cookie.expires + cookie.path + cookie.domain + cookie.secure;
81                } catch (e) {
82                        return false;
83                }
84                return true;
85        },
86
87        /**
88         * Removes a particular cookie (name value pair) form the Cookie Jar.
89         */
90        remove: function(name) {
91                name = this.appendString + name;
92                cookie = this.options;
93                try {
94                        var date = new Date();
95                        date.setTime(date.getTime() - (3600 * 1000));
96                        var expires = '; expires=' + date.toGMTString();
97                        document.cookie = name + "=" + expires + cookie.path + cookie.domain + cookie.secure;
98                } catch (e) {
99                        return false;
100                }
101                return true;
102        },
103
104        /**
105         * Return a particular cookie by name;
106         */
107        get: function(name) {
108                name = this.appendString + name;
109                var cookies = document.cookie.match(name + '=(.*?)(;|$)');
110                if (cookies) {
111                        return (unescape(cookies[1])).evalJSON();
112                } else {
113                        return null;
114                }
115        },
116
117        /**
118         * Empties the Cookie Jar. Deletes all the cookies.
119         */
120        empty: function() {
121                keys = this.getKeys();
122                size = keys.size();
123                for(i=0; i<size; i++) {
124                        this.remove(keys[i]);
125                }
126        },
127
128        /**
129         * Returns all cookies as a single object
130         */
131        getPack: function() {
132                pack = {};
133                keys = this.getKeys();
134
135                size = keys.size();
136                for(i=0; i<size; i++) {
137                        pack[keys[i]] = this.get(keys[i]);
138                }
139                return pack;
140        },
141
142        /**
143         * Returns all keys.
144         */
145        getKeys: function() {
146                keys = $A();
147                keyRe= /[^=; ]+(?=\=)/g;
148                str  = document.cookie;
149                CJRe = new RegExp("^" + this.appendString);
150                while((match = keyRe.exec(str)) != undefined) {
151                        if (CJRe.test(match[0].strip())) {
152                                keys.push(match[0].strip().gsub("^" + this.appendString,""));
153                        }
154                }
155                return keys;
156        }
157};
Note: See TracBrowser for help on using the repository browser.