Debian Codegraph @

R/html-deps.R

raw
1 #' Create a web dependency
2 #'
3 #' Ensure that a file-based HTML dependency (from the htmltools package) can be
4 #' served over Shiny's HTTP server. This function works by using
5 #' [addResourcePath()] to map the HTML dependency's directory to a
6 #' URL.
7 #'
8 #' @param dependency A single HTML dependency object, created using
9 #' [htmltools::htmlDependency()]. If the `src` value is named,
10 #' then `href` and/or `file` names must be present.
11 #' @param scrubFile If TRUE (the default), remove `src$file` for the
12 #' dependency. This prevents the local file path from being sent to the client
13 #' when dynamic web dependencies are used. If FALSE, don't remove
14 #' `src$file`. Setting it to FALSE should be needed only in very unusual
15 #' cases.
16 #'
17 #' @return A single HTML dependency object that has an `href`-named element
18 #' in its `src`.
19 #' @export
20 createWebDependency <- function(dependency, scrubFile = TRUE) {
21 if (is.null(dependency))
22 return(NULL)
23
24 if (!inherits(dependency, "html_dependency"))
25 stop("Unexpected non-html_dependency type")
26
27 if (is.null(dependency$src$href)) {
28 prefix <- paste(dependency$name, "-", dependency$version, sep = "")
29 addResourcePath(prefix, dependency$src$file)
30 dependency$src$href <- prefix
31 }
32
33 # Don't leak local file path to client
34 if (scrubFile)
35 dependency$src$file <- NULL
36
37 return(dependency)
38 }
39
40
41 # Given a Shiny tag object, process singletons and dependencies. Returns a list
42 # with rendered HTML and dependency objects.
43 # This implementation is very similar to renderTags(), but ignores
44 # <head> handling (it should only be used after the user session has started)
45 processDeps <- function(tags, session) {
46 tags <- utils::getFromNamespace("tagify", "htmltools")(tags)
47 ui <- takeSingletons(tags, session$singletons, desingleton = FALSE)$ui
48 ui <- surroundSingletons(ui)
49 dependencies <- lapply(
50 resolveDependencies(findDependencies(ui, tagify = FALSE)),
51 createWebDependency
52 )
53 names(dependencies) <- NULL
54
55 list(
56 html = doRenderTags(ui),
57 deps = dependencies
58 )
59 }