Skip to content

Commit

Permalink
Merge branch 'main' into rc-v0.5.6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsievert committed Oct 6, 2023
2 parents c53e843 + 2a26d1f commit 180cfbf
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 65 deletions.
61 changes: 32 additions & 29 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,40 +1,48 @@
Package: htmltools
Type: Package
Package: htmltools
Title: Tools for HTML
Version: 0.5.6.1
Version: 0.5.6.9001
Authors@R: c(
person("Joe", "Cheng", role = "aut", email = "[email protected]"),
person("Carson", "Sievert", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0002-4958-2844")),
person("Barret", "Schloerke", role = "aut", email = "[email protected]", comment = c(ORCID = "0000-0001-9986-114X")),
person("Winston", "Chang", role = "aut", email = "[email protected]", comment = c(ORCID = "0000-0002-1576-2126")),
person("Yihui", "Xie", role = "aut", email = "[email protected]"),
person("Jeff", "Allen", role = "aut"),
person("Posit Software, PBC", role = c("cph", "fnd"))
)
person("Joe", "Cheng", , "[email protected]", role = "aut"),
person("Carson", "Sievert", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-4958-2844")),
person("Barret", "Schloerke", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0001-9986-114X")),
person("Winston", "Chang", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0002-1576-2126")),
person("Yihui", "Xie", , "[email protected]", role = "aut"),
person("Jeff", "Allen", role = "aut"),
person("Posit Software, PBC", role = c("cph", "fnd"))
)
Description: Tools for HTML generation and output.
License: GPL (>= 2)
URL: https://github.com/rstudio/htmltools,
https://rstudio.github.io/htmltools/
BugReports: https://github.com/rstudio/htmltools/issues
Depends:
R (>= 2.14.1)
Imports:
utils,
digest,
grDevices,
base64enc,
rlang (>= 0.4.12),
digest,
ellipsis,
fastmap (>= 1.1.0),
ellipsis
grDevices,
rlang (>= 1.0.0),
utils
Suggests:
markdown,
testthat,
withr,
Cairo,
markdown,
ragg,
shiny
Enhances: knitr
License: GPL (>= 2)
URL: https://github.com/rstudio/htmltools, https://rstudio.github.io/htmltools/
BugReports: https://github.com/rstudio/htmltools/issues
RoxygenNote: 7.2.3
shiny,
testthat,
withr
Enhances:
knitr
Config/Needs/check: knitr
Config/Needs/website: rstudio/quillt, bench
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Collate:
'colors.R'
'fill.R'
Expand All @@ -50,8 +58,3 @@ Collate:
'utils.R'
'tags.R'
'template.R'
Roxygen: list(markdown = TRUE)
Config/Needs/check: knitr
Config/Needs/website:
rstudio/quillt,
bench
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ S3method(print,shiny.tag)
S3method(print,shiny.tag.env)
S3method(print,shiny.tag.list)
S3method(print,shiny.tag.query)
S3method(save_html,default)
S3method(str,shiny.tag.env)
export("htmlDependencies<-")
export(HTML)
Expand Down
16 changes: 16 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# htmltools (development version)

## New Features

* `save_html()` is now an S3 generic, allowing for more customization over how certain classes are saved to an HTML file. (#411)

## Improvements

* Fill items no longer set `overflow: auto` or `width: 100%` by default. (#401)

* `css()` now fully supports setting custom CSS properties (or CSS variables) via inline styles. When the name of a value passed to `css()` starts with `--`, it will be treated as a custom CSS property and absolutely no changes will be made to the variable. For example, `css("--font_size" = "3em")` returns `--font_size:3em;` while `css(font_size = "3em")` will return `font-size:3em`. (#402)

## Bug fixes

* `{htmltools}` now requires `{rlang}` version 1.0.0 or higher. (#403)

# htmltools 0.5.6.1

## Improvements
Expand Down
23 changes: 16 additions & 7 deletions R/html_print.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,27 @@ html_print <- function(html, background = "white", viewer = getOption("viewer",

#' Save an HTML object to a file
#'
#' Save the specified HTML object to a file, copying all of it's
#' dependencies to the directory specified via `libdir`.
#' An S3 generic method for saving an HTML-like object to a file. The default
#' method copies dependency files to the directory specified via `libdir`.
#'
#' @param html HTML content to print
#' @param background Background color for web page
#' @param html HTML content to print.
#' @param file File path or connection. If a file path containing a
#' sub-directory, the sub-directory must already exist.
#' @param libdir Directory to copy dependencies to
#' @param lang Value of the `<html>` `lang` attribute
#' @param ... Further arguments passed to other methods.
#'
#' @export
save_html <- function(html, file, background = "white", libdir = "lib", lang = "en") {
save_html <- function(html, file, ...) {
UseMethod("save_html")
}

#' @rdname save_html
#' @param background Background color for web page.
#' @param libdir Directory to copy dependencies to.
#' @param lang Value of the `<html>` `lang` attribute.
#' @export
save_html.default <- function(html, file, background = "white", libdir = "lib", lang = "en", ...) {
rlang::check_dots_empty()

force(html)
force(background)
force(libdir)
Expand Down
9 changes: 9 additions & 0 deletions R/htmltools-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@
#' @importFrom rlang obj_address
## usethis namespace: end
NULL


# For usethis::use_release_issue()
release_bullets <- function() {
c(
"Update static imports: `staticimports::import()`"
)
}

22 changes: 16 additions & 6 deletions R/staticimports.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,19 @@ get_package_version <- function(pkg) {

is_installed <- function(pkg, version = NULL) {
installed <- isNamespaceLoaded(pkg) || nzchar(system_file_cached(package = pkg))

if (is.null(version)) {
return(installed)
}

if (!is.character(version) && !inherits(version, "numeric_version")) {
# Avoid https://bugs.r-project.org/show_bug.cgi?id=18548
alert <- if (identical(Sys.getenv("TESTTHAT"), "true")) stop else warning
alert("`version` must be a character string or a `package_version` or `numeric_version` object.")

version <- numeric_version(sprintf("%0.9g", version))
}

installed && isTRUE(get_package_version(pkg) >= version)
}

Expand Down Expand Up @@ -81,11 +91,9 @@ system_file <- function(..., package = "base") {
normalizePath(files, winslash = "/")
}

# A wrapper for `system.file()`, which caches the results, because
# `system.file()` can be slow. Note that because of caching, if
# `system_file_cached()` is called on a package that isn't installed, then the
# package is installed, and then `system_file_cached()` is called again, it will
# still return "".
# A wrapper for `system.file()`, which caches the package path because
# `system.file()` can be slow. If a package is not installed, the result won't
# be cached.
system_file_cached <- local({
pkg_dir_cache <- character()

Expand All @@ -97,7 +105,9 @@ system_file_cached <- local({
not_cached <- is.na(match(package, names(pkg_dir_cache)))
if (not_cached) {
pkg_dir <- system.file(package = package)
pkg_dir_cache[[package]] <<- pkg_dir
if (nzchar(pkg_dir)) {
pkg_dir_cache[[package]] <<- pkg_dir
}
} else {
pkg_dir <- pkg_dir_cache[[package]]
}
Expand Down
14 changes: 12 additions & 2 deletions R/tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -1947,8 +1947,10 @@ css <- function(..., collapse_ = "") {
return(NULL)
}

# Replace all '.' and '_' in property names to '-'
names(props) <- gsub("[._]", "-", tolower(gsub("([A-Z])", "-\\1", names(props))))
# Translate camelCase, snake_case, and dot.case to kebab-case
# For standard CSS properties only, not CSS variables
is_css_var <- grepl("^--", names(props))
names(props)[!is_css_var] <- standardize_property_names(names(props)[!is_css_var])

# Create "!important" suffix for each property whose name ends with !, then
# remove the ! from the property name
Expand All @@ -1961,3 +1963,11 @@ css <- function(..., collapse_ = "") {
empty <- function(x) {
length(x) == 0 || (is.character(x) && !any(nzchar(x)))
}

standardize_property_names <- function(x) {
# camelCase to kebab-case
x <- gsub("([A-Z])", "-\\1", x)
x <- tolower(x)
# snake_case and dot.case to kebab-case
gsub("[._]", "-", x)
}
12 changes: 5 additions & 7 deletions inst/fill/fill.css
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
.html-fill-container {
display: flex;
flex-direction: column;
width: 100%;
min-width: 0;
/* Prevent the container from expanding vertically or horizontally beyond its
parent's constraints. */
min-height: 0;
min-width: 0;
}
.html-fill-container > .html-fill-item {
/* Fill items can grow and shrink freely within
available vertical space in fillable container */
flex: 1 1 auto;
overflow: auto;
width: 100%;
min-height: 0;
min-width: 0;
}
.html-fill-container > :not(.html-fill-item) {
/* Prevent shrinking or growing of non-fill items */
flex: 0 0 auto;
}
.html-fill-container > .html-fill-item.html-fill-item-overflow-hidden {
overflow: hidden;
}
19 changes: 12 additions & 7 deletions man/save_html.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/test-print.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,13 @@ test_that("save_html() can write to a file connection", {
grepl("<h2>Howdy</h2>", paste(readLines(f), collapse = " "))
)
})

test_that("save_html.default() throws when undefined arguments are provided", {
expect_error(
save_html(div(), tempfile(), foo = "bar")
)

expect_error(
save_html(div(), tempfile(), background = "white", libdir = "lib", lang = "en", "bar")
)
})
54 changes: 47 additions & 7 deletions tests/testthat/test-tags.r
Original file line number Diff line number Diff line change
Expand Up @@ -835,18 +835,58 @@ test_that("Indenting can be controlled/suppressed", {
test_that("cssList tests", {
expect_identical(NULL, css())
expect_identical(NULL, css())

# Regular CSS properties with conveniences
expect_identical(css(font.size = "12px"), "font-size:12px;")
expect_identical(css(font_size = "12px"), "font-size:12px;")
expect_identical(css(fontSize = "12px"), "font-size:12px;")
expect_identical(css(`font-style` = "italic"), "font-style:italic;")
expect_null(css(font.variant = NULL))
expect_identical(
css(.webkit.animation = "fade-in 1s"),
"-webkit-animation:fade-in 1s;"
)
expect_identical(
css(font.family = 'Helvetica, "Segoe UI"'),
"font-family:Helvetica, \"Segoe UI\";"
)
expect_identical(
css("font-weight!" = factor("bold")),
"font-weight:bold !important;"
)
expect_identical(
css(padding = c("10px", "9px", "8px")),
"padding:10px 9px 8px;"
)

# CSS variables
expect_identical(css("--_foo" = "bar"), "--_foo:bar;")
expect_identical(css("--fooBar" = "baz"), "--fooBar:baz;")
expect_identical(css("--foo_bar" = "baz"), "--foo_bar:baz;")
expect_identical(css("--_foo!" = "bar"), "--_foo:bar !important;")
expect_identical(css("--fooBar!" = "baz"), "--fooBar:baz !important;")
expect_identical(css("--foo_bar!" = "baz"), "--foo_bar:baz !important;")

# Mix of CSS variables and regular CSS properties
expect_identical(
css(
font.family = 'Helvetica, "Segoe UI"',
font_size = "12px",
`font-style` = "italic",
font.variant = NULL,
"font-weight!" = factor("bold"),
padding = c("10px", "9px", "8px")
"--empty" = NULL,
"--_foo" = "bar",
`_foo` = "bar",
"--foo_bar" = "baz",
foo_bar = "baz",
"--fooBar" = "baz",
fooBar = "baz",
),
"font-family:Helvetica, \"Segoe UI\";font-size:12px;font-style:italic;font-weight:bold !important;padding:10px 9px 8px;"
"--_foo:bar;-foo:bar;--foo_bar:baz;foo-bar:baz;--fooBar:baz;foo-bar:baz;"
)

# Lists can be spliced
expect_identical(css(!!!list(a = 1, b = 2)), "a:1;b:2;")

# Factors are coerced to strings
expect_identical(css(a = factor('a')), "a:a;")

# Unnamed args not allowed
expect_error(css("10"))
expect_error(css(1, b=2))
Expand Down

0 comments on commit 180cfbf

Please sign in to comment.