includes/clientside/static/misc.js
changeset 426 f5718d7c2a6a
parent 420 301f546688d1
child 436 242353360e37
equal deleted inserted replaced
423:990ccfb20120 426:f5718d7c2a6a
   870   } else if( document.body && ( document.body.clientWidth || document.body.clientWidth ) ) {
   870   } else if( document.body && ( document.body.clientWidth || document.body.clientWidth ) ) {
   871     myWidth = document.body.clientWidth;
   871     myWidth = document.body.clientWidth;
   872   }
   872   }
   873   return myWidth;
   873   return myWidth;
   874 }
   874 }
       
   875 
       
   876 /**
       
   877  * Sanitizes a page URL string so that it can safely be stored in the database.
       
   878  * @param string Page ID to sanitize
       
   879  * @return string Cleaned text
       
   880  */
       
   881 
       
   882 function sanitize_page_id(page_id)
       
   883 {
       
   884   // Remove character escapes
       
   885   page_id = dirtify_page_id(page_id);
       
   886 
       
   887   var regex = new RegExp('[A-Za-z0-9\\[\\]\./:;\(\)@_-]', 'g');
       
   888   pid_clean = page_id.replace(regex, 'X');
       
   889   var pid_dirty = [];
       
   890   for ( var i = 0; i < pid_clean.length; i++ )
       
   891     pid_dirty[i] = pid_clean.substr(i, 1);
       
   892 
       
   893   for ( var i = 0; i < pid_dirty.length; i++ )
       
   894   {
       
   895     var char = pid_dirty[i];
       
   896     if ( char == 'X' )
       
   897       continue;
       
   898     var cid = char.charCodeAt(0);
       
   899     cid = cid.toString(16).toUpperCase();
       
   900     if ( cid.length < 2 )
       
   901     {
       
   902       cid = '0' + cid;
       
   903     }
       
   904     pid_dirty[i] = "." + cid;
       
   905   }
       
   906   
       
   907   var pid_chars = [];
       
   908   for ( var i = 0; i < page_id.length; i++ )
       
   909     pid_chars[i] = page_id.substr(i, 1);
       
   910   
       
   911   var page_id_cleaned = '';
       
   912 
       
   913   for ( var id in pid_chars )
       
   914   {
       
   915     var char = pid_chars[id];
       
   916     if ( pid_dirty[id] == 'X' )
       
   917       page_id_cleaned += char;
       
   918     else
       
   919       page_id_cleaned += pid_dirty[id];
       
   920   }
       
   921   
       
   922   return page_id_cleaned;
       
   923 }
       
   924 
       
   925 /**
       
   926  * Removes character escapes in a page ID string
       
   927  * @param string Page ID string to dirty up
       
   928  * @return string
       
   929  */
       
   930 
       
   931 function dirtify_page_id(page_id)
       
   932 {
       
   933   // First, replace spaces with underscores
       
   934   page_id = page_id.replace(/ /g, '_');
       
   935 
       
   936   var matches = page_id.match(/\.[A-Fa-f0-9][A-Fa-f0-9]/g);
       
   937   
       
   938   if ( matches != null )
       
   939   {
       
   940     for ( var i = 0; i < matches.length; i++ )
       
   941     {
       
   942       var match = matches[i];
       
   943       var byt = (match.substr(1)).toUpperCase();
       
   944       var code = eval("0x" + byt);
       
   945       var regex = new RegExp('\\.' + byt, 'g');
       
   946       page_id = page_id.replace(regex, String.fromCharCode(code));
       
   947     }
       
   948   }
       
   949   
       
   950   return page_id;
       
   951 }