* Escapes special characters and HTML entities in a given html string. * * @param {string} string HTML string to escape for later insertion * @return {string} * @public
(string: string)
| 51 | */ |
| 52 | |
| 53 | function escapeHtml(string: string) { |
| 54 | if (__DEV__) { |
| 55 | checkHtmlStringCoercion(string); |
| 56 | } |
| 57 | const str = '' + string; |
| 58 | const match = matchHtmlRegExp.exec(str); |
| 59 | |
| 60 | if (!match) { |
| 61 | return str; |
| 62 | } |
| 63 | |
| 64 | let escape; |
| 65 | let html = ''; |
| 66 | let index; |
| 67 | let lastIndex = 0; |
| 68 | |
| 69 | for (index = match.index; index < str.length; index++) { |
| 70 | switch (str.charCodeAt(index)) { |
| 71 | case 34: // " |
| 72 | escape = '"'; |
| 73 | break; |
| 74 | case 38: // & |
| 75 | escape = '&'; |
| 76 | break; |
| 77 | case 39: // ' |
| 78 | escape = '''; // modified from escape-html; used to be ''' |
| 79 | break; |
| 80 | case 60: // < |
| 81 | escape = '<'; |
| 82 | break; |
| 83 | case 62: // > |
| 84 | escape = '>'; |
| 85 | break; |
| 86 | default: |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | if (lastIndex !== index) { |
| 91 | html += str.slice(lastIndex, index); |
| 92 | } |
| 93 | |
| 94 | lastIndex = index + 1; |
| 95 | html += escape; |
| 96 | } |
| 97 | |
| 98 | return lastIndex !== index ? html + str.slice(lastIndex, index) : html; |
| 99 | } |
| 100 | // end code copied and modified from escape-html |
| 101 | |
| 102 | /** |
no test coverage detected