Skip to main content

parse_fragment

Function parse_fragment 

Source
pub fn parse_fragment(markup: &str) -> Result<ParsedFragment, String>
Expand description

Parse a markup chunk that may be a document FRAGMENT — several sibling nodes, or bare text — and return its owning document together with the top-level nodes to insert. Returning the two together is deliberate: libxml Nodes are handles into the document that owns them, so the caller must keep the [Document] alive for exactly as long as it uses the nodes.

Intentional divergence from Perl (OXIDIZED_DESIGN #66). Perl’s Common::XML::Parser::parseChunk is explicitly single-node — its own comment reads “This expects only a single node, not a document fragment” — and LaTeXML ships no fragment parser at all, so a Perl binding has to wrap its own markup. Yet Document::appendTree already has an XML_DOCUMENT_FRAG_NODE branch (ported at document.rs): the INSERTION half understands fragments perfectly well, Perl simply never feeds it one. Accepting them here inserts more of the author’s content correctly and can never emit fewer errors than Perl, so it is a safe extension rather than a parity break.

Strategy: parse as-is FIRST, so every single-root chunk — including one led by an XML declaration, which may not be preceded by anything — behaves exactly as parse_chunk always did; only if that fails do we retry inside a throwaway wrapper. Recovery stays OFF in both attempts, so genuinely malformed markup (<p>unclosed, a bare &, an undeclared &nbsp;) is still rejected rather than silently salvaged.

Namespaces are the caller’s business, as in Perl: nodes that declare none land in no namespace. Markup destined for <ltx:rawhtml> must therefore carry its own xmlns (or an xhtml: prefix), exactly as it must in a Perl binding.