Skip to content

Commit

Permalink
Remove script and image element from Svg (microsoft#1231)
Browse files Browse the repository at this point in the history
* Added implementation to remove script and image tag

* Added Unit tests for SvgPreviewHandlerHelper

* Updated Unit tests for SvgPreviewControl

* Moved the hardcoded string to resource file

* Changed the LocalMachine to CurrentUser for preview handler registration

* Added unit tests for multiple blocked elements tags
  • Loading branch information
udit3333 committed Mar 10, 2020
1 parent 22dea3e commit dbe61ef
Show file tree
Hide file tree
Showing 9 changed files with 586 additions and 30 deletions.
72 changes: 72 additions & 0 deletions src/modules/previewpane/SvgPreviewHandler/Resource.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 123 additions & 0 deletions src/modules/previewpane/SvgPreviewHandler/Resource.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="BlockedElementInfoText" xml:space="preserve">
<value>Some elements have been blocked to help prevent the sender from identifying your computer. Open this item to view all elements.</value>
</data>
</root>
103 changes: 87 additions & 16 deletions src/modules/previewpane/SvgPreviewHandler/SvgPreviewControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using Common;
using Common.Utilities;
using SvgPreviewHandler.Utilities;

namespace SvgPreviewHandler
{
Expand All @@ -16,7 +22,20 @@ namespace SvgPreviewHandler
/// </summary>
public class SvgPreviewControl : FormHandlerControl
{
private Stream dataSourceStream;
/// <summary>
/// Browser Control to display Svg.
/// </summary>
private WebBrowser browser;

/// <summary>
/// Text box to display the information about blocked elements from Svg.
/// </summary>
private RichTextBox textBox;

/// <summary>
/// Represent if any blocked element is found in the Svg.
/// </summary>
private bool foundFilteredElements = false;

/// <summary>
/// Start the preview on the Control.
Expand All @@ -26,30 +45,82 @@ public override void DoPreview<T>(T dataSource)
{
this.InvokeOnControlThread(() =>
{
WebBrowser browser = new WebBrowser();
this.dataSourceStream = new StreamWrapper(dataSource as IStream);

browser.DocumentStream = this.dataSourceStream;
browser.Dock = DockStyle.Fill;
browser.IsWebBrowserContextMenuEnabled = false;
browser.ScriptErrorsSuppressed = true;
browser.ScrollBarsEnabled = true;
this.Controls.Add(browser);
this.foundFilteredElements = false;
string svgData = null;
using (var stream = new StreamWrapper(dataSource as IStream))
{
using (var reader = new StreamReader(stream))
{
svgData = reader.ReadToEnd();
}
}

svgData = string.IsNullOrWhiteSpace(svgData) ? svgData : SvgPreviewHandlerHelper.RemoveElements(svgData, out this.foundFilteredElements);
if (this.foundFilteredElements)
{
this.AddTextBoxControl();
}

this.AddBrowserControl(svgData);
this.Resize += this.FormResized;
base.DoPreview(dataSource);
});
}

/// <summary>
/// Free resources on the unload of Preview.
/// Occurs when RichtextBox is resized.
/// </summary>
public override void Unload()
/// <param name="sender">Reference to resized control.</param>
/// <param name="e">Provides data for the ContentsResized event.</param>
private void RTBContentsResized(object sender, ContentsResizedEventArgs e)
{
base.Unload();
if (this.dataSourceStream != null)
var richTextBox = sender as RichTextBox;
richTextBox.Height = e.NewRectangle.Height + 5;
}

/// <summary>
/// Occurs when form is resized.
/// </summary>
/// <param name="sender">Reference to resized control.</param>
/// <param name="e">Provides data for the resize event.</param>
private void FormResized(object sender, EventArgs e)
{
if (this.foundFilteredElements)
{
this.dataSourceStream.Dispose();
this.dataSourceStream = null;
this.textBox.Width = this.Width;
}
}

/// <summary>
/// Adds a Web Browser Control to Control Collection.
/// </summary>
/// <param name="svgData">Svg to display on Browser Control.</param>
private void AddBrowserControl(string svgData)
{
this.browser = new WebBrowser();
this.browser.DocumentText = svgData;
this.browser.Dock = DockStyle.Fill;
this.browser.IsWebBrowserContextMenuEnabled = false;
this.browser.ScriptErrorsSuppressed = true;
this.browser.ScrollBarsEnabled = true;
this.Controls.Add(this.browser);
}

/// <summary>
/// Adds a Text Box in Controls for showing information about blocked elements.
/// </summary>
private void AddTextBoxControl()
{
this.textBox = new RichTextBox();
this.textBox.Text = Resource.BlockedElementInfoText;
this.textBox.BackColor = Color.LightYellow;
this.textBox.Multiline = true;
this.textBox.Dock = DockStyle.Top;
this.textBox.ReadOnly = true;
this.textBox.ContentsResized += this.RTBContentsResized;
this.textBox.ScrollBars = RichTextBoxScrollBars.None;
this.textBox.BorderStyle = BorderStyle.None;
this.Controls.Add(this.textBox);
}
}
}
13 changes: 13 additions & 0 deletions src/modules/previewpane/SvgPreviewHandler/SvgPreviewHandler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand All @@ -56,11 +57,17 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Resource.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resource.resx</DependentUpon>
</Compile>
<Compile Include="SvgPreviewControl.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="SvgPreviewHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utilities\SvgPreviewHandlerHelper.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\common\PreviewHandlerCommon.csproj">
Expand All @@ -81,5 +88,11 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resource.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resource.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Loading

0 comments on commit dbe61ef

Please sign in to comment.