Skip to content

Commit

Permalink
Merge pull request #13 from askonomm/12-html-entity-usage-results-in-…
Browse files Browse the repository at this point in the history
…an-exception

Convert HTML entities to Unicode to avoid compatibility issues with X…
  • Loading branch information
askonomm authored Oct 21, 2024
2 parents a36107b + 60da58b commit 350d81e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
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());
}
}

0 comments on commit 350d81e

Please sign in to comment.