Skip to content

Commit

Permalink
Merge pull request #23 from askonomm/22-add-support-for-xml-doc-decla…
Browse files Browse the repository at this point in the history
…rations

Add support for XML documents.
  • Loading branch information
askonomm authored Nov 3, 2024
2 parents f3d8974 + e323edd commit 95c590a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
41 changes: 6 additions & 35 deletions Htmt/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ public partial class Parser
/// </summary>
private XmlNamespaceManager _nsManager = null!;

/// <summary>
/// True if the template is an HTML document.
/// </summary>
private bool _isHtml;

/// <summary>
/// The doctype of the document.
/// </summary>
Expand Down Expand Up @@ -74,16 +69,10 @@ private void Parse()
{
_nsManager = new XmlNamespaceManager(Xml.NameTable);
_nsManager.AddNamespace("x", HtmtNamespace);
_docType = GetDoctype(Template);

if (IsHtml(Template))
{
_isHtml = true;
_docType = GetDoctype(Template);

RemoveDoctype();
CloseVoidElements();
}

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

var templateStr = $"<root xmlns:x=\"{HtmtNamespace}\">{Template}</root>";
Expand Down Expand Up @@ -133,25 +122,12 @@ public static IExpressionModifier[] DefaultExpressionModifiers()
new CountExpressionModifier(),
];
}

/// <summary>
/// Tests if the template is an HTML document.
/// </summary>
/// <param name="template">The template to test.</param>
/// <returns>Returns true if the template is an HTML document.</returns>
private static bool IsHtml(string template)
{
var doctype = template.Trim().StartsWith("<!DOCTYPE", StringComparison.OrdinalIgnoreCase);
var htmlTag = template.Trim().StartsWith("<html", StringComparison.OrdinalIgnoreCase);

return doctype || htmlTag;
}

/// <summary>
/// The regex for the doctype.
/// The regex for the doc declaration (HTML or XML).
/// </summary>
/// <returns></returns>
[GeneratedRegex(@"<!DOCTYPE[^>]*>", RegexOptions.IgnoreCase, "en-US")]
[GeneratedRegex(@"(<!DOCTYPE[^>]*>)|(<\?xml[^>]*\?>)", RegexOptions.IgnoreCase, "en-US")]
private static partial Regex DocTypeRegex();

/// <summary>
Expand Down Expand Up @@ -248,12 +224,7 @@ public string ToHtml()

if (Xml.DocumentElement == null) return string.Empty;

if (_isHtml)
{
return $"{_docType}{Xml.DocumentElement.FirstChild?.OuterXml}";
}

return Xml.DocumentElement.FirstChild?.OuterXml ?? string.Empty;
return $"{_docType}{Xml.DocumentElement.InnerXml}";
}

/// <summary>
Expand Down
23 changes: 23 additions & 0 deletions HtmtTests/ExpressionModifierTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,27 @@ public void TestCountModifierWithEnumerable()

Assert.AreEqual("<html><body><p>5</p></body></html>", html);
}

[TestMethod]
public void TestCountModifierWithListOfPosts()
{
const string template = "<html><body><p x:inner-text=\"{posts | count}\"></p></body></html>";

var data = new Dictionary<string, object?>
{
{
"posts", new List<Dictionary<string, object?>>
{
new() { { "title", "Post 1" }, { "content", "Content 1" } },
new() { { "title", "Post 2" }, { "content", "Content 2" } },
new() { { "title", "Post 3" }, { "content", "Content 3" } },
}
}
};

var parser = new Parser { Template = template, Data = data };
var html = parser.ToHtml();

Assert.AreEqual("<html><body><p>3</p></body></html>", html);
}
}
14 changes: 12 additions & 2 deletions HtmtTests/ParserTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Xml;

namespace HtmtTests;

[TestClass]
Expand Down Expand Up @@ -50,6 +48,18 @@ public void TestHtml4Document()
html);
}

[TestMethod]
public void TestXmlDocument()
{
const string template = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><title x:inner-text=\"{title}\"></title><heading x:inner-text=\"{heading}\"></heading>";
var data = new Dictionary<string, object?>
{ { "title", "Hello, World!" }, { "heading", "Welcome to the world!" } };
var parser = new Htmt.Parser { Template = template, Data = data };
var html = parser.ToHtml();

Assert.AreEqual("<?xml version=\"1.0\" encoding=\"UTF-8\"?><title>Hello, World!</title><heading>Welcome to the world!</heading>", html);
}

[TestMethod]
public void TestVoidElButClosed()
{
Expand Down

0 comments on commit 95c590a

Please sign in to comment.