2 // System.Web.HttpUtility
5 // Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 // Wictor Wilén (decode/encode functions) (wictor@ibizkit.se)
7 // Tim Coleman (tim@timcoleman.com)
8 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System
.Collections
;
33 using System
.Globalization
;
36 using System
.Web
.Util
;
38 namespace System
.Web
{
39 public sealed class HttpUtility
{
43 const string _hex
= "0123456789ABCDEF";
44 const string _chars
= "<>;:.?=&@*+%/\\";
45 static Hashtable entities
;
46 static object lock_
= new object ();
50 static Hashtable Entities
{
63 static void InitEntities ()
65 // Build the hash table of HTML entity references. This list comes
66 // from the HTML 4.01 W3C recommendation.
67 entities
= new Hashtable ();
68 entities
.Add ("nbsp", '\u00A0');
69 entities
.Add ("iexcl", '\u00A1');
70 entities
.Add ("cent", '\u00A2');
71 entities
.Add ("pound", '\u00A3');
72 entities
.Add ("curren", '\u00A4');
73 entities
.Add ("yen", '\u00A5');
74 entities
.Add ("brvbar", '\u00A6');
75 entities
.Add ("sect", '\u00A7');
76 entities
.Add ("uml", '\u00A8');
77 entities
.Add ("copy", '\u00A9');
78 entities
.Add ("ordf", '\u00AA');
79 entities
.Add ("laquo", '\u00AB');
80 entities
.Add ("not", '\u00AC');
81 entities
.Add ("shy", '\u00AD');
82 entities
.Add ("reg", '\u00AE');
83 entities
.Add ("macr", '\u00AF');
84 entities
.Add ("deg", '\u00B0');
85 entities
.Add ("plusmn", '\u00B1');
86 entities
.Add ("sup2", '\u00B2');
87 entities
.Add ("sup3", '\u00B3');
88 entities
.Add ("acute", '\u00B4');
89 entities
.Add ("micro", '\u00B5');
90 entities
.Add ("para", '\u00B6');
91 entities
.Add ("middot", '\u00B7');
92 entities
.Add ("cedil", '\u00B8');
93 entities
.Add ("sup1", '\u00B9');
94 entities
.Add ("ordm", '\u00BA');
95 entities
.Add ("raquo", '\u00BB');
96 entities
.Add ("frac14", '\u00BC');
97 entities
.Add ("frac12", '\u00BD');
98 entities
.Add ("frac34", '\u00BE');
99 entities
.Add ("iquest", '\u00BF');
100 entities
.Add ("Agrave", '\u00C0');
101 entities
.Add ("Aacute", '\u00C1');
102 entities
.Add ("Acirc", '\u00C2');
103 entities
.Add ("Atilde", '\u00C3');
104 entities
.Add ("Auml", '\u00C4');
105 entities
.Add ("Aring", '\u00C5');
106 entities
.Add ("AElig", '\u00C6');
107 entities
.Add ("Ccedil", '\u00C7');
108 entities
.Add ("Egrave", '\u00C8');
109 entities
.Add ("Eacute", '\u00C9');
110 entities
.Add ("Ecirc", '\u00CA');
111 entities
.Add ("Euml", '\u00CB');
112 entities
.Add ("Igrave", '\u00CC');
113 entities
.Add ("Iacute", '\u00CD');
114 entities
.Add ("Icirc", '\u00CE');
115 entities
.Add ("Iuml", '\u00CF');
116 entities
.Add ("ETH", '\u00D0');
117 entities
.Add ("Ntilde", '\u00D1');
118 entities
.Add ("Ograve", '\u00D2');
119 entities
.Add ("Oacute", '\u00D3');
120 entities
.Add ("Ocirc", '\u00D4');
121 entities
.Add ("Otilde", '\u00D5');
122 entities
.Add ("Ouml", '\u00D6');
123 entities
.Add ("times", '\u00D7');
124 entities
.Add ("Oslash", '\u00D8');
125 entities
.Add ("Ugrave", '\u00D9');
126 entities
.Add ("Uacute", '\u00DA');
127 entities
.Add ("Ucirc", '\u00DB');
128 entities
.Add ("Uuml", '\u00DC');
129 entities
.Add ("Yacute", '\u00DD');
130 entities
.Add ("THORN", '\u00DE');
131 entities
.Add ("szlig", '\u00DF');
132 entities
.Add ("agrave", '\u00E0');
133 entities
.Add ("aacute", '\u00E1');
134 entities
.Add ("acirc", '\u00E2');
135 entities
.Add ("atilde", '\u00E3');
136 entities
.Add ("auml", '\u00E4');
137 entities
.Add ("aring", '\u00E5');
138 entities
.Add ("aelig", '\u00E6');
139 entities
.Add ("ccedil", '\u00E7');
140 entities
.Add ("egrave", '\u00E8');
141 entities
.Add ("eacute", '\u00E9');
142 entities
.Add ("ecirc", '\u00EA');
143 entities
.Add ("euml", '\u00EB');
144 entities
.Add ("igrave", '\u00EC');
145 entities
.Add ("iacute", '\u00ED');
146 entities
.Add ("icirc", '\u00EE');
147 entities
.Add ("iuml", '\u00EF');
148 entities
.Add ("eth", '\u00F0');
149 entities
.Add ("ntilde", '\u00F1');
150 entities
.Add ("ograve", '\u00F2');
151 entities
.Add ("oacute", '\u00F3');
152 entities
.Add ("ocirc", '\u00F4');
153 entities
.Add ("otilde", '\u00F5');
154 entities
.Add ("ouml", '\u00F6');
155 entities
.Add ("divide", '\u00F7');
156 entities
.Add ("oslash", '\u00F8');
157 entities
.Add ("ugrave", '\u00F9');
158 entities
.Add ("uacute", '\u00FA');
159 entities
.Add ("ucirc", '\u00FB');
160 entities
.Add ("uuml", '\u00FC');
161 entities
.Add ("yacute", '\u00FD');
162 entities
.Add ("thorn", '\u00FE');
163 entities
.Add ("yuml", '\u00FF');
164 entities
.Add ("fnof", '\u0192');
165 entities
.Add ("Alpha", '\u0391');
166 entities
.Add ("Beta", '\u0392');
167 entities
.Add ("Gamma", '\u0393');
168 entities
.Add ("Delta", '\u0394');
169 entities
.Add ("Epsilon", '\u0395');
170 entities
.Add ("Zeta", '\u0396');
171 entities
.Add ("Eta", '\u0397');
172 entities
.Add ("Theta", '\u0398');
173 entities
.Add ("Iota", '\u0399');
174 entities
.Add ("Kappa", '\u039A');
175 entities
.Add ("Lambda", '\u039B');
176 entities
.Add ("Mu", '\u039C');
177 entities
.Add ("Nu", '\u039D');
178 entities
.Add ("Xi", '\u039E');
179 entities
.Add ("Omicron", '\u039F');
180 entities
.Add ("Pi", '\u03A0');
181 entities
.Add ("Rho", '\u03A1');
182 entities
.Add ("Sigma", '\u03A3');
183 entities
.Add ("Tau", '\u03A4');
184 entities
.Add ("Upsilon", '\u03A5');
185 entities
.Add ("Phi", '\u03A6');
186 entities
.Add ("Chi", '\u03A7');
187 entities
.Add ("Psi", '\u03A8');
188 entities
.Add ("Omega", '\u03A9');
189 entities
.Add ("alpha", '\u03B1');
190 entities
.Add ("beta", '\u03B2');
191 entities
.Add ("gamma", '\u03B3');
192 entities
.Add ("delta", '\u03B4');
193 entities
.Add ("epsilon", '\u03B5');
194 entities
.Add ("zeta", '\u03B6');
195 entities
.Add ("eta", '\u03B7');
196 entities
.Add ("theta", '\u03B8');
197 entities
.Add ("iota", '\u03B9');
198 entities
.Add ("kappa", '\u03BA');
199 entities
.Add ("lambda", '\u03BB');
200 entities
.Add ("mu", '\u03BC');
201 entities
.Add ("nu", '\u03BD');
202 entities
.Add ("xi", '\u03BE');
203 entities
.Add ("omicron", '\u03BF');
204 entities
.Add ("pi", '\u03C0');
205 entities
.Add ("rho", '\u03C1');
206 entities
.Add ("sigmaf", '\u03C2');
207 entities
.Add ("sigma", '\u03C3');
208 entities
.Add ("tau", '\u03C4');
209 entities
.Add ("upsilon", '\u03C5');
210 entities
.Add ("phi", '\u03C6');
211 entities
.Add ("chi", '\u03C7');
212 entities
.Add ("psi", '\u03C8');
213 entities
.Add ("omega", '\u03C9');
214 entities
.Add ("thetasym", '\u03D1');
215 entities
.Add ("upsih", '\u03D2');
216 entities
.Add ("piv", '\u03D6');
217 entities
.Add ("bull", '\u2022');
218 entities
.Add ("hellip", '\u2026');
219 entities
.Add ("prime", '\u2032');
220 entities
.Add ("Prime", '\u2033');
221 entities
.Add ("oline", '\u203E');
222 entities
.Add ("frasl", '\u2044');
223 entities
.Add ("weierp", '\u2118');
224 entities
.Add ("image", '\u2111');
225 entities
.Add ("real", '\u211C');
226 entities
.Add ("trade", '\u2122');
227 entities
.Add ("alefsym", '\u2135');
228 entities
.Add ("larr", '\u2190');
229 entities
.Add ("uarr", '\u2191');
230 entities
.Add ("rarr", '\u2192');
231 entities
.Add ("darr", '\u2193');
232 entities
.Add ("harr", '\u2194');
233 entities
.Add ("crarr", '\u21B5');
234 entities
.Add ("lArr", '\u21D0');
235 entities
.Add ("uArr", '\u21D1');
236 entities
.Add ("rArr", '\u21D2');
237 entities
.Add ("dArr", '\u21D3');
238 entities
.Add ("hArr", '\u21D4');
239 entities
.Add ("forall", '\u2200');
240 entities
.Add ("part", '\u2202');
241 entities
.Add ("exist", '\u2203');
242 entities
.Add ("empty", '\u2205');
243 entities
.Add ("nabla", '\u2207');
244 entities
.Add ("isin", '\u2208');
245 entities
.Add ("notin", '\u2209');
246 entities
.Add ("ni", '\u220B');
247 entities
.Add ("prod", '\u220F');
248 entities
.Add ("sum", '\u2211');
249 entities
.Add ("minus", '\u2212');
250 entities
.Add ("lowast", '\u2217');
251 entities
.Add ("radic", '\u221A');
252 entities
.Add ("prop", '\u221D');
253 entities
.Add ("infin", '\u221E');
254 entities
.Add ("ang", '\u2220');
255 entities
.Add ("and", '\u2227');
256 entities
.Add ("or", '\u2228');
257 entities
.Add ("cap", '\u2229');
258 entities
.Add ("cup", '\u222A');
259 entities
.Add ("int", '\u222B');
260 entities
.Add ("there4", '\u2234');
261 entities
.Add ("sim", '\u223C');
262 entities
.Add ("cong", '\u2245');
263 entities
.Add ("asymp", '\u2248');
264 entities
.Add ("ne", '\u2260');
265 entities
.Add ("equiv", '\u2261');
266 entities
.Add ("le", '\u2264');
267 entities
.Add ("ge", '\u2265');
268 entities
.Add ("sub", '\u2282');
269 entities
.Add ("sup", '\u2283');
270 entities
.Add ("nsub", '\u2284');
271 entities
.Add ("sube", '\u2286');
272 entities
.Add ("supe", '\u2287');
273 entities
.Add ("oplus", '\u2295');
274 entities
.Add ("otimes", '\u2297');
275 entities
.Add ("perp", '\u22A5');
276 entities
.Add ("sdot", '\u22C5');
277 entities
.Add ("lceil", '\u2308');
278 entities
.Add ("rceil", '\u2309');
279 entities
.Add ("lfloor", '\u230A');
280 entities
.Add ("rfloor", '\u230B');
281 entities
.Add ("lang", '\u2329');
282 entities
.Add ("rang", '\u232A');
283 entities
.Add ("loz", '\u25CA');
284 entities
.Add ("spades", '\u2660');
285 entities
.Add ("clubs", '\u2663');
286 entities
.Add ("hearts", '\u2665');
287 entities
.Add ("diams", '\u2666');
288 entities
.Add ("quot", '\u0022');
289 entities
.Add ("amp", '\u0026');
290 entities
.Add ("lt", '\u003C');
291 entities
.Add ("gt", '\u003E');
292 entities
.Add ("OElig", '\u0152');
293 entities
.Add ("oelig", '\u0153');
294 entities
.Add ("Scaron", '\u0160');
295 entities
.Add ("scaron", '\u0161');
296 entities
.Add ("Yuml", '\u0178');
297 entities
.Add ("circ", '\u02C6');
298 entities
.Add ("tilde", '\u02DC');
299 entities
.Add ("ensp", '\u2002');
300 entities
.Add ("emsp", '\u2003');
301 entities
.Add ("thinsp", '\u2009');
302 entities
.Add ("zwnj", '\u200C');
303 entities
.Add ("zwj", '\u200D');
304 entities
.Add ("lrm", '\u200E');
305 entities
.Add ("rlm", '\u200F');
306 entities
.Add ("ndash", '\u2013');
307 entities
.Add ("mdash", '\u2014');
308 entities
.Add ("lsquo", '\u2018');
309 entities
.Add ("rsquo", '\u2019');
310 entities
.Add ("sbquo", '\u201A');
311 entities
.Add ("ldquo", '\u201C');
312 entities
.Add ("rdquo", '\u201D');
313 entities
.Add ("bdquo", '\u201E');
314 entities
.Add ("dagger", '\u2020');
315 entities
.Add ("Dagger", '\u2021');
316 entities
.Add ("permil", '\u2030');
317 entities
.Add ("lsaquo", '\u2039');
318 entities
.Add ("rsaquo", '\u203A');
319 entities
.Add ("euro", '\u20AC');
322 public HttpUtility ()
326 #endregion // Constructors
330 public static void HtmlAttributeEncode (string s
, TextWriter output
)
332 output
.Write(HtmlAttributeEncode(s
));
335 public static string HtmlAttributeEncode (string s
)
340 if (s
.IndexOf ('&') == -1 && s
.IndexOf ('"') == -1)
343 StringBuilder output
= new StringBuilder ();
344 foreach (char c
in s
)
347 output
.Append ("&");
350 output
.Append (""");
357 return output
.ToString();
360 public static string UrlDecode (string str
)
362 return UrlDecode(str
, Encoding
.UTF8
);
365 private static char [] GetChars (MemoryStream b
, Encoding e
)
367 return e
.GetChars (b
.GetBuffer (), 0, (int) b
.Length
);
370 public static string UrlDecode (string s
, Encoding e
)
375 if (s
.IndexOf ('%') == -1 && s
.IndexOf ('+') == -1)
381 StringBuilder output
= new StringBuilder ();
383 NumberStyles hexa
= NumberStyles
.HexNumber
;
384 MemoryStream bytes
= new MemoryStream ();
386 for (int i
= 0; i
< len
; i
++) {
387 if (s
[i
] == '%' && i
+ 2 < len
) {
388 if (s
[i
+ 1] == 'u' && i
+ 5 < len
) {
389 if (bytes
.Length
> 0) {
390 output
.Append (GetChars (bytes
, e
));
393 output
.Append ((char) Int32
.Parse (s
.Substring (i
+ 2, 4), hexa
));
396 bytes
.WriteByte ((byte) Int32
.Parse (s
.Substring (i
+ 1, 2), hexa
));
402 if (bytes
.Length
> 0) {
403 output
.Append (GetChars (bytes
, e
));
410 output
.Append (s
[i
]);
414 if (bytes
.Length
> 0) {
415 output
.Append (GetChars (bytes
, e
));
419 return output
.ToString ();
422 public static string UrlDecode (byte [] bytes
, Encoding e
)
427 return UrlDecode (bytes
, 0, bytes
.Length
, e
);
430 private static int GetInt (byte b
)
432 char c
= Char
.ToUpper ((char) b
);
433 if (c
>= '0' && c
<= '9')
436 if (c
< 'A' || c
> 'F')
439 return (c
- 'A' + 10);
442 private static char GetChar (byte [] bytes
, int offset
, int length
)
445 int end
= length
+ offset
;
446 for (int i
= offset
; i
< end
; i
++)
447 value = (value << 4) + GetInt (bytes
[i
]);
452 public static string UrlDecode (byte [] bytes
, int offset
, int count
, Encoding e
)
454 if (bytes
== null || count
== 0)
458 throw new ArgumentNullException ("bytes");
460 if (offset
< 0 || offset
> bytes
.Length
)
461 throw new ArgumentOutOfRangeException ("offset");
463 if (count
< 0 || offset
+ count
> bytes
.Length
)
464 throw new ArgumentOutOfRangeException ("count");
466 StringBuilder output
= new StringBuilder ();
467 MemoryStream acc
= new MemoryStream ();
469 int end
= count
+ offset
;
470 for (int i
= offset
; i
< end
; i
++) {
471 if (bytes
[i
] == '%' && i
+ 2 < count
) {
472 if (bytes
[i
+ 1] == (byte) 'u' && i
+ 5 < end
) {
473 if (acc
.Length
> 0) {
474 output
.Append (GetChars (acc
, e
));
477 output
.Append (GetChar (bytes
, i
+ 2, 4));
480 acc
.WriteByte ((byte) GetChar (bytes
, i
+ 1, 2));
486 if (acc
.Length
> 0) {
487 output
.Append (GetChars (acc
, e
));
491 if (bytes
[i
] == '+') {
494 output
.Append ((char) bytes
[i
]);
498 if (acc
.Length
> 0) {
499 output
.Append (GetChars (acc
, e
));
503 return output
.ToString ();
506 public static byte [] UrlDecodeToBytes (byte [] bytes
)
511 return UrlDecodeToBytes (bytes
, 0, bytes
.Length
);
514 public static byte [] UrlDecodeToBytes (string str
)
516 return UrlDecodeToBytes (str
, Encoding
.UTF8
);
519 public static byte [] UrlDecodeToBytes (string str
, Encoding e
)
525 throw new ArgumentNullException ("e");
527 return UrlDecodeToBytes (e
.GetBytes (str
));
530 public static byte [] UrlDecodeToBytes (byte [] bytes
, int offset
, int count
)
535 int len
= bytes
.Length
;
536 if (offset
< 0 || offset
>= len
)
537 throw new ArgumentOutOfRangeException("offset");
539 if (count
< 0 || offset
> len
- count
)
540 throw new ArgumentOutOfRangeException("count");
542 MemoryStream result
= new MemoryStream ();
543 int end
= offset
+ count
;
544 for (int i
= offset
; i
< end
; i
++){
545 char c
= (char) bytes
[i
];
548 else if (c
== '%' && i
< end
- 2) {
549 c
= GetChar (bytes
, i
, 2);
552 result
.WriteByte ((byte) c
);
555 return result
.ToArray ();
558 public static string UrlEncode(string str
)
560 return UrlEncode(str
, Encoding
.UTF8
);
563 public static string UrlEncode (string s
, Encoding Enc
)
571 byte [] bytes
= Enc
.GetBytes (s
);
572 return Encoding
.ASCII
.GetString (UrlEncodeToBytes (bytes
, 0, bytes
.Length
));
575 public static string UrlEncode (byte [] bytes
)
580 if (bytes
.Length
== 0)
583 return Encoding
.ASCII
.GetString (UrlEncodeToBytes (bytes
, 0, bytes
.Length
));
586 public static string UrlEncode (byte [] bytes
, int offset
, int count
)
591 if (bytes
.Length
== 0)
594 return Encoding
.ASCII
.GetString (UrlEncodeToBytes (bytes
, offset
, count
));
597 public static byte [] UrlEncodeToBytes (string str
)
599 return UrlEncodeToBytes (str
, Encoding
.UTF8
);
602 public static byte [] UrlEncodeToBytes (string str
, Encoding e
)
610 byte [] bytes
= e
.GetBytes (str
);
611 return UrlEncodeToBytes (bytes
, 0, bytes
.Length
);
614 public static byte [] UrlEncodeToBytes (byte [] bytes
)
619 if (bytes
.Length
== 0)
622 return UrlEncodeToBytes (bytes
, 0, bytes
.Length
);
625 static char [] hexChars
= "0123456789abcdef".ToCharArray ();
627 public static byte [] UrlEncodeToBytes (byte [] bytes
, int offset
, int count
)
632 int len
= bytes
.Length
;
636 if (offset
< 0 || offset
>= len
)
637 throw new ArgumentOutOfRangeException("offset");
639 if (count
< 0 || count
> len
- offset
)
640 throw new ArgumentOutOfRangeException("count");
642 MemoryStream result
= new MemoryStream ();
643 int end
= offset
+ count
;
644 for (int i
= offset
; i
< end
; i
++) {
645 char c
= (char) bytes
[i
];
646 if ((c
== ' ') || (c
< '0' && c
!= '-' && c
!= '.') ||
647 (c
< 'A' && c
> '9') ||
648 (c
> 'Z' && c
< 'a' && c
!= '_') ||
650 result
.WriteByte ((byte) '%');
651 int idx
= ((int) c
) >> 4;
652 result
.WriteByte ((byte) hexChars
[idx
]);
653 idx
= ((int) c
) & 0x0F;
654 result
.WriteByte ((byte) hexChars
[idx
]);
656 result
.WriteByte ((byte) c
);
660 return result
.ToArray ();
663 public static string UrlEncodeUnicode (string str
)
668 StringBuilder result
= new StringBuilder ();
669 int end
= str
.Length
;
670 for (int i
= 0; i
< end
; i
++) {
674 result
.Append ("%u");
675 idx
= ((int) c
) >> 24;
676 result
.Append (hexChars
[idx
]);
677 idx
= (((int) c
) >> 16) & 0x0F;
678 result
.Append (hexChars
[idx
]);
679 idx
= (((int) c
) >> 8) & 0x0F;
680 result
.Append (hexChars
[idx
]);
681 idx
= ((int) c
) & 0x0F;
682 result
.Append (hexChars
[idx
]);
686 if ((c
== ' ') || (c
< '0' && c
!= '-' && c
!= '.') ||
687 (c
< 'A' && c
> '9') ||
688 (c
> 'Z' && c
< 'a' && c
!= '_') ||
691 idx
= ((int) c
) >> 4;
692 result
.Append (hexChars
[idx
]);
693 idx
= ((int) c
) & 0x0F;
694 result
.Append (hexChars
[idx
]);
701 return result
.ToString ();
704 public static byte [] UrlEncodeUnicodeToBytes (string str
)
712 return Encoding
.ASCII
.GetBytes (UrlEncodeUnicode (str
));
716 /// Decodes an HTML-encoded string and returns the decoded string.
718 /// <param name="s">The HTML string to decode. </param>
719 /// <returns>The decoded text.</returns>
720 public static string HtmlDecode (string s
)
723 throw new ArgumentNullException ("s");
725 if (s
.IndexOf ('&') == -1)
728 bool insideEntity
= false; // used to indicate that we are in a potential entity
729 string entity
= String
.Empty
;
730 StringBuilder output
= new StringBuilder ();
733 for (int i
= 0; i
< len
; i
++) {
737 output
.Append (entity
);
748 int length
= entity
.Length
;
749 if (length
>= 2 && entity
[1] == '#' && entity
[2] != ';')
750 entity
= ((char) Int32
.Parse (entity
.Substring (2, entity
.Length
- 3))).ToString();
751 else if (length
> 1 && Entities
.ContainsKey (entity
.Substring (1, entity
.Length
- 2)))
752 entity
= Entities
[entity
.Substring (1, entity
.Length
- 2)].ToString ();
754 output
.Append (entity
);
755 entity
= String
.Empty
;
756 insideEntity
= false;
766 output
.Append (entity
);
767 return output
.ToString ();
771 /// Decodes an HTML-encoded string and sends the resulting output to a TextWriter output stream.
773 /// <param name="s">The HTML string to decode</param>
774 /// <param name="output">The TextWriter output stream containing the decoded string. </param>
775 public static void HtmlDecode(string s
, TextWriter output
)
778 output
.Write (HtmlDecode (s
));
782 /// HTML-encodes a string and returns the encoded string.
784 /// <param name="s">The text string to encode. </param>
785 /// <returns>The HTML-encoded text.</returns>
786 public static string HtmlEncode (string s
)
791 StringBuilder output
= new StringBuilder ();
793 foreach (char c
in s
)
796 output
.Append ("&");
799 output
.Append (">");
802 output
.Append ("<");
805 output
.Append (""");
809 output
.Append ("&#");
810 output
.Append (((int) c
).ToString ());
817 return output
.ToString ();
821 /// HTML-encodes a string and sends the resulting output to a TextWriter output stream.
823 /// <param name="s">The string to encode. </param>
824 /// <param name="output">The TextWriter output stream containing the encoded string. </param>
825 public static void HtmlEncode(string s
, TextWriter output
)
828 output
.Write (HtmlEncode (s
));
832 public static string UrlPathEncode (string s
)
837 int idx
= s
.IndexOf ("?");
840 s2
= s
.Substring (0, idx
-1);
841 s2
= UrlEncode (s2
) + s
.Substring (idx
);
849 #endregion // Methods