Skip to content

Commit

Permalink
fix: add a workaround for net461's XmlAttributeCollection.InsertAfter
Browse files Browse the repository at this point in the history
  • Loading branch information
ramezgerges committed Jul 21, 2023
1 parent 2d197da commit 20e0cf9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
18 changes: 16 additions & 2 deletions src/Uno.XamlMerge.Task/BatchMergeXaml.cs
Expand Up @@ -254,8 +254,22 @@ private static bool TryMergeHashUsings(string existingNamespaceString, string ne
var ownerElement = propertyAttributeToUpdate.Key.OwnerElement;
var newAttribute = ownerElement.OwnerDocument.CreateAttribute(propertyAttributeToUpdate.Key.Prefix, propertyAttributeToUpdate.Key.LocalName, merged);
newAttribute.Value = propertyAttributeToUpdate.Key.Value;
ownerElement.Attributes.InsertAfter(newNode: newAttribute, refNode: propertyAttributeToUpdate.Key);
ownerElement.RemoveAttributeNode(propertyAttributeToUpdate.Key);

var refNode = propertyAttributeToUpdate.Key;

if (newAttribute.Name == refNode.Name || newAttribute.NamespaceURI == refNode.NamespaceURI)
{
// This is a workaround for a bug in net461's InsertAfter that crashes if refNode has the same name as newAttribute
// If refNode and newAttribute are duplicates, it doesn't matter whether we insert before or after,
// since refNode will be removed before the insertion anyway.
ownerElement.Attributes.InsertBefore(newNode: newAttribute, refNode);
}
else
{
ownerElement.Attributes.InsertAfter(newNode: newAttribute, refNode);
}

ownerElement.RemoveAttributeNode(refNode);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/Uno.XamlMerge.Tests/Given_BatchMergeXaml.cs
Expand Up @@ -42,7 +42,9 @@ public void When_Different_Properties_Different_Namespaces()
{
var task = CreateMerger();

task.Execute(); // Shouldn't throw an exception
task.Execute();

ValidateOutput(task);
}

[TestMethod]
Expand Down
@@ -0,0 +1,10 @@
<ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="skia" xmlns:skia="http://uno.ui/skia" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<!--origin: When_Different_Properties_Different_Namespaces\Input_Dictionary.xml-->
<Style TargetType="ScrollViewer">
<Setter Property="Template">
<Setter.Value>
<ScrollBar Height="16" skia:Width="16" />
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
2 changes: 1 addition & 1 deletion src/Uno.XamlMerge.Tests/Uno.XamlMerge.Tests.csproj
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net6.0;net461</TargetFrameworks>
<LangVersion>11</LangVersion>
<LangVersion>10</LangVersion>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
Expand Down

0 comments on commit 20e0cf9

Please sign in to comment.