Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for htmlentities.cs

(view source code of htmlentities.cs as plain text)

  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Web;
  4. using System.Windows.Forms;
  5.  
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	static class HTMLEntities
  10. 	{
  11. 		/// <summary>
  12. 		///  The main entry point for the application.
  13. 		/// </summary>
  14. 		[STAThread]
  15. 		static void Main()
  16. 		{
  17. 			Application.SetHighDpiMode( HighDpiMode.SystemAware );
  18. 			Application.EnableVisualStyles( );
  19. 			Application.SetCompatibleTextRenderingDefault( false );
  20. 			Application.Run( new FormHTMLEntities( ) );
  21. 		}
  22. 	}
  23.  
  24.  
  25. 	public partial class FormHTMLEntities : Form
  26. 	{
  27. 		private void ConvertUnicode()
  28. 		{
  29. 			if ( !string.IsNullOrWhiteSpace( this.textBoxUnicode.Text ) )
  30. 			{
  31. 				int entity = Convert.ToInt32( (char)this.textBoxUnicode.Text[0] );
  32. 				this.textBoxHTMLEntity.Text = entity.ToString( );
  33. 				this.textBoxURLEncoded.Text = HttpUtility.UrlEncode( this.textBoxUnicode.Text );
  34. 				MoveCursorToEnd( );
  35. 			}
  36. 		}
  37.  
  38. 		private void MoveCursorToEnd()
  39. 		{
  40. 			this.textBoxHTMLEntity.SelectionStart = this.textBoxHTMLEntity.Text.Length;
  41. 			this.textBoxHTMLEntity.SelectionLength = 0;
  42. 			this.textBoxUnicode.SelectionStart = this.textBoxUnicode.Text.Length;
  43. 			this.textBoxUnicode.SelectionLength = 0;
  44. 			this.textBoxURLEncoded.SelectionStart = this.textBoxURLEncoded.Text.Length;
  45. 			this.textBoxURLEncoded.SelectionLength = 0;
  46.  
  47. 		}
  48.  
  49. 		private void ValidateHTMLEntity()
  50. 		{
  51. 			string text = this.textBoxHTMLEntity.Text;
  52. 			Regex regex = new Regex( "[^\\d]" );
  53. 			if ( regex.IsMatch( text ) )
  54. 			{
  55. 				int cursorpos = this.textBoxHTMLEntity.SelectionStart + this.textBoxHTMLEntity.SelectionLength;
  56. 				text = regex.Replace( text, "" );
  57. 				this.textBoxHTMLEntity.SelectionStart = Math.Min( cursorpos, text.Length );
  58. 				this.textBoxHTMLEntity.SelectionLength = 0;
  59. 			}
  60. 			else if ( text.Length > 1 )
  61. 			{
  62. 				int entity = Convert.ToInt32( text );
  63. 				this.textBoxUnicode.Text = System.Net.WebUtility.HtmlDecode( string.Format( "&#{0};", text ) );
  64. 				this.textBoxURLEncoded.Text = HttpUtility.UrlEncode( this.textBoxUnicode.Text );
  65. 			}
  66. 		}
  67.  
  68. 		private void ValidatURLEncoded()
  69. 		{
  70. 			string text = this.textBoxURLEncoded.Text;
  71. 			Regex regex = new Regex( "[^%\\dA-Fa-f]" );
  72. 			if ( regex.IsMatch( text ) )
  73. 			{
  74. 				int cursorpos = this.textBoxURLEncoded.SelectionStart + this.textBoxURLEncoded.SelectionLength;
  75. 				text = regex.Replace( text, "" );
  76. 				this.textBoxURLEncoded.SelectionStart = Math.Min( cursorpos, text.Length );
  77. 				this.textBoxURLEncoded.SelectionLength = 0;
  78. 			}
  79. 			else if ( text.Length > 2 )
  80. 			{
  81. 				regex = new Regex( "^(%[\\dA-Fa-f]{2}){1,2}$" );
  82. 				if ( regex.IsMatch( text ) )
  83. 				{
  84. 					char newchar = HttpUtility.UrlDecode( text )[0];
  85. 					this.textBoxUnicode.Text = newchar.ToString( );
  86. 					int entity = Convert.ToInt32( newchar );
  87. 					this.textBoxHTMLEntity.Text = entity.ToString( );
  88. 				}
  89. 				else
  90. 				{
  91. 					this.textBoxUnicode.Text = string.Empty;
  92. 					this.textBoxHTMLEntity.Text = string.Empty;
  93. 				}
  94. 			}
  95. 		}
  96. 	}
  97. }

page last uploaded: 2021-01-27