Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with defining column widths in cols_width() #561

Merged
merged 16 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions R/modify_columns.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ cols_width <- function(data,
rlang::f_rhs() %>%
rlang::eval_tidy()

# If a bare numeric value is provided, give that the `px` dimension
if (is.numeric(width)) width <- paste_right(as.character(width), "px")

for (column in columns) {
data <- data %>% dt_boxhead_edit(var = column, column_width = list(width))
}
Expand Down
52 changes: 14 additions & 38 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -1019,48 +1019,24 @@ validate_table_id <- function(id) {
}
}

is_css_length <- function(x,
must_have_unit = FALSE,
allow_empty = TRUE) {

# This is adapted from `htmltools::validateCssUnit()` to be a test
# function that either returns TRUE or FALSE
if (is.null(x) || is.na(x)) {
return(FALSE)
}

if (!is.character(x) && !is.numeric(x)) {
return(FALSE)
}

if (is.character(x) && nchar(x) < 1) {
if (!allow_empty) return(FALSE) else return(TRUE)
}

if (is.character(x) && gsub("^[0-9.]*$", "", x) == "") {
x <- as.numeric(x)
}
validate_css_lengths <- function(x) {

if (is.numeric(x)) {
if (!must_have_unit) return(TRUE) else return(FALSE)
}

pattern <- "^(auto|inherit|calc\\(.*\\)|((\\.\\d+)|(\\d+(\\.\\d+)?))(%|in|cm|mm|ch|em|ex|rem|pt|pc|px|vh|vw|vmin|vmax))$"
grepl(pattern, x)
}

are_css_lengths <- function(x,
must_have_unit = FALSE,
allow_empty = TRUE) {
# Don't include empty strings in the validation; these lengths
# should be handled downstream (i.e., using `htmltools::css()`,
# where empty strings and NULL values don't create rules at all)
x_units_non_empty <- x[!(x == "")]

# While this returns a vector of corrected CSS units, we
# primarily want to verify that the vector of provided values
# don't contain any invalid suffixes; this throws if that's the
# case and returns `TRUE` otherwise
vapply(
x,
FUN = is_css_length,
must_have_unit = FALSE,
allow_empty = TRUE,
FUN.VALUE = logical(1),
x_units_non_empty,
FUN = htmltools::validateCssUnit,
FUN.VALUE = character(1),
USE.NAMES = FALSE
)
) %>%
is.character()
}

column_classes_are_valid <- function(data, columns, valid_classes) {
Expand Down
10 changes: 2 additions & 8 deletions R/utils_render_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,7 @@ get_table_defs <- function(data) {

# Stop function if all length dimensions (where provided)
# don't conform to accepted CSS length definitions
if (!all(are_css_lengths(widths))) {

stop("Disallowed inputs provided to `cols_width()`.\n",
"* Only the `px()` and `pct()` functions should be used on the ",
"RHS of all column width exprs",
call. = FALSE)
}
validate_css_lengths(widths)

# If all of the widths are defined as px values for all columns,
# then ensure that the width values are strictly respected as
Expand All @@ -116,7 +110,7 @@ get_table_defs <- function(data) {
table_colgroups <-
htmltools::tags$colgroup(
lapply(widths, function(width) {
rich-iannone marked this conversation as resolved.
Show resolved Hide resolved
htmltools::tags$col(style = paste0("width: ", width))
htmltools::tags$col(style = htmltools::css(width = width))
})
)

Expand Down
Loading