|
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,156 @@ 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(''); |
| 44 | + expect(sanitizeTextForRender(undefined)).toBe(''); |
| 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( |
| 51 | + 'Multiple<br><br>Newlines' |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should escape stray < characters', () => { |
| 56 | + expect(sanitizeTextForRender('if x < 5')).toBe('if x < 5'); |
| 57 | + expect(sanitizeTextForRender('< this is not a tag')).toBe( |
| 58 | + '< this is not a tag' |
| 59 | + ); |
| 60 | + expect(sanitizeTextForRender('price < $100')).toBe('price < $100'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should escape stray > characters', () => { |
| 64 | + expect(sanitizeTextForRender('if x > 5')).toBe('if x > 5'); |
| 65 | + expect(sanitizeTextForRender('this is not a tag >')).toBe( |
| 66 | + 'this is not a tag >' |
| 67 | + ); |
| 68 | + expect(sanitizeTextForRender('score > 90%')).toBe('score > 90%'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should escape both stray < and > characters', () => { |
| 72 | + expect(sanitizeTextForRender('5 < x < 10')).toBe('5 < x < 10'); |
| 73 | + expect(sanitizeTextForRender('x > 5 && y < 10')).toBe( |
| 74 | + 'x > 5 && y < 10' |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should preserve valid HTML tags', () => { |
| 79 | + expect(sanitizeTextForRender('<div>Hello</div>')).toBe('<div>Hello</div>'); |
| 80 | + expect(sanitizeTextForRender('<span class="test">World</span>')).toBe( |
| 81 | + '<span class="test">World</span>' |
| 82 | + ); |
| 83 | + expect(sanitizeTextForRender('<br>')).toBe('<br>'); |
| 84 | + expect(sanitizeTextForRender('<img src="test.jpg" />')).toBe( |
| 85 | + '<img src="test.jpg" />' |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should preserve nested HTML tags', () => { |
| 90 | + expect(sanitizeTextForRender('<div><span>Nested</span></div>')).toBe( |
| 91 | + '<div><span>Nested</span></div>' |
| 92 | + ); |
| 93 | + expect( |
| 94 | + sanitizeTextForRender('<ul><li>Item 1</li><li>Item 2</li></ul>') |
| 95 | + ).toBe('<ul><li>Item 1</li><li>Item 2</li></ul>'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should handle mixed content with valid tags and stray characters', () => { |
| 99 | + expect(sanitizeTextForRender('Price < $100 <strong>on sale</strong>')).toBe( |
| 100 | + 'Price < $100 <strong>on sale</strong>' |
| 101 | + ); |
| 102 | + expect(sanitizeTextForRender('<p>x > 5</p> and y < 10')).toBe( |
| 103 | + '<p>x > 5</p> and y < 10' |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should handle edge cases with malformed HTML-like content', () => { |
| 108 | + expect(sanitizeTextForRender('<<invalid>>')).toBe('<<invalid>>'); |
| 109 | + expect(sanitizeTextForRender('<not a tag')).toBe('<not a tag'); |
| 110 | + expect(sanitizeTextForRender('not a tag>')).toBe('not a tag>'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should handle email addresses and URLs with angle brackets', () => { |
| 114 | + expect(sanitizeTextForRender('Contact: <user@example.com>')).toBe( |
| 115 | + 'Contact: <user@example.com>' |
| 116 | + ); |
| 117 | + expect(sanitizeTextForRender('Email me at < user@example.com >')).toBe( |
| 118 | + 'Email me at < user@example.com >' |
| 119 | + ); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should handle mathematical expressions', () => { |
| 123 | + expect(sanitizeTextForRender('if (x < y && y > z)')).toBe( |
| 124 | + 'if (x < y && y > z)' |
| 125 | + ); |
| 126 | + expect(sanitizeTextForRender('array[i] < array[j]')).toBe( |
| 127 | + 'array[i] < array[j]' |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should handle HTML entities within valid tags', () => { |
| 132 | + expect(sanitizeTextForRender('<div><escaped></div>')).toBe( |
| 133 | + '<div><escaped></div>' |
| 134 | + ); |
| 135 | + expect(sanitizeTextForRender('<span>already & escaped</span>')).toBe( |
| 136 | + '<span>already & escaped</span>' |
| 137 | + ); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should handle complex real-world email content', () => { |
| 141 | + const emailContent = `Hello,\n\nThe price is < $50 for items where quantity > 10.\n<p>Best regards,</p>\n<strong>Sales Team</strong>`; |
| 142 | + const expected = `Hello,<br><br>The price is < $50 for items where quantity > 10.<br><p>Best regards,</p><br><strong>Sales Team</strong>`; |
| 143 | + expect(sanitizeTextForRender(emailContent)).toBe(expected); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should handle quoted email content', () => { |
| 147 | + const quoted = `Original message:\n> User wrote: x < 5\n<blockquote>Previous reply</blockquote>`; |
| 148 | + const expected = `Original message:<br>> User wrote: x < 5<br><blockquote>Previous reply</blockquote>`; |
| 149 | + expect(sanitizeTextForRender(quoted)).toBe(expected); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should handle self-closing tags correctly', () => { |
| 153 | + expect(sanitizeTextForRender('<br />')).toBe('<br />'); |
| 154 | + expect(sanitizeTextForRender('<img src="test.jpg" />')).toBe( |
| 155 | + '<img src="test.jpg" />' |
| 156 | + ); |
| 157 | + expect(sanitizeTextForRender('<input type="text" value="test" />')).toBe( |
| 158 | + '<input type="text" value="test" />' |
| 159 | + ); |
| 160 | + expect(sanitizeTextForRender('<hr/>')).toBe('<hr/>'); |
| 161 | + expect(sanitizeTextForRender('Text before <br /> text after')).toBe( |
| 162 | + 'Text before <br /> text after' |
| 163 | + ); |
| 164 | + expect(sanitizeTextForRender('<meta charset="UTF-8" />')).toBe( |
| 165 | + '<meta charset="UTF-8" />' |
| 166 | + ); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should handle complex URLs in attributes', () => { |
| 170 | + expect( |
| 171 | + sanitizeTextForRender( |
| 172 | + '<img src="https://example.com/image.jpg?width=100&height=200&format=webp" />' |
| 173 | + ) |
| 174 | + ).toBe( |
| 175 | + '<img src="https://example.com/image.jpg?width=100&height=200&format=webp" />' |
| 176 | + ); |
| 177 | + expect( |
| 178 | + sanitizeTextForRender( |
| 179 | + '<a href="https://api.example.com/v2/users/123/profile?include=posts&sort=desc">Profile</a>' |
| 180 | + ) |
| 181 | + ).toBe( |
| 182 | + '<a href="https://api.example.com/v2/users/123/profile?include=posts&sort=desc">Profile</a>' |
| 183 | + ); |
| 184 | + expect( |
| 185 | + sanitizeTextForRender( |
| 186 | + '<iframe src="//cdn.example.com/embed/video/12345?autoplay=1&loop=0" />' |
| 187 | + ) |
| 188 | + ).toBe( |
| 189 | + '<iframe src="//cdn.example.com/embed/video/12345?autoplay=1&loop=0" />' |
| 190 | + ); |
| 191 | + }); |
| 192 | +}); |
0 commit comments