Crate tl[][src]

tl is an efficient and easy to use HTML parser written in Rust.

It does minimal to no copying during parsing by borrowing parts of the input string. Additionally, it keeps track of parsed elements and stores elements with an id attribute in an internal HashMap, which makes element lookups by ID/class name very fast.

Examples

Finding an element by its id attribute and printing the inner text:

let input = r#"<p id="text">Hello</p>"#;
let dom = tl::parse(input);

let element = dom.get_element_by_id("text").expect("Failed to find element");

println!("Inner text: {}", element.inner_text());

Owned DOM

Calling tl::parse() returns a DOM struct that borrows from the input string, which means that the string must be kept alive. The input string must outlive this DOM. If this is not acceptable or you need to keep the DOM around for longer, consider using tl::parse_owned(). VDomGuard takes ownership over the string, which means you don’t have to keep the string around.

// Notice how it takes ownership over the string:
let dom_guard = unsafe { tl::parse_owned(String::from(r#"<p id="text">Hello</p>"#)) };

// Obtain reference to underlying VDom
let dom = dom_guard.get_ref();

// Now, use `dom` as you would if it was a regular `VDom`
let element = dom.get_element_by_id("text").expect("Failed to find element");

println!("Inner text: {}", element.inner_text());

Bytes

Some methods return a Bytes struct, which is an internal struct that is used to borrow a part of the input string. This is mainly used over a raw &[u8] for its Debug implementation.

Structs

Attributes

Stores all attributes of an HTML tag, as well as additional metadata such as id and class

Bytes

A wrapper around a DST-slice

HTMLTag

Represents a single HTML element

VDom

VDom represents a Document Object Model

VDomGuard

A RAII guarded version of VDom

Enums

HTMLVersion

HTML Version (<!DOCTYPE>)

Node

An HTML Node

Traits

AsBytes

A trait for converting a type into Bytes

Functions

parse

Parses the given input string

parse_owned

Parses the given input string and returns an owned, RAII guarded DOM

Type Definitions

Tree

A list of shared HTML nodes