| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @emails react-core |
| 8 | */ |
| 9 | |
| 10 | 'use strict'; |
| 11 | |
| 12 | let React; |
| 13 | let ReactDOMServer; |
| 14 | |
| 15 | describe('escapeTextForBrowser', () => { |
| 16 | beforeEach(() => { |
| 17 | jest.resetModules(); |
| 18 | React = require('react'); |
| 19 | ReactDOMServer = require('react-dom/server'); |
| 20 | }); |
| 21 | |
| 22 | it('ampersand is escaped when passed as text content', () => { |
| 23 | const response = ReactDOMServer.renderToString(<span>{'&'}</span>); |
| 24 | expect(response).toMatch('<span>&</span>'); |
| 25 | }); |
| 26 | |
| 27 | it('double quote is escaped when passed as text content', () => { |
| 28 | const response = ReactDOMServer.renderToString(<span>{'"'}</span>); |
| 29 | expect(response).toMatch('<span>"</span>'); |
| 30 | }); |
| 31 | |
| 32 | it('single quote is escaped when passed as text content', () => { |
| 33 | const response = ReactDOMServer.renderToString(<span>{"'"}</span>); |
| 34 | expect(response).toMatch('<span>'</span>'); |
| 35 | }); |
| 36 | |
| 37 | it('greater than entity is escaped when passed as text content', () => { |
| 38 | const response = ReactDOMServer.renderToString(<span>{'>'}</span>); |
| 39 | expect(response).toMatch('<span>></span>'); |
| 40 | }); |
| 41 | |
| 42 | it('lower than entity is escaped when passed as text content', () => { |
| 43 | const response = ReactDOMServer.renderToString(<span>{'<'}</span>); |
| 44 | expect(response).toMatch('<span><</span>'); |
| 45 | }); |
| 46 | |
| 47 | it('number is correctly passed as text content', () => { |
| 48 | const response = ReactDOMServer.renderToString(<span>{42}</span>); |
| 49 | expect(response).toMatch('<span>42</span>'); |
| 50 | }); |
| 51 | |
| 52 | it('number is escaped to string when passed as text content', () => { |
| 53 | const response = ReactDOMServer.renderToString(<img data-attr={42} />); |
| 54 | expect(response).toMatch('<img data-attr="42"/>'); |
| 55 | }); |
| 56 | |
| 57 | it('escape text content representing a script tag', () => { |
| 58 | const response = ReactDOMServer.renderToString( |
| 59 | <span>{'<script type=\'\' src=""></script>'}</span>, |
| 60 | ); |
| 61 | expect(response).toMatch( |
| 62 | '<span><script type='' ' + |
| 63 | 'src=""></script></span>', |
| 64 | ); |
| 65 | }); |
| 66 | }); |