UNPKG

@nodecfdi/cfdi-cleaner

Version:

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

38 lines (37 loc) 1.7 kB
import { Mixin } from 'ts-mixer'; import xpath from 'xpath'; import XmlAttributeMethods from '#src/mixins/xml_attribute_methods'; import XmlElementMethods from '#src/mixins/xml_element_methods'; import XmlNamespaceMethods from '#src/mixins/xml_namespace_methods'; export default class RemoveNonSatNamespacesNodes extends Mixin(XmlAttributeMethods, XmlElementMethods, XmlNamespaceMethods) { clean(document) { const namespaces = this.obtainNamespacesFromDocument(document); for (const namespace of namespaces) { if (!this.isNamespaceRelatedToSat(namespace)) { this.removeElementsWithNamespace(document, namespace); this.removeAttributesWithNamespace(document, namespace); } } } obtainNamespacesFromDocument(document) { const namespaces = []; for (const namespaceNode of this.iterateNonReservedNamespaces(document)) { namespaces.push(namespaceNode.nodeValue); } return namespaces.filter((v, index, a) => a.indexOf(v) === index); } removeElementsWithNamespace(document, namespace) { // @ts-expect-error misssing Node properties are not needed const elements = xpath.select(`//*[namespace-uri()="${namespace}"]`, document); for (const element of elements) { this.elementRemove(element); } } removeAttributesWithNamespace(document, namespace) { // @ts-expect-error misssing Node properties are not needed const attributes = xpath.select(`//@*[namespace-uri()="${namespace}"]`, document); for (const attribute of attributes) { this.attributeRemove(attribute); } } }