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

Add support for XML documents. #23

Merged
merged 1 commit into from
Nov 3, 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
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