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

Convert HTML entities to Unicode to avoid compatibility issues with X… #13

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Htmt/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private void Parse()

RemoveDoctype();
CloseVoidElements();
TransformHtmlEntities();
}

var templateStr = $"<root xmlns:x=\"{HtmtNamespace}\">{Template}</root>";
Expand Down Expand Up @@ -137,6 +138,28 @@ private void CloseVoidElements()
Template = Template.Replace(element, newElement);
}
}

[GeneratedRegex(@"&(?<entity>\w+);")]
private static partial Regex HtmlEntityRegex();

/**
* Transforms HTML entities to their respective characters.
*
* This is necessary because the XML parser does not handle HTML entities.
*/
private void TransformHtmlEntities()
{
var entityRegex = HtmlEntityRegex();
var matches = entityRegex.Matches(Template);

foreach (Match match in matches)
{
var entity = match.Groups["entity"].Value;
var replacement = System.Net.WebUtility.HtmlDecode($"&{entity};");

Template = Template.Replace(match.Value, replacement);
}
}

/**
* Parses the template and returns it as HTML.
Expand Down
9 changes: 9 additions & 0 deletions HtmtTests/ParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,13 @@ public void TestVoidElButOpen()

Assert.AreEqual("<html><head></head><body><img src=\"asd\" /><br /><hr /></body></html>", parser.ToHtml());
}

[TestMethod]
public void TestHtmlEntities()
{
const string template = "<html><head></head><body>&lt;div&gt;Hello, World!&lt;/div&gt;</body></html>";
var parser = new Htmt.Parser { Template = template };

Assert.AreEqual("<html><head></head><body><div>Hello, World!</div></body></html>", parser.ToHtml());
}
}