Skip to content

Commit

Permalink
fix: Properly merge namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Feb 9, 2023
1 parent d435941 commit 597372e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/Uno.XamlMerge.Task/BatchMergeXaml.cs
Expand Up @@ -123,19 +123,21 @@ class BatchMerger
{
try
{
mergedDictionary.MergeContent(
mergedDictionary.Prepare(
content: File.ReadAllText(page),
filePath: Path.GetFullPath(page)
.Replace(projectBasePath, "")
.TrimStart(Path.DirectorySeparatorChar));
}
catch (Exception)
{
owner.LogError($"Exception found when merging page {page}!");
owner.LogError($"Exception found when merging namespaces for page {page}!");
throw;
}
}

mergedDictionary.MergeContent();

mergedDictionary.FinalizeXaml();

Directory.CreateDirectory(Path.GetDirectoryName(mergedXamlFile));
Expand Down
60 changes: 52 additions & 8 deletions src/Uno.XamlMerge.Task/MergedDictionary.cs
Expand Up @@ -36,11 +36,8 @@ public override string ToString()
return Utils.UnEscapeAmpersand(sw.ToString());
}

public void MergeContent(String content, string filePath)
public void Prepare(String content, string filePath)
{
var comment = owningDocument.CreateComment($"origin: {filePath}");
nodeList.Add(comment);

content = Utils.EscapeAmpersand(content);

var document = new XmlDocument();
Expand All @@ -56,13 +53,59 @@ public void MergeContent(String content, string filePath)
{
AddNamespace(entry.Key, entry.Value);
}
foreach (XmlNode node in document.ChildNodes)

documents.Add((document, xmlnsReplacementDictionary, filePath));
}

public void MergeContent()
{
foreach (var (document, xmlnsReplacementDictionary, filePath) in documents)
{
if (node.Name == "ResourceDictionary")
var comment = owningDocument.CreateComment($"origin: {filePath}");
nodeList.Add(comment);

foreach (XmlNode node in document.ChildNodes)
{
foreach (XmlNode xmlNode in node.ChildNodes)
if (node.Name == "ResourceDictionary")
{
AddNode(xmlNode, xmlnsReplacementDictionary);
foreach (XmlNode xmlNode in node.ChildNodes)
{
if (xmlNode is XmlElement xmlElement)
{
var xmlNodeCloned = (XmlElement)xmlNode.CloneNode(true);
xmlNodeCloned.RemoveAllAttributes();

foreach (XmlAttribute xmlAttribute in xmlElement.Attributes)
{
if (xmlAttribute.Name.Contains(":"))
{
var split = xmlAttribute.Name.Split(':');
var prefix = split[0];
var nsName = split[1];
if (knownNamespaces.TryGetValue(prefix, out var nsUri))
{
xmlNodeCloned.SetAttribute(nsName, knownNamespaces[prefix], xmlAttribute.Value);
}
else
{
xmlNodeCloned.SetAttribute(xmlAttribute.Name, xmlAttribute.Value);
}
}
else
{
xmlNodeCloned.SetAttribute(xmlAttribute.Name, xmlAttribute.Value);
}

}

AddNode(xmlNodeCloned, xmlnsReplacementDictionary);
}
else
{
AddNode(xmlNode, xmlnsReplacementDictionary);
}

}
}
}
}
Expand Down Expand Up @@ -525,6 +568,7 @@ private static XmlNode FindNode(XmlDocument document, string name)
private XmlElement xmlElement;
private XmlDocument owningDocument;
private List<XmlNode> nodeList;
private List<(XmlDocument, Dictionary<string, string>, string)> documents = new();

// We'll want to remove nodes from the node list if a child dictionary has the same node,
// but if we just removed the nodes then the values in nodeKeyToNodeListIndexDictionary would be wrong.
Expand Down
@@ -1,8 +1,8 @@
<ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wasm="http://uno.ui/wasm#using:NS1;NS2" xmlns:android="http://uno.ui/android#using:AndroidNS" xmlns:ios="http://uno.ui/ios#using:iosNS" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<!--origin: When_Different_Namespace_Should_Be_Merged\Input_Dictionary_1.xml-->
<MyElement1 android:MyProp="P1" />
<MyElement2 ios:MyProp="P2" xmlns:ios="http://uno.ui/ios" />
<MyElement2 ios:MyProp="P2" />
<!--origin: When_Different_Namespace_Should_Be_Merged\Input_Dictionary_2.xml-->
<MyElement3 android:MyProp="P3" xmlns:android="http://uno.ui/android" />
<MyElement3 android:MyProp="P3" />
<MyElement4 ios:MyProp="P4" />
</ResourceDictionary>

0 comments on commit 597372e

Please sign in to comment.