Skip to content

Commit

Permalink
[X] revert #5611
Browse files Browse the repository at this point in the history
As reported in #17461, OnPlatform simplification cause some issues
- while used as a Resource (#17461, unable to unit test for now)
- doesn't type convert. On nodes shouldn't be replaced by the Value, but
  an element node (<On Platform="..." Value="Red" /> should generate
<Color x:Key="foo">Red</Color>. right now it only generated "Red"

- fixes #17461
  • Loading branch information
StephaneDelcroix committed Sep 29, 2023
1 parent ceb507c commit 3194294
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 54 deletions.
102 changes: 51 additions & 51 deletions src/Controls/src/Xaml/SimplifyOnPlatformVisitor.cs
Expand Up @@ -76,57 +76,57 @@ public void Visit(ElementNode node, INode parentNode)
}

//`<OnPlatform>` elements
if (node.XmlType.Name == "OnPlatform" && node.XmlType.NamespaceUri == XamlParser.MauiUri)
{
var onNode = GetOnNode(node, Target) ?? GetDefault(node);

//Property node
if (ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out XmlName name)
&& parentNode is IElementNode parentEnode)
{
if (onNode != null)
parentEnode.Properties[name] = onNode;
else
parentEnode.Properties.Remove(name);
return;
}

//Collection item
if (onNode != null && parentNode is IElementNode parentEnode2)
parentEnode2.CollectionItems[parentEnode2.CollectionItems.IndexOf(node)] = onNode;

}

INode GetOnNode(ElementNode onPlatform, string target)
{
foreach (var onNode in onPlatform.CollectionItems)
{
if ((onNode as ElementNode).Properties.TryGetValue(new XmlName("", "Platform"), out var platform))
{
var splits = ((platform as ValueNode).Value as string).Split(',');
foreach (var split in splits)
{
if (string.IsNullOrWhiteSpace(split))
continue;
if (split.Trim() == target)
{
if ((onNode as ElementNode).Properties.TryGetValue(new XmlName("", "Value"), out var node))
return node;

return (onNode as ElementNode).CollectionItems.FirstOrDefault();
}
}
}
}
return null;
}

INode GetDefault(ElementNode onPlatform)
{
if (node.Properties.TryGetValue(new XmlName("", "Default"), out INode defaultNode))
return defaultNode;
return null;
}
//if (node.XmlType.Name == "OnPlatform" && node.XmlType.NamespaceUri == XamlParser.MauiUri)
//{
// var onNode = GetOnNode(node, Target) ?? GetDefault(node);

// //Property node
// if (ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out XmlName name)
// && parentNode is IElementNode parentEnode)
// {
// if (onNode != null)
// parentEnode.Properties[name] = onNode;
// else
// parentEnode.Properties.Remove(name);
// return;
// }

// //Collection item
// if (onNode != null && parentNode is IElementNode parentEnode2)
// parentEnode2.CollectionItems[parentEnode2.CollectionItems.IndexOf(node)] = onNode;

//}

//INode GetOnNode(ElementNode onPlatform, string target)
//{
// foreach (var onNode in onPlatform.CollectionItems)
// {
// if ((onNode as ElementNode).Properties.TryGetValue(new XmlName("", "Platform"), out var platform))
// {
// var splits = ((platform as ValueNode).Value as string).Split(',');
// foreach (var split in splits)
// {
// if (string.IsNullOrWhiteSpace(split))
// continue;
// if (split.Trim() == target)
// {
// if ((onNode as ElementNode).Properties.TryGetValue(new XmlName("", "Value"), out var node))
// return node;

// return (onNode as ElementNode).CollectionItems.FirstOrDefault();
// }
// }
// }
// }
// return null;
//}

//INode GetDefault(ElementNode onPlatform)
//{
// if (node.Properties.TryGetValue(new XmlName("", "Default"), out INode defaultNode))
// return defaultNode;
// return null;
//}

}

Expand Down
4 changes: 2 additions & 2 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui17461.xaml
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui17461">
<ContentPage.Resources>
<Color x:Key="LightSmoke">#4FFFFFFF</Color>
<!--<Color x:Key="LightSmoke">#4FFFFFFF</Color>-->
<OnPlatform x:Key="EntryBackgroundColor" x:TypeArguments="Color">
<On Platform="iOS" Value="{StaticResource LightSmoke}" />
<On Platform="Android" Value="Transparent" />
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/tests/Xaml.UnitTests/Issues/Maui17461.xaml.cs
Expand Up @@ -32,7 +32,7 @@ class Test
[Test]
public void MissingKeyException([Values("net7.0-ios", "net7.0-android", "net7.0-macos")] string targetFramework)
{
MockCompiler.Compile(typeof(Maui17461), targetFramework: targetFramework);
MockCompiler.Compile(typeof(Maui17461), out var methodDef, targetFramework: targetFramework);
}
}
}

0 comments on commit 3194294

Please sign in to comment.