Skip to content

Instantly share code, notes, and snippets.

@jonmaim
Last active March 20, 2017 11:58
Show Gist options
  • Save jonmaim/ca8b9b6597ea46e5b88e5ba508c0a1e5 to your computer and use it in GitHub Desktop.
Save jonmaim/ca8b9b6597ea46e5b88e5ba508c0a1e5 to your computer and use it in GitHub Desktop.
Karma tests jqlite element HTML beautifier - adapted from http://stackoverflow.com/a/26361620/418831
function format(node, level) {
var indentBefore = new Array(level++ + 1).join(' ');
var indentAfter = new Array(level - 1).join(' ');
var textNode;
for (var i = 0; i < node.children.length; i++) {
textNode = document.createTextNode('\n' + indentBefore);
node.insertBefore(textNode, node.children[i]);
format(node.children[i], level);
if (node.lastElementChild === node.children[i]) {
textNode = document.createTextNode('\n' + indentAfter);
node.appendChild(textNode);
}
}
return node;
}
function jqlite2html(str) {
if (typeof str !== 'string') { str = str.html(); }
var div = document.createElement('div');
div.innerHTML = str.trim();
return format(div, 0).innerHTML;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment