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 Oct 17, 2023
1 parent f60cf02 commit 7fb67e0
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 51 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
13 changes: 13 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui17461.xaml
@@ -0,0 +1,13 @@
<?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>-->
<OnPlatform x:Key="EntryBackgroundColor" x:TypeArguments="Color">
<On Platform="iOS" Value="{StaticResource LightSmoke}" />
<On Platform="Android" Value="Transparent" />
</OnPlatform>
</ContentPage.Resources>
</ContentPage>
38 changes: 38 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui17461.xaml.cs
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.Controls.Shapes;
using Microsoft.Maui.Devices;
using Microsoft.Maui.Graphics;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.Xaml.UnitTests;

public partial class Maui17461 : ContentPage
{

public Maui17461() => InitializeComponent();

public Maui17461(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}

[TestFixture]
class Test
{
[SetUp] public void Setup() => AppInfo.SetCurrent(new MockAppInfo());


[TearDown] public void TearDown() => AppInfo.SetCurrent(null);

[Test]
public void MissingKeyException([Values("net7.0-ios", "net7.0-android", "net7.0-macos")] string targetFramework)
{
MockCompiler.Compile(typeof(Maui17461), out var methodDef, targetFramework: targetFramework);
}
}
}

0 comments on commit 7fb67e0

Please sign in to comment.