Expand description
Lenient HTML5 parser.
Wraps html5ever — the canonical
Rust HTML5 parser from Servo — driving it into our arena
Document so the rest of SupXML
(XPath, Selector, serializer) “just works” on the result.
Gated behind the html Cargo feature: users who don’t need HTML
parsing pay no compile-time or dep-tree cost.
§Quick start
use sup_xml_core::html::parse_html_str;
let html = r#"<html><body><p>hello <b>world</b></p></body></html>"#;
let doc = parse_html_str(html).unwrap();
assert!(doc.is_html());§Design
- html5ever for spec-compliant HTML5 parsing (~95% of html5lib-tests).
- Output is the arena
Documentthat XPath/Selector/serializer all work on natively. - DoS-protection limits (
max_element_depth,max_text_bytes) are enforced inside ourTreeSinksince html5ever has none built in. - HTML defaults to lenient (
recovery_mode: true); inverted fromParseOptionsbecause HTML is a lenient format by spec.
Because html5ever implements the WHATWG/HTML5 spec exactly (the same
algorithm browsers use), recovery of malformed input can differ from
libxml2’s older ad-hoc HTML parser — e.g. a stray </body> is
recovered silently and a stray </p> inserts an implied <p>, where
libxml2 flags the former and drops the latter. This is a deliberate
divergence (our output matches browsers); see the sup-xml-compat
crate docs, “Behavioral divergences”, for the affected lxml tests.
Re-exports§
pub use encoding::decode_html_input;pub use encoding::prescan_meta_charset;pub use encoding::sniff_html_encoding;pub use events::HtmlAttribute;pub use events::HtmlAttrs;pub use events::HtmlAttrsIter;pub use events::HtmlEvent;pub use options::HtmlParseOptions;
Modules§
- encoding
- WHATWG byte-stream encoding sniffing for HTML input.
- events
- Event types emitted by
HtmlReaderand dispatched toHtmlSaxHandler. - options
Structs§
- Html
Bytes Reader - Bytes-flavoured pull reader. Same as
HtmlReaderbut takes raw bytes — html5ever does lossy UTF-8 decoding internally. - Html
Reader - Pull-based HTML event reader (XmlReader twin). Iterate events
with
next(); the reader feeds the next 8 KiB chunk into html5ever whenever its internal queue runs dry. - Html
SaxParser - Push-based HTML parser. Caller drives input chunks via
feed; the parser dispatches events synchronously into the registered handler as they’re produced.
Traits§
- Html
SaxHandler - Callback trait for the push-based
HtmlSaxParser. All methods have default no-op implementations — implementers override only the events they care about (e.g. an<a href>extractor only needsstart_element).
Functions§
- parse_
html_ bytes - Parse an HTML document from raw bytes into the arena DOM.
- parse_
html_ bytes_ opts - Parse HTML bytes into the arena DOM with explicit options.
- parse_
html_ ⚠bytes_ opts_ with_ dict - Variant of
parse_html_bytes_optsthat interns element / attribute names throughdictinstead of an internal one. Mirrorscrate::parser::parse_bytes_with_dtd_and_dictfor the HTML side — used by the C-ABI layer to share a parser context’s dict with the resulting document. - parse_
html_ ⚠bytes_ opts_ with_ dict_ arena - As
parse_html_bytes_opts_with_dictbut also adopts a caller-suppliedbumpalo::Bumparena. Seecrate::parser::parse_bytes_with_dtd_dict_arenafor the architectural rationale. - parse_
html_ ⚠bytes_ recovered_ with_ dict_ arena - Like
parse_html_bytes_opts_with_dict_arenabut always returns the (always-produced) HTML document together with the errors html5ever recovered from. The C-ABI shim uses this to setctxt->wellFormed(empty list ⇒ well-formed) so lxml’srecover=Falseraises whilerecover=Truekeeps the repaired tree. - parse_
html_ bytes_ with_ recovered - Bytes equivalent of
parse_html_str_with_recovered. - parse_
html_ str - Parse an HTML document from a UTF-8 string into the arena DOM.
- parse_
html_ str_ opts - Parse an HTML string into the arena DOM with explicit options.
- parse_
html_ str_ with_ recovered - Parse an HTML document from a string, returning both the document and the
list of recovered well-formedness errors. The document is returned even if
errors were recovered; callers can inspect the
Vec<XmlError>to see what was repaired.