Skip to content

Instantly share code, notes, and snippets.

@crisward
Created December 11, 2018 11:32
Show Gist options
  • Save crisward/b61bd926d44c1e58d05f0c0c472262a4 to your computer and use it in GitHub Desktop.
Save crisward/b61bd926d44c1e58d05f0c0c472262a4 to your computer and use it in GitHub Desktop.
function fixhtml(html) {
var div, dom, hasBlockChild, needWrapping, textOrInlineChildren, treeWalker;
dom = document.createElement('div');
html = html.replace(/\s+/g, " ").replace(/> </g, "><"); // removes white space
while (html.match(/<(h[1-5]|p|strong|div|u|em|a|bi) ?[^>]*>\s?<\/\1>/g)) {
html = html.replace(/<(h[1-5]|p|strong|div|u|em|a|b|i) ?[^>]*>\s?<\/\1>/g, ""); //removes empty tags recursively, except iframes
}
dom.innerHTML = html;
Array.from(dom.querySelectorAll("h1 h1")).forEach(function(node) {
return node.outerHTML = node.innerHTML; //unwrap nested h1's
});
needWrapping = [];
// walk dom, get all block node
treeWalker = document.createTreeWalker(dom, NodeFilter.SHOW_ELEMENT, {
acceptNode: (node) => {
if (BLOCK_TAGS[node.nodeName.toLowerCase()] != null) {
return NodeFilter.FILTER_ACCEPT;
} else {
return NodeFilter.FILTER_SKIP;
}
}
});
// get all node need wrapping
while (treeWalker.nextNode()) {
div = treeWalker.currentNode;
hasBlockChild = Array.from(div.childNodes).find((node) => {
return BLOCK_TAGS[node.nodeName.toLowerCase()] != null;
});
if (hasBlockChild) {
textOrInlineChildren = Array.from(div.childNodes).filter((node) => {
return BLOCK_TAGS[node.nodeName.toLowerCase()] == null;
});
needWrapping.push(...textOrInlineChildren);
}
}
// wrap all nodes
needWrapping.forEach((tn) => {
var wrapper;
if (tn.textContent.length === 0) { // remove empty inline tags
return tn.remove();
}
wrapper = document.createElement('div');
wrapper.className = "inlinewrapper";
tn.parentNode.replaceChild(wrapper, tn);
return wrapper.appendChild(tn);
});
return dom.innerHTML.replace(/\s+/g, " ").replace(/> </g, "><").replace(/(<input[^>]+)(>)/g, "$1 /$2");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment