encodehtml
Version:
escaping and unescaping HTML entities – commonly needed utils to prevent XSS attacks when rendering user generated content
33 lines (32 loc) • 666 B
JavaScript
/**
* Escape special characters in the given string of html
*
* @param {String} html
* @return {String}
*
*/
module.exports = {
escape: function(html){
return String(html)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>');
},
/**
* Unescape special characters in the given string of html
*
* @param {String} html
* @return {String}
*
*/
unescape: function(html){
return String(html)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, '\'')
.replace(/</g, '<')
.replace(/>/g, '>');
}
}