Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return new Promise((resolve, reject) => {
// Create the SAX parser.
const parser = new SAXParser(true);
// Parsed is the full parsed object. Current is the current node being parsed. Stack is the current stack of
// nodes leading to the current one.
let parsed: INode, current: INode;
const stack: INode[] = [];
// On error: Reject the promise.
parser.onerror = reject;
// On text nodes: If it is all whitespace, do nothing. Otherwise, try to convert to a number and add as a child.
parser.ontext = text => {
if (allWhitespaceRegex.test(text)) {
if (current && current.attributes && current.attributes['xml:space'] === 'preserve') {
if (!current.children) current.children = [];
current.children.push(text);
}