UNPKG

xmlapi

Version:

a common interface for Node and browser native XML APIs

137 lines (136 loc) 4.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NS_XML = 'http://www.w3.org/XML/1998/namespace'; //////////////////////////////////////////////////////////////////////////////// function wrappedOrNull(ctor, val) { return val ? new ctor(val) : null; } exports.wrappedOrNull = wrappedOrNull; //////////////////////////////////////////////////////////////////////////////// /** class decorator, see http://www.typescriptlang.org/docs/handbook/mixins.html */ function mixin(...baseCtors) { return derivedCtor => { for (let baseCtor of baseCtors) { for (let name of Object.getOwnPropertyNames(baseCtor.prototype)) { if (name !== 'constructor' && !derivedCtor.prototype.hasOwnProperty(name)) { Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name)); } } } }; } exports.mixin = mixin; //////////////////////////////////////////////////////////////////////////////// function countGenerated(generator) { let i = 0; while (!generator.next().done) { ++i; } return i; } exports.countGenerated = countGenerated; //////////////////////////////////////////////////////////////////////////////// function ithGenerated(generator, index) { let cur = generator.next(); while (index-- && !cur.done) { cur = generator.next(); } return cur.value; } exports.ithGenerated = ithGenerated; //////////////////////////////////////////////////////////////////////////////// function isOddball(value) { return value === null || value === undefined; } exports.isOddball = isOddball; //////////////////////////////////////////////////////////////////////////////// // edited from https://github.com/vkiryukhin/pretty-data function prettify(xmlstr, gentle = false) { let shift = ['\n']; // array of shifts // initialize array with shifts // for (let i = 0; i < 100; ++i) { shift.push(shift[i] + ' '); } let ar = xmlstr .replace(/>\s+</g, '> <') .replace(/</g, '~::~<') .replace(/xmlns\:/g, '~::~xmlns:') .replace(/xmlns\=/g, '~::~xmlns=') .split('~::~'); let inComment = false; let deep = 0; let str = ''; for (let i = 0; i < ar.length; i++) { // start comment or <![CDATA[...]]> or <!DOCTYPE // if (ar[i].search(/<!/) > -1) { str += shift[deep] + ar[i]; inComment = true; // end comment or <![CDATA[...]]> // if (ar[i].search(/-->/) > -1 || ar[i].search(/\]>/) > -1 || ar[i].search(/!DOCTYPE/) > -1) { inComment = false; } } else // end comment or <![CDATA[...]]> // if (ar[i].search(/-->/) > -1 || ar[i].search(/\]>/) > -1) { str += ar[i]; inComment = false; } else // <elm></elm> // if (/^<\w/.exec(ar[i - 1]) && /^<\/\w/.exec(ar[i]) && /^<[\w:\-\.\,]+/.exec(ar[i - 1])[0] === /^<\/[\w:\-\.\,]+/.exec(ar[i])[0].replace('/', '')) { str += ar[i]; if (!inComment) { --deep; } } else // <elm> // if (ar[i].search(/<\w/) > -1 && ar[i].search(/<\//) === -1 && ar[i].search(/\/>/) === -1) { if (inComment) { str += ar[i]; } else if (gentle && !/\s$/.test(ar[i - 1])) { str += ar[i]; ++deep; } else { str += shift[deep++] + ar[i]; } } else // <elm>...</elm> // if (ar[i].search(/<\w/) > -1 && ar[i].search(/<\//) > -1) { str = !inComment ? str += shift[deep] + ar[i] : str += ar[i]; } else // </elm> // if (ar[i].search(/<\//) > -1) { str = !inComment ? str += shift[--deep] + ar[i] : str += ar[i]; } else // <elm/> // if (ar[i].search(/\/>/) > -1) { str = !inComment ? str += shift[deep] + ar[i] : str += ar[i]; } else // <? xml ... ?> // if (ar[i].search(/<\?/) > -1) { str += shift[deep] + ar[i]; } else // xmlns // if (ar[i].search(/xmlns\:/) > -1 || ar[i].search(/xmlns\=/) > -1) { str += shift[deep] + ar[i]; } else { str += ar[i]; } } if (str[0] === '\n') { str = str.slice(1); } str = str.replace(/\s+\n/g, '\n'); return str; } exports.prettify = prettify;