Improve escapeHTML

Replacing one char after another is not a best practice, due to the order dependency of replacing, xss risk and performance.

Fix #1577
This commit is contained in:
Alberto Santini
2017-02-10 18:15:07 +01:00
committed by GitHub
parent 2b494398f2
commit 0a4ca56da9

View File

@@ -1,10 +1,16 @@
function escapeHTML(str) {
return str.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;")
.replace(/\//g,"&#x2F;");
var escaped = {
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#039;",
"/": "&#x2F;"
};
return str.replace(/[&<>'"\/]/g, function(m) {
return escaped[m];
});
}
function randomId() {