Skip to content

Commit

Permalink
Clean Xmlns attribute from final markup children.
Browse files Browse the repository at this point in the history
  • Loading branch information
askonomm committed Nov 3, 2024
1 parent 0d80c83 commit 357a8c2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 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>2.1.2</Version>
<Version>2.1.3</Version>
<Description>Templating library with native AOT and trimming support.</Description>
<PackageTags>html xml template templating engine compiler parser aot trimming</PackageTags>
<PackageProjectUrl>https://github.com/askonomm/htmt</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/askonomm/htmt</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>Cleaning X namespace from final markup.</PackageReleaseNotes>
<PackageReleaseNotes>Cleaning Xmlns attribute from final markup children.</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

Expand Down
27 changes: 27 additions & 0 deletions Htmt/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ private void Parse()
RunAttributeParsers();
RemoveIdentifierFromNodes();
RemoveXNamespace();
RemoveXmlnsFromChildren();
}

/// <summary>
Expand Down Expand Up @@ -345,4 +346,30 @@ private void RemoveXNamespace()
}
}
}

/// <summary>
/// Removes the xmlns attribute from all children.
/// </summary>
private void RemoveXmlnsFromChildren()
{
if (Xml.DocumentElement == null) return;

var nodesToProcess = new Queue<XmlNode>(Xml.DocumentElement.ChildNodes.Cast<XmlNode>());

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);
}
}
}
}
10 changes: 10 additions & 0 deletions HtmtTests/ParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,14 @@ public void TestMoreHtmlEntities()

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

[TestMethod]
public void TestRemoveXmlnsFromChildren()
{
const string template = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head></head><body><div x:for=\"items\" x:as=\"item\"><span x:inner-text=\"{item}\"></span></div></body></html>";
var data = new Dictionary<string, object?> { { "items", new[] { "One", "Two", "Three" } } };
var parser = new Htmt.Parser { Template = template, Data = data };

Assert.AreEqual("<html xmlns=\"http://www.w3.org/1999/xhtml\"><head></head><body><div><span>One</span></div><div><span>Two</span></div><div><span>Three</span></div></body></html>", parser.ToHtml());
}
}

0 comments on commit 357a8c2

Please sign in to comment.