open-agreements
Version:
Open-source legal template filling CLI and library
35 lines • 1.34 kB
JavaScript
/**
* Defensive XML access helpers for OOXML-style DOMs.
*
* DOCX parts in the wild may mix namespace-bound attributes/elements with
* prefixed or bare names. These helpers centralize the fallback order without
* changing each caller's null handling.
*/
/**
* Read an attribute by namespace, then optional prefix, then bare local name.
*/
export function getAttributeSafe(el, ns, localName, prefix, options) {
const useBareFallback = options?.bareFallback ?? true;
if (options?.emptyIsMissing) {
return (el.getAttributeNS(ns, localName) ||
(prefix ? el.getAttribute(`${prefix}:${localName}`) : null) ||
(useBareFallback ? el.getAttribute(localName) : null) ||
null);
}
return (el.getAttributeNS(ns, localName) ??
(prefix ? el.getAttribute(`${prefix}:${localName}`) : null) ??
(useBareFallback ? el.getAttribute(localName) : null));
}
/**
* Return the first descendant element matching namespace/local-name, or null.
*/
export function getFirstChild(parent, ns, localName) {
return parent.getElementsByTagNameNS(ns, localName).item(0);
}
/**
* Return the first descendant element matching tag name, or null.
*/
export function findChild(parent, tagName) {
return parent.getElementsByTagName(tagName).item(0);
}
//# sourceMappingURL=xml-helpers.js.map