|
1 | | -import { parseBoolean } from '../src'; |
| 1 | +import { parseBoolean, sanitizeTextForRender } from '../src'; |
2 | 2 |
|
3 | 3 | describe('#parseBoolean', () => { |
4 | 4 | test('returns true for input "true"', () => { |
@@ -37,3 +37,90 @@ describe('#parseBoolean', () => { |
37 | 37 | expect(parseBoolean(undefined)).toBe(false); |
38 | 38 | }); |
39 | 39 | }); |
| 40 | + |
| 41 | +describe('#sanitizeTextForRender', () => { |
| 42 | + it('should handle null and undefined values', () => { |
| 43 | + expect(sanitizeTextForRender(null)).toBe(null); |
| 44 | + expect(sanitizeTextForRender(undefined)).toBe(undefined); |
| 45 | + expect(sanitizeTextForRender('')).toBe(''); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should convert newlines to <br> tags', () => { |
| 49 | + expect(sanitizeTextForRender('Line 1\nLine 2')).toBe('Line 1<br>Line 2'); |
| 50 | + expect(sanitizeTextForRender('Multiple\n\nNewlines')).toBe('Multiple<br><br>Newlines'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should escape stray < characters', () => { |
| 54 | + expect(sanitizeTextForRender('if x < 5')).toBe('if x < 5'); |
| 55 | + expect(sanitizeTextForRender('< this is not a tag')).toBe('< this is not a tag'); |
| 56 | + expect(sanitizeTextForRender('price < $100')).toBe('price < $100'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should escape stray > characters', () => { |
| 60 | + expect(sanitizeTextForRender('if x > 5')).toBe('if x > 5'); |
| 61 | + expect(sanitizeTextForRender('this is not a tag >')).toBe('this is not a tag >'); |
| 62 | + expect(sanitizeTextForRender('score > 90%')).toBe('score > 90%'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should escape both stray < and > characters', () => { |
| 66 | + expect(sanitizeTextForRender('5 < x < 10')).toBe('5 < x < 10'); |
| 67 | + expect(sanitizeTextForRender('x > 5 && y < 10')).toBe('x > 5 && y < 10'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should preserve valid HTML tags', () => { |
| 71 | + expect(sanitizeTextForRender('<div>Hello</div>')).toBe('<div>Hello</div>'); |
| 72 | + expect(sanitizeTextForRender('<span class="test">World</span>')).toBe('<span class="test">World</span>'); |
| 73 | + expect(sanitizeTextForRender('<br>')).toBe('<br>'); |
| 74 | + expect(sanitizeTextForRender('<img src="test.jpg" />')).toBe('<img src="test.jpg" />'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should preserve nested HTML tags', () => { |
| 78 | + expect(sanitizeTextForRender('<div><span>Nested</span></div>')).toBe('<div><span>Nested</span></div>'); |
| 79 | + expect(sanitizeTextForRender('<ul><li>Item 1</li><li>Item 2</li></ul>')) |
| 80 | + .toBe('<ul><li>Item 1</li><li>Item 2</li></ul>'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should handle mixed content with valid tags and stray characters', () => { |
| 84 | + expect(sanitizeTextForRender('Price < $100 <strong>on sale</strong>')) |
| 85 | + .toBe('Price < $100 <strong>on sale</strong>'); |
| 86 | + expect(sanitizeTextForRender('<p>x > 5</p> and y < 10')) |
| 87 | + .toBe('<p>x > 5</p> and y < 10'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should handle edge cases with malformed HTML-like content', () => { |
| 91 | + expect(sanitizeTextForRender('<<invalid>>')).toBe('<<invalid>>'); |
| 92 | + expect(sanitizeTextForRender('<not a tag')).toBe('<not a tag'); |
| 93 | + expect(sanitizeTextForRender('not a tag>')).toBe('not a tag>'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should handle email addresses and URLs with angle brackets', () => { |
| 97 | + expect(sanitizeTextForRender('Contact: <[email protected]>')) |
| 98 | + .toBe('Contact: <[email protected]>'); |
| 99 | + expect(sanitizeTextForRender('Email me at < [email protected] >')) |
| 100 | + .toBe('Email me at < [email protected] >'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should handle mathematical expressions', () => { |
| 104 | + expect(sanitizeTextForRender('if (x < y && y > z)')).toBe('if (x < y && y > z)'); |
| 105 | + expect(sanitizeTextForRender('array[i] < array[j]')).toBe('array[i] < array[j]'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should handle HTML entities within valid tags', () => { |
| 109 | + expect(sanitizeTextForRender('<div><escaped></div>')) |
| 110 | + .toBe('<div><escaped></div>'); |
| 111 | + expect(sanitizeTextForRender('<span>already & escaped</span>')) |
| 112 | + .toBe('<span>already & escaped</span>'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should handle complex real-world email content', () => { |
| 116 | + const emailContent = `Hello,\n\nThe price is < $50 for items where quantity > 10.\n<p>Best regards,</p>\n<strong>Sales Team</strong>`; |
| 117 | + const expected = `Hello,<br><br>The price is < $50 for items where quantity > 10.<br><p>Best regards,</p><br><strong>Sales Team</strong>`; |
| 118 | + expect(sanitizeTextForRender(emailContent)).toBe(expected); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should handle quoted email content', () => { |
| 122 | + const quoted = `Original message:\n> User wrote: x < 5\n<blockquote>Previous reply</blockquote>`; |
| 123 | + const expected = `Original message:<br>> User wrote: x < 5<br><blockquote>Previous reply</blockquote>`; |
| 124 | + expect(sanitizeTextForRender(quoted)).toBe(expected); |
| 125 | + }); |
| 126 | +}); |
0 commit comments