// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Turning a result tree into bytes, by one of the three output methods XSLT //! defines. //! //! The result of a transformation is a tree, not a string, and a good deal of //! what "the output" looks like is decided here rather than by the stylesheet: //! whether an empty element is `
` or `
`, whether the text inside //! `\n\n", "", .{ .method = .html }, ); } test "indenting leaves mixed content alone" { try expectOutput( "\n \n \n \n\n", "", .{ .method = .xml, .omit_xml_declaration = true, .indent = true }, ); // `` has a text child, so its children are written where they are. try expectOutput( "one\n", "one", .{ .method = .xml, .omit_xml_declaration = true, .indent = true }, ); } test "a doctype is written when asked for" { try expectOutput( "\n
\n", "
", .{ .method = .html, .doctype_public = "-//W3C//DTD HTML 4.01//EN", .doctype_system = "http://www.w3.org/TR/html4/strict.dtd", }, ); } test "a character above ASCII passes through whole" { // The bug this pins: `U+00A0` is 0xC2 0xA0 in UTF-8, and a serializer that // compares single bytes against 0xA0 splits it -- emitting the lead byte // and then an entity, which is not valid UTF-8 at all. try expectOutput( "a\u{00A0}b \u{2014} \u{0451}\n", "a b — ё", .{ .method = .html }, ); try expectOutput( "\u{00A0}\n", " ", .{ .method = .xml, .omit_xml_declaration = true }, ); } test "an attribute never borrows the default declaration" { // `xmlns="urn:a"` puts elements in `urn:a` and attributes nowhere: an // unprefixed attribute name is in no namespace however the default is // bound. So an attribute that really is in `urn:a` cannot be written as // `attr=` -- it needs a prefix of its own, even though the element beside // it needs none. const gpa = testing.allocator; const doc = try tree.parse(gpa, "", .{}); defer doc.destroy(); const r = doc.documentElement().?; _ = try doc.setAttribute(r, .{ .uri = "urn:a", .local = "attr" }, "1"); const out = try toOwnedSlice(gpa, doc, .{ .method = .xml, .omit_xml_declaration = true }); defer gpa.free(out); try testing.expectEqualStrings("\n", out); } test "one namespace declaration can be added without disturbing the rest" { const gpa = testing.allocator; const doc = try tree.parse(gpa, "", .{}); defer doc.destroy(); const r = doc.documentElement().?; try doc.addNsDecl(r, "b", "urn:b"); // A prefix already declared here is replaced rather than repeated. try doc.addNsDecl(r, "a", "urn:a2"); try doc.addNsDecl(r, "b", "urn:b"); const out = try toOwnedSlice(gpa, doc, .{ .method = .xml, .omit_xml_declaration = true }); defer gpa.free(out); try testing.expectEqualStrings("\n", out); } test "the HTML method escapes a URI attribute" { try expectOutput( "x\n", "x", .{ .method = .html }, ); // `name` is a URI only on an anchor, and `%` is left alone so that a value // which was already escaped is not escaped twice. try expectOutput( "\n", "", .{ .method = .html }, ); try expectOutput( "\n", "", .{ .method = .html }, ); try expectOutput( "x\n", "x", .{ .method = .html }, ); } test "an encoding that is not UTF-8 gets character references" { // Nothing here transcodes: the bytes written are always UTF-8. An encoding // that cannot carry a character is honoured by not writing one, since a // numeric reference says the same thing in every encoding a document is // likely to declare. try expectOutput( "ё—", "\u{0451}\u{2014}", .{ .method = .xml, .encoding = "ascii", .omit_xml_declaration = true, .indent = false }, ); // Spelled either way, UTF-8 passes through whole. try expectOutput( "\u{0451}", "\u{0451}", .{ .method = .xml, .encoding = "UTF8", .omit_xml_declaration = true, .indent = false }, ); // ISO 8859-1 is the one encoding that can be served without transcoding: // its 256 characters are the first 256 of Unicode, so `ü` is one byte. // Anything above that still needs a reference. try expectOutput( "\xfcё", "\u{00fc}\u{0451}", .{ .method = .xml, .encoding = "iso-8859-1", .omit_xml_declaration = true, .indent = false }, ); } test "the HTML method with version 5 writes the HTML 5 document type" { try expectOutput( "\n\n", "", .{ .method = .html, .version = "5" }, ); // Any other version, or a declaration that names an identifier, is left // to say what it says. try expectOutput("\n", "", .{ .method = .html }); try expectOutput( "\n\n", "", .{ .method = .html, .version = "5", .doctype_system = "about:legacy-compat" }, ); } test "the HTML method leaves a newline in an attribute alone" { // XML normalizes an attribute value, so a literal newline there has to be // written as a reference to survive a round trip. HTML normalizes nothing, // so there is nothing to survive. try expectOutput( "y\n", "y", .{ .method = .html }, ); try expectOutput( "", "", .{ .method = .xml, .omit_xml_declaration = true, .indent = false }, ); } test "indenting stops for a whole subtree, not just one element" { // `r` holds text, so its children are written where they stand -- and so // are `a`'s, even though `a` holds nothing but elements. libxml2 turns // formatting off for the subtree of the element that holds the text, which // is what makes a result carrying whitespace copied from its source come // out as it was built. try expectOutput( "x\n", "x", .{ .method = .xml, .omit_xml_declaration = true, .indent = true }, ); try expectOutput( "\n \n \n \n\n", "", .{ .method = .xml, .omit_xml_declaration = true, .indent = true }, ); } test "an XHTML document gets the encoding declaration HTML 4 looks for" { const xhtml = "http://www.w3.org/1999/xhtml"; try expectOutput( "\n" ++ "\n" ++ " \n" ++ " \n" ++ " t\n" ++ " \n" ++ "\n", "t", .{ .method = .xml, .omit_xml_declaration = true, .indent = true, .encoding = "US-ASCII", .doctype_public = "-//W3C//DTD XHTML 1.0 Strict//EN", .doctype_system = "x.dtd", }, ); // XHTML writes a void element as `
` -- with the space an HTML // parser needs in front of a slash it does not understand -- and // everything else with both tags, because an HTML parser would not treat // `

` as empty. The elements are in the XHTML namespace and are still // `br` and `img`, so the table is consulted by local name here. try expectOutput( "\n" ++ "

", "

", .{ .method = .xml, .omit_xml_declaration = true, .indent = false, .doctype_public = "-//W3C//DTD XHTML 1.0 Strict//EN", .doctype_system = "x.dtd", }, ); // An `html` document element in no namespace is given the XHTML // namespace: a document with an XHTML document type declaration and an // `html` root is XHTML whatever its author forgot to say. A root by any // other name gets nothing, and one already in a namespace keeps it. try expectOutput( "\n" ++ "

", "

", .{ .method = .xml, .omit_xml_declaration = true, .indent = false, .doctype_public = "-//W3C//DTD XHTML 1.0 Strict//EN", .doctype_system = "x.dtd", }, ); try expectOutput( "\n" ++ "", "", .{ .method = .xml, .omit_xml_declaration = true, .indent = false, .doctype_public = "-//W3C//DTD XHTML 1.0 Strict//EN", .doctype_system = "x.dtd", }, ); // An empty `` still gets one, by either method. try expectOutput( "\n" ++ "", "", .{ .method = .xml, .omit_xml_declaration = true, .indent = false, .doctype_public = "-//W3C//DTD XHTML 1.0 Strict//EN", .doctype_system = "x.dtd", }, ); try expectOutput( "\n", "", .{ .method = .html }, ); // A document type that is not XHTML's gets nothing inserted. try expectOutput( "\n" ++ "t", "t", .{ .method = .xml, .omit_xml_declaration = true, .indent = false, .doctype_public = "-//W3C//DTD HTML 4.01//EN", .doctype_system = "x.dtd", }, ); } test "a prefix is never undeclared" { // XML 1.0 has no way to undeclare a prefix -- only to rebind one -- so // `xmlns:p=""` is not something a reader can be given. A declaration like // that reaches the writer from a namespace alias whose target is no // namespace at all, and dropping it is the only answer that leaves the // output readable. The *default* namespace is the exception: `xmlns=""` // is legal and says the element is in no namespace. const gpa = testing.allocator; const doc = try tree.parse(gpa, "", .{}); defer doc.destroy(); const r = doc.documentElement().?; try doc.setNsDecls(r, &.{ .{ .prefix = "p", .uri = "" }, .{ .prefix = "q", .uri = "urn:q" }, }); const out = try toOwnedSlice(gpa, doc, .{ .method = .xml, .omit_xml_declaration = true }); defer gpa.free(out); try testing.expectEqualStrings("\n", out); } test "an invented namespace prefix is not leaked" { // The testing allocator fails this test on a leak, which is the whole // check: an attribute in a namespace nothing declares makes the writer // invent a prefix, and that name has to outlive the call that made it. const gpa = testing.allocator; const doc = try tree.parse(gpa, "", .{}); defer doc.destroy(); const r = doc.documentElement().?; _ = try doc.setAttribute(r, .{ .uri = "urn:a", .local = "x" }, "1"); _ = try doc.setAttribute(r, .{ .uri = "urn:b", .local = "y" }, "2"); const out = try toOwnedSlice(gpa, doc, .{ .method = .xml, .omit_xml_declaration = true }); defer gpa.free(out); try testing.expect(std.mem.indexOf(u8, out, "xmlns:ns_1=\"urn:a\"") != null); try testing.expect(std.mem.indexOf(u8, out, "xmlns:ns_2=\"urn:b\"") != null); } test "a carriage return in text is written as a reference" { // It arrived as ` ` and has to leave as one: a raw CR is normalized // back to a line feed by whatever reads the output. try expectOutput( " \n", " ", .{ .method = .xml, .omit_xml_declaration = true }, ); // A line feed is ordinary text and stays as it is. try expectOutput( "\n\n", " ", .{ .method = .xml, .omit_xml_declaration = true }, ); } test "the implicit xml prefix is never declared" { // `xml` is bound everywhere by the Namespaces specification, so an // `xml:lang` must not drag a declaration along behind it. Inventing one is // legal and nothing else does it. try expectOutput( "x\n", "x", .{ .method = .xml, .omit_xml_declaration = true }, ); } test "a newline follows a comment, and the last child, and nothing else" { const gpa = testing.allocator; const options: Output = .{ .method = .xml, .omit_xml_declaration = true }; // A result that is nothing but text still ends with a newline, which is // the case that matters most: it is every text-producing stylesheet. { const doc = try Document.create(gpa); defer doc.destroy(); _ = try doc.appendText(doc.root, "bare text"); const out = try toOwnedSlice(gpa, doc, options); defer gpa.free(out); try testing.expectEqualStrings("bare text\n", out); } // A comment in the prolog is followed by one; a processing instruction is // not, and neither is an element that is not last. try expectOutput("\n\n", "", options); try expectOutput("\n", "", options); try expectOutput("\n", "", options); try expectOutput("\n\n\n", "", options); // Asking for no indenting suppresses these too, which is a third state // and not the same as leaving it unspecified. const flat: Output = .{ .method = .xml, .omit_xml_declaration = true, .indent = false }; try expectOutput("", "", flat); try expectOutput("", "", flat); // The HTML method ends its document with a newline whatever `indent` // says, which is the one place the three states collapse back to two. try expectOutput("
\n", "
", .{ .method = .html, .indent = false }); } test "namespace declarations survive a round trip" { try expectOutput( "\n", "", .{ .method = .xml, .omit_xml_declaration = true }, ); } test "cdata-section-elements wrap text and split around a terminator" { const doc = try tree.parse(testing.allocator, "
x]]>y", .{}); defer doc.destroy(); const out = try toOwnedSlice(testing.allocator, doc, .{ .method = .xml, .omit_xml_declaration = true, .cdata_section_elements = &.{.{ .local = "raw" }}, }); defer testing.allocator.free(out); try testing.expectEqualStrings("y]]>\n", out); }