diff --git a/CefSharp.Example/CefExample.cs b/CefSharp.Example/CefExample.cs index 672d7fe52c..6a00fac224 100644 --- a/CefSharp.Example/CefExample.cs +++ b/CefSharp.Example/CefExample.cs @@ -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; @@ -52,6 +53,9 @@ public static void RegisterTestResources(IWebBrowser browser) { const string responseBody = "

Success

This document is loaded from a System.IO.Stream

"; handler.RegisterHandler(TestResourceUrl, ResourceHandler.FromString(responseBody)); + + const string unicodeResponseBody = "整体满意度"; + handler.RegisterHandler(TestUnicodeResourceUrl, ResourceHandler.FromString(unicodeResponseBody)); } } } diff --git a/CefSharp.OffScreen/ChromiumWebBrowser.cs b/CefSharp.OffScreen/ChromiumWebBrowser.cs index 20affe0003..302962c700 100644 --- a/CefSharp.OffScreen/ChromiumWebBrowser.cs +++ b/CefSharp.OffScreen/ChromiumWebBrowser.cs @@ -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; @@ -240,6 +241,11 @@ 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) @@ -247,7 +253,7 @@ public void LoadHtml(string html, string url) 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); } diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index 5f424d1596..08e33a5d9e 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -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; @@ -103,6 +104,11 @@ 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) @@ -110,7 +116,7 @@ public void LoadHtml(string html, string url) 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); } diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index be8ace1aa9..f7320fb7b9 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -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; @@ -1007,6 +1008,11 @@ 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) @@ -1014,7 +1020,7 @@ public void LoadHtml(string html, string url) 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); } diff --git a/CefSharp/IWebBrowser.cs b/CefSharp/IWebBrowser.cs index db9dd8c765..32d34d98f2 100644 --- a/CefSharp/IWebBrowser.cs +++ b/CefSharp/IWebBrowser.cs @@ -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 @@ -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 and loads the provided url using /// the method. + /// Defaults to using for character encoding /// /// The HTML content. /// The URL that will be treated as the address of the content. void LoadHtml(string html, string url); + /// + /// Registers and loads a that represents the HTML content. + /// + /// + /// `Cef` Native `LoadHtml` is unpredictable and only works sometimes, this method wraps + /// the provided HTML in a and loads the provided url using + /// the method. + /// + /// The HTML content. + /// The URL that will be treated as the address of the content. + /// Character Encoding + void LoadHtml(string html, string url, Encoding encoding); + /// /// Registers a Javascript object in this specific browser instance. /// diff --git a/CefSharp/ResourceHandler.cs b/CefSharp/ResourceHandler.cs index a992c8576e..dc06792a08 100644 --- a/CefSharp/ResourceHandler.cs +++ b/CefSharp/ResourceHandler.cs @@ -84,13 +84,59 @@ public static ResourceHandler FromFileName(string fileName, string fileExtension } /// - /// Gets the resource from the string. + /// Gets a that represents a string. + /// Defaults to and includes encoding preamble /// /// The text. /// ResourceHandler. public static ResourceHandler FromString(string text) { - return new ResourceHandler { Stream = new MemoryStream(Encoding.UTF8.GetBytes(text)) }; + return FromString(text, Encoding.UTF8, true); + } + + /// + /// Gets a that represents a string. + /// Uses the specified encoding and includes encoding preamble. + /// + /// The html string + /// Character Encoding + /// ResourceHandler + public static ResourceHandler FromString(string text, Encoding encoding) + { + return FromString(text, encoding, true); + } + + /// + /// Gets a that represents a string. + /// Without a Preamble, Cef will use BrowserSettings.DefaultEncoding to load the html. + /// + /// The html string + /// Character Encoding + /// Include encoding preamble + /// ResourceHandler + 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)); } ///