diff --git a/.github/actions/spell-check/expect.txt b/.github/actions/spell-check/expect.txt
index 4186914fddc..6c848b3e3b9 100644
--- a/.github/actions/spell-check/expect.txt
+++ b/.github/actions/spell-check/expect.txt
@@ -136,6 +136,7 @@ BITMAPFILEHEADER
bitmapimage
BITMAPINFO
BITMAPINFOHEADER
+Bitmaps
bitmask
BITSPIXEL
bla
diff --git a/src/modules/previewpane/UnitTests-SvgThumbnailProvider/HelperFiles/WithComments.svg b/src/modules/previewpane/UnitTests-SvgThumbnailProvider/HelperFiles/WithComments.svg
new file mode 100644
index 00000000000..3acbefe3d12
--- /dev/null
+++ b/src/modules/previewpane/UnitTests-SvgThumbnailProvider/HelperFiles/WithComments.svg
@@ -0,0 +1,15 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/modules/previewpane/UnitTests-SvgThumbnailProvider/SvgThumbnailProviderTests.cs b/src/modules/previewpane/UnitTests-SvgThumbnailProvider/SvgThumbnailProviderTests.cs
index 4e50e765c3c..4fd38cf164f 100644
--- a/src/modules/previewpane/UnitTests-SvgThumbnailProvider/SvgThumbnailProviderTests.cs
+++ b/src/modules/previewpane/UnitTests-SvgThumbnailProvider/SvgThumbnailProviderTests.cs
@@ -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 @@ 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);
+ }
}
}
diff --git a/src/modules/previewpane/UnitTests-SvgThumbnailProvider/UnitTests-SvgThumbnailProvider.csproj b/src/modules/previewpane/UnitTests-SvgThumbnailProvider/UnitTests-SvgThumbnailProvider.csproj
index 100bc8ecdb5..6aec341be5e 100644
--- a/src/modules/previewpane/UnitTests-SvgThumbnailProvider/UnitTests-SvgThumbnailProvider.csproj
+++ b/src/modules/previewpane/UnitTests-SvgThumbnailProvider/UnitTests-SvgThumbnailProvider.csproj
@@ -38,15 +38,19 @@
-
-
+
+
+
-
+
Always
-
+
Always
+
\ No newline at end of file
diff --git a/src/modules/previewpane/common/Utilities/SvgPreviewHandlerHelper.cs b/src/modules/previewpane/common/Utilities/SvgPreviewHandlerHelper.cs
index d9623b8490c..180158152cd 100644
--- a/src/modules/previewpane/common/Utilities/SvgPreviewHandlerHelper.cs
+++ b/src/modules/previewpane/common/Utilities/SvgPreviewHandlerHelper.cs
@@ -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;
}
@@ -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] != '?')
{
@@ -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;
@@ -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);
@@ -213,7 +218,7 @@ public static string AddStyleSVG(string stringSvgData)
scaling += $" _height:expression(this.scrollHeight > {heightR} ? " {height}" : "auto"); _width:expression(this.scrollWidth > {widthR} ? "{width}" : "auto");";
string newStyle = $"style=\"{scaling}{centering}{oldStyle}\"";
- int insertAt = stringSvgData.IndexOf(">", StringComparison.InvariantCultureIgnoreCase);
+ int insertAt = firstXmlCloseTagIndex;
stringSvgData = stringSvgData.Insert(insertAt, " " + newStyle);