Click or drag to resize
MimeKit

HtmlTagId Enumeration

HTML tag identifiers.

Namespace: MimeKit.Text
Assembly: MimeKit (in MimeKit.dll) Version: 4.7.1
Syntax
C#
public enum HtmlTagId
Members
Member nameValueDescription
Unknown0 An unknown HTML tag identifier.
A1 The HTML <a> tag.
Abbr2 The HTML <abbr> tag.
Acronym3 The HTML <acronym> tag.
Address4 The HTML <address> tag.
Applet5 The HTML <applet> tag.
Area6 The HTML <area> tag.
Article7 The HTML <article> tag.
Aside8 The HTML <aside> tag.
Audio9 The HTML <audio> tag.
B10 The HTML <b> tag.
Base11 The HTML <base> tag.
BaseFont12 The HTML <basefont> tag.
Bdi13 The HTML <bdi> tag.
Bdo14 The HTML <bdo> tag.
BGSound15 The HTML <bgsound> tag.
Big16 The HTML <big> tag.
Blink17 The HTML <blink> tag.
BlockQuote18 The HTML <blockquote> tag.
Body19 The HTML <body> tag.
Br20 The HTML <br> tag.
Button21 The HTML <button> tag.
Canvas22 The HTML <canvas> tag.
Caption23 The HTML <caption> tag.
Center24 The HTML <center> tag.
Cite25 The HTML <cite> tag.
Code26 The HTML <code> tag.
Col27 The HTML <col> tag.
ColGroup28 The HTML <colgroup> tag.
Command29 The HTML <command> tag.
Comment30 The HTML comment tag.
DataList31 The HTML <datalist> tag.
DD32 The HTML <dd> tag.
Del33 The HTML <del> tag.
Details34 The HTML <details> tag.
Dfn35 The HTML <dfn> tag.
Dialog36 The HTML <dialog> tag.
Dir37 The HTML <dir> tag.
Div38 The HTML <div> tag.
DL39 The HTML <dl> tag.
DT40 The HTML <dt> tag.
EM41 The HTML <em> tag.
Embed42 The HTML <embed> tag.
FieldSet43 The HTML <fieldset> tag.
FigCaption44 The HTML <figcaption> tag.
Figure45 The HTML <figure> tag.
Font46 The HTML <font> tag.
Footer47 The HTML <footer> tag.
Form48 The HTML <form> tag.
Frame49 The HTML <frame> tag.
FrameSet50 The HTML <frameset> tag.
H151 The HTML <h1> tag.
H252 The HTML <h2> tag.
H353 The HTML <h3> tag.
H454 The HTML <h4> tag.
H555 The HTML <h5> tag.
H656 The HTML <h6> tag.
Head57 The HTML <head> tag.
Header58 The HTML <header> tag.
HR59 The HTML <hr> tag.
Html60 The HTML <html> tag.
I61 The HTML <i> tag.
IFrame62 The HTML <iframe> tag.
Image63 The HTML <img> tag.
Input64 The HTML <input> tag.
Ins65 The HTML <ins> tag.
IsIndex66 The HTML <isindex> tag.
Kbd67 The HTML <kbd> tag.
Keygen68 The HTML <keygen> tag.
Label69 The HTML <label> tag.
Legend70 The HTML <legend> tag.
LI71 The HTML <li> tag.
Link72 The HTML <link> tag.
Listing73 The HTML <listing> tag.
Main74 The HTML <main> tag.
Map75 The HTML <map> tag.
Mark76 The HTML <mark> tag.
Marquee77 The HTML <marquee> tag.
Menu78 The HTML <menu> tag.
MenuItem79 The HTML <menuitem> tag.
Meta80 The HTML <meta> tag.
Meter81 The HTML <meter> tag.
Nav82 The HTML <nav> tag.
NextId83 The HTML <nextid> tag.
NoBR84 The HTML <nobr> tag.
NoEmbed85 The HTML <noembed> tag.
NoFrames86 The HTML <noframes> tag.
NoScript87 The HTML <noscript> tag.
Object88 The HTML <object> tag.
OL89 The HTML <ol> tag.
OptGroup90 The HTML <optgroup> tag.
Option91 The HTML <option> tag.
Output92 The HTML <output> tag.
P93 The HTML <p> tag.
Param94 The HTML <param> tag.
PlainText95 The HTML <plaintext> tag.
Pre96 The HTML <pre> tag.
Progress97 The HTML <progress> tag.
Q98 The HTML <q> tag.
RP99 The HTML <rp> tag.
RT100 The HTML <rt> tag.
Ruby101 The HTML <ruby> tag.
S102 The HTML <s> tag.
Samp103 The HTML <samp> tag.
Script104 The HTML <script> tag.
Section105 The HTML <section> tag.
Select106 The HTML <select> tag.
Small107 The HTML <small> tag.
Source108 The HTML <source> tag.
Span109 The HTML <span> tag.
Strike110 The HTML <strike> tag.
Strong111 The HTML <strong> tag.
Style112 The HTML <style> tag.
Sub113 The HTML <sub> tag.
Summary114 The HTML <summary> tag.
Sup115 The HTML <sup> tag.
Table116 The HTML <table> tag.
TBody117 The HTML <tbody> tag.
TD118 The HTML <td> tag.
TextArea119 The HTML <textarea> tag.
Tfoot120 The HTML <tfoot> tag.
TH121 The HTML <th> tag.
THead122 The HTML <thead> tag.
Time123 The HTML <time> tag.
Title124 The HTML <title> tag.
TR125 The HTML <tr> tag.
Track126 The HTML <track> tag.
TT127 The HTML <tt> tag.
U128 The HTML <u> tag.
UL129 The HTML <ul> tag.
Var130 The HTML <var> tag.
Video131 The HTML <video> tag.
Wbr132 The HTML <wbr> tag.
Xml133 The HTML <xml> tag.
Xmp134 The HTML <xmp> tag.
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 if (ctx.TagId == HtmlTagId.Body && !ctx.IsEndTag) {
            ctx.WriteTag (htmlWriter, false);

            // add and/or replace oncontextmenu="return false;"
            foreach (var attribute in ctx.Attributes) {
                if (attribute.Name.Equals ("oncontextmenu", StringComparison.OrdinalIgnoreCase))
                    continue;

                htmlWriter.WriteAttribute (attribute);
            }

            htmlWriter.WriteAttribute ("oncontextmenu", "return false;");
        } 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
        attachments.AddRange (entity.ExtractAttachments ());
    }

    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);
    }
}
See Also