diff --git a/Htmt/Htmt.csproj b/Htmt/Htmt.csproj index 7a40db6..7a3aa2f 100644 --- a/Htmt/Htmt.csproj +++ b/Htmt/Htmt.csproj @@ -6,14 +6,14 @@ enable Asko Nõmm Htmt - 2.1.2 + 2.1.3 Templating library with native AOT and trimming support. html xml template templating engine compiler parser aot trimming https://github.com/askonomm/htmt MIT https://github.com/askonomm/htmt git - Cleaning X namespace from final markup. + Cleaning Xmlns attribute from final markup children. README.md diff --git a/Htmt/Parser.cs b/Htmt/Parser.cs index 0a9a986..5ece7cb 100644 --- a/Htmt/Parser.cs +++ b/Htmt/Parser.cs @@ -83,6 +83,7 @@ private void Parse() RunAttributeParsers(); RemoveIdentifierFromNodes(); RemoveXNamespace(); + RemoveXmlnsFromChildren(); } /// @@ -345,4 +346,30 @@ private void RemoveXNamespace() } } } + + /// + /// Removes the xmlns attribute from all children. + /// + private void RemoveXmlnsFromChildren() + { + if (Xml.DocumentElement == null) return; + + var nodesToProcess = new Queue(Xml.DocumentElement.ChildNodes.Cast()); + + while (nodesToProcess.Count > 0) + { + var node = nodesToProcess.Dequeue(); + if (node is not XmlElement element) continue; + + foreach (XmlNode child in element.ChildNodes) + { + if (child is XmlElement childElement) + { + childElement.RemoveAttribute("xmlns"); + } + + nodesToProcess.Enqueue(child); + } + } + } } diff --git a/HtmtTests/ParserTest.cs b/HtmtTests/ParserTest.cs index 873daf7..1caf6bf 100644 --- a/HtmtTests/ParserTest.cs +++ b/HtmtTests/ParserTest.cs @@ -96,4 +96,14 @@ public void TestMoreHtmlEntities() Assert.AreEqual("→", parser.ToHtml()); } + + [TestMethod] + public void TestRemoveXmlnsFromChildren() + { + const string template = "
"; + var data = new Dictionary { { "items", new[] { "One", "Two", "Three" } } }; + var parser = new Htmt.Parser { Template = template, Data = data }; + + Assert.AreEqual("
One
Two
Three
", parser.ToHtml()); + } } \ No newline at end of file