niso-xml
Generated from crates/niso-xml/CONTRACT.md when the site was built at 7aa23e2.
What parse and serialize promise, and what a Builder or a Tree
must guarantee for them.
Parsing
Section titled “Parsing”- XML 1.0 and Namespaces in XML 1.0.
parseaccepts exactly the well-formed, namespace-well-formed documents, and reports the first error of any other with Expat’s message (the one Firefox’s error documents show) and its line and column. Line breaks are normalized first (CR LF and CR become LF) and a leading byte order mark is dropped; a character XML’s Char production doesn’t allow is an error wherever it is. - No DTD processing beyond the internal subset’s entities. The internal subset’s general entities are expanded where they’re referenced, in content (where their text is parsed as markup) and in attribute values; the five predefined entities and character references always work. External entities aren’t read (a reference to one expands to nothing in content and is an error in an attribute value), a reference to an unparsed (NDATA) entity is an error, and other declarations are skipped, so no default attributes come from a DTD. As in Expat, declarations after a parameter entity reference are skipped too (it isn’t read), and when the doctype has an external subset or the internal subset a parameter entity reference, a reference to an undeclared entity is skipped unless the document is standalone; otherwise it is an error. A doctype with one of the XHTML or MathML DTDs’ public IDs (as Firefox’s catalog has them) defines HTML’s named character references, from markup5ever’s table.
- Limits. Entity references nest at most
MAX_ENTITY_DEPTH(32) deep. Their output (the replacement texts read, andNODE_COST(128) for each node and attribute built from one) may be at mostMAX_AMPLIFICATION(100) times the document’s size, orAMPLIFICATION_THRESHOLD(8 MiB) if that is more, as Expat’s amplification limit. Past either, the parse fails. Parsing doesn’t recurse per element, so a document may nest as deep as it likes, and its attribute and namespace checks take linear time in the number of attributes. - What the builder sees.
documentonce, then each node is created and appended to its parent before anything after it in the source: the doctype, comments and processing instructions around the document element are the document’s children. Character data between markup is one text node (references included); a CDATA section is a node of its own. The XML declaration makes no node. Element and attribute names come resolved: namespace (None for none), prefix and local name; namespace declarations are attributes in the XMLNS namespace,xmlnswith no prefix andxmlns:pwith the prefixxmlns. - On an error the builder holds the nodes made before it, appended where they belong; the caller decides what to do with them.
Serializing
Section titled “Serializing”serializeis DOM Parsing’s “produce an XML serialization”, with two changes WPT tests: an attribute namedxmlnsin no namespace declares the default namespace as one in the XMLNS namespace does, and a prefix for an attribute whose namespace has none in scope is always generated (ns1,ns2, …). Withrequire_well_formed, it returnsNotWellFormedwhere the standard throws.- It keeps a stack of its own, so a tree may be as deep as it likes.
What a Tree guarantees
Section titled “What a Tree guarantees”- Nodes are handles that stay valid, and the tree doesn’t change, during a
serialization.
kindis one ofKind’s for every node. childrenare a node’s children in order;template_contentsis an HTML template element’s contents (a document fragment), which the serialization uses in place of its children.nameandattributesgive namespaces as None for no namespace, never empty, with namespace declarations among the attributes in the XMLNS namespace.datais a text node’s, CDATA section’s, comment’s or processing instruction’s data;targeta processing instruction’s target;doctypea doctype’s name, public ID and system ID.
Bindings
Section titled “Bindings”- C: none of its own; the core’s
niso_dom_implementation_parse_documentandniso_node_xml_serialization(idl/dom-parsing.webidl, [NisoNative]) call this crate. - Glue: glue/dom-parsing.js, DOMParser and XMLSerializer over those two,
the optional dom-parsing section (
glue-dom-parsing). XMLHttpRequest’s documents call parseDocument from its own section.