mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-09 04:37:30 +02:00
Display infobar for blocked relative image path (#1695)
* Updated Parsing extension to show infobar when relative URL isblocked and updated corresponding tests * Updated Controller to display infobar when html img tag is embedded in markdown
This commit is contained in:
committed by
GitHub
parent
15cefc664a
commit
c2e219b446
@@ -91,23 +91,9 @@ namespace MarkdownPreviewHandler
|
|||||||
{
|
{
|
||||||
if (link.IsImage)
|
if (link.IsImage)
|
||||||
{
|
{
|
||||||
|
link.Url = "#";
|
||||||
link.GetAttributes().AddClass("img-fluid");
|
link.GetAttributes().AddClass("img-fluid");
|
||||||
}
|
this.imagesBlockedCallBack();
|
||||||
|
|
||||||
if (!Uri.TryCreate(link.Url, UriKind.Absolute, out _))
|
|
||||||
{
|
|
||||||
link.Url = link.Url.TrimStart('/', '\\');
|
|
||||||
this.BaseUrl = this.BaseUrl.TrimEnd('/', '\\');
|
|
||||||
Uri uriLink = new Uri(Path.Combine(this.BaseUrl, link.Url));
|
|
||||||
link.Url = uriLink.ToString();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (link.IsImage)
|
|
||||||
{
|
|
||||||
link.Url = "#";
|
|
||||||
this.imagesBlockedCallBack();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ using System.Drawing;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using Common;
|
using Common;
|
||||||
using Markdig;
|
using Markdig;
|
||||||
@@ -83,6 +84,12 @@ namespace MarkdownPreviewHandler
|
|||||||
string fileText = File.ReadAllText(filePath);
|
string fileText = File.ReadAllText(filePath);
|
||||||
this.extension.BaseUrl = Path.GetDirectoryName(filePath);
|
this.extension.BaseUrl = Path.GetDirectoryName(filePath);
|
||||||
|
|
||||||
|
Regex rgx = new Regex(@"<[ ]*img.*>");
|
||||||
|
if (rgx.IsMatch(fileText))
|
||||||
|
{
|
||||||
|
this.infoBarDisplayed = true;
|
||||||
|
}
|
||||||
|
|
||||||
MarkdownPipeline pipeline = this.pipelineBuilder.Build();
|
MarkdownPipeline pipeline = this.pipelineBuilder.Build();
|
||||||
string parsedMarkdown = Markdown.ToHtml(fileText, pipeline);
|
string parsedMarkdown = Markdown.ToHtml(fileText, pipeline);
|
||||||
sb.AppendFormat("{0}{1}{2}", this.htmlHeader, parsedMarkdown, this.htmlFooter);
|
sb.AppendFormat("{0}{1}{2}", this.htmlHeader, parsedMarkdown, this.htmlFooter);
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ namespace PreviewPaneUnitTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void Extension_UpdatesFigureClassAndRelativeUrltoAbsolute_WhenUsed()
|
public void Extension_UpdatesFigureClassAndBlocksRelativeUrl_WhenUsed()
|
||||||
{
|
{
|
||||||
// arrange
|
// arrange
|
||||||
String mdString = "";
|
String mdString = "";
|
||||||
@@ -58,37 +58,7 @@ namespace PreviewPaneUnitTests
|
|||||||
String html = Markdown.ToHtml(mdString, markdownPipeline);
|
String html = Markdown.ToHtml(mdString, markdownPipeline);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.AreEqual(html, "<p><img src=\"file:///C:/Users/a.jpg\" class=\"img-fluid\" alt=\"text\" title=\"Figure\" /></p>\n");
|
Assert.AreEqual(html, "<p><img src=\"#\" class=\"img-fluid\" alt=\"text\" title=\"Figure\" /></p>\n");
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void Extension_CreatesCorrectAbsoluteLinkByTrimmingForwardSlash_WhenUsed()
|
|
||||||
{
|
|
||||||
// arrange
|
|
||||||
String mdString = "";
|
|
||||||
HTMLParsingExtension htmlParsingExtension = new HTMLParsingExtension(() => { }, "C:\\Users\\");
|
|
||||||
MarkdownPipeline markdownPipeline = BuidPipeline(htmlParsingExtension);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
String html = Markdown.ToHtml(mdString, markdownPipeline);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.AreEqual(html, "<p><img src=\"file:///C:/Users/document/a.jpg\" class=\"img-fluid\" alt=\"text\" title=\"Figure\" /></p>\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void Extension_CreatesCorrectAbsoluteLinkByTrimmingBackwardSlash_WhenUsed()
|
|
||||||
{
|
|
||||||
// arrange
|
|
||||||
String mdString = "";
|
|
||||||
HTMLParsingExtension htmlParsingExtension = new HTMLParsingExtension(() => { }, "C:/Users/");
|
|
||||||
MarkdownPipeline markdownPipeline = BuidPipeline(htmlParsingExtension);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
String html = Markdown.ToHtml(mdString, markdownPipeline);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.AreEqual(html, "<p><img src=\"file:///C:/Users/document/a.jpg\" class=\"img-fluid\" alt=\"text\" title=\"Figure\" /></p>\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
## Something
|
||||||
|
<img src="./a.jpg" \>
|
||||||
@@ -42,6 +42,20 @@ namespace PreviewPaneUnitTests
|
|||||||
Assert.IsInstanceOfType(markdownPreviewHandlerControl.Controls[1], typeof(RichTextBox));
|
Assert.IsInstanceOfType(markdownPreviewHandlerControl.Controls[1], typeof(RichTextBox));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void MarkdownPreviewHandlerControl__AddsInfoBarToFormIfHTMLImageTagIsPresent_WhenDoPreviewIsCalled()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
MarkdownPreviewHandlerControl markdownPreviewHandlerControl = new MarkdownPreviewHandlerControl();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
markdownPreviewHandlerControl.DoPreview<string>("HelperFiles/MarkdownWithHTMLImageTag.txt");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(markdownPreviewHandlerControl.Controls.Count, 2);
|
||||||
|
Assert.IsInstanceOfType(markdownPreviewHandlerControl.Controls[1], typeof(RichTextBox));
|
||||||
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void MarkdownPreviewHandlerControl__DoesNotAddInfoBarToFormIfExternalImageLinkNotPresent_WhenDoPreviewIsCalled()
|
public void MarkdownPreviewHandlerControl__DoesNotAddInfoBarToFormIfExternalImageLinkNotPresent_WhenDoPreviewIsCalled()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -116,6 +116,9 @@
|
|||||||
<Content Include="HelperFiles\MarkdownWithExternalImage.txt">
|
<Content Include="HelperFiles\MarkdownWithExternalImage.txt">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</Content>
|
</Content>
|
||||||
|
<Content Include="HelperFiles\MarkdownWithHTMLImageTag.txt">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
<Content Include="HelperFiles\MarkdownWithscript.txt">
|
<Content Include="HelperFiles\MarkdownWithscript.txt">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</Content>
|
</Content>
|
||||||
|
|||||||
Reference in New Issue
Block a user