How to close html tags in JavaScript?
-
17-04-2021 - |
Question
I'm taking html content out of my database and displaying it on a page.
Unfortunately these pages often have unclosed html tags and cause problems later when the page is rendered.
I was wondering if there is a JavaScript implementation of something like tidy or htmlpurifier.
Basically some software which can preferably close html tags in a string.
Edit: I'm not in a browser environment (node.js)
Solution
As you tagged your question with node.js:
npm install tidy
OTHER TIPS
Something like this could work:
function tidy(htmldata) {
var d = document.createElement('div');
d.innerHTML = htmldata;
return d.innerHTML;
}
http://code.google.com/p/google-caja/wiki/JsHtmlSanitizer balances tags.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow