|
| 1 | +import { parseHelperContent } from './po-helper-content-utils'; |
| 2 | + |
| 3 | +describe('PoHelperSafeParser', () => { |
| 4 | + describe('parseHelperContent', () => { |
| 5 | + it('should return empty array for null/undefined/empty content', () => { |
| 6 | + expect(parseHelperContent(null)).toEqual([]); |
| 7 | + expect(parseHelperContent(undefined)).toEqual([]); |
| 8 | + expect(parseHelperContent('')).toEqual([]); |
| 9 | + }); |
| 10 | + |
| 11 | + it('should return single fragment for plain text without tags', () => { |
| 12 | + const result = parseHelperContent('Texto simples'); |
| 13 | + expect(result).toEqual([{ text: 'Texto simples', bold: false, italic: false, underline: false }]); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should parse bold tag correctly', () => { |
| 17 | + const result = parseHelperContent('Texto <b>negrito</b> normal'); |
| 18 | + expect(result).toEqual([ |
| 19 | + { text: 'Texto ', bold: false, italic: false, underline: false }, |
| 20 | + { text: 'negrito', bold: true, italic: false, underline: false }, |
| 21 | + { text: ' normal', bold: false, italic: false, underline: false } |
| 22 | + ]); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should parse italic tag correctly', () => { |
| 26 | + const result = parseHelperContent('Texto <i>itálico</i> normal'); |
| 27 | + expect(result).toEqual([ |
| 28 | + { text: 'Texto ', bold: false, italic: false, underline: false }, |
| 29 | + { text: 'itálico', bold: false, italic: true, underline: false }, |
| 30 | + { text: ' normal', bold: false, italic: false, underline: false } |
| 31 | + ]); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should parse underline tag correctly', () => { |
| 35 | + const result = parseHelperContent('Texto <u>sublinhado</u> normal'); |
| 36 | + expect(result).toEqual([ |
| 37 | + { text: 'Texto ', bold: false, italic: false, underline: false }, |
| 38 | + { text: 'sublinhado', bold: false, italic: false, underline: true }, |
| 39 | + { text: ' normal', bold: false, italic: false, underline: false } |
| 40 | + ]); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should parse nested tags correctly', () => { |
| 44 | + const result = parseHelperContent('<b><i>negrito e itálico</i></b>'); |
| 45 | + expect(result).toEqual([{ text: 'negrito e itálico', bold: true, italic: true, underline: false }]); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should parse multiple nested tags', () => { |
| 49 | + const result = parseHelperContent('<b><i><u>todos</u></i></b>'); |
| 50 | + expect(result).toEqual([{ text: 'todos', bold: true, italic: true, underline: true }]); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should handle mixed formatting in sequence', () => { |
| 54 | + const result = parseHelperContent('<b>negrito</b> e <i>itálico</i>'); |
| 55 | + expect(result).toEqual([ |
| 56 | + { text: 'negrito', bold: true, italic: false, underline: false }, |
| 57 | + { text: ' e ', bold: false, italic: false, underline: false }, |
| 58 | + { text: 'itálico', bold: false, italic: true, underline: false } |
| 59 | + ]); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should sanitize disallowed tags (script)', () => { |
| 63 | + const result = parseHelperContent('Texto <script>alert("xss")</script> seguro'); |
| 64 | + expect(result).toEqual([{ text: 'Texto alert("xss") seguro', bold: false, italic: false, underline: false }]); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should sanitize disallowed tags (div, span, a)', () => { |
| 68 | + const result = parseHelperContent('<div>conteúdo</div> <span>texto</span> <a href="x">link</a>'); |
| 69 | + expect(result).toEqual([{ text: 'conteúdo texto link', bold: false, italic: false, underline: false }]); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should sanitize tags with attributes', () => { |
| 73 | + const result = parseHelperContent('<b style="color:red">negrito</b>'); |
| 74 | + expect(result).toEqual([{ text: 'negrito', bold: true, italic: false, underline: false }]); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should handle unclosed tags gracefully', () => { |
| 78 | + const result = parseHelperContent('<b>negrito sem fechar'); |
| 79 | + expect(result).toEqual([{ text: 'negrito sem fechar', bold: true, italic: false, underline: false }]); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should handle extra closing tags gracefully', () => { |
| 83 | + const result = parseHelperContent('texto</b> normal'); |
| 84 | + expect(result).toEqual([ |
| 85 | + { text: 'texto', bold: false, italic: false, underline: false }, |
| 86 | + { text: ' normal', bold: false, italic: false, underline: false } |
| 87 | + ]); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should handle case-insensitive tags', () => { |
| 91 | + const result = parseHelperContent('<B>negrito</B> <I>itálico</I>'); |
| 92 | + expect(result).toEqual([ |
| 93 | + { text: 'negrito', bold: true, italic: false, underline: false }, |
| 94 | + { text: ' ', bold: false, italic: false, underline: false }, |
| 95 | + { text: 'itálico', bold: false, italic: true, underline: false } |
| 96 | + ]); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should prevent XSS via event handlers in allowed tags', () => { |
| 100 | + const result = parseHelperContent('<b onmouseover="alert(1)">texto</b>'); |
| 101 | + expect(result).toEqual([{ text: 'texto', bold: true, italic: false, underline: false }]); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should prevent XSS via img tag with onerror', () => { |
| 105 | + const result = parseHelperContent('<img src=x onerror="alert(1)">texto'); |
| 106 | + expect(result).toEqual([{ text: 'texto', bold: false, italic: false, underline: false }]); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should handle partially overlapping tags', () => { |
| 110 | + const result = parseHelperContent('<b>bold <i>bold+italic</b> italic</i>'); |
| 111 | + expect(result).toEqual([ |
| 112 | + { text: 'bold ', bold: true, italic: false, underline: false }, |
| 113 | + { text: 'bold+italic', bold: true, italic: true, underline: false }, |
| 114 | + { text: ' italic', bold: false, italic: true, underline: false } |
| 115 | + ]); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should parse <strong> as bold', () => { |
| 119 | + const result = parseHelperContent('Texto <strong>importante</strong> normal'); |
| 120 | + expect(result).toEqual([ |
| 121 | + { text: 'Texto ', bold: false, italic: false, underline: false }, |
| 122 | + { text: 'importante', bold: true, italic: false, underline: false }, |
| 123 | + { text: ' normal', bold: false, italic: false, underline: false } |
| 124 | + ]); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should parse <em> as italic', () => { |
| 128 | + const result = parseHelperContent('Texto <em>enfatizado</em> normal'); |
| 129 | + expect(result).toEqual([ |
| 130 | + { text: 'Texto ', bold: false, italic: false, underline: false }, |
| 131 | + { text: 'enfatizado', bold: false, italic: true, underline: false }, |
| 132 | + { text: ' normal', bold: false, italic: false, underline: false } |
| 133 | + ]); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should handle mixed <strong>, <em>, <b>, <i>, <u> together', () => { |
| 137 | + const result = parseHelperContent('<strong>bold</strong> <em>italic</em> <u>underline</u>'); |
| 138 | + expect(result).toEqual([ |
| 139 | + { text: 'bold', bold: true, italic: false, underline: false }, |
| 140 | + { text: ' ', bold: false, italic: false, underline: false }, |
| 141 | + { text: 'italic', bold: false, italic: true, underline: false }, |
| 142 | + { text: ' ', bold: false, italic: false, underline: false }, |
| 143 | + { text: 'underline', bold: false, italic: false, underline: true } |
| 144 | + ]); |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
0 commit comments