Permalink
Browse files

Fix: State saving deletion of cookies was somewhat broken. It would d…

…elete cookies out of order, which is not what we want. Rewrite how the 'overage' of cookies (4K limit) is handled
  • Loading branch information...
1 parent a43714b commit e25b377ee84b0681356a63ef8b0a11edd5adedc4 @DataTables committed Sep 13, 2012
Showing with 64 additions and 34 deletions.
  1. +32 −17 media/js/jquery.dataTables.js
  2. +32 −17 media/src/core/core.state.js
@@ -4508,35 +4508,50 @@
}
/* Are we going to go over the cookie limit of 4KiB? If so, try to delete a cookies
- * belonging to DataTables. This is FAR from bullet proof
+ * belonging to DataTables.
*/
- var sOldName="", iOldTime=9999999999999;
- var iLength = _fnReadCookie( sNameFile )!==null ? document.cookie.length :
- sFullCookie.length + document.cookie.length;
+ var
+ aCookies =document.cookie.split(';'),
+ iNewCookieLen = sFullCookie.split(';')[0].length,
+ aOldCookies = [];
- if ( iLength+10 > 4096 ) /* Magic 10 for padding */
+ if ( iNewCookieLen+document.cookie.length+10 > 4096 ) /* Magic 10 for padding */
{
- var aCookies =document.cookie.split(';');
for ( var i=0, iLen=aCookies.length ; i<iLen ; i++ )
{
if ( aCookies[i].indexOf( sBaseName ) != -1 )
{
/* It's a DataTables cookie, so eval it and check the time stamp */
var aSplitCookie = aCookies[i].split('=');
- try { oData = eval( '('+decodeURIComponent(aSplitCookie[1])+')' ); }
- catch( e ) { continue; }
-
- if ( oData.iCreate && oData.iCreate < iOldTime )
- {
- sOldName = aSplitCookie[0];
- iOldTime = oData.iCreate;
+ try {
+ oData = eval( '('+decodeURIComponent(aSplitCookie[1])+')' );
+
+ if ( oData && oData.iCreate )
+ {
+ aOldCookies.push( {
+ "name": aSplitCookie[0],
+ "time": oData.iCreate
+ } );
+ }
}
+ catch( e ) {}
}
}
-
- if ( sOldName !== "" )
- {
- document.cookie = sOldName+"=; expires=Thu, 01-Jan-1970 00:00:01 GMT; path="+
+
+ // Make sure we delete the oldest ones first
+ aOldCookies.sort( function (a, b) {
+ return b.time - a.time;
+ } );
+
+ // Eliminate as many old DataTables cookies as we need to
+ while ( iNewCookieLen + document.cookie.length + 10 > 4096 ) {
+ if ( aOldCookies.length === 0 ) {
+ // Deleted all DT cookies and still not enough space. Can't state save
+ return;
+ }
+
+ var old = aOldCookies.pop();
+ document.cookie = old.name+"=; expires=Thu, 01-Jan-1970 00:00:01 GMT; path="+
aParts.join('/') + "/";
}
}
@@ -132,35 +132,50 @@ function _fnCreateCookie ( sName, sValue, iSecs, sBaseName, fnCallback )
}
/* Are we going to go over the cookie limit of 4KiB? If so, try to delete a cookies
- * belonging to DataTables. This is FAR from bullet proof
+ * belonging to DataTables.
*/
- var sOldName="", iOldTime=9999999999999;
- var iLength = _fnReadCookie( sNameFile )!==null ? document.cookie.length :
- sFullCookie.length + document.cookie.length;
+ var
+ aCookies =document.cookie.split(';'),
+ iNewCookieLen = sFullCookie.split(';')[0].length,
+ aOldCookies = [];
- if ( iLength+10 > 4096 ) /* Magic 10 for padding */
+ if ( iNewCookieLen+document.cookie.length+10 > 4096 ) /* Magic 10 for padding */
{
- var aCookies =document.cookie.split(';');
for ( var i=0, iLen=aCookies.length ; i<iLen ; i++ )
{
if ( aCookies[i].indexOf( sBaseName ) != -1 )
{
/* It's a DataTables cookie, so eval it and check the time stamp */
var aSplitCookie = aCookies[i].split('=');
- try { oData = eval( '('+decodeURIComponent(aSplitCookie[1])+')' ); }
- catch( e ) { continue; }
-
- if ( oData.iCreate && oData.iCreate < iOldTime )
- {
- sOldName = aSplitCookie[0];
- iOldTime = oData.iCreate;
+ try {
+ oData = eval( '('+decodeURIComponent(aSplitCookie[1])+')' );
+
+ if ( oData && oData.iCreate )
+ {
+ aOldCookies.push( {
+ "name": aSplitCookie[0],
+ "time": oData.iCreate
+ } );
+ }
}
+ catch( e ) {}
}
}
-
- if ( sOldName !== "" )
- {
- document.cookie = sOldName+"=; expires=Thu, 01-Jan-1970 00:00:01 GMT; path="+
+
+ // Make sure we delete the oldest ones first
+ aOldCookies.sort( function (a, b) {
+ return b.time - a.time;
+ } );
+
+ // Eliminate as many old DataTables cookies as we need to
+ while ( iNewCookieLen + document.cookie.length + 10 > 4096 ) {
+ if ( aOldCookies.length === 0 ) {
+ // Deleted all DT cookies and still not enough space. Can't state save
+ return;
+ }
+
+ var old = aOldCookies.pop();
+ document.cookie = old.name+"=; expires=Thu, 01-Jan-1970 00:00:01 GMT; path="+
aParts.join('/') + "/";
}
}

0 comments on commit e25b377

Please sign in to comment.