Просмотр исходного кода

change regex pattern to match valid css class names (#69)

* The style functions are now more permissive about color names in the `color` argument. Color names should be valid CSS classes that can also be used as CSS variable names, but the `style_` function only stops with an error if the color name contains spaces. (@jdlom #69)
tags/v0.4.1
jdlom 4 лет назад
Родитель
Сommit
c606bdf7c9
Аккаунт пользователя с таким Email не найден
2 измененных файлов: 22 добавлений и 3 удалений
  1. +7
    -2
      R/color.R
  2. +15
    -1
      tests/testthat/test-color.R

+ 7
- 2
R/color.R Просмотреть файл

@@ -121,8 +121,13 @@ prepare_colors <- function(colors = NULL) {
call. = FALSE)
}

if (any(grepl("[^[:alpha:]_-]", names(colors)))) {
stop("Color names in `colors` must be valid CSS classes", call. = FALSE)
maybe_bad_css <- unique(grep("^[_-]|[ .>~*:|+}/]", names(colors), value = TRUE))
if (length(maybe_bad_css) > 0) {
warning(
"Color names in `colors` should be valid CSS classes: ",
paste0("'", maybe_bad_css, "'", collapse = ", "),
call. = FALSE
)
}

whisker::iteratelist(colors, "color_name")

+ 15
- 1
tests/testthat/test-color.R Просмотреть файл

@@ -13,9 +13,23 @@ describe("prepare_colors()", {

it("requires valid CSS names", {
expect_error(prepare_colors(c("light blue" = "#88f")))
expect_error(prepare_colors(c("light/blue" = "#88f")))
expect_warning(prepare_colors(c("light/blue" = "#88f")), "light/blue")
expect_warning(prepare_colors(c("-lightblue" = "#88f")), "-lightblue")
expect_warning(prepare_colors(c("_lightblue" = "#88f")), "_lightblue")
colors <- c("light_blue" = "#88f", "light-blue" = "#88f")
expect_silent(
expect_equal(
prepare_colors(colors),
list(
list(color_name = "light_blue", value = "#88f"),
list(color_name = "light-blue", value = "#88f")
)
)
)
})


it("returns list with color_name and value for each color", {
colors <- c('test' = "#4ed4ed")
prepared <- prepare_colors(colors)

Загрузка…
Отмена
Сохранить