// SPDX-License-Identifier: EUPL-1.2 // Canonical text of a parsed document, hashed into `contentSha256`. Two files that differ // only in serialisation (byte order mark, line endings, indentation, namespace prefixes, // attribute order, quote style, character references) have the same canonical text. // UBL has no mixed content, so character data between child elements is ignored. import type { XmlNode } from "./xml"; export function canonicalContent(root: XmlNode): string { const out: string[] = []; const visit = (node: XmlNode): void => { out.push("<", `{${node.namespace}}${node.local}`); for (const name of Object.keys(node.attrs).sort()) out.push(" ", name, "=", JSON.stringify(node.attrs[name])); out.push(">"); if (node.children.length === 0) out.push(JSON.stringify(node.text)); else for (const child of node.children) visit(child); out.push(""); }; visit(root); return out.join(""); }