Enum hamlet::Token [] [src]

pub enum Token<'a> {
    StartTag {
        name: Cow<'a, str>,
        attrs: AttributeList<'a>,
        self_closing: bool,
    },
    EndTag {
        name: Cow<'a, str>,
    },
    Text(Cow<'a, str>),
    RawText(Cow<'a, str>),
    Comment(Cow<'a, str>),
    DOCTYPE,
}

An HTML token, these are representations of everything needed to generate an HTML document.

By convention, Token::Text should be preferred over Token::RawText when a piece of text can be represented by both. For instance, use Text when tokenizing whitespaces or line-breaks, but use RawText for representing all text inside a <style> tag.

When Displaying a Token, the output stream is assumed to be Unicode, and therefore only five characters are escaped: &, <, >, ", and ' (ref).

Variants

StartTag

A start tag token.

Fields

name

The element's tag name.

attrs

Any attributes for the start tag.

self_closing

Marker indicating the tag should be self-closing, such as <br /> (although br is a void element so this has no effect on it).

EndTag

An end tag token.

Fields

name

The element's tag name.

Text

The text contained will be escaped on Display.

RawText

The text contained will be Displayed as-is.

Comment

Comments contained within <!-- and -->. No validation is done to ensure that the text conforms to the html comment syntax.

DOCTYPE

The HTML5 DOCTYPE declaration (<!DOCTYPE html>)

Methods

impl<'a> Token<'a>

fn start_tag<S>(name: S, attrs: AttributeList<'a>) -> Token<'a> where S: Into<Cow<'a, str>>

Create a StartTag token with specified element name and attributes, use closed() to set the self_closing flag.

Examples

assert_eq!(
    hamlet::Token::start_tag("script", attrs!()),
    hamlet::Token::StartTag {
        name: std::borrow::Cow::Borrowed("script"),
        attrs: attrs!(),
        self_closing: false,
    });

fn end_tag<S>(name: S) -> Token<'a> where S: Into<Cow<'a, str>>

Create an EndTag token with specified element name.

Examples

assert_eq!(
    hamlet::Token::end_tag("script"),
    hamlet::Token::EndTag {
        name: std::borrow::Cow::Borrowed("script"),
    });

fn text<S>(s: S) -> Token<'a> where S: Into<Cow<'a, str>>

Create a Text token with specified text content.

Examples

assert_eq!(
    hamlet::Token::text("hello world"),
    hamlet::Token::Text(std::borrow::Cow::Borrowed("hello world")));

fn raw_text<S>(s: S) -> Token<'a> where S: Into<Cow<'a, str>>

Create a RawText token with specified raw text content.

Examples

assert_eq!(
    hamlet::Token::raw_text("hello world"),
    hamlet::Token::RawText(std::borrow::Cow::Borrowed("hello world")));

fn comment<S>(s: S) -> Token<'a> where S: Into<Cow<'a, str>>

Create a Comment token with specified comment content.

Examples

assert_eq!(
    hamlet::Token::comment("hello world"),
    hamlet::Token::Comment(std::borrow::Cow::Borrowed("hello world")));

fn closed(self) -> Token<'a>

If self is a StartTag, returns a copy with self_closing set to true; otherwise, returns self.

Examples

assert_eq!(
    hamlet::Token::start_tag("br", attrs!()).closed(),
    hamlet::Token::StartTag {
        name: std::borrow::Cow::Borrowed("br"),
        attrs: attrs!(),
        self_closing: true,
    });
assert_eq!(
    hamlet::Token::text("hello world").closed(),
    hamlet::Token::Text(std::borrow::Cow::Borrowed("hello world")));

Trait Implementations

impl<'a> Display for Token<'a>

fn fmt(&self, f: &mut Formatter) -> Result

Derived Implementations

impl<'a> Eq for Token<'a>

impl<'a> PartialEq for Token<'a>

fn eq(&self, __arg_0: &Token<'a>) -> bool

fn ne(&self, __arg_0: &Token<'a>) -> bool

impl<'a> Debug for Token<'a>

fn fmt(&self, __arg_0: &mut Formatter) -> Result

impl<'a> Clone for Token<'a>

fn clone(&self) -> Token<'a>

fn clone_from(&mut self, source: &Self)