// Copyright © 2019 The CefSharp Authors. All rights reserved. // // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. using System; using System.IO; using System.Text; namespace CefSharp.Web { /// /// Represents an raw Html (not already encoded) /// When passed to a ChromiumWebBrowser constructor, the html will be converted to a Data Uri /// and loaded in the browser. /// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs for details /// public class HtmlString { private readonly string html; private readonly bool base64Encode; /// /// Initializes a new instance of the HtmlString class. /// /// raw html string (not already encoded) /// if true the html string will be base64 encoded using UTF8 encoding. public HtmlString(string html, bool base64Encode = false) { this.base64Encode = base64Encode; this.html = html; } /// /// The html as a Data Uri encoded string /// /// data Uri string suitable for passing to public string ToDataUriString() { if (base64Encode) { var base64EncodedHtml = Convert.ToBase64String(Encoding.UTF8.GetBytes(html)); return "data:text/html;base64," + base64EncodedHtml; } var uriEncodedHtml = Uri.EscapeDataString(html); return "data:text/html," + uriEncodedHtml; } /// /// HtmlString that will be base64 encoded /// /// raw html (not already encoded) public static explicit operator HtmlString(string html) { return new HtmlString(html, true); } /// /// Creates a HtmlString for the given file name /// Uses to read the /// text using encoding. /// /// file name /// HtmlString public static HtmlString FromFile(string fileName) { var html = File.ReadAllText(fileName, Encoding.UTF8); return (HtmlString)html; } } }