Skip to main content

Module html

Module html 

Source
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 Document that XPath/Selector/serializer all work on natively.
  • DoS-protection limits (max_element_depth, max_text_bytes) are enforced inside our TreeSink since html5ever has none built in.
  • HTML defaults to lenient (recovery_mode: true); inverted from ParseOptions because 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 HtmlReader and dispatched to HtmlSaxHandler.
options

Structs§

HtmlBytesReader
Bytes-flavoured pull reader. Same as HtmlReader but takes raw bytes — html5ever does lossy UTF-8 decoding internally.
HtmlReader
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.
HtmlSaxParser
Push-based HTML parser. Caller drives input chunks via feed; the parser dispatches events synchronously into the registered handler as they’re produced.

Traits§

HtmlSaxHandler
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 needs start_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_opts that interns element / attribute names through dict instead of an internal one. Mirrors crate::parser::parse_bytes_with_dtd_and_dict for 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_dict but also adopts a caller-supplied bumpalo::Bump arena. See crate::parser::parse_bytes_with_dtd_dict_arena for the architectural rationale.
parse_html_bytes_recovered_with_dict_arena
Like parse_html_bytes_opts_with_dict_arena but always returns the (always-produced) HTML document together with the errors html5ever recovered from. The C-ABI shim uses this to set ctxt->wellFormed (empty list ⇒ well-formed) so lxml’s recover=False raises while recover=True keeps 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.