forked from auth0/node-samlp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrim_xml.js
More file actions
28 lines (21 loc) · 755 Bytes
/
trim_xml.js
File metadata and controls
28 lines (21 loc) · 755 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"use strict";
const xmldom = require("xmldom");
const DOMParser = xmldom.DOMParser;
const XMLSerializer = xmldom.XMLSerializer;
const whitespace = /^\s+$/;
function removeEmptyNodes(node) {
for (let index = 0; index < node.childNodes.length; index += 1) {
const current = node.childNodes[index];
if (current.nodeType === 3 && whitespace.test(current.nodeValue)) {
node.removeChild(current);
} else if (current.nodeType === 1) {
removeEmptyNodes(current); //remove whitespace on child element's children
}
}
}
module.exports = function trimXML(xml) {
const dom = new DOMParser().parseFromString(xml);
const serializer = new XMLSerializer();
removeEmptyNodes(dom);
return serializer.serializeToString(dom);
};