@nodecfdi/cfdi-cleaner
Version:
Librería para limpiar comprobantes fiscales digitales v3.3 y v4.0
44 lines (43 loc) • 2.13 kB
JavaScript
;
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_attribute_methods_1 = __importDefault(require("#src/mixins/xml_attribute_methods"));
const xml_element_methods_1 = __importDefault(require("#src/mixins/xml_element_methods"));
const xml_namespace_methods_1 = __importDefault(require("#src/mixins/xml_namespace_methods"));
class RemoveNonSatNamespacesNodes extends (0, ts_mixer_1.Mixin)(xml_attribute_methods_1.default, xml_element_methods_1.default, xml_namespace_methods_1.default) {
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_1.default.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_1.default.select(`//@*[namespace-uri()="${namespace}"]`, document);
for (const attribute of attributes) {
this.attributeRemove(attribute);
}
}
}
exports.default = RemoveNonSatNamespacesNodes;