[SVGPreview]Handle comments properly (#28863)

* [SVGPreview] Handle comments properly

* f: spelling

* f: add tolerance to the bitmap eq test

* f: remove bitmap eq testing, since it doesn't work on CI for some reason

* f: parsing issue
This commit is contained in:
Andrey Nekrasov
2023-10-02 22:42:31 +02:00
committed by GitHub
parent 11f30f9d33
commit e14ff34b37
5 changed files with 47 additions and 12 deletions

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Generator: Adobe Illustrator 27.5.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!-- Generator: Adobe Illustrator 27.5.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg viewBox="0 0 100 100"
xmlns="http://www.w3.org/2000/svg">
<circle cx="50"
cy="50"
fill="rgba(0, 0, 255, 1)"
r="50">
<script>alert("valid-message")</script>
</circle>
</svg>
<!-- Generator: Adobe Illustrator 27.5.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->

After

Width:  |  Height:  |  Size: 552 B

View File

@@ -4,14 +4,12 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using Common.ComInterlop;
using Microsoft.PowerToys.STATestExtension;
using Microsoft.PowerToys.ThumbnailHandler.Svg;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace SvgThumbnailProviderUnitTests
{
@@ -211,5 +209,17 @@ namespace SvgThumbnailProviderUnitTests
Assert.IsTrue(bitmap != null);
}
[TestMethod]
public void SvgCommentsAreHandledCorrectly()
{
var filePath = "HelperFiles/WithComments.svg";
SvgThumbnailProvider svgThumbnailProvider = new SvgThumbnailProvider(filePath);
Bitmap bitmap = svgThumbnailProvider.GetThumbnail(8);
Assert.IsTrue(bitmap != null);
}
}
}

View File

@@ -38,15 +38,19 @@
<ProjectReference Include="..\SvgThumbnailProvider\SvgThumbnailProvider.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\STATestClassAttribute.cs" Link="STATestClassAttribute.cs" />
<Compile Include="..\STATestMethodAttribute.cs" Link="STATestMethodAttribute.cs" />
<Compile Include="..\STATestClassAttribute.cs"
Link="STATestClassAttribute.cs" />
<Compile Include="..\STATestMethodAttribute.cs"
Link="STATestMethodAttribute.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="HelperFiles\file1.svg">
<Content Include="HelperFiles\*.svg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="HelperFiles\file2.svg">
<Content Include="HelperFiles\*.bmp">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>

View File

@@ -119,7 +119,7 @@ namespace Common.Utilities
while ((index = s.IndexOf('<', index)) != -1)
{
if (index < s.Length - 1 && s[index + 1] != '?')
if (index < s.Length - 1 && s[index + 1] != '?' && s[index + 1] != '!')
{
return index;
}
@@ -130,11 +130,11 @@ namespace Common.Utilities
return -1;
}
private static int FindFirstXmlCloseTagIndex(string s)
private static int FindFirstXmlCloseTagIndex(string s, int openTagIndex)
{
int index = 1;
while ((index = s.IndexOf('>', index)) != -1)
while ((index = s.IndexOf('>', openTagIndex)) != -1)
{
if (index > 0 && s[index - 1] != '?')
{
@@ -160,7 +160,7 @@ namespace Common.Utilities
return stringSvgData;
}
int firstXmlCloseTagIndex = FindFirstXmlCloseTagIndex(stringSvgData);
int firstXmlCloseTagIndex = FindFirstXmlCloseTagIndex(stringSvgData, firstXmlOpenTagIndex);
if (firstXmlCloseTagIndex == -1)
{
return stringSvgData;
@@ -192,13 +192,18 @@ namespace Common.Utilities
styleIndex -= numRemoved;
}
firstXmlCloseTagIndex -= numRemoved;
stringSvgData = RemoveAttribute(stringSvgData, heightIndex, HeightAttribute, out numRemoved);
if (styleIndex != -1 && styleIndex > heightIndex)
{
styleIndex -= numRemoved;
}
firstXmlCloseTagIndex -= numRemoved;
stringSvgData = RemoveAttribute(stringSvgData, styleIndex, StyleAttribute, out numRemoved);
firstXmlCloseTagIndex -= numRemoved;
width = CheckUnit(width);
height = CheckUnit(height);
@@ -213,7 +218,7 @@ namespace Common.Utilities
scaling += $" _height:expression(this.scrollHeight &gt; {heightR} ? &quot; {height}&quot; : &quot;auto&quot;); _width:expression(this.scrollWidth &gt; {widthR} ? &quot;{width}&quot; : &quot;auto&quot;);";
string newStyle = $"style=\"{scaling}{centering}{oldStyle}\"";
int insertAt = stringSvgData.IndexOf(">", StringComparison.InvariantCultureIgnoreCase);
int insertAt = firstXmlCloseTagIndex;
stringSvgData = stringSvgData.Insert(insertAt, " " + newStyle);