Skip to content

Commit

Permalink
Update source
Browse files Browse the repository at this point in the history
Update source
  • Loading branch information
JonathanMagnan committed Oct 1, 2019
1 parent 227b51f commit 8ef14d0
Showing 1 changed file with 41 additions and 17 deletions.
58 changes: 41 additions & 17 deletions src/HtmlAgilityPack.Shared/HtmlEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
// Forum & Issues: https://github.com/zzzprojects/html-agility-pack
// License: https://github.com/zzzprojects/html-agility-pack/blob/master/LICENSE
// More projects: http://www.zzzprojects.com/
// Copyright © ZZZ Projects Inc. 2014 - 2017. All rights reserved.
// Copyright © ZZZ Projects Inc. 2014 - 2017. All rights reserved.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace HtmlAgilityPack
Expand All @@ -22,6 +23,10 @@ public class HtmlEntity
{
#region Static Members

#if !FX20 && !FX35
public static bool UseWebUtility { get; set; }
#endif

private static readonly int _maxEntitySize;
private static Dictionary<int, string> _entityName;
private static Dictionary<string, int> _entityValue;
Expand Down Expand Up @@ -782,30 +787,49 @@ public static string Entitize(string text, bool useNames, bool entitizeQuotAmpAn
return text;

StringBuilder sb = new StringBuilder(text.Length);
for (int i = 0; i < text.Length; i++)

#if !FX20 && !FX35
if (UseWebUtility)
{
int code = text[i];
if ((code > 127) ||
(entitizeQuotAmpAndLtGt && ((code == 34) || (code == 38) || (code == 60) || (code == 62))))
TextElementEnumerator enumerator = StringInfo.GetTextElementEnumerator(text);
while (enumerator.MoveNext())
{
string entity = null;
if (useNames)
EntityName.TryGetValue(code, out entity);

if (entity == null))
sb.Append(System.Net.WebUtility.HtmlEncode(enumerator.GetTextElement()));
}
}
else
{
#endif
for (int i = 0; i < text.Length; i++)
{
int code = text[i];
if ((code > 127) ||
(entitizeQuotAmpAndLtGt && ((code == 34) || (code == 38) || (code == 60) || (code == 62))))
{
sb.Append("&#" + code + ";");
string entity = null;

if (useNames)
{
EntityName.TryGetValue(code, out entity);
}

if (entity == null)
{
sb.Append("&#" + code + ";");
}
else
{
sb.Append("&" + entity + ";");
}
}
else
{
sb.Append("&" + entity + ";");
sb.Append(text[i]);
}
}
else
{
sb.Append(text[i]);
}
#if !FX20 && !FX35
}
#endif

return sb.ToString();
}
Expand Down Expand Up @@ -860,4 +884,4 @@ private enum ParseState

#endregion
}
}
}

0 comments on commit 8ef14d0

Please sign in to comment.