|
| 1 | +"""Tests for TableData.to_html() method.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from gslides_api.agnostic.element import TableData |
| 6 | + |
| 7 | + |
| 8 | +class TestTableDataToHtml: |
| 9 | + def test_basic_table(self): |
| 10 | + td = TableData(headers=["Name", "Value"], rows=[["Alice", "100"], ["Bob", "200"]]) |
| 11 | + result = td.to_html() |
| 12 | + assert "<table>" in result |
| 13 | + assert "<thead>" in result |
| 14 | + assert "<tbody>" in result |
| 15 | + assert "<th>Name</th>" in result |
| 16 | + assert "<th>Value</th>" in result |
| 17 | + assert "<td>Alice</td>" in result |
| 18 | + assert "<td>100</td>" in result |
| 19 | + assert "<td>Bob</td>" in result |
| 20 | + assert "<td>200</td>" in result |
| 21 | + |
| 22 | + def test_with_css_class(self): |
| 23 | + td = TableData(headers=["A"], rows=[["1"]]) |
| 24 | + result = td.to_html(css_class="dtbl") |
| 25 | + assert '<table class="dtbl">' in result |
| 26 | + |
| 27 | + def test_without_css_class(self): |
| 28 | + td = TableData(headers=["A"], rows=[["1"]]) |
| 29 | + result = td.to_html() |
| 30 | + assert "<table>" in result |
| 31 | + assert "class=" not in result |
| 32 | + |
| 33 | + def test_empty_headers(self): |
| 34 | + td = TableData(headers=[], rows=[]) |
| 35 | + assert td.to_html() == "" |
| 36 | + |
| 37 | + def test_html_escaping(self): |
| 38 | + td = TableData( |
| 39 | + headers=["<script>", "A&B"], |
| 40 | + rows=[["x < y", '"quoted"']], |
| 41 | + ) |
| 42 | + result = td.to_html() |
| 43 | + assert "<script>" in result |
| 44 | + assert "A&B" in result |
| 45 | + assert "x < y" in result |
| 46 | + assert ""quoted"" in result |
| 47 | + assert "<script>" not in result # must be escaped |
| 48 | + |
| 49 | + def test_css_class_escaping(self): |
| 50 | + td = TableData(headers=["A"], rows=[["1"]]) |
| 51 | + result = td.to_html(css_class='x" onclick="alert(1)') |
| 52 | + # The double quote must be escaped so it can't break out of the attribute |
| 53 | + assert '"' in result |
| 54 | + assert 'class="x" onclick="alert(1)"' in result |
| 55 | + |
| 56 | + def test_short_row_pads_with_empty(self): |
| 57 | + td = TableData(headers=["A", "B", "C"], rows=[["only_one"]]) |
| 58 | + result = td.to_html() |
| 59 | + assert "<td>only_one</td>" in result |
| 60 | + assert result.count("<td></td>") == 2 |
| 61 | + |
| 62 | + def test_structure_order(self): |
| 63 | + td = TableData(headers=["H"], rows=[["R"]]) |
| 64 | + result = td.to_html() |
| 65 | + # Verify structural ordering |
| 66 | + assert result.index("<thead>") < result.index("</thead>") |
| 67 | + assert result.index("</thead>") < result.index("<tbody>") |
| 68 | + assert result.index("<tbody>") < result.index("</tbody>") |
| 69 | + assert result.index("</tbody>") < result.index("</table>") |
0 commit comments