pub struct SerializeOptions {
pub write_xml_decl: bool,
pub format: bool,
pub indent: String,
pub html_mode: bool,
pub xhtml: bool,
pub out_charset: OutputCharset,
}Expand description
Options that control how a Document is serialized back to XML text.
§Example
use sup_xml_core::{parse_str, serialize_with, SerializeOptions, ParseOptions};
let doc = parse_str("<root><child/></root>", &ParseOptions::default()).unwrap();
let opts = SerializeOptions { format: true, ..SerializeOptions::default() };
println!("{}", serialize_with(&doc, &opts));Fields§
§write_xml_decl: boolEmit <?xml version="…" encoding="…"?> as the first line.
Default: true. Has no effect when html_mode
is on (HTML5 has no XML declaration).
format: boolPretty-print with newlines and indentation. When false (the
default), whitespace text nodes are preserved exactly as parsed.
When true, whitespace-only text nodes between elements are dropped
and fresh indentation is added. Default: false.
indent: StringThe string used for one indentation level when format is true.
Default: two spaces (" ").
html_mode: boolEmit HTML5-style serialization rather than XML. Differences:
- Void elements (
<br>,<img>,<input>, …) emit with no end tag and no self-closing slash —<br>not<br/>. - Raw-text elements (
<script>,<style>) emit their text content verbatim — no entity escaping. This is required because the HTML5 tokenizer treats those tags’ contents as literal text up to the close tag. - Empty non-void elements emit as
<div></div>, not<div/>. HTML5 doesn’t support self-closing on non-void elements; browsers parse<div/>as<div>with no close. - Boolean attributes use shorthand:
<input disabled>instead of<input disabled="">or<input disabled="disabled">. Detected when the attribute value is empty or matches the attribute name (case-insensitive) — these are spec-equivalent forms in HTML5. - The XML declaration is suppressed regardless of
write_xml_decl. - If the document carries
html_metadatawith a captured DOCTYPE, that DOCTYPE is emitted as the first line. For documents without a stored DOCTYPE we don’t fabricate one — caller’s responsibility to insert<!DOCTYPE html>if they want it.
Default: false. Turn on when emitting output meant to be
consumed by browsers / HTML parsers rather than XML parsers.
xhtml: boolSerialize per libxml2’s xhtmlNodeDumpOutput (XML syntax with
XHTML accommodations), used when the document’s DOCTYPE identifies
it as XHTML. Distinct from html_mode: output
is still well-formed XML, but a root <html> carrying no namespace
gains xmlns="http://www.w3.org/1999/xhtml", an empty non-void
element is written <tag></tag> (browsers mis-parse <tag/>), and
an empty void element (br, img, …) is written <tag />.
Default: false.
out_charset: OutputCharsetThe charset the output bytes will ultimately be encoded into.
Characters it cannot represent are written as numeric character
references in text / attribute content — matching libxml2, where
a non-UTF-8 output encoding (or no encoding at all, which defaults
to ASCII) escapes anything outside its repertoire. Default:
OutputCharset::Utf8 (no extra escaping).
Trait Implementations§
Source§impl Clone for SerializeOptions
impl Clone for SerializeOptions
Source§fn clone(&self) -> SerializeOptions
fn clone(&self) -> SerializeOptions
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more