Skip to content

Commit

Permalink
Merge pull request posit-dev#308 from posit-dev/fix-group-heading-row…
Browse files Browse the repository at this point in the history
…-invalid-html

Ensure that group label rows have valid HTML
  • Loading branch information
rich-iannone committed Apr 22, 2024
2 parents b9ad5e0 + 8f1f538 commit b4d102a
Show file tree
Hide file tree
Showing 6 changed files with 867 additions and 855 deletions.
24 changes: 14 additions & 10 deletions great_tables/_utils_render_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,13 +463,15 @@ def create_body_component_h(data: GTData) -> str:
"gt_empty_group_heading" if group_label == "" else "gt_group_heading_row"
)

body_cells.append(
f"<tr class={group_class}>"
f' <th class="gt_group_heading" colspan="{colspan_value}">'
+ group_label
+ "</th></tr>"
)
group_row = f""" <tr class="{group_class}">
<th class="gt_group_heading" colspan="{colspan_value}">{group_label}</th>
</tr>"""

prev_group_label = group_label

body_rows.append(group_row)

# Create a single cell and append result to `body_cells`
for colinfo in column_vars:
cell_content: Any = _get_cell(tbl_data, i, colinfo.var)
cell_str: str = str(cell_content)
Expand Down Expand Up @@ -498,18 +500,20 @@ def create_body_component_h(data: GTData) -> str:
cell_styles = ""

if is_stub_cell:
body_cells.append(' <th class="gt_row gt_left gt_stub">' + cell_str + "</th>")
body_cells.append(f""" <th class="gt_row gt_left gt_stub">{cell_str}</th>""")
else:
body_cells.append(
f' <td {cell_styles}class="gt_row gt_{cell_alignment}">' + cell_str + "</td>"
f""" <td {cell_styles}class="gt_row gt_{cell_alignment}">{cell_str}</td>"""
)

prev_group_label = group_label
body_rows.append("<tr>\n" + "\n".join(body_cells) + "\n</tr>")
body_rows.append(" <tr>\n" + "\n".join(body_cells) + "\n </tr>")

all_body_rows = "\n".join(body_rows)

return f'<tbody class="gt_table_body">\n{all_body_rows}\n</tbody>'
return f"""<tbody class="gt_table_body">
{all_body_rows}
</tbody>"""


def create_source_notes_component_h(data: GTData) -> str:
Expand Down
104 changes: 54 additions & 50 deletions tests/__snapshots__/test_export.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -64,56 +64,60 @@
<th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="currency">currency</th>
</tr>
<tbody class="gt_table_body">
<tr>
<tr class=gt_group_heading_row> <th class="gt_group_heading" colspan="4">grp_a</th></tr>
<th class="gt_row gt_left gt_stub">row_1</th>
<td class="gt_row gt_right">0.11</td>
<td class="gt_row gt_left">apricot</td>
<td class="gt_row gt_right">$49.95</td>
</tr>
<tr>
<th class="gt_row gt_left gt_stub">row_2</th>
<td class="gt_row gt_right">2.22</td>
<td class="gt_row gt_left">banana</td>
<td class="gt_row gt_right">$17.95</td>
</tr>
<tr>
<th class="gt_row gt_left gt_stub">row_3</th>
<td class="gt_row gt_right">33.33</td>
<td class="gt_row gt_left">coconut</td>
<td class="gt_row gt_right">$1.39</td>
</tr>
<tr>
<th class="gt_row gt_left gt_stub">row_4</th>
<td class="gt_row gt_right">444.40</td>
<td class="gt_row gt_left">durian</td>
<td class="gt_row gt_right">$65,100.00</td>
</tr>
<tr>
<tr class=gt_group_heading_row> <th class="gt_group_heading" colspan="4">grp_b</th></tr>
<th class="gt_row gt_left gt_stub">row_5</th>
<td class="gt_row gt_right">5,550.00</td>
<td class="gt_row gt_left"><NA></td>
<td class="gt_row gt_right">$1,325.81</td>
</tr>
<tr>
<th class="gt_row gt_left gt_stub">row_6</th>
<td class="gt_row gt_right">nan</td>
<td class="gt_row gt_left">fig</td>
<td class="gt_row gt_right">$13.26</td>
</tr>
<tr>
<th class="gt_row gt_left gt_stub">row_7</th>
<td class="gt_row gt_right">777,000.00</td>
<td class="gt_row gt_left">grapefruit</td>
<td class="gt_row gt_right"><NA></td>
</tr>
<tr>
<th class="gt_row gt_left gt_stub">row_8</th>
<td class="gt_row gt_right">8,880,000.00</td>
<td class="gt_row gt_left">honeydew</td>
<td class="gt_row gt_right">$0.44</td>
</tr>
<tr class="gt_group_heading_row">
<th class="gt_group_heading" colspan="4">grp_a</th>
</tr>
<tr>
<th class="gt_row gt_left gt_stub">row_1</th>
<td class="gt_row gt_right">0.11</td>
<td class="gt_row gt_left">apricot</td>
<td class="gt_row gt_right">$49.95</td>
</tr>
<tr>
<th class="gt_row gt_left gt_stub">row_2</th>
<td class="gt_row gt_right">2.22</td>
<td class="gt_row gt_left">banana</td>
<td class="gt_row gt_right">$17.95</td>
</tr>
<tr>
<th class="gt_row gt_left gt_stub">row_3</th>
<td class="gt_row gt_right">33.33</td>
<td class="gt_row gt_left">coconut</td>
<td class="gt_row gt_right">$1.39</td>
</tr>
<tr>
<th class="gt_row gt_left gt_stub">row_4</th>
<td class="gt_row gt_right">444.40</td>
<td class="gt_row gt_left">durian</td>
<td class="gt_row gt_right">$65,100.00</td>
</tr>
<tr class="gt_group_heading_row">
<th class="gt_group_heading" colspan="4">grp_b</th>
</tr>
<tr>
<th class="gt_row gt_left gt_stub">row_5</th>
<td class="gt_row gt_right">5,550.00</td>
<td class="gt_row gt_left"><NA></td>
<td class="gt_row gt_right">$1,325.81</td>
</tr>
<tr>
<th class="gt_row gt_left gt_stub">row_6</th>
<td class="gt_row gt_right">nan</td>
<td class="gt_row gt_left">fig</td>
<td class="gt_row gt_right">$13.26</td>
</tr>
<tr>
<th class="gt_row gt_left gt_stub">row_7</th>
<td class="gt_row gt_right">777,000.00</td>
<td class="gt_row gt_left">grapefruit</td>
<td class="gt_row gt_right"><NA></td>
</tr>
<tr>
<th class="gt_row gt_left gt_stub">row_8</th>
<td class="gt_row gt_right">8,880,000.00</td>
<td class="gt_row gt_left">honeydew</td>
<td class="gt_row gt_right">$0.44</td>
</tr>
</tbody>
<tfoot class="gt_sourcenotes">

Expand Down
Loading

0 comments on commit b4d102a

Please sign in to comment.