Skip to main content

sup_xml_core/html/
options.rs

1#![forbid(unsafe_code)]
2
3/// Options for the lenient HTML5 parser.
4///
5/// Construct via [`HtmlParseOptions::default()`] and override individual
6/// fields.  Defaults are tuned for the common case (browser-equivalent
7/// output, lenient recovery).
8///
9/// HTML parsing differs from XML in that *recovery is the normal mode*
10/// — most real-world HTML is malformed and the WHATWG spec mandates
11/// "do something sensible anyway."  `recovery_mode` therefore defaults
12/// to `true` (inverted from [`crate::ParseOptions`]).
13#[derive(Debug, Clone)]
14pub struct HtmlParseOptions {
15    /// Reject inputs deeper than this — DoS protection.  html5ever
16    /// itself has no built-in depth limit; we enforce it inside the
17    /// sink.  Default: 256.
18    pub max_element_depth: u32,
19
20    /// Maximum total bytes of accumulated text content across the
21    /// whole document.  Caps adversarial inputs that try to blow up
22    /// memory through repeated entity expansion or massive text
23    /// nodes.  Default: 10 MB.
24    pub max_text_bytes: u64,
25
26    /// Treat the parser as if scripting were enabled.  Affects how
27    /// `<noscript>` content is parsed: when `true`, `<noscript>`
28    /// content is treated as raw text and not parsed as elements
29    /// (matching what a browser with JS enabled would see).  When
30    /// `false`, `<noscript>` content is parsed normally.  Default:
31    /// `true` — most scrapers want the JS-enabled view.
32    pub scripting_enabled: bool,
33
34    /// Discard a leading byte-order mark if present in the input.
35    /// Default: `true`.
36    pub discard_bom: bool,
37
38    /// Treat input as if it came from an iframe `srcdoc` attribute.
39    /// Affects quirks-mode determination from DOCTYPE.  Default:
40    /// `false`.
41    pub iframe_srcdoc: bool,
42
43    /// Continue past parse errors instead of returning `Err`.
44    /// Recovery is the *normal* mode for HTML — most callers want
45    /// this on.  Default: `true` (inverted vs. [`crate::ParseOptions`],
46    /// which defaults to strict because XML is a strict format).
47    ///
48    /// When `false`, the first parse error reported by html5ever
49    /// causes the parser to return `Err` from `finish()`.  Useful
50    /// for HTML linters and strict validators.
51    pub recovery_mode: bool,
52
53    /// Override the WHATWG byte-stream encoding-sniffing result with
54    /// a caller-supplied label (e.g. from an HTTP `Content-Type`
55    /// header).  Wins over BOM and meta-charset detection.
56    ///
57    /// Accepts any WHATWG encoding label — `"UTF-8"`, `"windows-1252"`,
58    /// `"Shift_JIS"`, etc.  Unknown labels route through
59    /// `encoding_rs` (when the `full-encodings` feature is on).
60    ///
61    /// Default: `None` (let the sniffer decide).
62    pub encoding_override: Option<String>,
63
64    /// Maximum bytes the meta-charset prescan looks at when no
65    /// caller-supplied encoding and no BOM are present.  WHATWG
66    /// recommends 1024.  Increase only for documents with very
67    /// long `<head>` sections that push the `<meta charset>` past
68    /// the default window.
69    ///
70    /// Default: 1024.
71    pub encoding_sniff_window: usize,
72}
73
74impl Default for HtmlParseOptions {
75    fn default() -> Self {
76        Self {
77            max_element_depth: 256,
78            max_text_bytes: 10 * 1024 * 1024,
79            scripting_enabled: true,
80            discard_bom: true,
81            iframe_srcdoc: false,
82            recovery_mode: true,
83            encoding_override: None,
84            encoding_sniff_window: 1024,
85        }
86    }
87}