UNPKG

449 BJavaScriptView Raw
1// @flow
2// Based on _.escape https://github.com/lodash/lodash/blob/master/escape.js
3const reUnescapedHtml = /[&<>"']/g;
4const reHasUnescapedHtml = RegExp(reUnescapedHtml.source);
5
6const htmlEscapes = {
7 '&': '&amp;',
8 '<': '&lt;',
9 '>': '&gt;',
10 '"': '&quot;',
11 "'": '&#39;',
12};
13
14export function escapeHTML(s: string): string {
15 if (reHasUnescapedHtml.test(s)) {
16 return s.replace(reUnescapedHtml, c => htmlEscapes[c]);
17 }
18
19 return s;
20}