Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] refactor Entitize method, use HtmlEncode, add unit tests #325

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 4 additions & 29 deletions src/HtmlAgilityPack.Shared/HtmlEntity.cs
Expand Up @@ -8,6 +8,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace HtmlAgilityPack
Expand Down Expand Up @@ -766,14 +767,6 @@ public static string Entitize(string text, bool useNames)
/// <param name="entitizeQuotAmpAndLtGt">If set to true, the [quote], [ampersand], [lower than] and [greather than] characters will be entitized.</param>
/// <returns>The result text</returns>
public static string Entitize(string text, bool useNames, bool entitizeQuotAmpAndLtGt)
// _entityValue.Add("quot", 34); // quotation mark = APL quote, U+0022 ISOnum
// _entityName.Add(34, "quot");
// _entityValue.Add("amp", 38); // ampersand, U+0026 ISOnum
// _entityName.Add(38, "amp");
// _entityValue.Add("lt", 60); // less-than sign, U+003C ISOnum
// _entityName.Add(60, "lt");
// _entityValue.Add("gt", 62); // greater-than sign, U+003E ISOnum
// _entityName.Add(62, "gt");
{
if (text == null)
return null;
Expand All @@ -782,28 +775,10 @@ 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++)
TextElementEnumerator enumerator = StringInfo.GetTextElementEnumerator(text);
while (enumerator.MoveNext())
{
int code = text[i];
if ((code > 127) ||
(entitizeQuotAmpAndLtGt && ((code == 34) || (code == 38) || (code == 60) || (code == 62))))
{
string entity;
EntityName.TryGetValue(code, out entity);

if ((entity == null) || (!useNames))
{
sb.Append("&#" + code + ";");
}
else
{
sb.Append("&" + entity + ";");
}
}
else
{
sb.Append(text[i]);
}
sb.Append(System.Net.WebUtility.HtmlEncode(enumerator.GetTextElement()));
}

return sb.ToString();
Expand Down
Expand Up @@ -56,6 +56,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="HtmlDocumentTests.cs" />
<Compile Include="HtmlEntityTests.cs" />
<Compile Include="HtmlNode.Tests.cs" />
<Compile Include="UnitTest1.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
23 changes: 23 additions & 0 deletions src/Tests/HtmlAgilityPack.Tests.Net45/HtmlEntityTests.cs
@@ -0,0 +1,23 @@
using NUnit.Framework;

namespace HtmlAgilityPack.Tests.fx._4._5
{
public class HtmlEntityTests
{
[Test]
public void Entitize_PassEmojiUnicode_ShouldCorrectlyEntitize()
{
string result = HtmlEntity.Entitize("😂");

Assert.AreEqual("&#128514;", result);
}

[Test]
public void Entitize_PassSimpleText_ShouldCorrectlyEntitize()
{
string result = HtmlEntity.Entitize("qwerty");

Assert.AreEqual("qwerty", result);
}
}
}