Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/Aspire.Dashboard/Model/ResourceGraph/ResourceGraphMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,21 @@ private static string ResolvedEndpointText(DisplayedUrl? endpoint)

public static string GetIconPathData(Icon icon)
{
var p = icon.Content;
var e = XElement.Parse(p);
return e.Attribute("d")!.Value;
// Fluent UI icon content is an SVG fragment. Most icons contain one path:
// <path d="M..." />
// Some icons, such as DocumentMultiple, contain sibling paths:
// <path d="M..." /><path d="M..." />
// Wrap the fragment so XML parsing accepts both shapes, then combine the path data into one compound SVG path.
var iconContent = XElement.Parse($"<svg>{icon.Content}</svg>");
var pathData = iconContent.Elements()
.Select(e => e.Attribute("d")?.Value ?? throw new InvalidOperationException($"Icon '{icon.Name}' contains an element without path data."))
.ToArray();

if (pathData.Length == 0)
{
throw new InvalidOperationException($"Icon '{icon.Name}' doesn't contain path data.");
}

return string.Join(' ', pathData);
}
}
26 changes: 26 additions & 0 deletions tests/Aspire.Dashboard.Tests/Model/ResourceGraphMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;
using System.Xml.Linq;
using Aspire.Dashboard.Model;
using Aspire.Dashboard.Model.ResourceGraph;
using Aspire.Dashboard.Resources;
using Aspire.Tests.Shared.DashboardModel;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;
using Icons = Microsoft.FluentUI.AspNetCore.Components.Icons;

namespace Aspire.Dashboard.Tests.Model;

Expand Down Expand Up @@ -156,4 +158,28 @@ public void MapResource_ReferenceToResourceExcludedFromGraph_Ignored()

Assert.Empty(dto.ReferencedNames);
}

[Fact]
public void GetIconPathData_SinglePath_ReturnsPathData()
{
var icon = new Icons.Filled.Size24.Box();
var expectedPathData = XElement.Parse(icon.Content).Attribute("d")!.Value;

var pathData = ResourceGraphMapper.GetIconPathData(icon);

Assert.Equal(expectedPathData, pathData);
}

[Fact]
public void GetIconPathData_MultiplePaths_ReturnsCombinedPathData()
{
var icon = new Icons.Filled.Size24.DocumentMultiple();
var iconContent = XElement.Parse($"<svg>{icon.Content}</svg>");
var expectedPaths = iconContent.Elements().Select(e => e.Attribute("d")!.Value).ToArray();
Assert.Equal(3, expectedPaths.Length);

var pathData = ResourceGraphMapper.GetIconPathData(icon);

Assert.Equal(string.Join(' ', expectedPaths), pathData);
}
}
Loading