Skip to content

Commit

Permalink
Fix issue with multiple root nodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
askonomm committed Nov 7, 2024
1 parent 92c22ba commit 0414d71
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Htmt/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,18 @@ public string ToHtml()
public XmlNode ToXml()
{
Parse();

var fragment = Xml.CreateDocumentFragment();
var childNodes = Xml.DocumentElement?.ChildNodes;

if (childNodes == null) return fragment;

foreach (XmlNode node in childNodes)
{
fragment.AppendChild(Xml.ImportNode(node, true));
}

return Xml.DocumentElement?.FirstChild ?? Xml.CreateElement("root");
return fragment;
}

/// <summary>
Expand Down
11 changes: 11 additions & 0 deletions HtmtTests/ParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,15 @@ public void TestFillVoidAttributes2()

Assert.AreEqual("<html><head><script defer=\"\" src=\"\"></script></head><body></body></html>", parser.ToHtml());
}

[TestMethod]
public void TestMultipleRootNodesInPartials()
{
const string template = "<html><head></head><body><div x:outer-partial=\"partial\"></div></body></html>";
const string partial = "<div>hello</div><span x:inner-text=\"{item}\"></span>";
var data = new Dictionary<string, object?> { { "partial", partial }, { "item", "world" } };
var parser = new Htmt.Parser { Template = template, Data = data };

Assert.AreEqual("<html><head></head><body><div>hello</div><span>world</span></body></html>", parser.ToHtml());
}
}

0 comments on commit 0414d71

Please sign in to comment.