Add js lib function for escaping HTML
This commit is contained in:
20
js/lib/escape.js
Normal file
20
js/lib/escape.js
Normal file
@@ -0,0 +1,20 @@
|
||||
// https://stackoverflow.com/a/6020820
|
||||
|
||||
// List of HTML entities for escaping.
|
||||
const htmlEscapes = {
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
"'": ''',
|
||||
'/': '/',
|
||||
}
|
||||
|
||||
const htmlEscaper = /[&<>"'\/]/g
|
||||
|
||||
// Escape a string for HTML interpolation.
|
||||
const escape = string => {
|
||||
return ('' + string).replace(htmlEscaper, match => htmlEscapes[match])
|
||||
}
|
||||
|
||||
export default escape
|
Reference in New Issue
Block a user