Code to Allow Page ValidateRequest to be left on

Feb 15, 2008 at 3:22 PM
The project I am working on requires the Page RequestValidation to be left on. In order to use the RTE I had to make some modifications to the source code to escape the characters before sending it's html through the Page Request. If anyone is interested here is what I had to do:

In the RteStartup.js file, modified the CopyText() Function:

Replaced the Line:
editor.value = richeditor.toHtmlString();

With the following Lines:
var hstr = richeditor.toHtmlString();
editor.value = escape(hstr);

This escapes the HTML characters before sending to the Page Request

In the Rte.cs file I made the following modifications & additions:

Added quickly written code to "unescape" javascript escaped characters. There must be a better way to do this, I just haven't the time to look in to it.
/// <summary>
/// Converts a string with javascript escaped characters back to it's original state.
/// </summary>
/// <param name="strInput">String containing javascript escaped characters.</param>
/// <returns>String with java escaped character returned to there real value</returns>
private string EntityToCharacter(string strInput)
{

string strOutput = strInput;

//loop through hash table containing all characters needed to be converted.
//hashtable is generated by the LoadEntityList function.
foreach (System.Collections.DictionaryEntry entry in LoadEntityList())
{
//define string to be replaced. The javascript escaped characters appear in '%xx' format
string key = "%" + entry.Key;
//define the replacement string by casting the entry value to string.
string value = (string)entry.Value;
//replace the text globally throughout the string.
strOutput = strOutput.Replace(key, value);
}

//Convert carriage returns back to \n
strOutput = strOutput.Replace("%0D%0A", "\n");

//return the results.
return strOutput;
}

/// <summary>
/// Creates a hashtable containing a hex string to character map. hex to char values were generated
/// from http://en.wikibooks.org/wiki/Unicode/Character_reference/0000-0FFF
/// </summary>
/// <returns>Hashtable containing hex string to character map</returns>
private System.Collections.Hashtable LoadEntityList()
{
//define hashtable
System.Collections.Hashtable entityList = new System.Collections.Hashtable();
//set all values.
entityList.Add("20", " ");
entityList.Add("21", "!");
entityList.Add("22", "\"");
entityList.Add("23", "#");
entityList.Add("24", "$");
entityList.Add("25", "%");
entityList.Add("26", "&");
entityList.Add("27", "'");
entityList.Add("28", "(");
entityList.Add("29", ")");
entityList.Add("2A", "*");
entityList.Add("2B", "+");
entityList.Add("2C", ",");
entityList.Add("2D", "-");
entityList.Add("2E", ".");
entityList.Add("2F", "/");
entityList.Add("3A", ":");
entityList.Add("3B", ";");
entityList.Add("3C", "<");
entityList.Add("3D", "=");
entityList.Add("3E", ">");
entityList.Add("3F", "?");
entityList.Add("40", "@");
entityList.Add("5B", "[");
entityList.Add("5C", "\\");
entityList.Add("5D", "]");
entityList.Add("5E", "^");
entityList.Add("5F", "_");
entityList.Add("60", "`");
entityList.Add("7B", "{");
entityList.Add("7C", "|");
entityList.Add("7D", "}");
entityList.Add("7E", "~");
entityList.Add("A1", "¡");
entityList.Add("A2", "¢");
entityList.Add("A3", "£");
entityList.Add("A4", "¤");
entityList.Add("A5", "¥");
entityList.Add("A6", "¦");
entityList.Add("A7", "§");
entityList.Add("A8", "¨");
entityList.Add("A9", "©");
entityList.Add("AA", "ª");
entityList.Add("AB", "«");
entityList.Add("AC", "¬");
entityList.Add("AE", "®");
entityList.Add("AF", "¯");
entityList.Add("B0", "°");
entityList.Add("B1", "±");
entityList.Add("B2", "²");
entityList.Add("B3", "³");
entityList.Add("B4", "´");
entityList.Add("B5", "µ");
entityList.Add("B6", "¶");
entityList.Add("B7", "·");
entityList.Add("B8", "¸");
entityList.Add("B9", "¹");
entityList.Add("BA", "º");
entityList.Add("BB", "»");
entityList.Add("BC", "¼");
entityList.Add("BD", "½");
entityList.Add("BE", "¾");
entityList.Add("BF", "¿");
entityList.Add("C0", "À");
entityList.Add("C1", "Á");
entityList.Add("C2", "Â");
entityList.Add("C3", "Ã");
entityList.Add("C4", "Ä");
entityList.Add("C5", "Å");
entityList.Add("C6", "Æ");
entityList.Add("C7", "Ç");
entityList.Add("C8", "È");
entityList.Add("C9", "É");
entityList.Add("CA", "Ê");
entityList.Add("CB", "Ë");
entityList.Add("CC", "Ì");
entityList.Add("CD", "Í");
entityList.Add("CE", "Î");
entityList.Add("CF", "Ï");
entityList.Add("D0", "Ð");
entityList.Add("D1", "Ñ");
entityList.Add("D2", "Ò");
entityList.Add("D3", "Ó");
entityList.Add("D4", "Ô");
entityList.Add("D5", "Õ");
entityList.Add("D6", "Ö");
entityList.Add("D7", "×");
entityList.Add("D8", "Ø");
entityList.Add("D9", "Ù");
entityList.Add("DA", "Ú");
entityList.Add("DB", "Û");
entityList.Add("DC", "Ü");
entityList.Add("DD", "Ý");
entityList.Add("DE", "Þ");
entityList.Add("DF", "ß");
entityList.Add("E0", "à");
entityList.Add("E1", "á");
entityList.Add("E2", "â");
entityList.Add("E3", "ã");
entityList.Add("E4", "ä");
entityList.Add("E5", "å");
entityList.Add("E6", "æ");
entityList.Add("E7", "ç");
entityList.Add("E8", "è");
entityList.Add("E9", "é");
entityList.Add("EA", "ê");
entityList.Add("EB", "ë");
entityList.Add("EC", "ì");
entityList.Add("ED", "í");
entityList.Add("EE", "î");
entityList.Add("EF", "ï");
entityList.Add("F0", "ð");
entityList.Add("F1", "ñ");
entityList.Add("F2", "ò");
entityList.Add("F3", "ó");
entityList.Add("F4", "ô");
entityList.Add("F5", "õ");
entityList.Add("F6", "ö");
entityList.Add("F7", "÷");
entityList.Add("F8", "ø");
entityList.Add("F9", "ù");
entityList.Add("FA", "ú");
entityList.Add("FB", "û");
entityList.Add("FC", "ü");
entityList.Add("FD", "ý");
entityList.Add("FE", "þ");
entityList.Add("FF", "ÿ");

//return completed hashtable
return entityList;
}
}

Modified the Text property get method to call the above functions:
Replaced original line:
return this.text;
With:
string strEscapedText = this.text;
string strUnEscapedText = EntityToCharacter(strEscapedText);

return strUnEscapedText;

It's not pretty, but I hope it helps anyone in a similar position find a workable solution.