Changeset 32774 for main

Show
Ignore:
Timestamp:
13.02.2019 17:55:54 (10 months ago)
Author:
ak19
Message:

Kathy's style of char replacement reads simpler.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/web/interfaces/default/js/document_scripts.js

    r32773 r32774  
    4343/* URL encode RESERVED characters in a non-URL context of a URL, such as the inline template (ilt) parameter value of a URL */ 
    4444function makeSafeForURL(url_part, encode_percentages) { 
    45     // https://stackoverflow.com/questions/7368407/javascript-replace-a-set-of-characters-with-another-one 
    46     var reserved_mappings = { 
    47     ';': '%3B', 
    48     '/': '%2F', 
    49     '?': '%3F', 
    50     ':': '%3A', 
    51     '@': '%40', 
    52     '=': '%3D', 
    53     '&': '%26' 
    54     }; 
    5545    // https://stackoverflow.com/questions/12797118/how-can-i-declare-optional-function-parameters-in-javascript 
    5646    encode_percentages = encode_percentages || 1; // this method forces the URL-encoding of any % in url_part, e.g. do this for inline-templates that haven't ever been encoded 
    57     url_part = makeURLSafe(url_part, encode_percentages); 
    58  
    59     var url_encoded = url_part.replace(/[\;\/\?\:\@\=\&]/g, function(s) { 
    60     return reserved_mappings[s]; 
    61     }); 
    6247     
    63     //var url_encoded = url_part.replace(/;/g, "%3B").replace(/\//g, "%2F").replace(/\?/g, "%3F").replace(/\:/g, "%3A").replace(/\@/g, "%40").replace(/=/g, "%3D").replace(/\&/g,"%26"); 
     48    var url_encoded = makeURLSafe(url_part, encode_percentages); 
     49    url_encoded = url_encoded.replace(/;/g, "%3B").replace(/\//g, "%2F").replace(/\?/g, "%3F").replace(/\:/g, "%3A").replace(/\@/g, "%40").replace(/=/g, "%3D").replace(/\&/g,"%26"); 
    6450    return url_encoded; 
    6551} 
     
    6955   Set encode_percentages to 1 (true) if the url isn't already partly URL encoded 
    7056*/ 
    71 function makeURLSafe(url, encode_percentages) { 
    72     // https://stackoverflow.com/questions/12797118/how-can-i-declare-optional-function-parameters-in-javascript 
    73     encode_percentages = encode_percentages || 0; 
    74      
    75     var unsafe_mappings = { 
    76     ' ': '%20', 
    77     '"': '%22', 
    78     '<': '%3C', 
    79     '>': '%3E', 
    80     '#': '%23', 
    81     '{': '%7B', 
    82     '}': '%7D', 
    83     '|': '%7C', 
    84     '\\': '%5C', 
    85     '^': '%5E', 
    86     //'~': '~', // unreserved char (but is it then unsafe?), as per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI 
    87     '[': '%5B', 
    88     ']': '%5D', 
    89     '`': '%60' 
    90     };     
     57function makeURLSafe(url, encode_percentages) {     
     58    encode_percentages = encode_percentages || 0; // https://stackoverflow.com/questions/12797118/how-can-i-declare-optional-function-parameters-in-javascript 
    9159 
    9260    var url_encoded = url; 
    93     if(encode_percentages) { 
    94     // https://stackoverflow.com/questions/1168807/how-can-i-add-a-key-value-pair-to-a-javascript-object 
    95     //unsafe_mappings["%"] = "%25"; 
    96     url_encoded = url_encoded.replace(/\%/g,"%25"); // encode % first 
    97  
    98     } 
    99     url_encoded = url_encoded.replace(/[\ \"\<\>\#\{\}\|\\\^\[\]\`]/g, function(s) { 
    100     return unsafe_mappings[s]; 
    101     }); 
    102      
    103  
    104     //var url_encoded = url; 
    105     ///if(encode_percentages) { url_encoded = url_encoded.replace(/\%/g,"%25"); } // encode % first 
    106     //url_encoded = url_encoded.replace(/ /g, "%20").replace(/\"/g,"%22").replace(/\</g,"%3C").replace(/\>/g,"%3E").replace(/\#/g,"%23").replace(/\{/g,"%7B").replace(/\}/g,"%7D"); 
    107     //url_encoded = url_encoded.replace(/\|/g,"%7C").replace(/\\/g,"%5C").replace(/\^/g,"%5E").replace(/\[/g,"%5B").replace(/\]/g,"%5D").replace(/\`/g,"%60"); 
    108      
     61    if(encode_percentages) { url_encoded = url_encoded.replace(/\%/g,"%25"); } // encode % first 
     62    url_encoded = url_encoded.replace(/ /g, "%20").replace(/\"/g,"%22").replace(/\</g,"%3C").replace(/\>/g,"%3E").replace(/\#/g,"%23").replace(/\{/g,"%7B").replace(/\}/g,"%7D"); 
     63    url_encoded = url_encoded.replace(/\|/g,"%7C").replace(/\\/g,"%5C").replace(/\^/g,"%5E").replace(/\[/g,"%5B").replace(/\]/g,"%5D").replace(/\`/g,"%60"); 
     64    // Should we handle ~, but then what is its URL encoded value? Because https://meyerweb.com/eric/tools/dencoder/ URLencodes ~ to ~. 
    10965    return url_encoded; 
    11066}