Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CefSharp.Example/CefExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public static class CefExample
{
public const string DefaultUrl = "custom://cefsharp/BindingTest.html";
public const string TestResourceUrl = "http://test/resource/load";
public const string TestUnicodeResourceUrl = "http://test/resource/loadUnicode";

// Use when debugging the actual SubProcess, to make breakpoints etc. inside that project work.
private static readonly bool DebuggingSubProcess = Debugger.IsAttached;
Expand Down Expand Up @@ -52,6 +53,9 @@ public static void RegisterTestResources(IWebBrowser browser)
{
const string responseBody = "<html><body><h1>Success</h1><p>This document is loaded from a System.IO.Stream</p></body></html>";
handler.RegisterHandler(TestResourceUrl, ResourceHandler.FromString(responseBody));

const string unicodeResponseBody = "<html><body>整体满意度</body></html>";
handler.RegisterHandler(TestUnicodeResourceUrl, ResourceHandler.FromString(unicodeResponseBody));
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion CefSharp.OffScreen/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
Expand Down Expand Up @@ -240,14 +241,19 @@ public void Load(string url)
}

public void LoadHtml(string html, string url)
{
LoadHtml(html, url, Encoding.UTF8);
}

public void LoadHtml(string html, string url, Encoding encoding)
{
var handler = ResourceHandler;
if (handler == null)
{
throw new Exception("Implement IResourceHandler and assign to the ResourceHandler property to use this feature");
}

handler.RegisterHandler(url, CefSharp.ResourceHandler.FromString(html));
handler.RegisterHandler(url, CefSharp.ResourceHandler.FromString(html, encoding, true));

Load(url);
}
Expand Down
8 changes: 7 additions & 1 deletion CefSharp.WinForms/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using System;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp.Internals;
Expand Down Expand Up @@ -103,14 +104,19 @@ public void Load(String url)
}

public void LoadHtml(string html, string url)
{
LoadHtml(html, url, Encoding.UTF8);
}

public void LoadHtml(string html, string url, Encoding encoding)
{
var handler = ResourceHandler;
if (handler == null)
{
throw new Exception("Implement IResourceHandler and assign to the ResourceHandler property to use this feature");
}

handler.RegisterHandler(url, CefSharp.ResourceHandler.FromString(html));
handler.RegisterHandler(url, CefSharp.ResourceHandler.FromString(html, encoding, true));

Load(url);
}
Expand Down
8 changes: 7 additions & 1 deletion CefSharp.Wpf/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using System.Text;
using CefSharp.Internals;
using Microsoft.Win32.SafeHandles;
using System;
Expand Down Expand Up @@ -1007,14 +1008,19 @@ public void Load(string url)
}

public void LoadHtml(string html, string url)
{
LoadHtml(html, url, Encoding.UTF8);
}

public void LoadHtml(string html, string url, Encoding encoding)
{
var handler = ResourceHandler;
if (handler == null)
{
throw new Exception("Implement IResourceHandler and assign to the ResourceHandler property to use this feature");
}

handler.RegisterHandler(url, CefSharp.ResourceHandler.FromString(html));
handler.RegisterHandler(url, CefSharp.ResourceHandler.FromString(html, encoding, true));

Load(url);
}
Expand Down
15 changes: 15 additions & 0 deletions CefSharp/IWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using System;
using System.Text;
using System.Threading.Tasks;

namespace CefSharp
Expand Down Expand Up @@ -59,11 +60,25 @@ public interface IWebBrowser : IDisposable
/// `Cef` Native `LoadHtml` is unpredictable and only works sometimes, this method wraps
/// the provided HTML in a <see cref="ResourceHandler"/> and loads the provided url using
/// the <see cref="Load"/> method.
/// Defaults to using <see cref="Encoding.UTF8"/> for character encoding
/// </remarks>
/// <param name="html">The HTML content.</param>
/// <param name="url">The URL that will be treated as the address of the content.</param>
void LoadHtml(string html, string url);

/// <summary>
/// Registers and loads a <see cref="ResourceHandler"/> that represents the HTML content.
/// </summary>
/// <remarks>
/// `Cef` Native `LoadHtml` is unpredictable and only works sometimes, this method wraps
/// the provided HTML in a <see cref="ResourceHandler"/> and loads the provided url using
/// the <see cref="Load"/> method.
/// </remarks>
/// <param name="html">The HTML content.</param>
/// <param name="url">The URL that will be treated as the address of the content.</param>
/// <param name="encoding">Character Encoding</param>
void LoadHtml(string html, string url, Encoding encoding);

/// <summary>
/// Registers a Javascript object in this specific browser instance.
/// </summary>
Expand Down
50 changes: 48 additions & 2 deletions CefSharp/ResourceHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,59 @@ public static ResourceHandler FromFileName(string fileName, string fileExtension
}

/// <summary>
/// Gets the resource from the string.
/// Gets a <see cref="ResourceHandler"/> that represents a string.
/// Defaults to <see cref="Encoding.UTF8"/> and includes encoding preamble
/// </summary>
/// <param name="text">The text.</param>
/// <returns>ResourceHandler.</returns>
public static ResourceHandler FromString(string text)
{
return new ResourceHandler { Stream = new MemoryStream(Encoding.UTF8.GetBytes(text)) };
return FromString(text, Encoding.UTF8, true);
}

/// <summary>
/// Gets a <see cref="ResourceHandler"/> that represents a string.
/// Uses the specified encoding and includes encoding preamble.
/// </summary>
/// <param name="text">The html string</param>
/// <param name="encoding">Character Encoding</param>
/// <returns>ResourceHandler</returns>
public static ResourceHandler FromString(string text, Encoding encoding)
{
return FromString(text, encoding, true);
}

/// <summary>
/// Gets a <see cref="ResourceHandler"/> that represents a string.
/// Without a Preamble, Cef will use BrowserSettings.DefaultEncoding to load the html.
/// </summary>
/// <param name="text">The html string</param>
/// <param name="encoding">Character Encoding</param>
/// <param name="includePreamble">Include encoding preamble</param>
/// <returns>ResourceHandler</returns>
public static ResourceHandler FromString(string text, Encoding encoding, bool includePreamble)
{
return new ResourceHandler { Stream = GetStream(text, encoding, includePreamble) };
}

private static MemoryStream GetStream(string text, Encoding encoding, bool includePreamble)
{
if (includePreamble)
{
var preamble = encoding.GetPreamble();
var bytes = encoding.GetBytes(text);

var memoryStream = new MemoryStream();

memoryStream.Write(preamble, 0, preamble.Length);
memoryStream.Write(bytes, 0, bytes.Length);

memoryStream.Position = 0;

return memoryStream;
}

return new MemoryStream(encoding.GetBytes(text));
}

/// <summary>
Expand Down