From e875d52c5d9b473737652e0295e4b082185c8ff6 Mon Sep 17 00:00:00 2001 From: Scott Harner Date: Sun, 4 Aug 2019 19:43:33 -0400 Subject: [PATCH] make urls safe for chromium --- src/MarkPad/Preview/HtmlPreview.xaml.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/MarkPad/Preview/HtmlPreview.xaml.cs b/src/MarkPad/Preview/HtmlPreview.xaml.cs index a69bb5e7..a37546b6 100644 --- a/src/MarkPad/Preview/HtmlPreview.xaml.cs +++ b/src/MarkPad/Preview/HtmlPreview.xaml.cs @@ -1,5 +1,6 @@ using CefSharp; using System; +using System.Web; using System.Windows; using System.Windows.Controls; @@ -103,7 +104,7 @@ private static void HtmlChanged(DependencyObject d, DependencyPropertyChangedEve if (htmlPreview.host != null && htmlPreview.host.IsBrowserInitialized) { var fileName = (htmlPreview.FileName ?? "blank").Replace(" ", "-"); - var fileUrl = string.Format("http://{0}/", fileName); + var fileUrl = string.Format("http://{0}/", MakeUrlSegmentSafe(fileName)); var newValue = e.NewValue as string; if (newValue == null) @@ -157,7 +158,7 @@ private void Host_IsBrowserInitializedChanged(object sender, DependencyPropertyC private void InitializeData() { var fileName = (FileName ?? "blank").Replace(" ", "-"); - var fileUrl = string.Format("http://{0}/", fileName); + var fileUrl = string.Format("http://{0}/", MakeUrlSegmentSafe(fileName)); var html = Html ?? string.Empty; @@ -181,5 +182,12 @@ public async void UpdateZoomLevel(double fontSize) }); host.SetZoomLevel(fontSize * scale * 2 / Constants.FONT_SIZE_ENUM_ADJUSTMENT); } + + private static string MakeUrlSegmentSafe(string urlSegment) + { + // it appears that the chromium web browser does not like parens or exclamations in a url + // replace these chars and also do a url path encode to try to avoid other potential problem chars + return HttpUtility.UrlEncode(urlSegment).Replace("(", "%28").Replace(")", "%29").Replace("!", "%21"); + } } } \ No newline at end of file