Skip to content

Commit

Permalink
[SVGPreview]Handle comments properly (#28863)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
yuyoyuppe committed Oct 2, 2023
1 parent 11f30f9 commit e14ff34
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/actions/spell-check/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ BITMAPFILEHEADER
bitmapimage
BITMAPINFO
BITMAPINFOHEADER
Bitmaps
bitmask
BITSPIXEL
bla
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -211,5 +209,17 @@ public void GetThumbnailValidStreamHTML()

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private static int FindFirstXmlOpenTagIndex(string s)

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;
}
Expand All @@ -130,11 +130,11 @@ private static int FindFirstXmlOpenTagIndex(string s)
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] != '?')
{
Expand All @@ -160,7 +160,7 @@ public static string AddStyleSVG(string stringSvgData)
return stringSvgData;
}

int firstXmlCloseTagIndex = FindFirstXmlCloseTagIndex(stringSvgData);
int firstXmlCloseTagIndex = FindFirstXmlCloseTagIndex(stringSvgData, firstXmlOpenTagIndex);
if (firstXmlCloseTagIndex == -1)
{
return stringSvgData;
Expand Down Expand Up @@ -192,13 +192,18 @@ public static string AddStyleSVG(string stringSvgData)
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);
Expand All @@ -213,7 +218,7 @@ public static string AddStyleSVG(string stringSvgData)
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);

Expand Down

0 comments on commit e14ff34

Please sign in to comment.