|
| 1 | +"""Tests for semantic HTML tag style extraction.""" |
| 2 | +import pytest |
| 3 | +from html_to_text import html_to_text |
| 4 | +from lxml import etree |
| 5 | + |
| 6 | + |
| 7 | +def test_bold_tag_creates_style(): |
| 8 | + """Test that <b> tag creates font-weight style.""" |
| 9 | + html = '<html><body><p>This is <b>bold</b> text</p></body></html>' |
| 10 | + |
| 11 | + styles_found = [] |
| 12 | + |
| 13 | + def style_callback(element, start, end): |
| 14 | + style = element.get('style') |
| 15 | + styles_found.append({ |
| 16 | + 'tag': element.tag, |
| 17 | + 'style': style, |
| 18 | + 'start': start, |
| 19 | + 'end': end |
| 20 | + }) |
| 21 | + |
| 22 | + text = html_to_text(html, style_callback=style_callback) |
| 23 | + |
| 24 | + # Should find one style node for <b> |
| 25 | + assert len(styles_found) == 1 |
| 26 | + assert 'font-weight: bold' in styles_found[0]['style'] |
| 27 | + assert styles_found[0]['tag'] == 'b' |
| 28 | + |
| 29 | + |
| 30 | +def test_italic_tag_creates_style(): |
| 31 | + """Test that <i> tag creates font-style.""" |
| 32 | + html = '<html><body><p>This is <i>italic</i> text</p></body></html>' |
| 33 | + |
| 34 | + styles_found = [] |
| 35 | + |
| 36 | + def style_callback(element, start, end): |
| 37 | + style = element.get('style') |
| 38 | + styles_found.append({ |
| 39 | + 'tag': element.tag, |
| 40 | + 'style': style, |
| 41 | + 'start': start, |
| 42 | + 'end': end |
| 43 | + }) |
| 44 | + |
| 45 | + text = html_to_text(html, style_callback=style_callback) |
| 46 | + |
| 47 | + assert len(styles_found) == 1 |
| 48 | + assert 'font-style: italic' in styles_found[0]['style'] |
| 49 | + |
| 50 | + |
| 51 | +def test_em_tag_creates_style(): |
| 52 | + """Test that <em> tag creates font-style.""" |
| 53 | + html = '<html><body><p><em>Emphasis</em> text</p></body></html>' |
| 54 | + |
| 55 | + styles_found = [] |
| 56 | + |
| 57 | + def style_callback(element, start, end): |
| 58 | + styles_found.append({'style': element.get('style')}) |
| 59 | + |
| 60 | + text = html_to_text(html, style_callback=style_callback) |
| 61 | + |
| 62 | + assert len(styles_found) == 1 |
| 63 | + assert 'font-style: italic' in styles_found[0]['style'] |
| 64 | + |
| 65 | + |
| 66 | +def test_strong_tag_creates_style(): |
| 67 | + """Test that <strong> tag creates font-weight.""" |
| 68 | + html = '<html><body><p><strong>Strong</strong> text</p></body></html>' |
| 69 | + |
| 70 | + styles_found = [] |
| 71 | + |
| 72 | + def style_callback(element, start, end): |
| 73 | + styles_found.append({'style': element.get('style')}) |
| 74 | + |
| 75 | + text = html_to_text(html, style_callback=style_callback) |
| 76 | + |
| 77 | + assert len(styles_found) == 1 |
| 78 | + assert 'font-weight: bold' in styles_found[0]['style'] |
| 79 | + |
| 80 | + |
| 81 | +def test_underline_tag_creates_style(): |
| 82 | + """Test that <u> tag creates text-decoration.""" |
| 83 | + html = '<html><body><p><u>Underlined</u> text</p></body></html>' |
| 84 | + |
| 85 | + styles_found = [] |
| 86 | + |
| 87 | + def style_callback(element, start, end): |
| 88 | + styles_found.append({'style': element.get('style')}) |
| 89 | + |
| 90 | + text = html_to_text(html, style_callback=style_callback) |
| 91 | + |
| 92 | + assert len(styles_found) == 1 |
| 93 | + assert 'text-decoration: underline' in styles_found[0]['style'] |
| 94 | + |
| 95 | + |
| 96 | +def test_nested_semantic_tags(): |
| 97 | + """Test that nested semantic tags create multiple overlapping styles.""" |
| 98 | + html = '<html><body><p>Text with <b>bold and <i>bold italic</i></b></p></body></html>' |
| 99 | + |
| 100 | + styles_found = [] |
| 101 | + |
| 102 | + def style_callback(element, start, end): |
| 103 | + styles_found.append({ |
| 104 | + 'tag': element.tag, |
| 105 | + 'style': element.get('style'), |
| 106 | + 'start': start, |
| 107 | + 'end': end, |
| 108 | + 'text_range': (start, end) |
| 109 | + }) |
| 110 | + |
| 111 | + text = html_to_text(html, style_callback=style_callback) |
| 112 | + |
| 113 | + # Should have 2 style nodes: one for <b>, one for <i> |
| 114 | + assert len(styles_found) == 2 |
| 115 | + |
| 116 | + # Find bold and italic styles |
| 117 | + bold_styles = [s for s in styles_found if 'font-weight: bold' in s['style']] |
| 118 | + italic_styles = [s for s in styles_found if 'font-style: italic' in s['style']] |
| 119 | + |
| 120 | + assert len(bold_styles) == 1 |
| 121 | + assert len(italic_styles) == 1 |
| 122 | + |
| 123 | + # The italic range should be contained within the bold range |
| 124 | + bold_start, bold_end = bold_styles[0]['start'], bold_styles[0]['end'] |
| 125 | + italic_start, italic_end = italic_styles[0]['start'], italic_styles[0]['end'] |
| 126 | + |
| 127 | + assert italic_start >= bold_start |
| 128 | + assert italic_end <= bold_end |
| 129 | + |
| 130 | + |
| 131 | +def test_multiple_separate_semantic_tags(): |
| 132 | + """Test multiple separate semantic tags in same paragraph.""" |
| 133 | + html = '<html><body><p><b>Bold</b> and <i>italic</i> and <u>underline</u></p></body></html>' |
| 134 | + |
| 135 | + styles_found = [] |
| 136 | + |
| 137 | + def style_callback(element, start, end): |
| 138 | + styles_found.append({ |
| 139 | + 'tag': element.tag, |
| 140 | + 'style': element.get('style') |
| 141 | + }) |
| 142 | + |
| 143 | + text = html_to_text(html, style_callback=style_callback) |
| 144 | + |
| 145 | + # Should have 3 separate style nodes |
| 146 | + assert len(styles_found) == 3 |
| 147 | + |
| 148 | + tags = [s['tag'] for s in styles_found] |
| 149 | + assert 'b' in tags |
| 150 | + assert 'i' in tags |
| 151 | + assert 'u' in tags |
| 152 | + |
| 153 | + |
| 154 | +def test_semantic_tag_with_existing_style_attribute(): |
| 155 | + """Test that semantic tags work alongside existing style attributes.""" |
| 156 | + html = '<html><body><p><b style="color: red">Bold red</b></p></body></html>' |
| 157 | + |
| 158 | + styles_found = [] |
| 159 | + |
| 160 | + def style_callback(element, start, end): |
| 161 | + styles_found.append({ |
| 162 | + 'tag': element.tag, |
| 163 | + 'style': element.get('style') |
| 164 | + }) |
| 165 | + |
| 166 | + text = html_to_text(html, style_callback=style_callback) |
| 167 | + |
| 168 | + # Should have TWO style callbacks: |
| 169 | + # 1. One from the original <b style="color: red"> (line 388-394) |
| 170 | + # 2. One from our semantic tag tracking (new code) |
| 171 | + assert len(styles_found) == 2 |
| 172 | + |
| 173 | + # One should have color: red, one should have font-weight: bold |
| 174 | + styles_str = ' '.join([s['style'] for s in styles_found]) |
| 175 | + assert 'color: red' in styles_str |
| 176 | + assert 'font-weight: bold' in styles_str |
| 177 | + |
| 178 | + |
| 179 | +def test_empty_semantic_tag(): |
| 180 | + """Test that empty semantic tags don't create style nodes.""" |
| 181 | + html = '<html><body><p><b></b>text</p></body></html>' |
| 182 | + |
| 183 | + styles_found = [] |
| 184 | + |
| 185 | + def style_callback(element, start, end): |
| 186 | + styles_found.append({'tag': element.tag}) |
| 187 | + |
| 188 | + text = html_to_text(html, style_callback=style_callback) |
| 189 | + |
| 190 | + # Empty tag should not create a style node (start == end check) |
| 191 | + assert len(styles_found) == 0 |
| 192 | + |
| 193 | + |
| 194 | +def test_semantic_tags_positions(): |
| 195 | + """Test that style positions match text positions correctly.""" |
| 196 | + html = '<html><body><p>Start <b>bold</b> end</p></body></html>' |
| 197 | + |
| 198 | + styles_found = [] |
| 199 | + |
| 200 | + def style_callback(element, start, end): |
| 201 | + styles_found.append({ |
| 202 | + 'start': start, |
| 203 | + 'end': end |
| 204 | + }) |
| 205 | + |
| 206 | + text = html_to_text(html, style_callback=style_callback) |
| 207 | + |
| 208 | + # Text should be "Start bold end" |
| 209 | + assert text.strip() == "Start bold end" |
| 210 | + |
| 211 | + # The style should cover "bold" which starts at position 6 (after "Start ") |
| 212 | + assert len(styles_found) == 1 |
| 213 | + styled_text = text[styles_found[0]['start']:styles_found[0]['end']] |
| 214 | + assert styled_text == "bold" |
| 215 | + |
| 216 | + |
| 217 | +def test_no_style_callback_no_processing(): |
| 218 | + """Test that semantic tags don't cause errors when no callback provided.""" |
| 219 | + html = '<html><body><p><b>Bold</b> text</p></body></html>' |
| 220 | + |
| 221 | + # Should not raise any errors |
| 222 | + text = html_to_text(html) |
| 223 | + assert text.strip() == "Bold text" |
| 224 | + |
| 225 | + |
| 226 | +def test_strikethrough_tags(): |
| 227 | + """Test strikethrough tags (<s>, <strike>, <del>).""" |
| 228 | + html = '<html><body><p><s>Strike</s> <del>Deleted</del></p></body></html>' |
| 229 | + |
| 230 | + styles_found = [] |
| 231 | + |
| 232 | + def style_callback(element, start, end): |
| 233 | + styles_found.append({ |
| 234 | + 'tag': element.tag, |
| 235 | + 'style': element.get('style') |
| 236 | + }) |
| 237 | + |
| 238 | + text = html_to_text(html, style_callback=style_callback) |
| 239 | + |
| 240 | + assert len(styles_found) == 2 |
| 241 | + for style_info in styles_found: |
| 242 | + assert 'text-decoration: line-through' in style_info['style'] |
| 243 | + |
| 244 | + |
| 245 | +def test_semantic_tags_with_callback(): |
| 246 | + """Integration test: semantic tags with style callback.""" |
| 247 | + html = ''' |
| 248 | + <html> |
| 249 | + <body> |
| 250 | + <h1>Test</h1> |
| 251 | + <p>This is a <b>test</b></p> |
| 252 | + <p><em>Emphasis</em> <i>italic</i></p> |
| 253 | + <p><span style="font-weight: bold">Bold using styles</span></p> |
| 254 | + </body> |
| 255 | + </html> |
| 256 | + ''' |
| 257 | + |
| 258 | + nodes = [] |
| 259 | + |
| 260 | + def callback(element, start, end): |
| 261 | + style = element.get('style', '') |
| 262 | + if 'bold' in style or 'italic' in style: |
| 263 | + nodes.append({ |
| 264 | + 'start': start, |
| 265 | + 'end': end, |
| 266 | + 'style': style |
| 267 | + }) |
| 268 | + |
| 269 | + text = html_to_text(html, style_callback=callback) |
| 270 | + |
| 271 | + # Should find at least 4 style nodes: <b>, <em>, <i>, and <span> |
| 272 | + assert len(nodes) >= 4 |
0 commit comments