Created
June 21, 2016 11:34
-
-
Save akirattii/f7ccc57a799f851b1cdabc5262927b29 to your computer and use it in GitHub Desktop.
HTML Sanitizer
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 Sanitizer = { | |
encode: function(str) { | |
if (!str) return; | |
str = "" + str; | |
return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, '''); | |
}, | |
decode: function(str) { | |
if (!str) return; | |
str = "" + str; | |
return str.replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, '\'').replace(/&/g, '&'); | |
} | |
}; | |
var encoded = Sanitizer.encode("<html>"); | |
var decoded = Sanitizer.decode(encoded); | |
console.log(encoded, decoded); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment