Created
July 5, 2018 18:11
-
-
Save callemo/cee399e96c7e9d875e5bc643fc87a71a to your computer and use it in GitHub Desktop.
Escape HTML text. Distilled from `handlebars/utils.js`.
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
const entity = { | |
"&": "&", | |
"<": "<", | |
">": ">", | |
'"': """, | |
"'": "'", | |
"`": "`", | |
"=": "=" | |
}; | |
function escapeHTML(text) { | |
if (typeof text !== "string") { | |
if (text == null) { | |
return ""; | |
} else if (!text) { | |
return text + ""; | |
} | |
text = "" + text; | |
} | |
if (!/[&<>"'`=]/.test(text)) { | |
return text; | |
} | |
return text.replace(/[&<>"'`=]/g, char => entity[char]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment