-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathescape_html.js
More file actions
39 lines (36 loc) · 1.04 KB
/
escape_html.js
File metadata and controls
39 lines (36 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import coerceToString from 'helper/string/coerce_to_string';
import { REGEXP_HTML_SPECIAL_CHARACTERS } from 'helper/reg_exp/const';
const 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>'
*/
export default function escapeHtml(subject) {
return coerceToString(subject).replace(REGEXP_HTML_SPECIAL_CHARACTERS, replaceSpecialCharacter);
}