mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-05-25 14:37:05 +08:00
$cookies service refactoring
- remove obsolete code in tests - add warning logs when maximum cookie limits (as specified via RFC 2965) were reached - non-string values will now get dropped - after each update $cookies hash will reflect the actual state of browser cookies this means that if browser drops some cookies due to cookie overflow, $cookies will reflect that - $sessionStore got renamed to $cookieStore to avoid name conflicts with html5's sessionStore
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
var browserSingleton;
|
||||
angularService('$browser', function browserFactory(){
|
||||
angularService('$browser', function($log){
|
||||
if (!browserSingleton) {
|
||||
browserSingleton = new Browser(
|
||||
window.location,
|
||||
jqLite(window.document),
|
||||
jqLite(window.document.getElementsByTagName('head')[0]),
|
||||
XHR);
|
||||
XHR,
|
||||
$log);
|
||||
browserSingleton.startPoller(50, function(delay, fn){setTimeout(delay,fn);});
|
||||
browserSingleton.bind();
|
||||
}
|
||||
return browserSingleton;
|
||||
});
|
||||
}, {inject:['$log']});
|
||||
|
||||
extend(angular, {
|
||||
'element': jqLite,
|
||||
|
||||
@@ -8,7 +8,7 @@ var XHR = window.XMLHttpRequest || function () {
|
||||
throw new Error("This browser does not support XMLHttpRequest.");
|
||||
};
|
||||
|
||||
function Browser(location, document, head, XHR) {
|
||||
function Browser(location, document, head, XHR, $log) {
|
||||
var self = this;
|
||||
self.isMock = false;
|
||||
|
||||
@@ -106,28 +106,50 @@ function Browser(location, document, head, XHR) {
|
||||
var rawDocument = document[0];
|
||||
var lastCookies = {};
|
||||
var lastCookieString = '';
|
||||
|
||||
/**
|
||||
* cookies() -> hash of all cookies
|
||||
* cookies(name, value) -> set name to value
|
||||
* if value is undefined delete it
|
||||
* cookies(name) -> should get value, but deletes (no one calls it right now that way)
|
||||
* The cookies method provides a 'private' low level access to browser cookies. It is not meant to
|
||||
* be used directly, use the $cookie service instead.
|
||||
*
|
||||
* The return values vary depending on the arguments that the method was called with as follows:
|
||||
* <ul><li>
|
||||
* cookies() -> hash of all cookies, this is NOT a copy of the internal state, so do not modify it
|
||||
* </li><li>
|
||||
* cookies(name, value) -> set name to value, if value is undefined delete the cookie
|
||||
* </li><li>
|
||||
* cookies(name) -> the same as (name, undefined) == DELETES (no one calls it right now that way)
|
||||
* </li></ul>
|
||||
*/
|
||||
self.cookies = function (name, value){
|
||||
self.cookies = function (/**string*/name, /**string*/value){
|
||||
var cookieLength, cookieArray, i, keyValue;
|
||||
|
||||
if (name) {
|
||||
if (value === _undefined) {
|
||||
delete lastCookies[name];
|
||||
rawDocument.cookie = escape(name) + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
|
||||
} else {
|
||||
rawDocument.cookie = escape(name) + '=' + escape(lastCookies[name] = ''+value);
|
||||
if (isString(value)) {
|
||||
rawDocument.cookie = escape(name) + '=' + escape(lastCookies[name] = value);
|
||||
|
||||
cookieLength = name.length + value.length + 1;
|
||||
if (cookieLength > 4096) {
|
||||
$log.warn("Cookie '"+ name +"' possibly not set or overflowed because it was too large ("+
|
||||
cookieLength + " > 4096 bytes)!");
|
||||
}
|
||||
if (lastCookies.length > 20) {
|
||||
$log.warn("Cookie '"+ name +"' possibly not set or overflowed because too many cookies " +
|
||||
"were already set (" + lastCookies.length + " > 20 )");
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (rawDocument.cookie !== lastCookieString) {
|
||||
lastCookieString = rawDocument.cookie;
|
||||
var cookieArray = lastCookieString.split("; ");
|
||||
cookieArray = lastCookieString.split("; ");
|
||||
lastCookies = {};
|
||||
|
||||
for (var i = 0; i < cookieArray.length; i++) {
|
||||
var keyValue = cookieArray[i].split("=");
|
||||
for (i = 0; i < cookieArray.length; i++) {
|
||||
keyValue = cookieArray[i].split("=");
|
||||
if (keyValue.length === 2) { //ignore nameless cookies
|
||||
lastCookies[unescape(keyValue[0])] = unescape(keyValue[1]);
|
||||
}
|
||||
|
||||
@@ -397,8 +397,18 @@ angularService('$resource', function($xhr){
|
||||
}, {inject: ['$xhr.cache']});
|
||||
|
||||
|
||||
/**
|
||||
* $cookies service provides read/write access to the browser cookies. Currently only session
|
||||
* cookies are supported.
|
||||
*
|
||||
* Only a simple Object is exposed and by adding or removing properties to/from this object, new
|
||||
* cookies are created or deleted from the browser at the end of the current eval.
|
||||
*/
|
||||
angularService('$cookies', function($browser) {
|
||||
var cookies = {}, rootScope = this, lastCookies;
|
||||
var cookies = {},
|
||||
rootScope = this,
|
||||
lastCookies;
|
||||
|
||||
$browser.addPollFn(function(){
|
||||
var currentCookies = $browser.cookies();
|
||||
if (lastCookies != currentCookies) {
|
||||
@@ -407,38 +417,61 @@ angularService('$cookies', function($browser) {
|
||||
rootScope.$eval();
|
||||
}
|
||||
});
|
||||
|
||||
this.$onEval(PRIORITY_FIRST, update);
|
||||
this.$onEval(PRIORITY_LAST, update);
|
||||
|
||||
return cookies;
|
||||
|
||||
function update(){
|
||||
var name, browserCookies = $browser.cookies();
|
||||
var name,
|
||||
browserCookies = $browser.cookies();
|
||||
|
||||
//$cookies -> $browser
|
||||
for(name in cookies) {
|
||||
if (browserCookies[name] !== cookies[name]) {
|
||||
$browser.cookies(name, browserCookies[name] = cookies[name]);
|
||||
if (cookies[name] !== browserCookies[name]) {
|
||||
$browser.cookies(name, cookies[name]);
|
||||
}
|
||||
}
|
||||
|
||||
//get what was actually stored in the browser
|
||||
browserCookies = $browser.cookies();
|
||||
|
||||
//$browser -> $cookies
|
||||
for(name in browserCookies) {
|
||||
if (browserCookies[name] !== cookies[name]) {
|
||||
if (isUndefined(cookies[name])) {
|
||||
$browser.cookies(name, _undefined);
|
||||
} else {
|
||||
cookies[name] = browserCookies[name];
|
||||
}
|
||||
}
|
||||
|
||||
//drop cookies in $cookies for cookies that $browser or real browser dropped
|
||||
for (name in cookies) {
|
||||
if (isUndefined(browserCookies[name])) {
|
||||
delete cookies[name];
|
||||
}
|
||||
}
|
||||
}
|
||||
}, {inject: ['$browser']});
|
||||
|
||||
|
||||
angularService('$sessionStore', function($store) {
|
||||
/**
|
||||
* $cookieStore provides a key-value (string-object) storage that is backed by session cookies.
|
||||
* Objects put or retrieved from this storage are automatically serialized or deserialized.
|
||||
*/
|
||||
angularService('$cookieStore', function($store) {
|
||||
|
||||
return {
|
||||
get: function(key) {
|
||||
get: function(/**string*/key) {
|
||||
return fromJson($store[key]);
|
||||
},
|
||||
|
||||
put: function(key, value) {
|
||||
put: function(/**string*/key, /**Object*/value) {
|
||||
$store[key] = toJson(value);
|
||||
},
|
||||
|
||||
remove: function(key) {
|
||||
remove: function(/**string*/key) {
|
||||
delete $store[key];
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user