Last active
March 20, 2017 11:58
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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