Skip to content

Commit

Permalink
Close #167. Update test of dependency manager to be more robust to ch…
Browse files Browse the repository at this point in the history
…anges in htmltools/htmlwidgets (#168)
  • Loading branch information
cpsievert authored Mar 21, 2024
1 parent f6efa76 commit c9c1e9a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 30 deletions.
63 changes: 37 additions & 26 deletions R/repr_htmlwidget.r
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#' @importFrom htmltools renderTags
embed_tags <- function(obj, ...) {
obj <- render_tags(obj, ...)

# render dependencies as data URIs (for standalone HTML)
html_deps <- lapply(obj$dependencies, render_dependency)

sprintf(HTML_SKELETON, paste(html_deps, collapse = '\n'), obj$html)
}

render_tags <- function(obj, ...) {
obj <- renderTags(obj)

if (nchar(obj$head) > 0) {
Expand All @@ -17,33 +26,35 @@ embed_tags <- function(obj, ...) {
html_dependencies$add(obj$dependencies)
}

# render dependencies as data URIs (for standalone HTML)
html_deps <- lapply(obj$dependencies, function(dep) {
html <- c()

if (length(dep$script) > 0) {
f <- file.path(dep$src$file, dep$script)
# TODO: is this *always* the correct mime type?
html <- c(html, sprintf(
'<script title="%s" src="%s"></script>',
sub('"', '', dep$name),
data_uris(mime = 'application/javascript', files = f)
))
}

if (length(dep$stylesheet) > 0) {
f <- file.path(dep$src$file, dep$stylesheet)
# TODO: is this *always* the correct mime type? Use base64enc::checkUTF8() to ensure UTF-8 is OK?
html <- c(html, sprintf(
'<link href="%s" rel="stylesheet" />',
data_uris(mime = 'text/css;charset-utf-8', files = f)
))
}

paste(html, collapse = '\n')
})
list(
dependencies = obj$dependencies,
html = obj$html
)
}

render_dependency <- function(dep) {
html <- c()

sprintf(HTML_SKELETON, paste(html_deps, collapse = '\n'), obj$html)
if (length(dep$script) > 0) {
f <- file.path(dep$src$file, dep$script)
# TODO: is this *always* the correct mime type?
html <- c(html, sprintf(
'<script title="%s" src="%s"></script>',
sub('"', '', dep$name),
data_uris(mime = 'application/javascript', files = f)
))
}

if (length(dep$stylesheet) > 0) {
f <- file.path(dep$src$file, dep$stylesheet)
# TODO: is this *always* the correct mime type? Use base64enc::checkUTF8() to ensure UTF-8 is OK?
html <- c(html, sprintf(
'<link href="%s" rel="stylesheet" />',
data_uris(mime = 'text/css;charset-utf-8', files = f)
))
}

paste(html, collapse = '\n')
}

HTML_SKELETON <-
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test_repr_htmlwidget.r
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ test_that('The dependency manager works', {
on.exit(options(o))
html_dependencies$clear()

r <- repr_html(str_view('xy', 'y'))
expect_match(r, '<meta charset="utf-8">\n\t\t<script', fixed = TRUE, all = FALSE)
r <- render_tags(str_view('xy', 'y'))
expect_true(length(r$dependencies) > 0)

r <- repr_html(str_view('xy', 'y'))
expect_match(r, '<meta charset="utf-8">\n\t\t\n', fixed = TRUE, all = FALSE) #no deps here
r <- render_tags(str_view('xy', 'y'))
expect_true(length(r$dependencies) == 0) #no deps here
})

test_that('Leaflet HTML and deps can be represented', {
Expand Down

0 comments on commit c9c1e9a

Please sign in to comment.