Skip to content

Commit e14ff34

Browse files
authored
[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
1 parent 11f30f9 commit e14ff34

File tree

5 files changed

+47
-12
lines changed

5 files changed

+47
-12
lines changed

.github/actions/spell-check/expect.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ BITMAPFILEHEADER
136136
bitmapimage
137137
BITMAPINFO
138138
BITMAPINFOHEADER
139+
Bitmaps
139140
bitmask
140141
BITSPIXEL
141142
bla
Lines changed: 15 additions & 0 deletions
Loading

src/modules/previewpane/UnitTests-SvgThumbnailProvider/SvgThumbnailProviderTests.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44

55
using System;
66
using System.Drawing;
7+
using System.Drawing.Imaging;
78
using System.Runtime.InteropServices;
8-
using System.Runtime.InteropServices.ComTypes;
99
using System.Text;
10-
using Common.ComInterlop;
1110
using Microsoft.PowerToys.STATestExtension;
1211
using Microsoft.PowerToys.ThumbnailHandler.Svg;
1312
using Microsoft.VisualStudio.TestTools.UnitTesting;
14-
using Moq;
1513

1614
namespace SvgThumbnailProviderUnitTests
1715
{
@@ -211,5 +209,17 @@ public void GetThumbnailValidStreamHTML()
211209

212210
Assert.IsTrue(bitmap != null);
213211
}
212+
213+
[TestMethod]
214+
public void SvgCommentsAreHandledCorrectly()
215+
{
216+
var filePath = "HelperFiles/WithComments.svg";
217+
218+
SvgThumbnailProvider svgThumbnailProvider = new SvgThumbnailProvider(filePath);
219+
220+
Bitmap bitmap = svgThumbnailProvider.GetThumbnail(8);
221+
222+
Assert.IsTrue(bitmap != null);
223+
}
214224
}
215225
}

src/modules/previewpane/UnitTests-SvgThumbnailProvider/UnitTests-SvgThumbnailProvider.csproj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,19 @@
3838
<ProjectReference Include="..\SvgThumbnailProvider\SvgThumbnailProvider.csproj" />
3939
</ItemGroup>
4040
<ItemGroup>
41-
<Compile Include="..\STATestClassAttribute.cs" Link="STATestClassAttribute.cs" />
42-
<Compile Include="..\STATestMethodAttribute.cs" Link="STATestMethodAttribute.cs" />
41+
<Compile Include="..\STATestClassAttribute.cs"
42+
Link="STATestClassAttribute.cs" />
43+
<Compile Include="..\STATestMethodAttribute.cs"
44+
Link="STATestMethodAttribute.cs" />
4345
</ItemGroup>
46+
4447
<ItemGroup>
45-
<Content Include="HelperFiles\file1.svg">
48+
<Content Include="HelperFiles\*.svg">
4649
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
4750
</Content>
48-
<Content Include="HelperFiles\file2.svg">
51+
<Content Include="HelperFiles\*.bmp">
4952
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
5053
</Content>
5154
</ItemGroup>
55+
5256
</Project>

src/modules/previewpane/common/Utilities/SvgPreviewHandlerHelper.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private static int FindFirstXmlOpenTagIndex(string s)
119119

120120
while ((index = s.IndexOf('<', index)) != -1)
121121
{
122-
if (index < s.Length - 1 && s[index + 1] != '?')
122+
if (index < s.Length - 1 && s[index + 1] != '?' && s[index + 1] != '!')
123123
{
124124
return index;
125125
}
@@ -130,11 +130,11 @@ private static int FindFirstXmlOpenTagIndex(string s)
130130
return -1;
131131
}
132132

133-
private static int FindFirstXmlCloseTagIndex(string s)
133+
private static int FindFirstXmlCloseTagIndex(string s, int openTagIndex)
134134
{
135135
int index = 1;
136136

137-
while ((index = s.IndexOf('>', index)) != -1)
137+
while ((index = s.IndexOf('>', openTagIndex)) != -1)
138138
{
139139
if (index > 0 && s[index - 1] != '?')
140140
{
@@ -160,7 +160,7 @@ public static string AddStyleSVG(string stringSvgData)
160160
return stringSvgData;
161161
}
162162

163-
int firstXmlCloseTagIndex = FindFirstXmlCloseTagIndex(stringSvgData);
163+
int firstXmlCloseTagIndex = FindFirstXmlCloseTagIndex(stringSvgData, firstXmlOpenTagIndex);
164164
if (firstXmlCloseTagIndex == -1)
165165
{
166166
return stringSvgData;
@@ -192,13 +192,18 @@ public static string AddStyleSVG(string stringSvgData)
192192
styleIndex -= numRemoved;
193193
}
194194

195+
firstXmlCloseTagIndex -= numRemoved;
196+
195197
stringSvgData = RemoveAttribute(stringSvgData, heightIndex, HeightAttribute, out numRemoved);
196198
if (styleIndex != -1 && styleIndex > heightIndex)
197199
{
198200
styleIndex -= numRemoved;
199201
}
200202

203+
firstXmlCloseTagIndex -= numRemoved;
204+
201205
stringSvgData = RemoveAttribute(stringSvgData, styleIndex, StyleAttribute, out numRemoved);
206+
firstXmlCloseTagIndex -= numRemoved;
202207

203208
width = CheckUnit(width);
204209
height = CheckUnit(height);
@@ -213,7 +218,7 @@ public static string AddStyleSVG(string stringSvgData)
213218
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;);";
214219

215220
string newStyle = $"style=\"{scaling}{centering}{oldStyle}\"";
216-
int insertAt = stringSvgData.IndexOf(">", StringComparison.InvariantCultureIgnoreCase);
221+
int insertAt = firstXmlCloseTagIndex;
217222

218223
stringSvgData = stringSvgData.Insert(insertAt, " " + newStyle);
219224

0 commit comments

Comments
 (0)