UNPKG

@nodecfdi/cfdi-cleaner

Version:

Librería para limpiar comprobantes fiscales digitales v3.3 y v4.0

46 lines (45 loc) 2.16 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const ts_mixer_1 = require("ts-mixer"); const xpath_1 = __importDefault(require("xpath")); const xml_namespace_methods_1 = __importDefault(require("#src/mixins/xml_namespace_methods")); class RemoveUnusedNamespaces extends (0, ts_mixer_1.Mixin)(xml_namespace_methods_1.default) { clean(document) { for (const namespaceNode of this.iterateNonReservedNamespaces(document)) { this.checkNamespaceNode(document, namespaceNode); } } hasElementsOnNamespace(document, namespace, prefix) { const elements = xpath_1.default.select(`//*[namespace-uri()="${namespace}" and name()=concat("${prefix}", local-name())]`, // @ts-expect-error misssing Node properties are not needed document); return Boolean(elements && elements.length > 0); } hasAttributesOnNamespace(document, namespace, prefix) { const attributes = xpath_1.default.select(`//@*[namespace-uri()="${namespace}" and name()=concat("${prefix}", local-name())]`, // @ts-expect-error misssing Node properties are not needed document); return Boolean(attributes && attributes.length > 0); } checkNamespaceNode(document, namespaceNode) { const namespace = namespaceNode.nodeValue; /* istanbul ignore if -- For usage always is not null but for default nodeValue is posible null @preserve */ if (namespace === null) { return; } const localName = `${namespaceNode.localName}:`; if (!this.isPrefixedNamespaceOnUse(document, namespace, localName)) { this.removeNamespaceNodeAttribute(namespaceNode); } } isPrefixedNamespaceOnUse(document, namespace, prefix) { if (this.hasElementsOnNamespace(document, namespace, prefix)) { return true; } return this.hasAttributesOnNamespace(document, namespace, prefix); } } exports.default = RemoveUnusedNamespaces;