9 releases (breaking)

0.7.0 Nov 14, 2023
0.6.1 Apr 22, 2023
0.5.3 Apr 22, 2023
0.5.2 Aug 21, 2022
0.4.0 Mar 19, 2022

#122 in Text editors

Download history 205/week @ 2023-12-14 39/week @ 2023-12-21 15/week @ 2023-12-28 274/week @ 2024-01-04 325/week @ 2024-01-11 408/week @ 2024-01-18 416/week @ 2024-01-25 465/week @ 2024-02-01 232/week @ 2024-02-08 224/week @ 2024-02-15 164/week @ 2024-02-22 187/week @ 2024-02-29 288/week @ 2024-03-07 234/week @ 2024-03-14 297/week @ 2024-03-21 185/week @ 2024-03-28

1,025 downloads per month
Used in 4 crates (3 directly)

MIT license

52KB
933 lines

crate-badge downloads-badge License check-badge

HTML Editor

Pure and simple HTML parser and editor.

Examples

Parse HTML segment/document

let document: Vec<Node> = parse("<!doctype html><html><head></head><body></body></html>")?;
println!("{:#?}", document);

Output:

[
    Doctype(
        Html,
    ),
    Element {
        name: "html",
        attrs: {},
        children: [
            Element {
                name: "head",
                attrs: {},
                children: [],
            },
            Element {
                name: "body",
                attrs: {},
                children: [],
            },
        ],
    },
]

You can also use try_parse to parse the html which contains tolerable errors

let document: Vec<Node> = try_parse("<div><span>Ipsum</div>");

Query an element / elements by selector

// let html = r#"..."#
let nodes: Vec<Node> = parse(html)?;
let selector: Selector = Selector::from(".box");

let element: Option<Element> = nodes.query(&selector);
let elements: Vec<Element> = nodes.query_all(&selector);

Edit the HTML

// let html = r#"..."#
let a: String = parse(html)?.trim().html();
let b: String = parse(html)?.insert_to(&selector, node).html();
let c: String = parse(html)?.remove_by(&selector).html();
let d: String = parse(html)?.replace_with(&selector, |el| Node::Comment(el.html())).html();

You can find more examples in the documentation.

Changelog

See in CHANGELOG.md

No runtime deps