Bug with cookie persistance when the path may or may not include a trailing slash (patch included)

Bug with cookie persistance when the path may or may not include a trailing slash (patch included)

bcarrbcarr Posts: 1Questions: 0Answers: 0
edited March 2012 in Bug reports
When a trailing slash is included in a path, for example, /something_id/1/, the name of the persistance cookie becomes
[code]_something_id_[/code]
However, when the path omits the trailing slash, the cookie name becomes
[code]_something_id_1[/code]

This behavior causes 2 cookies to be generated when a page may be loaded either way (or if window.location is modified by something like the jquery history plugin), leading to inconsistent behavior related to persisting or restoring a user's filtering and paging preferences. This issue was found in version 1.7.4 of the plugin, but it appears the problematic lines of code are still present in the 1.9.0 sources. I have included a patch against the 1.7.4 sources below. The changes should work against the 1.9.0 sources as well, but I have tested against the newer version.

Thanks,
Brian Carr


[code]
diff --git a/media/js/jquery.dataTables.js b/media/js/jquery.dataTables.js
index fce5a6b..162ea67 100644
--- a/media/js/jquery.dataTables.js
+++ b/media/js/jquery.dataTables.js
@@ -5968,6 +5968,23 @@
}

/*
+ * Function: _fnGetCookieName
+ * Purpose: Constructs the name of the cookie from the configured prefix and the first non-empty item in the path
+ * Inputs: string:sName - base name of the cookie to create
+ * array:aParts - the array of path components. this array will be mutated in the process of determining
+ the full name of the cookie. While this is highly undesirable, it is necessary for the
+ way the cookie path is actually written.
+ * Returns: the full name of the cookie to persist
+ */
+ function _fnGetCookieName(sName, aParts) {
+ var postfix = "";
+ do {
+ postfix = aParts.pop().replace(/[\/:]/g,"").toLowerCase();
+ } while (!postfix.length)
+ return sName + '_' + postfix;
+ }
+
+ /*
* Function: _fnCreateCookie
* Purpose: Create a new cookie with a value to store the state of a table
* Returns: -
@@ -5989,7 +6006,7 @@
* patch to use at least some of the path
*/
var aParts = window.location.pathname.split('/');
- var sNameFile = sName + '_' + aParts.pop().replace(/[\/:]/g,"").toLowerCase();
+ var sNameFile = _fnGetCookieName(sName, aParts);
var sFullCookie, oData;

if ( fnCallback !== null )
@@ -6050,10 +6067,8 @@
*/
function _fnReadCookie ( sName )
{
- var
- aParts = window.location.pathname.split('/'),
- sNameEQ = sName + '_' + aParts[aParts.length-1].replace(/[\/:]/g,"").toLowerCase() + '=',
- sCookieContents = document.cookie.split(';');
+ var sNameEQ = _fnGetCookieName(sName, window.location.pathname.split('/')) + '=';
+ var sCookieContents = document.cookie.split(';');

for( var i=0 ; i
This discussion has been closed.