Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#466 - make urls safe for chromium #474

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/MarkPad/Preview/HtmlPreview.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CefSharp;
using System;
using System.Web;
using System.Windows;
using System.Windows.Controls;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;

Expand All @@ -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");
}
}
}