HtmlTagId Enumeration

HTML tag identifiers.

Definition

Namespace: MimeKit.Text
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
C#
public enum HtmlTagId

Remarks

HTML tag identifiers.

Example

C#
                    /// <summary>
/// Visits a MimeMessage and generates HTML suitable to be rendered by a browser control.
/// </summary>
class HtmlPreviewVisitor : MimeVisitor
{
    List<MultipartRelated> stack = new List<MultipartRelated> ();
    List<MimeEntity> attachments = new List<MimeEntity> ();
    string body;

    /// <summary>
    /// Creates a new HtmlPreviewVisitor.
    /// </summary>
    public HtmlPreviewVisitor ()
    {
    }

    /// <summary>
    /// The list of attachments that were in the MimeMessage.
    /// </summary>
    public IList<MimeEntity> Attachments {
        get { return attachments; }
    }

    /// <summary>
    /// The HTML string that can be set on the BrowserControl.
    /// </summary>
    public string HtmlBody {
        get { return body ?? string.Empty; }
    }

    protected override void VisitMultipartAlternative (MultipartAlternative alternative)
    {
        // walk the multipart/alternative children backwards from greatest level of faithfulness to the least faithful
        for (int i = alternative.Count - 1; i >= 0 && body == null; i--)
            alternative[i].Accept (this);
    }

    protected override void VisitMultipartRelated (MultipartRelated related)
    {
        var root = related.Root;

        // push this multipart/related onto our stack
        stack.Add (related);

        // visit the root document
        root.Accept (this);

        // pop this multipart/related off our stack
        stack.RemoveAt (stack.Count - 1);
    }

    // look up the image based on the img src url within our multipart/related stack
    bool TryGetImage (string url, out MimePart image)
    {
        UriKind kind;
        int index;
        Uri uri;

        if (Uri.IsWellFormedUriString (url, UriKind.Absolute))
            kind = UriKind.Absolute;
        else if (Uri.IsWellFormedUriString (url, UriKind.Relative))
            kind = UriKind.Relative;
        else
            kind = UriKind.RelativeOrAbsolute;

        try {
            uri = new Uri (url, kind);
        } catch {
            image = null;
            return false;
        }

        for (int i = stack.Count - 1; i >= 0; i--) {
            if ((index = stack[i].IndexOf (uri)) == -1)
                continue;

            image = stack[i][index] as MimePart;
            return image != null;
        }

        image = null;

        return false;
    }

    /// <summary>
    /// Get a data: URI for the image attachment.
    /// </summary>
    /// <remarks>
    /// Encodes the image attachment into a string suitable for setting as a src= attribute value in
    /// an img tag.
    /// </remarks>
    /// <returns>The data: URI.</returns>
    /// <param name="image">The image attachment.</param>
    string GetDataUri (MimePart image)
    {
        using (var memory = new MemoryStream ()) {
            image.Content.DecodeTo (memory);
            var buffer = memory.GetBuffer ();
            var length = (int) memory.Length;
            var base64 = Convert.ToBase64String (buffer, 0, length);

            return string.Format ("data:{0};base64,{1}", image.ContentType.MimeType, base64);
        }
    }

    // Replaces <img src=...> urls that refer to images embedded within the message with
    // "file://" urls that the browser control will actually be able to load.
    void HtmlTagCallback (HtmlTagContext ctx, HtmlWriter htmlWriter)
    {
        if (ctx.TagId == HtmlTagId.Meta && !ctx.IsEndTag) {
            bool isContentType = false;

            ctx.WriteTag (htmlWriter, false);

            // replace charsets with "utf-8" since our output will be in utf-8 (and not whatever the original charset was)
            foreach (var attribute in ctx.Attributes) {
                if (attribute.Id == HtmlAttributeId.Charset) {
                    htmlWriter.WriteAttributeName (attribute.Name);
                    htmlWriter.WriteAttributeValue ("utf-8");
                } else if (isContentType && attribute.Id == HtmlAttributeId.Content) {
                    htmlWriter.WriteAttributeName (attribute.Name);
                    htmlWriter.WriteAttributeValue ("text/html; charset=utf-8");
                } else {
                    if (attribute.Id == HtmlAttributeId.HttpEquiv && attribute.Value != null
                        && attribute.Value.Equals ("Content-Type", StringComparison.OrdinalIgnoreCase))
                        isContentType = true;

                    htmlWriter.WriteAttribute (attribute);
                }
            }
        } else if (ctx.TagId == HtmlTagId.Image && !ctx.IsEndTag && stack.Count > 0) {
            ctx.WriteTag (htmlWriter, false);

            // replace the src attribute with a "data:" URL
            foreach (var attribute in ctx.Attributes) {
                if (attribute.Id == HtmlAttributeId.Src) {
                    if (!TryGetImage (attribute.Value, out var image)) {
                        htmlWriter.WriteAttribute (attribute);
                        continue;
                    }

                    var dataUri = GetDataUri (image);

                    htmlWriter.WriteAttributeName (attribute.Name);
                    htmlWriter.WriteAttributeValue (dataUri);
                } else {
                    htmlWriter.WriteAttribute (attribute);
                }
            }
        } else {
            // pass the tag through to the output
            ctx.WriteTag (htmlWriter, true);
        }
    }

    protected override void VisitTextPart (TextPart entity)
    {
        TextConverter converter;

        if (body != null) {
            // since we've already found the body, treat this as an attachment
            attachments.Add (entity);
            return;
        }

        if (entity.IsHtml) {
            converter = new HtmlToHtml {
                HtmlTagCallback = HtmlTagCallback
            };
        } else if (entity.IsFlowed) {
            var flowed = new FlowedToHtml ();
            string delsp;

            if (entity.ContentType.Parameters.TryGetValue ("delsp", out delsp))
                flowed.DeleteSpace = delsp.Equals ("yes", StringComparison.OrdinalIgnoreCase);

            converter = flowed;
        } else {
            converter = new TextToHtml ();
        }

        body = converter.Convert (entity.Text);
    }

    protected override void VisitTnefPart (TnefPart entity)
    {
        // extract any attachments in the MS-TNEF part
        using (var tnef = entity.LoadTnefMessage ()) {
            // the converted message (and its attachments) outlives the TnefMessage
            var result = tnef.ConvertToMime ();

            attachments.AddRange (result.Message.Attachments);
        }
    }

    protected override void VisitMessagePart (MessagePart entity)
    {
        // treat message/rfc822 parts as attachments
        attachments.Add (entity);
    }

    protected override void VisitMimePart (MimePart entity)
    {
        // realistically, if we've gotten this far, then we can treat this as an attachment
        // even if the IsAttachment property is false.
        attachments.Add (entity);
    }
}
                  

Members

Unknown 0 An unknown HTML tag identifier.
A 1 The HTML <a> tag.
Abbr 2 The HTML <abbr> tag.
Acronym 3 The HTML <acronym> tag.
Address 4 The HTML <address> tag.
Applet 5 The HTML <applet> tag.
Area 6 The HTML <area> tag.
Article 7 The HTML <article> tag.
Aside 8 The HTML <aside> tag.
Audio 9 The HTML <audio> tag.
B 10 The HTML <b> tag.
Base 11 The HTML <base> tag.
BaseFont 12 The HTML <basefont> tag.
Bdi 13 The HTML <bdi> tag.
Bdo 14 The HTML <bdo> tag.
BGSound 15 The HTML <bgsound> tag.
Big 16 The HTML <big> tag.
Blink 17 The HTML <blink> tag.
BlockQuote 18 The HTML <blockquote> tag.
Body 19 The HTML <body> tag.
Br 20 The HTML <br> tag.
Button 21 The HTML <button> tag.
Canvas 22 The HTML <canvas> tag.
Caption 23 The HTML <caption> tag.
Center 24 The HTML <center> tag.
Cite 25 The HTML <cite> tag.
Code 26 The HTML <code> tag.
Col 27 The HTML <col> tag.
ColGroup 28 The HTML <colgroup> tag.
Command 29 The HTML <command> tag.
Comment 30 The HTML comment tag.
DataList 31 The HTML <datalist> tag.
DD 32 The HTML <dd> tag.
Del 33 The HTML <del> tag.
Details 34 The HTML <details> tag.
Dfn 35 The HTML <dfn> tag.
Dialog 36 The HTML <dialog> tag.
Dir 37 The HTML <dir> tag.
Div 38 The HTML <div> tag.
DL 39 The HTML <dl> tag.
DT 40 The HTML <dt> tag.
EM 41 The HTML <em> tag.
Embed 42 The HTML <embed> tag.
FieldSet 43 The HTML <fieldset> tag.
FigCaption 44 The HTML <figcaption> tag.
Figure 45 The HTML <figure> tag.
Font 46 The HTML <font> tag.
Footer 47 The HTML <footer> tag.
Form 48 The HTML <form> tag.
Frame 49 The HTML <frame> tag.
FrameSet 50 The HTML <frameset> tag.
H1 51 The HTML <h1> tag.
H2 52 The HTML <h2> tag.
H3 53 The HTML <h3> tag.
H4 54 The HTML <h4> tag.
H5 55 The HTML <h5> tag.
H6 56 The HTML <h6> tag.
Head 57 The HTML <head> tag.
Header 58 The HTML <header> tag.
HR 59 The HTML <hr> tag.
Html 60 The HTML <html> tag.
I 61 The HTML <i> tag.
IFrame 62 The HTML <iframe> tag.
Image 63 The HTML <img> tag.
Input 64 The HTML <input> tag.
Ins 65 The HTML <ins> tag.
IsIndex 66 The HTML <isindex> tag.
Kbd 67 The HTML <kbd> tag.
Keygen 68 The HTML <keygen> tag.
Label 69 The HTML <label> tag.
Legend 70 The HTML <legend> tag.
LI 71 The HTML <li> tag.
Link 72 The HTML <link> tag.
Listing 73 The HTML <listing> tag.
Main 74 The HTML <main> tag.
Map 75 The HTML <map> tag.
Mark 76 The HTML <mark> tag.
Marquee 77 The HTML <marquee> tag.
Menu 78 The HTML <menu> tag.
MenuItem 79 The HTML <menuitem> tag.
Meta 80 The HTML <meta> tag.
Meter 81 The HTML <meter> tag.
Nav 82 The HTML <nav> tag.
NextId 83 The HTML <nextid> tag.
NoBR 84 The HTML <nobr> tag.
NoEmbed 85 The HTML <noembed> tag.
NoFrames 86 The HTML <noframes> tag.
NoScript 87 The HTML <noscript> tag.
Object 88 The HTML <object> tag.
OL 89 The HTML <ol> tag.
OptGroup 90 The HTML <optgroup> tag.
Option 91 The HTML <option> tag.
Output 92 The HTML <output> tag.
P 93 The HTML <p> tag.
Param 94 The HTML <param> tag.
PlainText 95 The HTML <plaintext> tag.
Pre 96 The HTML <pre> tag.
Progress 97 The HTML <progress> tag.
Q 98 The HTML <q> tag.
RP 99 The HTML <rp> tag.
RT 100 The HTML <rt> tag.
Ruby 101 The HTML <ruby> tag.
S 102 The HTML <s> tag.
Samp 103 The HTML <samp> tag.
Script 104 The HTML <script> tag.
Section 105 The HTML <section> tag.
Select 106 The HTML <select> tag.
Small 107 The HTML <small> tag.
Source 108 The HTML <source> tag.
Span 109 The HTML <span> tag.
Strike 110 The HTML <strike> tag.
Strong 111 The HTML <strong> tag.
Style 112 The HTML <style> tag.
Sub 113 The HTML <sub> tag.
Summary 114 The HTML <summary> tag.
Sup 115 The HTML <sup> tag.
Table 116 The HTML <table> tag.
TBody 117 The HTML <tbody> tag.
TD 118 The HTML <td> tag.
TextArea 119 The HTML <textarea> tag.
Tfoot 120 The HTML <tfoot> tag.
TH 121 The HTML <th> tag.
THead 122 The HTML <thead> tag.
Time 123 The HTML <time> tag.
Title 124 The HTML <title> tag.
TR 125 The HTML <tr> tag.
Track 126 The HTML <track> tag.
TT 127 The HTML <tt> tag.
U 128 The HTML <u> tag.
UL 129 The HTML <ul> tag.
Var 130 The HTML <var> tag.
Video 131 The HTML <video> tag.
Wbr 132 The HTML <wbr> tag.
Xml 133 The HTML <xml> tag.
Xmp 134 The HTML <xmp> tag.

Extension Methods

IsEmptyElement Determines whether the HTML tag is an empty element.
(Defined by HtmlTagIdExtensions)
IsFormattingElement Determines whether the HTML tag is a formatting element.
(Defined by HtmlTagIdExtensions)
ToHtmlTagName Converts the enum value into the equivalent tag name.
(Defined by HtmlTagIdExtensions)

See Also