admin
2020-06-10 a610f2ab6e543d2cb78c1ef212ac6a74ddc067d9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// 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
{
    /// <summary>
    /// 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
    /// </summary>
    public class HtmlString
    {
        private readonly string html;
        private readonly bool base64Encode;
 
        /// <summary>
        /// Initializes a new instance of the HtmlString class.
        /// </summary>
        /// <param name="html">raw html string (not already encoded)</param>
        /// <param name="base64Encode">if true the html string will be base64 encoded using UTF8 encoding.</param>
        public HtmlString(string html, bool base64Encode = false)
        {
            this.base64Encode = base64Encode;
            this.html = html;
        }
 
        /// <summary>
        /// The html as a Data Uri encoded string
        /// </summary>
        /// <returns>data Uri string suitable for passing to <see cref="IWebBrowser.Load(string)"/></returns>
        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;
        }
 
        /// <summary>
        /// HtmlString that will be base64 encoded
        /// </summary>
        /// <param name="html">raw html (not already encoded)</param>
        public static explicit operator HtmlString(string html)
        {
            return new HtmlString(html, true);
        }
 
        /// <summary>
        /// Creates a HtmlString for the given file name
        /// Uses <see cref="File.ReadAllText(string, Encoding)"/> to read the
        /// text using <see cref="Encoding.UTF8"/> encoding.
        /// </summary>
        /// <param name="fileName">file name</param>
        /// <returns>HtmlString</returns>
        public static HtmlString FromFile(string fileName)
        {
            var html = File.ReadAllText(fileName, Encoding.UTF8);
 
            return (HtmlString)html;
        }
    }
}