MCPcopy
hub / github.com/facebook/react / escapeHtml

Function escapeHtml

packages/react-dom-bindings/src/server/escapeTextForBrowser.js:53–99  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

51 */
52
53function 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 = '&quot;';
73 break;
74 case 38: // &
75 escape = '&amp;';
76 break;
77 case 39: // '
78 escape = '&#x27;'; // modified from escape-html; used to be '&#39'
79 break;
80 case 60: // <
81 escape = '&lt;';
82 break;
83 case 62: // >
84 escape = '&gt;';
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/**

Callers 1

escapeTextForBrowserFunction · 0.85

Calls 1

checkHtmlStringCoercionFunction · 0.90

Tested by

no test coverage detected