From e6d8ae96a62ead823abbfcab4fa736bd9545e0b9 Mon Sep 17 00:00:00 2001 From: Gauarv Chaudhary Date: Tue, 7 Oct 2025 22:11:24 +0530 Subject: [PATCH 1/8] Add validation for selector names to prevent browser rendering failures(#232). --- NEWS.md | 6 + R/z_animint.R | 3 + R/z_animintHelpers.R | 48 ++++++ ...est-compiler-invalid-selector-characters.R | 138 ++++++++++++++++++ 4 files changed, 195 insertions(+) create mode 100644 tests/testthat/test-compiler-invalid-selector-characters.R diff --git a/NEWS.md b/NEWS.md index 35c848a08..6d826bd22 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,12 @@ +<<<<<<< HEAD # Changes in version 2025.10.9 (PR#242) - Improve common chunk detection, output `na_group` and `row_in_group` when there are missing values. +======= +# Changes in version 2025.10.6 (Issue #232) + +- Added validation for selector names to prevent browser rendering failures. Selector names (from data values used in `clickSelects` and `showSelected`) cannot contain CSS special characters like `#`, `@`, `!`, `$`, etc., as these interfere with JavaScript DOM selectors and cause blank visualizations in the browser. The compiler now stops with a clear error message identifying problematic selector names, helping users fix data issues before attempting to render. +>>>>>>> 32e15c5d (Add validation for selector names to prevent browser rendering failures(#232).) # Changes in version 2025.10.3 (PR#240) diff --git a/R/z_animint.R b/R/z_animint.R index 17477090f..2d444dd73 100644 --- a/R/z_animint.R +++ b/R/z_animint.R @@ -457,6 +457,9 @@ animint2dir <- function ## For a static data viz with no interactive aes, no need to check ## for trivial showSelected variables with only 1 level. checkSingleShowSelectedValue(meta$selectors) + + ## Check selector names for CSS compatibility (no special characters like #) + checkSelectorNames(meta$selectors) ## Go through options and add to the list. for(v.name in names(meta$duration)){ diff --git a/R/z_animintHelpers.R b/R/z_animintHelpers.R index 4566e7892..5e0db66c1 100644 --- a/R/z_animintHelpers.R +++ b/R/z_animintHelpers.R @@ -638,6 +638,54 @@ checkSingleShowSelectedValue <- function(selectors){ } +#' Validate selector names for CSS compatibility +#' @param selectors selectors to validate +#' @return \code{NULL}. Throws error if invalid characters found. +checkSelectorNames <- function(selectors){ + if(length(selectors) == 0){ + return(NULL) + } + + selector.names <- names(selectors) + + ## Characters that are invalid in CSS selectors and cause issues in browser + ## CSS selector special characters: # . : [ ] , + > ~ ( ) @ ! $ % ^ & * = | \ / ' " ` ? < > + ## Most problematic: # (hash/id selector), . (class selector), : (pseudo-class) + ## We check for these common problematic characters + invalid.chars <- c("#", "!", "@", "$", "%", "^", "&", "*", "=", "|", "\\", "/", "'", "\"", "`", "?", "<", ">", "(", ")", "[", "]") + + ## Check each selector name for invalid characters + has.invalid <- sapply(selector.names, function(name) { + any(sapply(invalid.chars, function(char) { + grepl(char, name, fixed = TRUE) + })) + }) + + if(any(has.invalid)){ + invalid.names <- selector.names[has.invalid] + ## Find which character(s) are problematic for each name + problematic <- sapply(invalid.names, function(name) { + found.chars <- invalid.chars[sapply(invalid.chars, function(char) { + grepl(char, name, fixed = TRUE) + })] + paste0("'", found.chars, "'", collapse = ", ") + }) + + error.msg <- paste0( + "Invalid character(s) in selector name(s). ", + "Selector names cannot contain special characters that interfere with CSS selectors.\n", + "The following selector(s) contain invalid characters:\n", + paste0(" - '", invalid.names, "' contains ", problematic, collapse = "\n"), + "\n\nPlease remove or replace these characters in your variable names." + ) + + stop(error.msg) + } + + return(NULL) +} + + #' Set plot width and height for all plots #' @param meta meta object with all information #' @param AllPlotsInfo plot info list diff --git a/tests/testthat/test-compiler-invalid-selector-characters.R b/tests/testthat/test-compiler-invalid-selector-characters.R new file mode 100644 index 000000000..cd06a8495 --- /dev/null +++ b/tests/testthat/test-compiler-invalid-selector-characters.R @@ -0,0 +1,138 @@ +acontext("Invalid selector character validation") + +# Test that selector names with special characters cause errors +# Note: Selector names come from the VALUES in the data when using .variable/.value pattern +test_that("selector name with # causes error in clickSelects", { + viz <- list( + plot1 = ggplot() + + geom_point( + aes(Sepal.Length, Petal.Length), + data = data.frame( + Sepal.Length = 1:10, + Petal.Length = rnorm(10), + regularization = "# nearest neighbors", # This VALUE becomes the selector name + parameter = 1:10 + ), + clickSelects = c(regularization = "parameter") + ) + ) + + expect_error( + animint2dir(viz, open.browser = FALSE), + "Invalid character\\(s\\) in selector name\\(s\\)", + info = "Selector names with '#' should cause an error" + ) + + expect_error( + animint2dir(viz, open.browser = FALSE), + "# nearest neighbors", + info = "Error message should mention the problematic selector name" + ) +}) + +test_that("selector name with @ causes error in clickSelects", { + viz <- list( + plot1 = ggplot() + + geom_point( + aes(Sepal.Length, Petal.Length), + data = data.frame( + Sepal.Length = 1:10, + Petal.Length = rnorm(10), + regularization = "model@version1", # This VALUE becomes selector name + parameter = 1:10 + ), + clickSelects = c(regularization = "parameter") + ) + ) + + expect_error( + animint2dir(viz, open.browser = FALSE), + "Invalid character\\(s\\) in selector name\\(s\\)", + info = "Selector names with '@' should cause an error" + ) + + expect_error( + animint2dir(viz, open.browser = FALSE), + "model@version", + info = "Error message should mention the problematic selector" + ) +}) + +test_that("selector name with ! causes error", { + viz <- list( + plot1 = ggplot() + + geom_point( + aes(x=1:10, y=rnorm(10)), + data = data.frame( + model = "model!important", + parameter = 1:10 + ), + clickSelects = c(model = "parameter") + ) + ) + + expect_error( + animint2dir(viz, open.browser = FALSE), + "Invalid character\\(s\\) in selector name\\(s\\)", + info = "Selector names with '!' should cause an error" + ) +}) + +test_that("valid selector names work with clickSelects", { + viz <- list( + plot1 = ggplot() + + geom_point( + aes(x=1:10, y=rnorm(10)), + data = data.frame( + regularization = "polynomial_degree", # Valid name + parameter = 0:9 + ), + clickSelects = c(regularization = "parameter") + ) + ) + + # Should NOT error + info <- animint2dir(viz, open.browser = FALSE) + expect_true(TRUE, info = "Valid selector names should not cause errors") +}) + +test_that("selector names with spaces work", { + viz <- list( + plot1 = ggplot() + + geom_point( + aes(x=1:10, y=rnorm(10)), + data = data.frame( + regularization = "nearest neighbors", # spaces are OK + parameter = 1:10 + ), + clickSelects = c(regularization = "parameter") + ) + ) + + # Should NOT error - spaces are fine + info <- animint2dir(viz, open.browser = FALSE) + expect_true(TRUE, info = "Selector names with spaces should work") +}) + +test_that("multiple values with invalid characters all reported", { + viz <- list( + plot1 = ggplot() + + geom_point( + aes(x=1:10, y=rnorm(10)), + data = data.frame( + regularization = rep(c("#bad", "!worse"), 5), # Both have invalid chars + parameter = 1:10 + ), + clickSelects = c(regularization = "parameter") + ) + ) + + error_msg <- tryCatch( + animint2dir(viz, open.browser = FALSE), + error = function(e) as.character(e$message) + ) + + # Should catch both bad selector names + expect_match(error_msg, "Invalid character", info = "Should report invalid characters") + expect_match(error_msg, "#bad|!worse", info = "Should mention at least one problematic selector") +}) From 258284ca2ff5a0d63cb731e4b979a33c19f24d2b Mon Sep 17 00:00:00 2001 From: Gauarv Chaudhary Date: Thu, 9 Oct 2025 12:59:41 +0530 Subject: [PATCH 2/8] Apply maintainer feedback for PR#246 - Update NEWS.md to reference PR#246 instead of Issue #232 - Refactor checkSelectorNames() to use sprintf instead of paste0 - Remove unnecessary if(length==0) check and explicit return(NULL) - Remove all empty lines from test_that blocks per style guidelines --- NEWS.md | 6 ++---- R/z_animintHelpers.R | 15 +++++---------- .../test-compiler-invalid-selector-characters.R | 12 ------------ 3 files changed, 7 insertions(+), 26 deletions(-) diff --git a/NEWS.md b/NEWS.md index 6d826bd22..f618e25f1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,12 +1,10 @@ -<<<<<<< HEAD # Changes in version 2025.10.9 (PR#242) - Improve common chunk detection, output `na_group` and `row_in_group` when there are missing values. -======= -# Changes in version 2025.10.6 (Issue #232) + +# Changes in version 2025.10.6 (PR#246) - Added validation for selector names to prevent browser rendering failures. Selector names (from data values used in `clickSelects` and `showSelected`) cannot contain CSS special characters like `#`, `@`, `!`, `$`, etc., as these interfere with JavaScript DOM selectors and cause blank visualizations in the browser. The compiler now stops with a clear error message identifying problematic selector names, helping users fix data issues before attempting to render. ->>>>>>> 32e15c5d (Add validation for selector names to prevent browser rendering failures(#232).) # Changes in version 2025.10.3 (PR#240) diff --git a/R/z_animintHelpers.R b/R/z_animintHelpers.R index 5e0db66c1..00ec362a4 100644 --- a/R/z_animintHelpers.R +++ b/R/z_animintHelpers.R @@ -642,10 +642,6 @@ checkSingleShowSelectedValue <- function(selectors){ #' @param selectors selectors to validate #' @return \code{NULL}. Throws error if invalid characters found. checkSelectorNames <- function(selectors){ - if(length(selectors) == 0){ - return(NULL) - } - selector.names <- names(selectors) ## Characters that are invalid in CSS selectors and cause issues in browser @@ -671,18 +667,17 @@ checkSelectorNames <- function(selectors){ paste0("'", found.chars, "'", collapse = ", ") }) - error.msg <- paste0( + error.msg <- sprintf( + "%s%s\n%s\n%s\n\n%s", "Invalid character(s) in selector name(s). ", - "Selector names cannot contain special characters that interfere with CSS selectors.\n", - "The following selector(s) contain invalid characters:\n", + "Selector names cannot contain special characters that interfere with CSS selectors.", + "The following selector(s) contain invalid characters:", paste0(" - '", invalid.names, "' contains ", problematic, collapse = "\n"), - "\n\nPlease remove or replace these characters in your variable names." + "Please remove or replace these characters in your variable names." ) stop(error.msg) } - - return(NULL) } diff --git a/tests/testthat/test-compiler-invalid-selector-characters.R b/tests/testthat/test-compiler-invalid-selector-characters.R index cd06a8495..fe9f9303e 100644 --- a/tests/testthat/test-compiler-invalid-selector-characters.R +++ b/tests/testthat/test-compiler-invalid-selector-characters.R @@ -16,13 +16,11 @@ test_that("selector name with # causes error in clickSelects", { clickSelects = c(regularization = "parameter") ) ) - expect_error( animint2dir(viz, open.browser = FALSE), "Invalid character\\(s\\) in selector name\\(s\\)", info = "Selector names with '#' should cause an error" ) - expect_error( animint2dir(viz, open.browser = FALSE), "# nearest neighbors", @@ -44,13 +42,11 @@ test_that("selector name with @ causes error in clickSelects", { clickSelects = c(regularization = "parameter") ) ) - expect_error( animint2dir(viz, open.browser = FALSE), "Invalid character\\(s\\) in selector name\\(s\\)", info = "Selector names with '@' should cause an error" ) - expect_error( animint2dir(viz, open.browser = FALSE), "model@version", @@ -70,7 +66,6 @@ test_that("selector name with ! causes error", { clickSelects = c(model = "parameter") ) ) - expect_error( animint2dir(viz, open.browser = FALSE), "Invalid character\\(s\\) in selector name\\(s\\)", @@ -90,8 +85,6 @@ test_that("valid selector names work with clickSelects", { clickSelects = c(regularization = "parameter") ) ) - - # Should NOT error info <- animint2dir(viz, open.browser = FALSE) expect_true(TRUE, info = "Valid selector names should not cause errors") }) @@ -108,8 +101,6 @@ test_that("selector names with spaces work", { clickSelects = c(regularization = "parameter") ) ) - - # Should NOT error - spaces are fine info <- animint2dir(viz, open.browser = FALSE) expect_true(TRUE, info = "Selector names with spaces should work") }) @@ -126,13 +117,10 @@ test_that("multiple values with invalid characters all reported", { clickSelects = c(regularization = "parameter") ) ) - error_msg <- tryCatch( animint2dir(viz, open.browser = FALSE), error = function(e) as.character(e$message) ) - - # Should catch both bad selector names expect_match(error_msg, "Invalid character", info = "Should report invalid characters") expect_match(error_msg, "#bad|!worse", info = "Should mention at least one problematic selector") }) From a53f1431864ccf00f23c1ff3e1b8d530a0d38be9 Mon Sep 17 00:00:00 2001 From: Gauarv Chaudhary Date: Sat, 11 Oct 2025 13:18:30 +0530 Subject: [PATCH 3/8] Use literal string with newlines instead of sprintf per maintainer feedback Changed error message construction in checkSelectorNames() to use one long literal string with \n newlines instead of sprintf with multiple %s placeholders, following best practices from CRAN's potools vignette. --- R/z_animintHelpers.R | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/R/z_animintHelpers.R b/R/z_animintHelpers.R index 00ec362a4..033ba3f58 100644 --- a/R/z_animintHelpers.R +++ b/R/z_animintHelpers.R @@ -667,13 +667,13 @@ checkSelectorNames <- function(selectors){ paste0("'", found.chars, "'", collapse = ", ") }) - error.msg <- sprintf( - "%s%s\n%s\n%s\n\n%s", + invalid.list <- paste0(" - '", invalid.names, "' contains ", problematic, collapse = "\n") + error.msg <- paste0( "Invalid character(s) in selector name(s). ", - "Selector names cannot contain special characters that interfere with CSS selectors.", - "The following selector(s) contain invalid characters:", - paste0(" - '", invalid.names, "' contains ", problematic, collapse = "\n"), - "Please remove or replace these characters in your variable names." + "Selector names cannot contain special characters that interfere with CSS selectors.\n", + "The following selector(s) contain invalid characters:\n", + invalid.list, + "\n\nPlease remove or replace these characters in your variable names." ) stop(error.msg) From 4277d246ae1b16a020dbcbee17bc1b158867c416 Mon Sep 17 00:00:00 2001 From: Gauarv Chaudhary Date: Sun, 9 Nov 2025 20:26:28 +0530 Subject: [PATCH 4/8] simplify checkSelectorNames per maintainer feedback Address review comment to simplify nested sapply calls and fix test typo. --- R/z_animintHelpers.R | 24 +++++++------------ ...est-compiler-invalid-selector-characters.R | 2 +- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/R/z_animintHelpers.R b/R/z_animintHelpers.R index 033ba3f58..853766c38 100644 --- a/R/z_animintHelpers.R +++ b/R/z_animintHelpers.R @@ -651,29 +651,23 @@ checkSelectorNames <- function(selectors){ invalid.chars <- c("#", "!", "@", "$", "%", "^", "&", "*", "=", "|", "\\", "/", "'", "\"", "`", "?", "<", ">", "(", ")", "[", "]") ## Check each selector name for invalid characters - has.invalid <- sapply(selector.names, function(name) { - any(sapply(invalid.chars, function(char) { - grepl(char, name, fixed = TRUE) - })) - }) + ## Check if any invalid character appears in each selector name + has.invalid <- vapply(selector.names, function(name) { + any(vapply(invalid.chars, grepl, logical(1), x = name, fixed = TRUE)) + }, logical(1)) if(any(has.invalid)){ invalid.names <- selector.names[has.invalid] ## Find which character(s) are problematic for each name problematic <- sapply(invalid.names, function(name) { - found.chars <- invalid.chars[sapply(invalid.chars, function(char) { - grepl(char, name, fixed = TRUE) - })] + found.chars <- invalid.chars[sapply(invalid.chars, grepl, name, fixed = TRUE)] paste0("'", found.chars, "'", collapse = ", ") }) - invalid.list <- paste0(" - '", invalid.names, "' contains ", problematic, collapse = "\n") - error.msg <- paste0( - "Invalid character(s) in selector name(s). ", - "Selector names cannot contain special characters that interfere with CSS selectors.\n", - "The following selector(s) contain invalid characters:\n", - invalid.list, - "\n\nPlease remove or replace these characters in your variable names." + invalid.list <- paste(sprintf(" - '%s' contains %s", invalid.names, problematic), collapse = "\n") + error.msg <- sprintf( + "Invalid character(s) in selector name(s). Selector names cannot contain special characters that interfere with CSS selectors.\nThe following selector(s) contain invalid characters:\n%s\n\nPlease remove or replace these characters in your variable names.", + invalid.list ) stop(error.msg) diff --git a/tests/testthat/test-compiler-invalid-selector-characters.R b/tests/testthat/test-compiler-invalid-selector-characters.R index fe9f9303e..3e3559fc9 100644 --- a/tests/testthat/test-compiler-invalid-selector-characters.R +++ b/tests/testthat/test-compiler-invalid-selector-characters.R @@ -1,4 +1,4 @@ -acontext("Invalid selector character validation") +context("Invalid selector character validation") # Test that selector names with special characters cause errors # Note: Selector names come from the VALUES in the data when using .variable/.value pattern From 32adadee5b257ac2a2ba19a3b7300dec2b47530b Mon Sep 17 00:00:00 2001 From: Gauarv Chaudhary Date: Wed, 26 Nov 2025 12:42:00 +0530 Subject: [PATCH 5/8] simplify checkSelectorNames: use grepl with regex pattern --- R/z_animintHelpers.R | 34 +++++++++------------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/R/z_animintHelpers.R b/R/z_animintHelpers.R index 853766c38..c3a8e312f 100644 --- a/R/z_animintHelpers.R +++ b/R/z_animintHelpers.R @@ -643,34 +643,18 @@ checkSingleShowSelectedValue <- function(selectors){ #' @return \code{NULL}. Throws error if invalid characters found. checkSelectorNames <- function(selectors){ selector.names <- names(selectors) - ## Characters that are invalid in CSS selectors and cause issues in browser - ## CSS selector special characters: # . : [ ] , + > ~ ( ) @ ! $ % ^ & * = | \ / ' " ` ? < > - ## Most problematic: # (hash/id selector), . (class selector), : (pseudo-class) - ## We check for these common problematic characters - invalid.chars <- c("#", "!", "@", "$", "%", "^", "&", "*", "=", "|", "\\", "/", "'", "\"", "`", "?", "<", ">", "(", ")", "[", "]") - - ## Check each selector name for invalid characters - ## Check if any invalid character appears in each selector name - has.invalid <- vapply(selector.names, function(name) { - any(vapply(invalid.chars, grepl, logical(1), x = name, fixed = TRUE)) - }, logical(1)) - + ## ] must be first in character class, [ can be anywhere after + invalid.pattern <- "[][#!@$%^&*=|/'\"`?<>()\\\\]" + has.invalid <- grepl(invalid.pattern, selector.names) if(any(has.invalid)){ invalid.names <- selector.names[has.invalid] - ## Find which character(s) are problematic for each name - problematic <- sapply(invalid.names, function(name) { - found.chars <- invalid.chars[sapply(invalid.chars, grepl, name, fixed = TRUE)] - paste0("'", found.chars, "'", collapse = ", ") - }) - - invalid.list <- paste(sprintf(" - '%s' contains %s", invalid.names, problematic), collapse = "\n") - error.msg <- sprintf( - "Invalid character(s) in selector name(s). Selector names cannot contain special characters that interfere with CSS selectors.\nThe following selector(s) contain invalid characters:\n%s\n\nPlease remove or replace these characters in your variable names.", - invalid.list - ) - - stop(error.msg) + stop( + "Invalid character(s) in selector name(s).\n", + "Selector names cannot contain special characters that interfere with CSS selectors.\n", + "The following selector(s) contain invalid characters:\n", + paste("-", invalid.names, collapse="\n"), + "\n\nPlease remove or replace these characters in your variable names.") } } From b426697452a46ff2a173e9701d4a772eadd31900 Mon Sep 17 00:00:00 2001 From: Gauarv Chaudhary Date: Mon, 15 Dec 2025 21:13:42 +0530 Subject: [PATCH 6/8] Use sprintf with single string literal per the feedback --- R/z_animintHelpers.R | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/R/z_animintHelpers.R b/R/z_animintHelpers.R index f9be1630e..97cce5a7f 100644 --- a/R/z_animintHelpers.R +++ b/R/z_animintHelpers.R @@ -618,12 +618,7 @@ checkSelectorNames <- function(selectors){ has.invalid <- grepl(invalid.pattern, selector.names) if(any(has.invalid)){ invalid.names <- selector.names[has.invalid] - stop( - "Invalid character(s) in selector name(s).\n", - "Selector names cannot contain special characters that interfere with CSS selectors.\n", - "The following selector(s) contain invalid characters:\n", - paste("-", invalid.names, collapse="\n"), - "\n\nPlease remove or replace these characters in your variable names.") + stop(sprintf("Invalid character(s) in selector name(s).\nSelector names cannot contain special characters that interfere with CSS selectors.\nThe following selector(s) contain invalid characters:\n%s\n\nPlease remove or replace these characters in your variable names.", paste("-", invalid.names, collapse="\n"))) } } From 9396309aaf6979329fae817e89689cd761f9a112 Mon Sep 17 00:00:00 2001 From: Gauarv Chaudhary Date: Mon, 15 Dec 2025 22:20:28 +0530 Subject: [PATCH 7/8] Simplify test: use single expect_error with full error message per the feedback --- .../test-compiler-invalid-selector-characters.R | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/tests/testthat/test-compiler-invalid-selector-characters.R b/tests/testthat/test-compiler-invalid-selector-characters.R index 3e3559fc9..fe4d93353 100644 --- a/tests/testthat/test-compiler-invalid-selector-characters.R +++ b/tests/testthat/test-compiler-invalid-selector-characters.R @@ -18,13 +18,7 @@ test_that("selector name with # causes error in clickSelects", { ) expect_error( animint2dir(viz, open.browser = FALSE), - "Invalid character\\(s\\) in selector name\\(s\\)", - info = "Selector names with '#' should cause an error" - ) - expect_error( - animint2dir(viz, open.browser = FALSE), - "# nearest neighbors", - info = "Error message should mention the problematic selector name" + "Invalid character\\(s\\) in selector name\\(s\\).*# nearest neighbors" ) }) @@ -44,13 +38,7 @@ test_that("selector name with @ causes error in clickSelects", { ) expect_error( animint2dir(viz, open.browser = FALSE), - "Invalid character\\(s\\) in selector name\\(s\\)", - info = "Selector names with '@' should cause an error" - ) - expect_error( - animint2dir(viz, open.browser = FALSE), - "model@version", - info = "Error message should mention the problematic selector" + "Invalid character\\(s\\) in selector name\\(s\\).*model@version1" ) }) From 2126b360ebd913824b3aa2d761f3295778e9a1c5 Mon Sep 17 00:00:00 2001 From: Gauarv Chaudhary Date: Fri, 19 Dec 2025 19:56:48 +0530 Subject: [PATCH 8/8] use full error messages and fixed=TRUE in tests per the feedback --- ...est-compiler-invalid-selector-characters.R | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/tests/testthat/test-compiler-invalid-selector-characters.R b/tests/testthat/test-compiler-invalid-selector-characters.R index fe4d93353..29a5594de 100644 --- a/tests/testthat/test-compiler-invalid-selector-characters.R +++ b/tests/testthat/test-compiler-invalid-selector-characters.R @@ -18,7 +18,13 @@ test_that("selector name with # causes error in clickSelects", { ) expect_error( animint2dir(viz, open.browser = FALSE), - "Invalid character\\(s\\) in selector name\\(s\\).*# nearest neighbors" + "Invalid character(s) in selector name(s). +Selector names cannot contain special characters that interfere with CSS selectors. +The following selector(s) contain invalid characters: +- # nearest neighbors + +Please remove or replace these characters in your variable names.", + fixed = TRUE ) }) @@ -38,7 +44,13 @@ test_that("selector name with @ causes error in clickSelects", { ) expect_error( animint2dir(viz, open.browser = FALSE), - "Invalid character\\(s\\) in selector name\\(s\\).*model@version1" + "Invalid character(s) in selector name(s). +Selector names cannot contain special characters that interfere with CSS selectors. +The following selector(s) contain invalid characters: +- model@version1 + +Please remove or replace these characters in your variable names.", + fixed = TRUE ) }) @@ -56,8 +68,13 @@ test_that("selector name with ! causes error", { ) expect_error( animint2dir(viz, open.browser = FALSE), - "Invalid character\\(s\\) in selector name\\(s\\)", - info = "Selector names with '!' should cause an error" + "Invalid character(s) in selector name(s). +Selector names cannot contain special characters that interfere with CSS selectors. +The following selector(s) contain invalid characters: +- model!important + +Please remove or replace these characters in your variable names.", + fixed = TRUE ) }) @@ -105,10 +122,9 @@ test_that("multiple values with invalid characters all reported", { clickSelects = c(regularization = "parameter") ) ) - error_msg <- tryCatch( + expect_error( animint2dir(viz, open.browser = FALSE), - error = function(e) as.character(e$message) + "Invalid character(s) in selector name(s).\nSelector names cannot contain special characters that interfere with CSS selectors.\nThe following selector(s) contain invalid characters:\n- #bad\n- !worse\n\nPlease remove or replace these characters in your variable names.", + fixed = TRUE ) - expect_match(error_msg, "Invalid character", info = "Should report invalid characters") - expect_match(error_msg, "#bad|!worse", info = "Should mention at least one problematic selector") })