@nodecfdi/cfdi-cleaner
Version:
Librería para limpiar comprobantes fiscales digitales v3.3 y v4.0
42 lines (41 loc) • 1.88 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const cfdi_core_1 = require("@nodecfdi/cfdi-core");
const xpath_1 = __importDefault(require("xpath"));
const constants_1 = require("#src/utils/constants");
/**
* This provides methods used for xml namespaces handling. It´s not meant to
* be used directly.
*/
class XmlNamespaceMethods {
*iterateNonReservedNamespaces(document) {
const namespaceNodes = xpath_1.default.evaluate("(//*|//@*)[local-name(.)='xmlns' or starts-with(name(), 'xmlns')]",
// @ts-expect-error misssing Node properties are not needed
document, null, 0, null);
let namespaceNode = namespaceNodes.iterateNext();
while (namespaceNode) {
if (!this.isNamespaceReserved(`${namespaceNode.nodeValue}`) && (0, cfdi_core_1.isAttribute)(namespaceNode)) {
yield namespaceNode;
}
namespaceNode = namespaceNodes.iterateNext();
}
}
removeNamespaceNodeAttribute(namespaceNode) {
const { ownerElement, localName, nodeName } = namespaceNode;
/* istanbul ignore else -- For usage always is has attribute ns but for default bool is posible false @preserve */
if (ownerElement.hasAttributeNS(cfdi_core_1.NAMESPACE.XMLNS, `${localName}`)) {
ownerElement.removeAttribute(nodeName);
}
}
isNamespaceReserved(namespace) {
const reservedNameSpaces = [cfdi_core_1.NAMESPACE.XML, cfdi_core_1.NAMESPACE.XMLNS, constants_1.namespaceXsi];
return reservedNameSpaces.includes(namespace);
}
isNamespaceRelatedToSat(namespace) {
return namespace.startsWith('http://www.sat.gob.mx/');
}
}
exports.default = XmlNamespaceMethods;