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 Display
ing 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
| ||||||
EndTag | An end tag token. Fields
| ||||||
Text | The text contained will be escaped on | ||||||
RawText | The text contained will be | ||||||
Comment | Comments contained within | ||||||
DOCTYPE | The HTML5 DOCTYPE
declaration
( |
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")));