Permalink
Browse files

Move validateCssUnit to separate function

  • Loading branch information...
wch committed Jan 23, 2013
1 parent 076e6c9 commit 5ace0f13c950f0de2f74a62d1c3631c385fb4e19
Showing with 31 additions and 15 deletions.
  1. +14 −15 R/bootstrap.R
  2. +17 −0 inst/tests/test-bootstrap.r
View
@@ -754,20 +754,8 @@ verbatimTextOutput <- function(outputId) {
#' )
#' @export
plotOutput <- function(outputId, width = "100%", height="400px") {
-
- validate_css_unit <- function(x) {
- if (is.character(x) &&
- !grepl("^(auto|((\\.\\d+)|(\\d+(\\.\\d+)?))(%|in|cm|mm|em|ex|pt|pc|px))$", x)) {
- stop('"', x, '" is not a valid CSS unit (e.g., "100%", "400px", "auto")')
- }
- if (is.numeric(x)) {
- x <- paste(x, "px", sep = "")
- }
- x
- }
-
- style <- paste("width:", validate_css_unit(width), ";",
- "height:", validate_css_unit(height))
+ style <- paste("width:", validateCssUnit(width), ";",
+ "height:", validateCssUnit(height))
div(id = outputId, class="shiny-plot-output", style = style)
}
@@ -856,4 +844,15 @@ downloadLink <- function(outputId, label="Download", class=NULL) {
href='',
target='_blank',
label)
-}
+}
+
+validateCssUnit <- function(x) {
+ if (is.character(x) &&
+ !grepl("^(auto|((\\.\\d+)|(\\d+(\\.\\d+)?))(%|in|cm|mm|em|ex|pt|pc|px))$", x)) {
+ stop('"', x, '" is not a valid CSS unit (e.g., "100%", "400px", "auto")')
+ }
+ if (is.numeric(x)) {
+ x <- paste(x, "px", sep = "")
+ }
+ x
+}
@@ -0,0 +1,17 @@
+context("bootstrap")
+
+test_that("CSS unit validation", {
+ # On error, return NA; on success, return result
+ validateCssUnit_wrap <- function(x) {
+ tryCatch(validateCssUnit(x), error = function(e) { NA_character_ })
+ }
+
+ # Test strings and expected results
+ strings <- c("100x", "10px", "10.4px", ".4px", "1px0", "px", "5", "%", "5%", "auto", "1auto", "")
+ expected <- c(NA, "10px", "10.4px", ".4px", NA, NA, NA, NA, "5%", "auto", NA, NA)
+ results <- vapply(strings, validateCssUnit_wrap, character(1), USE.NAMES = FALSE)
+ expect_equal(results, expected)
+
+ # Numbers should return string with "px"
+ expect_equal(validateCssUnit(100), "100px")
+})

0 comments on commit 5ace0f1

Please sign in to comment.