import type { X2jOptions } from "fast-xml-parser"; import { XMLParser } from "fast-xml-parser"; import Handlebars from "handlebars"; import { helpers } from "./helpers.js"; export type XmlElement = { readonly tagName: string; readonly attributes: Record; readonly children: ReadonlyArray; readonly textContent?: string; }; export const parseHandlebarsXml = (template: string, data: unknown, partials: Record): ReadonlyArray => { return parseXml(renderHandlebars(template, data, partials)); }; export const renderHandlebars = (template: string, data: unknown, partials: Record): string => { const hbs = Handlebars.create(); // oxlint-disable-next-line typescript/no-explicit-any -- h.func is a generic Function (each helper has a different arity/signature); bridges it to Handlebars' HelperDelegate helpers.forEach((h) => hbs.registerHelper(h.name, h.func as any)); Object.entries(partials).forEach(([name, partial]) => hbs.registerPartial(name, partial)); return hbs.compile(template, { compat: true, preventIndent: true })(data); }; export function parseXmlCustom(text: string, options: Omit, "allowBooleanAttributes">): ReadonlyArray { // allowBooleanAttributes is forced off (not caller-configurable) so attribute values are always strings, matching XmlElement.attributes' declared type const parser = new XMLParser({ ...options, allowBooleanAttributes: false }); parser.addEntity("#x2F", "/"); parser.addEntity("#x3D", "="); return transformFXP(parser.parse(text)); } export const parseXml = (text: string): ReadonlyArray => transformFXP(xmlParser.parse(text)); export const parseXsd = (text: string): ReadonlyArray => transformFXP(xsdParser.parse(text.replace(/xs:/g, ""))); type FastXmlElement = Record | Record>; // Transforms fast-xml-parser format to a format that is much easier to work with function transformFXP(parsedXml: ReadonlyArray): ReadonlyArray { return parsedXml.flatMap((element) => { const key = Object.keys(element).find((k) => k !== ":@" && k !== "#text")!; if (key === "?xml") { return []; } const children = ((element[key] ?? []) as Array).filter((c) => { const key = Object.keys(c)[0]; return key !== "#text" && key !== ":@"; }); const attributes = (element[":@"] ?? {}) as Record; const textChilds: Array = ((element[key] ?? []) as Array) .filter((c) => { const key = Object.keys(c)[0]; return key === "#text"; }) .flatMap((t) => t["#text"] as unknown as string); const textContents = textChilds.flatMap((text) => { const cleaned = text.replace(/\n/g, "").replace(/\t/g, "").trim(); return cleaned ? [text] : []; }); const textContent = textContents.join("\n"); return { tagName: key, attributes: attributes, children: transformFXP(children), textContent: textContent }; }); } export function findElement(elements: ReadonlyArray, elementName: string | undefined): XmlElement | undefined { if (!elementName) { return undefined; } for (const elem of elements) { if (elem.tagName === "annotation" || elem.tagName === "attribute") { continue; } if (shouldSkipLevel(elem)) { const childElement = findElement(Array.from(elem.children), elementName); if (childElement) { return shouldSkipLevel(childElement) ? findElement(Array.from(childElement.children), elementName) : childElement; } } if (elem.attributes["name"] === elementName) { return elem; } } return undefined; } export function getChildren(elements: ReadonlyArray): ReadonlyArray { for (const elem of elements) { if (elem.tagName === "annotation" || elem.tagName === "attribute") { continue; } if (shouldSkipLevel(elem)) { const child = getChildren(Array.from(elem.children)); if (child.length > 0) { return child.flatMap((c) => (shouldSkipLevel(c) ? getChildren(Array.from(c.children)) : c)); } } return elements; } return []; } function shouldSkipLevel(tag: XmlElement): boolean { return tag.tagName === "all" || tag.tagName === "sequence" || tag.tagName === "choice" || tag.attributes["name"] === undefined; } const xmlParser = new XMLParser({ preserveOrder: true, ignoreAttributes: false, attributeNamePrefix: "", allowBooleanAttributes: false, trimValues: false, ignoreDeclaration: true, processEntities: true, htmlEntities: true, }); xmlParser.addEntity("#x2F", "/"); xmlParser.addEntity("#x3D", "="); const xsdParser = new XMLParser({ preserveOrder: true, ignoreAttributes: false, attributeNamePrefix: "", allowBooleanAttributes: false, trimValues: false, ignoreDeclaration: true, }); xsdParser.addEntity("#x2F", "/"); xsdParser.addEntity("#x3D", "=");