Skip to content

Commit

Permalink
Merge pull request #15 from askonomm/14-html-entities-fail-in-partials
Browse files Browse the repository at this point in the history
Fix html entities failing in partials.
  • Loading branch information
askonomm authored Oct 22, 2024
2 parents 350d81e + abf9da0 commit 2b3f6c9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Htmt/Htmt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
<Nullable>enable</Nullable>
<Authors>Asko Nõmm</Authors>
<PackageId>Htmt</PackageId>
<Version>1.2.0</Version>
<Version>1.2.1</Version>
<Description>HTML/XML templating library for .NET</Description>
<PackageTags>html xml template</PackageTags>
<PackageProjectUrl>https://github.com/askonomm/htmt</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/askonomm/htmt</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>Added support for partials, complex conditional expressions and auto-closing of void elements in HTML.</PackageReleaseNotes>
<PackageReleaseNotes>Fixed an issue where usage of HTML entities caused Htmt to throw an exception.</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

Expand Down
3 changes: 2 additions & 1 deletion Htmt/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ private void Parse()

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

TransformHtmlEntities();

var templateStr = $"<root xmlns:x=\"{HtmtNamespace}\">{Template}</root>";
using var reader = XmlReader.Create(new StringReader(templateStr), _xmlSettings);
Xml.Load(reader);
Expand Down
24 changes: 24 additions & 0 deletions HtmtTests/AttributeParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,18 @@ public void TestInnerPartialAttributeParserWithData()

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

[TestMethod]
public void TestInnerPartialAttributeParserWithHtmlEntities()
{
const string template = "<html><body><div x:inner-partial=\"partial\" /></body></html>";
const string partial = "<h1>This way &rarr;</h1>";
var data = new Dictionary<string, object?> { { "partial", partial } };
var parser = new Parser { Template = template, Data = data };
var html = parser.ToHtml();

Assert.AreEqual("<html><body><div><h1>This way →</h1></div></body></html>", html);
}

[TestMethod]
public void TestOuterPartialAttributeParser()
Expand All @@ -339,4 +351,16 @@ public void TestOuterPartialAttributeParserWithData()

Assert.AreEqual("<html><body><h1>Hello, World!</h1></body></html>", html);
}

[TestMethod]
public void TestOuterPartialAttributeParserWithHtmlEntities()
{
const string template = "<html><body><div x:outer-partial=\"partial\" /></body></html>";
const string partial = "<h1>This way &rarr;</h1>";
var data = new Dictionary<string, object?> { { "partial", partial } };
var parser = new Parser { Template = template, Data = data };
var html = parser.ToHtml();

Assert.AreEqual("<html><body><h1>This way →</h1></body></html>", html);
}
}
9 changes: 9 additions & 0 deletions HtmtTests/ParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,13 @@ public void TestHtmlEntities()

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

[TestMethod]
public void TestMoreHtmlEntities()
{
const string template = "<html><head></head><body>&rarr;</body></html>";
var parser = new Htmt.Parser { Template = template };

Assert.AreEqual("<html><head></head><body>→</body></html>", parser.ToHtml());
}
}

0 comments on commit 2b3f6c9

Please sign in to comment.