Last active
January 19, 2016 10:24
-
-
Save Crydust/8d41a132559ca20ea4ab to your computer and use it in GitHub Desktop.
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
// adapted from MIT licensed handlebars | |
var escape = { | |
'&': '&', | |
'<': '<', | |
'>': '>', | |
'"': '"', | |
"'": ''', | |
'`': '`', | |
'=': '=', | |
' ': ' ' | |
}; | |
var badChars = /[&<>"'`= ]/g; | |
var possible = /[&<>"'`= ]/; | |
function escapeChar(chr) { | |
return escape[chr]; | |
} | |
function escapeExpression(string) { | |
if (typeof string !== 'string') { | |
if (string == null) { | |
return ''; | |
} else if (!string) { | |
return string + ''; | |
} | |
string = '' + string; | |
} | |
if (!possible.test(string)) { | |
return string; | |
} | |
return string.replace(badChars, escapeChar); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment