HtmlTokenizer Class

An HTML tokenizer.

Definition

Namespace: MimeKit.Text
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
C#
public class HtmlTokenizer
Inheritance
Object    HtmlTokenizer

Remarks

Tokenizes HTML text, emitting an HtmlToken for each token it encounters.

Example

C#
                    public static IList<string> ExtractLinks (string html)
{
    var links = new List<string> ();

    using (var reader = new StringReader (html)) {
        // HtmlTokenizer is a forward-only HTML5 tokenizer. It does not build a DOM which
        // makes it fast and well-suited for scanning or rewriting HTML.
        var tokenizer = new HtmlTokenizer (reader);

        while (tokenizer.ReadNextToken (out var token)) {
            if (token.Kind != HtmlTokenKind.Tag)
                continue;

            var tag = (HtmlTagToken) token;

            // Tag and attribute names are mapped to HtmlTagId and HtmlAttributeId values
            // for fast comparisons.
            if (tag.Id != HtmlTagId.A || tag.IsEndTag)
                continue;

            foreach (var attribute in tag.Attributes) {
                if (attribute.Id == HtmlAttributeId.Href && !string.IsNullOrEmpty (attribute.Value))
                    links.Add (attribute.Value!);
            }
        }
    }

    return links;
}
                  

Constructors

HtmlTokenizer(Stream) Initialize a new instance of the HtmlTokenizer class.
HtmlTokenizer(TextReader) Initialize a new instance of the HtmlTokenizer class.
HtmlTokenizer(Stream, Encoding) Initialize a new instance of the HtmlTokenizer class.
HtmlTokenizer(Stream, Encoding, Boolean, Int32) Initialize a new instance of the HtmlTokenizer class.

Properties

ComplianceLogger Get or set the logger to use for reporting HTML compliance violations.
ComplianceOptions Get or set the options that control how HTML compliance violations are reported.
DecodeCharacterReferences Get or set whether the tokenizer should decode character references.
HtmlNamespace Get the current HTML namespace detected by the tokenizer.
IgnoreTruncatedTags Get or set whether the tokenizer should ignore truncated tags.
LineNumber Get the current line number.
LinePosition Get the current line position.
MaxElementDepth Get or set the maximum element depth.
ScriptingEnabled Get or set whether the tokenizer should behave as if scripting is enabled.
TokenizerState Get the current state of the tokenizer.

Methods

CreateAttribute Create an attribute.
CreateCDataToken Create an HTML character data token.
CreateCommentToken Create an HTML comment token.
CreateDataToken Create an HTML character data token.
CreateDocType Create a DOCTYPE token.
CreateScriptDataToken Create an HTML script data token.
CreateTagToken Create an HTML tag token.
Equals Determines whether the specified object is equal to the current object.
(Inherited from Object)
Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object)
GetHashCode Serves as the default hash function.
(Inherited from Object)
GetType Gets the Type of the current instance.
(Inherited from Object)
MemberwiseClone Creates a shallow copy of the current Object.
(Inherited from Object)
ReadNextToken Read the next token.
ToString Returns a string that represents the current object.
(Inherited from Object)

See Also