Skip to content

Commit 67d4925

Browse files
authored
Merge pull request #14 from alfattack/html-decoding
Use HttpUtility.HtmlDecode for decoding html
2 parents d936de6 + c02a7bb commit 67d4925

File tree

1 file changed

+2
-70
lines changed

1 file changed

+2
-70
lines changed

Source/HtmlRenderer/Core/Utils/HtmlUtils.cs

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
using System;
1414
using System.Collections.Generic;
15+
using System.Web;
1516

1617
namespace 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 (&#8364;)
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

Comments
 (0)