[FileExplorer][SVG preview] Swap order of default and svg namespace if default comes first (#18825)

* [FileExplorer Add-ons][SVG preview] Swap order of default and svg namespace if default comes first

Inkscape v1.1 swapped order of default and svg namespaces in svg file (default first, svg after).
That resulted in parser being unable to parse it correctly and instead of svg, text was previewed.
MS Edge and Firefox also couldn't preview svg files with mentioned order of namespaces definitions.

* expect.txt

* Update comment

* Minor fixes

* Use full namespace definition instead of prefix
This commit is contained in:
Stefan Markovic
2022-06-19 12:21:51 +02:00
committed by GitHub
parent 759ea40b22
commit 2159e2722e
3 changed files with 30 additions and 0 deletions

View File

@@ -616,6 +616,7 @@ Filterx
finalizer
findfast
findstr
Firefox
FIXEDFILEINFO
FLASHZONES
FLASHZONESONQUICKSWITCH
@@ -886,6 +887,7 @@ ingbuffer
inheritdoc
INITDIALOG
initguid
Inkscape
Inlines
inorder
INotification

View File

@@ -102,6 +102,10 @@ namespace Microsoft.PowerToys.PreviewHandler.Svg
try
{
// Fixes #17527 - Inkscape v1.1 swapped order of default and svg namespaces in svg file (default first, svg after).
// That resulted in parser being unable to parse it correctly and instead of svg, text was previewed.
// MS Edge and Firefox also couldn't preview svg files with mentioned order of namespaces definitions.
svgData = SvgPreviewHandlerHelper.SwapNamespaces(svgData);
svgData = SvgPreviewHandlerHelper.AddStyleSVG(svgData);
}
catch (Exception ex)

View File

@@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Xml.Linq;
@@ -154,6 +155,29 @@ namespace Common.Utilities
return length + "px";
}
/// <summary>
/// Swaps positions of default and svg namespace definitions if default namespace comes first in original SVG data
/// </summary>
/// <param name="svgData">SVG data</param>
/// <returns>Returns modified SVG data</returns>
public static string SwapNamespaces(string svgData)
{
const string defaultNamespace = "xmlns=\"http://www.w3.org/2000/svg\"";
const string svgNamespace = "xmlns:svg=\"http://www.w3.org/2000/svg\"";
int defaultNamespaceIndex = svgData.IndexOf(defaultNamespace, StringComparison.InvariantCultureIgnoreCase);
int svgNamespaceIndex = svgData.IndexOf(svgNamespace, StringComparison.InvariantCultureIgnoreCase);
if (defaultNamespaceIndex != -1 && svgNamespaceIndex != -1 && defaultNamespaceIndex < svgNamespaceIndex)
{
svgData = svgData.Replace(defaultNamespace, "{0}", StringComparison.InvariantCultureIgnoreCase);
svgData = svgData.Replace(svgNamespace, "{1}", StringComparison.InvariantCultureIgnoreCase);
svgData = string.Format(CultureInfo.InvariantCulture, svgData, svgNamespace, defaultNamespace);
}
return svgData;
}
/// <summary>
/// Remove a CSS unit from the end of the string
/// </summary>