Skip to content

Commit c8d24e3

Browse files
authored
Merge pull request #1125 from rstudio/font-fam-fix
Fix for multiword font-family values and `as_raw_html(inline_css = TRUE)`
2 parents 0abdbec + dd2d185 commit c8d24e3

File tree

3 files changed

+60
-18
lines changed

3 files changed

+60
-18
lines changed

R/render_as_html.R

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,36 @@ render_as_html <- function(data) {
3737
table_defs <- get_table_defs(data = data)
3838

3939
# Compose the HTML table
40-
as.character(
41-
htmltools::tags$table(
42-
class = "gt_table",
43-
style = table_defs$table_style,
44-
caption_component,
45-
table_defs$table_colgroups,
46-
heading_component,
47-
columns_component,
48-
body_component,
49-
source_notes_component,
50-
footnotes_component
51-
)
40+
finalize_html_table(
41+
class = "gt_table",
42+
style = table_defs$table_style,
43+
caption_component,
44+
table_defs$table_colgroups,
45+
heading_component,
46+
columns_component,
47+
body_component,
48+
source_notes_component,
49+
footnotes_component
5250
)
5351
}
52+
53+
finalize_html_table <- function(
54+
class,
55+
style,
56+
...) {
57+
58+
html_tbl <-
59+
as.character(
60+
htmltools::tags$table(
61+
class = "gt_table",
62+
style = style,
63+
...
64+
)
65+
)
66+
67+
# Unescape single quotes that may present as HTML entities (this is
68+
# needed since the CSS inliner cannot parse "&#39;")
69+
html_tbl <- gsub("&#39;", "'", html_tbl)
70+
71+
html_tbl
72+
}

tests/testthat/test-as_raw_html.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,27 @@ test_that("`as_raw_html()` produces the same table every time", {
114114

115115
gt_html_2_sha1 <- digest::sha1(gt_html_2)
116116
expect_equal(gt_html_2_sha1, "ead3b201f06d98f304d23d9c04a2eba0b18bbdf5")
117+
118+
# Expect that font family values with multiple words (i.e., have a space
119+
# character) added with `tab_style()` preserve single-quote characters
120+
expect_match(
121+
exibble[1, ] %>%
122+
gt() %>%
123+
tab_header(title = "Title") %>%
124+
tab_style(style = cell_text(font = "Two Words"), locations = cells_title()) %>%
125+
as_raw_html(),
126+
"font-family: 'Two Words';"
127+
)
128+
129+
expect_match(
130+
exibble[1, ] %>%
131+
gt() %>%
132+
tab_header(title = "Title") %>%
133+
tab_style(
134+
style = cell_text(font = c("Fira Sans", "Droid Sans", "Arial", "sans-serif")),
135+
locations = cells_title()
136+
) %>%
137+
as_raw_html(),
138+
"font-family: 'Fira Sans', 'Droid Sans', Arial, sans-serif;"
139+
)
117140
})

tests/testthat/test-r_table_parts.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test_that("a gt table contains the expected column spanner labels", {
1818
".*.trowd.trrh0.trhdr.*",
1919
".intbl \\{.f0 \\{.f0.fs20 \\}\\}.cell.*",
2020
".intbl \\{.f0 \\{.f0.fs20 perimeter\\}\\}.cell.*",
21-
".intbl \\{.f0 \\{.f0.fs20 perimeter\\}\\}.cell.*",
21+
".intbl \\{.f0 \\{.f0.fs20 }\\}.cell.*",
2222
".intbl \\{.f0 \\{.f0.fs20 \\}\\}.cell.*",
2323
".*area.*peri.*shape.*perm",
2424
".*"
@@ -53,9 +53,9 @@ test_that("a gt table contains the expected column spanner labels", {
5353
".*.trowd.trrh0.trhdr.*",
5454
".intbl \\{.f0 \\{.f0.fs20 \\}\\}.cell.*",
5555
".intbl \\{.f0 \\{.f0.fs20 v_1_2\\}\\}.cell.*",
56-
".intbl \\{.f0 \\{.f0.fs20 v_1_2\\}\\}.cell.*",
57-
".intbl \\{.f0 \\{.f0.fs20 v_4_5\\}\\}.cell.*",
56+
".intbl \\{.f0 \\{.f0.fs20 \\}\\}.cell.*",
5857
".intbl \\{.f0 \\{.f0.fs20 v_4_5\\}\\}.cell.*",
58+
".intbl \\{.f0 \\{.f0.fs20 \\}\\}.cell.*",
5959
".row.*",
6060
".*v_3.*v_1.*v_2.*v_4.*v_5",
6161
".*"
@@ -104,9 +104,9 @@ test_that("a gt table contains the expected column spanner labels", {
104104
paste0(
105105
".*.trowd.trrh0.trhdr.*",
106106
".intbl \\{.f0 \\{.f0.fs20 A\\{.super .i 1\\}\\}\\}.cell.*",
107-
".intbl \\{.f0 \\{.f0.fs20 A\\{.super .i 1\\}\\}\\}.cell.*",
108-
".intbl \\{.f0 \\{.f0.fs20 A\\}\\}.cell.*",
107+
".intbl \\{.f0 \\{.f0.fs20 \\}\\}.cell.*",
109108
".intbl \\{.f0 \\{.f0.fs20 A\\}\\}.cell.*",
109+
".intbl \\{.f0 \\{.f0.fs20 }\\}.cell.*",
110110
".row.*",
111111
".*A_X.*A_Y.*B_X.*B_Y",
112112
".*"
@@ -130,7 +130,7 @@ test_that("a gt table contains the expected column spanner labels", {
130130
paste0(
131131
".*.trowd.trrh0.trhdr.*",
132132
".intbl \\{.f0 \\{.f0.fs20 Sepal\\}\\}.cell.*",
133-
".intbl \\{.f0 \\{.f0.fs20 Sepal\\}\\}.cell.*",
133+
".intbl \\{.f0 \\{.f0.fs20 \\}\\}.cell.*",
134134
".intbl \\{.f0 \\{.f0.fs20 \\}\\}.cell.*",
135135
".intbl \\{.f0 \\{.f0.fs20 \\}\\}.cell.*",
136136
".intbl \\{.f0 \\{.f0.fs20 \\}\\}.cell.*",

0 commit comments

Comments
 (0)