Skip to content

Commit

Permalink
Merge pull request #194 from rstudio/na-css-color
Browse files Browse the repository at this point in the history
  • Loading branch information
wch authored Dec 2, 2020
2 parents 636b95e + 5e64737 commit bbf17a1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ htmltools 0.5.0.9002

* Closed #189: `validateCssUnit()` now accepts `fit-content`. (#190)

* Closed #161: `parseCssColors(x, mustWork = FALSE)` now returns NA for NA input values instead of throwing an error. (#194)

* `htmlPreserve()` can now optionally use the Pandoc `raw_attribute` extension to enclose HTML.

htmltools 0.5.0
Expand Down
10 changes: 10 additions & 0 deletions R/colors.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
#' @md
#' @export
parseCssColors <- function(str, mustWork = TRUE) {
# Logic below assumes a character string with non-missing values
# Note that an empty string is not a valid color, so parsing fails
# on NA input values, and thus, will be converted back to NA
# when `mustWork = FALSE`
isNA <- is.na(str)
if (!(is.character(str) || all(isNA))) {
stop("`str` must be a character vector (or NA).")
}
str[isNA] <- ""

# Strip insignificant whitespace
str <- color_strip_ws(str)

Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test-colors.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,23 @@ test_that("decode_color_keyword", {
expect_error(decode_color_keyword(" orange "))
expect_error(decode_color_keyword(NA))
})

test_that("parseCssColors() handles incoming NA values sensibly", {
expect_error(parseCssColors(NA))
expect_identical(
parseCssColors(NA, mustWork = FALSE),
NA_character_
)
expect_identical(
parseCssColors(rep(NA, 2), mustWork = FALSE),
rep(NA_character_, 2)
)
expect_identical(
parseCssColors(c(NA, "red"), mustWork = FALSE),
c(NA, "#FF0000")
)
expect_identical(
parseCssColors(c(NA, "blue", "notacolor"), mustWork = FALSE),
c(NA, "#0000FF", NA)
)
})

0 comments on commit bbf17a1

Please sign in to comment.