1
0
mirror of https://github.com/DataTables/DataTables.git synced 2025-03-15 16:29:16 +01:00

Fix: State saving deletion of cookies was somewhat broken. It would delete cookies out of order, which is not what we want. Rewrite how the 'overage' of cookies (4K limit) is handled

This commit is contained in:
Allan Jardine 2012-09-13 18:13:32 +01:00
parent a43714bfba
commit e25b377ee8
2 changed files with 64 additions and 34 deletions

View File

@ -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; }
try {
oData = eval( '('+decodeURIComponent(aSplitCookie[1])+')' );
if ( oData.iCreate && oData.iCreate < iOldTime )
{
sOldName = aSplitCookie[0];
iOldTime = oData.iCreate;
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('/') + "/";
}
}

View File

@ -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; }
try {
oData = eval( '('+decodeURIComponent(aSplitCookie[1])+')' );
if ( oData.iCreate && oData.iCreate < iOldTime )
{
sOldName = aSplitCookie[0];
iOldTime = oData.iCreate;
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('/') + "/";
}
}