voca
Version:
The ultimate JavaScript string library
45 lines (39 loc) • 1.14 kB
JavaScript
import './internal/is_nil.js';
import './is_string.js';
import { c as coerceToString } from './internal/coerce_to_string.js';
import { e as REGEXP_HTML_SPECIAL_CHARACTERS } from './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 coerceToString(subject).replace(REGEXP_HTML_SPECIAL_CHARACTERS, replaceSpecialCharacter);
}
export default escapeHtml;