voca
Version:
The ultimate JavaScript string library
47 lines (40 loc) • 1.15 kB
JavaScript
;
require('./internal/is_nil.js');
require('./is_string.js');
var coerce_to_string = require('./internal/coerce_to_string.js');
var _const = require('./internal/const.js');
var escapeCharactersMap = {
'<': '<',
'>': '>',
'&': '&',
'"': '"',
"'": ''',
'`': '`'
};
/**
* Return the escaped version of `character`.
*
* @ignore
* @param {string} character The character to be escape.
* @return {string} The escaped version of character.
*/
function replaceSpecialCharacter(character) {
return escapeCharactersMap[character];
}
/**
* Escapes HTML special characters <code>< > & ' " `</code> in <code>subject</code>.
*
* @function escapeHtml
* @static
* @since 1.0.0
* @memberOf Escape
* @param {string} [subject=''] The string to escape.
* @return {string} Returns the escaped string.
* @example
* v.escapeHtml('<p>wonderful world</p>');
* // => '<p>wonderful world</p>'
*/
function escapeHtml(subject) {
return coerce_to_string.coerceToString(subject).replace(_const.REGEXP_HTML_SPECIAL_CHARACTERS, replaceSpecialCharacter);
}
module.exports = escapeHtml;