Changeset 12333

Show
Ignore:
Timestamp:
02/09/08 19:23:35 (11 months ago)
Author:
peller
Message:

Use regexp to find cookie contents. (thanks, Pat) Fixes #2881. Correct docs on deleting cookies. Fixes #5782 !strict

Location:
dojo/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • dojo/trunk/cookie.js

    r11834 r12333  
    11dojo.provide("dojo.cookie"); 
     2 
     3dojo.require("dojo.regexp"); 
    24 
    35/*===== 
    46dojo.__cookieProps = function(kwArgs){ 
    5         //      expires: Date|Number? 
    6         //              If a number, seen as the number of days from today. If a date, the 
    7         //              date past which the cookie is invalid. If expires is in the past, 
    8         //              the cookie will be deleted If expires is left out or is 0, the 
    9         //              cookie will expire when the browser closes. 
     7        //      expires: Date|String|Number? 
     8        //              If a number, the number of days from today at which the cookie 
     9        //              will expire. If a date, the date past which the cookie will expire. 
     10        //              If expires is in the past, the cookie will be deleted. 
     11        //              If expires is omitted or is 0, the cookie will expire when the browser closes. 
    1012        //      path: String? 
    1113        //              The path to use for the cookie. 
     
    2224        //              Get or set a cookie. 
    2325        //      description: 
    24         //              If you pass in one argument, the the value of the cookie is returned 
    25         // 
    26         //              If you pass in two arguments, the cookie value is set to the second 
    27         //              argument. 
    28         // 
    29         //              If you pass in three arguments, the cookie value is set to the 
    30         //              second argument, and the options on the third argument are used for 
    31         //              extended properties on the cookie 
     26        //              If one argument is passed, returns the value of the cookie 
     27        //              For two or more arguments, acts as a setter. 
    3228        //      name: 
    33         //              The name of the cookie 
     29        //              Name of the cookie 
    3430        //      value: 
    35         //              Optional. The value for the cookie. 
     31        //              Value for the cookie 
    3632        //      props:  
    37         //              Optional additional properties for the cookie 
     33        //              Properties for the cookie 
    3834        //      example: 
    3935        //              set a cookie with the JSON-serialized contents of an object which 
     
    4743        //      example: 
    4844        //              delete a cookie: 
    49         //      |       dojo.cookie("configObj", null); 
     45        //      |       dojo.cookie("configObj", null, {expires: -1}); 
    5046        var c = document.cookie; 
    5147        if(arguments.length == 1){ 
    52                 var idx = c.lastIndexOf(name+'='); 
    53                 if(idx == -1){ return null; } 
    54                 var start = idx+name.length+1; 
    55                 var end = c.indexOf(';', idx+name.length+1); 
    56                 if(end == -1){ end = c.length; } 
    57                 return decodeURIComponent(c.substring(start, end));  
     48                var matches = c.match(new RegExp("(?:^|(?:; ))" + dojo.regexp.escapeString(name) + "=([^;]*)")); 
     49                return matches ? decodeURIComponent(matches[1]) : undefined; // String or undefined 
    5850        }else{ 
    5951                props = props || {}; 
    60                 value = encodeURIComponent(value); 
    61                 if(typeof(props.expires) == "number"){  
     52// FIXME: expires=0 seems to disappear right away, not on close? (FF3)  Change docs? 
     53                var exp = props.expires; 
     54                if(typeof exp == "number"){  
    6255                        var d = new Date(); 
    63                         d.setTime(d.getTime()+(props.expires*24*60*60*1000)); 
     56                        d.setTime(d.getTime() + exp*24*60*60*1000); 
    6457                        props.expires = d; 
    6558                } 
    66                 document.cookie = name + "=" + value  
    67                         + (props.expires ? "; expires=" + props.expires.toUTCString() : "") 
    68                         + (props.path ? "; path=" + props.path : "") 
    69                         + (props.domain ? "; domain=" + props.domain : "") 
    70                         + (props.secure ? "; secure" : ""); 
    71                 return null; 
     59                if(exp && exp.toUTCString){ props.expires = exp.toUTCString(); } 
     60 
     61                value = encodeURIComponent(value); 
     62                var updatedCookie = name + "=" + value; 
     63                for(propName in props){ 
     64                        updatedCookie += "; " + propName; 
     65                        var propValue = props[propName]; 
     66                        if(propValue !== true){ updatedCookie += "=" + propValue; } 
     67                } 
     68                document.cookie = updatedCookie; 
    7269        } 
    7370}; 
     
    9491                this(name, dojo.toJson(value), props||{}); 
    9592        } 
    96          
    9793}; 
    9894 
  • dojo/trunk/tests/cookie.html

    r11834 r12333  
    2929                                                                var start = document.cookie.indexOf(n+"=") + n.length + 1; 
    3030                                                                var end = document.cookie.indexOf(";", start); 
    31                                                                 if(end == -1) end = document.cookie.length; 
     31                                                                if(end == -1){ end = document.cookie.length; } 
    3232                                                                t.is(v, decodeURIComponent(document.cookie.substring(start, end))); 
    3333                                                        } 
     
    6666                                                                t.is(null, dojo.cookie("dojo_num")); 
    6767                                                        } 
     68                                                }, 
     69                                                { 
     70                                                        name: "nameSuffix", 
     71                                                        runTest: function(t){ 
     72                                                                // set two cookies with the same suffix 
     73                                                                dojo.cookie("user", "123", { expires: 10 }); 
     74                                                                dojo.cookie("xuser", "abc", { expires: 10 }); 
     75                                                                t.is("123", dojo.cookie("user")); 
     76                                                                t.is("abc", dojo.cookie("xuser")); 
     77 
     78                                                                // remove the cookie by setting it with a negative 
     79                                                                // numerical expires. value doesn't really matter here 
     80                                                                dojo.cookie("user", "-deleted-", { expires: -10 }); 
     81                                                                t.is(null, dojo.cookie("user")); 
     82                                                                dojo.cookie("xuser", "-deleted-", { expires: -10 }); 
     83                                                                t.is(null, dojo.cookie("xuser")); 
     84                                                        } 
    6885                                                } 
    6986                                        ]