|
| 1 | +import { indentHTML, indentLines } from '@semantic-ui/utils'; |
| 2 | + |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | + |
| 5 | +describe('HTML Utilities', () => { |
| 6 | + describe('indentLines', () => { |
| 7 | + it('should add 2 spaces of indentation by default', () => { |
| 8 | + const input = 'line 1\nline 2\nline 3'; |
| 9 | + const expected = ' line 1\n line 2\n line 3'; |
| 10 | + expect(indentLines(input)).toBe(expected); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should add custom number of spaces', () => { |
| 14 | + const input = 'line 1\nline 2'; |
| 15 | + const expected = ' line 1\n line 2'; |
| 16 | + expect(indentLines(input, 4)).toBe(expected); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should handle single line text', () => { |
| 20 | + expect(indentLines('single line')).toBe(' single line'); |
| 21 | + expect(indentLines('single line', 4)).toBe(' single line'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should handle empty string', () => { |
| 25 | + expect(indentLines('')).toBe(' '); |
| 26 | + expect(indentLines('', 4)).toBe(' '); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should handle non-string input', () => { |
| 30 | + expect(indentLines(null)).toBe(''); |
| 31 | + expect(indentLines(undefined)).toBe(''); |
| 32 | + expect(indentLines(123)).toBe(''); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should preserve existing indentation', () => { |
| 36 | + const input = ' already indented\n line 2'; |
| 37 | + const expected = ' already indented\n line 2'; |
| 38 | + expect(indentLines(input)).toBe(expected); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should work with tabs', () => { |
| 42 | + const input = 'line 1\nline 2'; |
| 43 | + expect(indentLines(input, 0)).toBe('line 1\nline 2'); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('indentHTML', () => { |
| 48 | + it('should properly indent nested HTML with default 2 spaces', () => { |
| 49 | + const input = '<div>\n<p>Content</p>\n</div>'; |
| 50 | + const expected = '<div>\n <p>Content</p>\n</div>'; |
| 51 | + expect(indentHTML(input)).toBe(expected); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should handle multiple levels of nesting', () => { |
| 55 | + const input = '<div>\n<div>\n<p>Content</p>\n</div>\n</div>'; |
| 56 | + const expected = '<div>\n <div>\n <p>Content</p>\n </div>\n</div>'; |
| 57 | + expect(indentHTML(input)).toBe(expected); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should handle void elements without increasing depth', () => { |
| 61 | + const input = '<div>\n<img src="test.jpg">\n<br>\n<input type="text">\n</div>'; |
| 62 | + const expected = '<div>\n <img src="test.jpg">\n <br>\n <input type="text">\n</div>'; |
| 63 | + expect(indentHTML(input)).toBe(expected); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should handle self-closing tags', () => { |
| 67 | + const input = '<div>\n<img src="test.jpg" />\n<component />\n</div>'; |
| 68 | + const expected = '<div>\n <img src="test.jpg" />\n <component />\n</div>'; |
| 69 | + expect(indentHTML(input)).toBe(expected); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should handle elements with opening and closing tags on same line', () => { |
| 73 | + const input = '<div>\n<p>Title</p>\n<span>Text</span>\n</div>'; |
| 74 | + const expected = '<div>\n <p>Title</p>\n <span>Text</span>\n</div>'; |
| 75 | + expect(indentHTML(input)).toBe(expected); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should handle HTML comments', () => { |
| 79 | + const input = '<div>\n<!-- Comment -->\n<p>Content</p>\n</div>'; |
| 80 | + const expected = '<div>\n <!-- Comment -->\n <p>Content</p>\n</div>'; |
| 81 | + expect(indentHTML(input)).toBe(expected); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should use custom indent string', () => { |
| 85 | + const input = '<div>\n<p>Content</p>\n</div>'; |
| 86 | + const expected = '<div>\n <p>Content</p>\n</div>'; |
| 87 | + expect(indentHTML(input, { indent: ' ' })).toBe(expected); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should use custom indent with tabs', () => { |
| 91 | + const input = '<div>\n<p>Content</p>\n</div>'; |
| 92 | + const expected = '<div>\n\t<p>Content</p>\n</div>'; |
| 93 | + expect(indentHTML(input, { indent: '\t' })).toBe(expected); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should respect startLevel option', () => { |
| 97 | + const input = '<div>\n<p>Content</p>\n</div>'; |
| 98 | + const expected = ' <div>\n <p>Content</p>\n </div>'; |
| 99 | + expect(indentHTML(input, { startLevel: 1 })).toBe(expected); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should remove empty lines by default', () => { |
| 103 | + const input = '<div>\n\n<p>Content</p>\n\n</div>'; |
| 104 | + const expected = '<div>\n <p>Content</p>\n</div>'; |
| 105 | + expect(indentHTML(input)).toBe(expected); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should preserve empty lines when trimEmptyLines is false', () => { |
| 109 | + const input = '<div>\n\n<p>Content</p>\n\n</div>'; |
| 110 | + const expected = '<div>\n \n <p>Content</p>\n \n</div>'; |
| 111 | + expect(indentHTML(input, { trimEmptyLines: false })).toBe(expected); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should handle messy indentation from template literals', () => { |
| 115 | + const input = `<div class="ui segment"> |
| 116 | +<div class="ui header">Title</div> |
| 117 | +<p>Content here</p> |
| 118 | +<div class="ui list"> |
| 119 | +<div class="item"> |
| 120 | +<img src="image.jpg" /> |
| 121 | +<div class="content">Item 1</div> |
| 122 | +</div> |
| 123 | +</div> |
| 124 | +</div>`; |
| 125 | + |
| 126 | + const expected = `<div class="ui segment"> |
| 127 | + <div class="ui header">Title</div> |
| 128 | + <p>Content here</p> |
| 129 | + <div class="ui list"> |
| 130 | + <div class="item"> |
| 131 | + <img src="image.jpg" /> |
| 132 | + <div class="content">Item 1</div> |
| 133 | + </div> |
| 134 | + </div> |
| 135 | +</div>`; |
| 136 | + |
| 137 | + expect(indentHTML(input)).toBe(expected); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should handle all void elements', () => { |
| 141 | + const voidTags = [ |
| 142 | + 'area', |
| 143 | + 'base', |
| 144 | + 'br', |
| 145 | + 'col', |
| 146 | + 'embed', |
| 147 | + 'hr', |
| 148 | + 'img', |
| 149 | + 'input', |
| 150 | + 'link', |
| 151 | + 'meta', |
| 152 | + 'param', |
| 153 | + 'source', |
| 154 | + 'track', |
| 155 | + 'wbr', |
| 156 | + ]; |
| 157 | + |
| 158 | + voidTags.forEach(tag => { |
| 159 | + const input = `<div>\n<${tag}>\n</div>`; |
| 160 | + const expected = `<div>\n <${tag}>\n</div>`; |
| 161 | + expect(indentHTML(input)).toBe(expected); |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should handle complex real-world HTML', () => { |
| 166 | + const input = `<div class="ui segment"> |
| 167 | +<div class="ui header"> |
| 168 | +Product List |
| 169 | +</div> |
| 170 | +<div class="ui list"> |
| 171 | +<div class="item"> |
| 172 | +<img src="product1.jpg" /> |
| 173 | +<div class="content"> |
| 174 | +<div class="header">Product 1</div> |
| 175 | +<div class="description">Description here</div> |
| 176 | +</div> |
| 177 | +</div> |
| 178 | +<div class="item"> |
| 179 | +<img src="product2.jpg" /> |
| 180 | +<div class="content"> |
| 181 | +<div class="header">Product 2</div> |
| 182 | +</div> |
| 183 | +</div> |
| 184 | +</div> |
| 185 | +</div>`; |
| 186 | + |
| 187 | + const result = indentHTML(input); |
| 188 | + |
| 189 | + // Verify structure by checking key lines |
| 190 | + expect(result).toContain(' <div class="ui header">'); |
| 191 | + expect(result).toContain(' <div class="item">'); |
| 192 | + expect(result).toContain(' <img src="product1.jpg" />'); |
| 193 | + expect(result).toContain(' <div class="header">Product 1</div>'); |
| 194 | + }); |
| 195 | + |
| 196 | + it('should handle non-string input', () => { |
| 197 | + expect(indentHTML(null)).toBe(''); |
| 198 | + expect(indentHTML(undefined)).toBe(''); |
| 199 | + expect(indentHTML(123)).toBe(''); |
| 200 | + }); |
| 201 | + |
| 202 | + it('should handle empty string', () => { |
| 203 | + expect(indentHTML('')).toBe(''); |
| 204 | + }); |
| 205 | + |
| 206 | + it('should not break on attributes with angle brackets in values', () => { |
| 207 | + const input = '<div>\n<input placeholder="Enter <value>">\n</div>'; |
| 208 | + const expected = '<div>\n <input placeholder="Enter <value>">\n</div>'; |
| 209 | + expect(indentHTML(input)).toBe(expected); |
| 210 | + }); |
| 211 | + }); |
| 212 | +}); |
0 commit comments