From 8ef14d0e16652de2a65f47cccdc59e0fc89da1a7 Mon Sep 17 00:00:00 2001 From: Jonathan Magnan Date: Tue, 1 Oct 2019 09:44:04 -0400 Subject: [PATCH] Update source Update source --- src/HtmlAgilityPack.Shared/HtmlEntity.cs | 58 +++++++++++++++++------- 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/src/HtmlAgilityPack.Shared/HtmlEntity.cs b/src/HtmlAgilityPack.Shared/HtmlEntity.cs index 331851a..d1f32bc 100644 --- a/src/HtmlAgilityPack.Shared/HtmlEntity.cs +++ b/src/HtmlAgilityPack.Shared/HtmlEntity.cs @@ -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 @@ -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 _entityName; private static Dictionary _entityValue; @@ -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(); } @@ -860,4 +884,4 @@ private enum ParseState #endregion } -} +} \ No newline at end of file