Skip to content

Commit

Permalink
Merge pull request #800 from vincentarelbundock/issue576
Browse files Browse the repository at this point in the history
issue #576: text input for font_size
  • Loading branch information
vincentarelbundock committed Dec 11, 2023
2 parents 05b7cf8 + e44f22d commit 2f1e765
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
3 changes: 2 additions & 1 deletion R/kable_styling.R
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,9 @@ htmlTable_styling <- function(kable_input,
kable_xml_style <- xml_attr(kable_xml, "style")
}
if (!is.null(font_size)) {
if (is.numeric(font_size)) font_size <- paste0(font_size, "px")
kable_xml_style <- c(kable_xml_style,
paste0("font-size: ", font_size, "px;"))
paste0("font-size: ", font_size, ";"))
kable_caption_node <- xml_tpart(kable_xml, "caption")
if (!is.null(kable_caption_node)) {
xml_attr(kable_caption_node, "style") <- "font-size: initial !important;"
Expand Down
3 changes: 2 additions & 1 deletion R/row_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ xml_cell_style <- function(x, bold, italic, monospace,
"text-align: ", align, ";")
}
if (!is.null(font_size)) {
if (is.numeric(font_size)) font_size <- paste0(font_size, "px")
xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
"font-size: ", font_size, "px;")
"font-size: ", font_size, ";")
}
if (!is.null(angle)) {
xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
Expand Down
34 changes: 34 additions & 0 deletions tests/testthat/_snaps/row_spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,37 @@
\bottomrule
\end{tabular}

# Issue #576: string font_size in html

Code
cat(data.frame(a = c("foo", "bar", "foo", "bar", "foo", "bar")) %>% kbl() %>%
row_spec(3, font_size = "xx-large"))
Output
<table>
<thead>
<tr>
<th style="text-align:left;"> a </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;"> foo </td>
</tr>
<tr>
<td style="text-align:left;"> bar </td>
</tr>
<tr>
<td style="text-align:left;font-size: xx-large;"> foo </td>
</tr>
<tr>
<td style="text-align:left;"> bar </td>
</tr>
<tr>
<td style="text-align:left;"> foo </td>
</tr>
<tr>
<td style="text-align:left;"> bar </td>
</tr>
</tbody>
</table>

11 changes: 11 additions & 0 deletions tests/testthat/test-row_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,15 @@ test_that("awesome table example", {
row_spec(c(1:7, 11:12) - 1, extra_latex_after = "\\rowcolor{gray!6}") %>%
collapse_rows(1, latex_hline = "none")
)
})



test_that("Issue #576: string font_size in html", {
expect_snapshot(
data.frame(a = c("foo", "bar", "foo", "bar", "foo", "bar")) %>%
kbl() %>%
row_spec(3, font_size = "xx-large") |>
cat()
)
})

0 comments on commit 2f1e765

Please sign in to comment.