mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 10:46:33 +02:00
Add info bar if blocked elements present in Svg (#1620)
* Added logic to check for blocked elements * Added unit tests * Fix warnings from msi solution
This commit is contained in:
@@ -15,6 +15,7 @@ using System.Xml.Linq;
|
||||
using Common;
|
||||
using Common.Utilities;
|
||||
using PreviewHandlerCommon;
|
||||
using SvgPreviewHandler.Utilities;
|
||||
|
||||
namespace SvgPreviewHandler
|
||||
{
|
||||
@@ -58,6 +59,13 @@ namespace SvgPreviewHandler
|
||||
}
|
||||
}
|
||||
|
||||
// Add a info bar on top of the Preview if any blocked element is present.
|
||||
if (SvgPreviewHandlerHelper.CheckBlockedElements(svgData))
|
||||
{
|
||||
this.infoBarAdded = true;
|
||||
this.AddTextBoxControl(Resource.BlockedElementInfoText);
|
||||
}
|
||||
|
||||
this.AddBrowserControl(svgData);
|
||||
this.Resize += this.FormResized;
|
||||
base.DoPreview(dataSource);
|
||||
|
||||
@@ -107,6 +107,7 @@
|
||||
</Compile>
|
||||
<Compile Include="SvgPreviewHandler.cs" />
|
||||
<Compile Include="SvgTelemetry.cs" />
|
||||
<Compile Include="Utilities\SvgPreviewHandlerHelper.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\common\PreviewHandlerCommon.csproj">
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace SvgPreviewHandler.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper utilities for Svg Preview Handler.
|
||||
/// </summary>
|
||||
public class SvgPreviewHandlerHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Dictionary of elements in lower case that are blocked from Svg for preview pane.
|
||||
/// Reference for list of Svg Elements: https://developer.mozilla.org/en-US/docs/Web/SVG/Element.
|
||||
/// </summary>
|
||||
private static Dictionary<string, bool> blockedElementsName = new Dictionary<string, bool>
|
||||
{
|
||||
{ "script", true },
|
||||
{ "image", true },
|
||||
{ "feimage", true },
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Check if any of the blocked elements present in Svg.
|
||||
/// </summary>
|
||||
/// <param name="svgData">Input Svg.</param>
|
||||
/// <returns>Returns true in case any of the blocked element is present otherwise false.</returns>
|
||||
public static bool CheckBlockedElements(string svgData)
|
||||
{
|
||||
bool foundBlockedElement = false;
|
||||
if (string.IsNullOrWhiteSpace(svgData))
|
||||
{
|
||||
return foundBlockedElement;
|
||||
}
|
||||
|
||||
// Check if any of the blocked element is present. If failed to parse or iterate over Svg return default false.
|
||||
// No need to throw because all the external content and script are blocked on the Web Browser Control itself.
|
||||
try
|
||||
{
|
||||
var doc = XDocument.Parse(svgData);
|
||||
var elements = doc.Descendants().ToList();
|
||||
foreach (XElement element in elements)
|
||||
{
|
||||
var elementName = element?.Name?.LocalName?.ToLower();
|
||||
if (elementName != null && blockedElementsName.ContainsKey(elementName))
|
||||
{
|
||||
foundBlockedElement = true;
|
||||
|
||||
// No need to iterate further since we are displaying info bar with condition of atleast one occurrence of blocked element is present.
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
|
||||
return foundBlockedElement;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user