Crate facet_html_dom

Crate facet_html_dom 

Source
Expand description

Typed HTML element definitions for use with facet-html.

This crate provides Facet-derived types for all standard HTML5 elements, allowing you to parse and serialize HTML documents with full type safety.

§Quick Start

use facet_html_dom::{Html, Body, Div, P, FlowContent};

// Parse an HTML document
let html_source = r#"
    <!DOCTYPE html>
    <html>
        <body>
            <div class="container">
                <p>Hello, world!</p>
            </div>
        </body>
    </html>
"#;

let doc: Html = facet_html::from_str(html_source).unwrap();

// Access the parsed structure
if let Some(body) = &doc.body {
    for child in &body.children {
        if let FlowContent::Div(div) = child {
            println!("Found div with class: {:?}", div.attrs.class);
        }
    }
}

// Serialize back to HTML
let output = facet_html::to_string_pretty(&doc).unwrap();

§Content Models

HTML elements are organized by their content model:

  • FlowContent - Block and inline elements that can appear in <body>, <div>, etc.
  • PhrasingContent - Inline elements that can appear in <p>, <span>, <a>, etc.

These enums allow mixed content with proper nesting validation at the type level.

§Global Attributes

All elements include GlobalAttrs via the attrs field, which provides:

  • Standard attributes: id, class, style, lang, dir, etc.
  • Common event handlers: onclick, onchange, onfocus, etc.
  • An extra field that captures unknown attributes like data-* and aria-*

§Custom Elements

Unknown HTML elements (like <my-component> or syntax highlighting tags like <a-k>) are captured via CustomElement and CustomPhrasingElement, preserving their tag names, attributes, and children during parse/serialize roundtrips.

§Element Categories

Elements are organized by category:

Structs§

A
Hyperlink.
Abbr
Abbreviation.
Address
Address/contact information.
Article
Self-contained article.
Aside
Sidebar content.
Audio
Audio player.
B
Bold.
Base
Base URL for relative URLs.
Bdi
Bidirectional isolation.
Bdo
Bidirectional override.
Blockquote
Block quotation.
Body
The document body containing visible content.
Br
Line break.
Button
Button.
Canvas
Canvas for graphics.
Caption
Table caption.
Cite
Citation.
Code
Code fragment.
Col
Table column.
Colgroup
Column group.
CustomElement
A custom HTML element with a dynamic tag name.
CustomPhrasingElement
A custom phrasing element with a dynamic tag name.
Data
Data with machine-readable value.
Datalist
Datalist.
Dd
Description details.
Details
Details disclosure widget.
Dfn
Definition term.
Dialog
Dialog box.
Div
Generic container (block).
Dl
Description list.
Dt
Description term.
Em
Emphasis.
Fieldset
Fieldset grouping.
Figcaption
Figure caption.
Figure
Figure with optional caption.
Footer
Page or section footer.
Form
Form.
GlobalAttrs
Global attributes that can appear on any HTML element.
H1
Level 1 heading.
H2
Level 2 heading.
H3
Level 3 heading.
H4
Level 4 heading.
H5
Level 5 heading.
H6
Level 6 heading.
Head
The document head containing metadata.
Header
Page header.
Hr
Horizontal rule (thematic break).
Html
The root HTML document element.
I
Italic.
Iframe
Inline frame.
Img
Image.
Input
Input control.
Kbd
Keyboard input.
Label
Form label.
Legend
Fieldset legend.
Li
List item.
Link
External resource link.
Main
Main content area.
Mark
Highlighted text.
Meta
Document metadata.
Meter
Meter/gauge.
Nav
Navigation section.
Noscript
Noscript fallback.
Object
Embedded object.
Ol
Ordered list.
Optgroup
Option group.
OptionElement
Option in a select.
Output
Output.
P
Paragraph.
Picture
Picture element for art direction.
Pre
Preformatted text.
Progress
Progress indicator.
Q
Inline quotation.
Ruby
Ruby annotation (for East Asian typography).
S
Strikethrough (no longer accurate).
Samp
Sample output.
Script
Script.
Section
Generic section.
Select
Select dropdown.
Slot
Slot for web components.
Small
Small print.
Source
Media source.
Span
Generic container (inline).
Strong
Strong importance.
Style
Inline stylesheet.
Sub
Subscript.
Summary
Details summary.
Sup
Superscript.
Svg
SVG root element (simplified).
Table
Table.
Tbody
Table body.
Td
Table data cell.
Template
Template.
Textarea
Textarea.
Tfoot
Table foot.
Th
Table header cell.
Thead
Table head.
Time
Time.
Title
The document title.
Tr
Table row.
Track
Text track for video/audio.
U
Underline.
Ul
Unordered list.
Var
Variable.
Video
Video player.
Wbr
Word break opportunity.

Enums§

FlowContent
Flow content - most block and inline elements.
PhrasingContent
Phrasing content - inline elements and text.