UNPKG

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
/** * Escape special characters in the given string of html * * @param {String} html * @return {String} * */ module.exports = { escape: function(html){ return String(html) .replace(/&/g, '&amp;') .replace(/"/g, '&quot;') .replace(/'/g, '&#39;') .replace(/</g, '&lt;') .replace(/>/g, '&gt;'); }, /** * Unescape special characters in the given string of html * * @param {String} html * @return {String} * */ unescape: function(html){ return String(html) .replace(/&amp;/g, '&') .replace(/&quot;/g, '"') .replace(/&#39;/g, '\'') .replace(/&lt;/g, '<') .replace(/&gt;/g, '>'); } }