1212
1313using System ;
1414using System . Collections . Generic ;
15+ using System . Web ;
1516
1617namespace TheArtOfDev . HtmlRenderer . Core . Utils
1718{
@@ -319,14 +320,7 @@ public static string DecodeHtml(string str)
319320 {
320321 if ( ! string . IsNullOrEmpty ( str ) )
321322 {
322- str = DecodeHtmlCharByCode ( str ) ;
323-
324- str = DecodeHtmlCharByName ( str ) ;
325-
326- foreach ( var encPair in _encodeDecode )
327- {
328- str = str . Replace ( encPair . Key , encPair . Value ) ;
329- }
323+ str = HttpUtility . HtmlDecode ( str ) ;
330324 }
331325 return str ;
332326 }
@@ -348,67 +342,5 @@ public static string EncodeHtml(string str)
348342 }
349343 return str ;
350344 }
351-
352-
353- #region Private methods
354-
355- /// <summary>
356- /// Decode html special charecters encoded using char entity code (€)
357- /// </summary>
358- /// <param name="str">the string to decode</param>
359- /// <returns>decoded string</returns>
360- private static string DecodeHtmlCharByCode ( string str )
361- {
362- var idx = str . IndexOf ( "&#" , StringComparison . OrdinalIgnoreCase ) ;
363- while ( idx > - 1 )
364- {
365- bool hex = str . Length > idx + 3 && char . ToLower ( str [ idx + 2 ] ) == 'x' ;
366- var endIdx = idx + 2 + ( hex ? 1 : 0 ) ;
367-
368- long num = 0 ;
369- while ( endIdx < str . Length && CommonUtils . IsDigit ( str [ endIdx ] , hex ) )
370- num = num * ( hex ? 16 : 10 ) + CommonUtils . ToDigit ( str [ endIdx ++ ] , hex ) ;
371- endIdx += ( endIdx < str . Length && str [ endIdx ] == ';' ) ? 1 : 0 ;
372-
373- string repl = string . Empty ;
374- if ( num >= 0 && num <= 0x10ffff && ! ( num >= 0xd800 && num <= 0xdfff ) )
375- repl = Char . ConvertFromUtf32 ( ( int ) num ) ;
376-
377- str = str . Remove ( idx , endIdx - idx ) ;
378- str = str . Insert ( idx , repl ) ;
379-
380- idx = str . IndexOf ( "&#" , idx + 1 ) ;
381- }
382- return str ;
383- }
384-
385- /// <summary>
386- /// Decode html special charecters encoded using char entity name (&#euro;)
387- /// </summary>
388- /// <param name="str">the string to decode</param>
389- /// <returns>decoded string</returns>
390- private static string DecodeHtmlCharByName ( string str )
391- {
392- var idx = str . IndexOf ( '&' ) ;
393- while ( idx > - 1 )
394- {
395- var endIdx = str . IndexOf ( ';' , idx ) ;
396- if ( endIdx > - 1 && endIdx - idx < 8 )
397- {
398- var key = str . Substring ( idx + 1 , endIdx - idx - 1 ) ;
399- char c ;
400- if ( _decodeOnly . TryGetValue ( key , out c ) )
401- {
402- str = str . Remove ( idx , endIdx - idx + 1 ) ;
403- str = str . Insert ( idx , c . ToString ( ) ) ;
404- }
405- }
406-
407- idx = str . IndexOf ( '&' , idx + 1 ) ;
408- }
409- return str ;
410- }
411-
412- #endregion
413345 }
414346}
0 commit comments