From 94faa554f6e1e35750fef889d14f1c99b5024728 Mon Sep 17 00:00:00 2001 From: Barret Schloerke Date: Thu, 22 Apr 2021 15:10:33 -0400 Subject: [PATCH 1/2] Allow tag query `$*Class()` methods to no-op on length 0 inputs --- R/tag_query.R | 26 ++++++++++++++++++++------ man/tagQuery.Rd | 4 ++-- tests/testthat/test-tag-query.R | 15 ++++++++++++++- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/R/tag_query.R b/R/tag_query.R index 7280e212..755189f0 100644 --- a/R/tag_query.R +++ b/R/tag_query.R @@ -732,12 +732,6 @@ tagQuery_ <- function( tagQueryClassRemove(selected_, class) invisible(self) }, - #' * `$hasClass(class)`: Determine whether the selected elements have a - #' given class. Returns a vector of logical values. - hasClass = function(class) { - rebuild_() - tagQueryClassHas(selected_, class) - }, #' * `$toggleClass(class)`: If the a class value is missing, add it. If #' a class value already exists, remove it. toggleClass = function(class) { @@ -745,6 +739,12 @@ tagQuery_ <- function( tagQueryClassToggle(selected_, class) invisible(self) }, + #' * `$hasClass(class)`: Determine whether the selected elements have a + #' given class. Returns a vector of logical values. + hasClass = function(class) { + rebuild_() + tagQueryClassHas(selected_, class) + }, #' * `$addAttrs(...)`: Add named attributes to all selected children. #' Similar to [`tagAppendAttributes()`]. @@ -1289,6 +1289,11 @@ joinCssClass <- function(classes) { } # return list of logical values telling if the classes exists tagQueryClassHas <- function(els, class) { + # Quit early if class == NULL | character(0) + if (length(class) == 0) { + return(rep(FALSE, length(els))) + } + classes <- getCssClass(class) unlist( tagQueryLapply(els, function(el) { @@ -1305,6 +1310,9 @@ tagQueryClassHas <- function(els, class) { } # add classes that don't already exist tagQueryClassAdd <- function(els, class) { + # Quit early if class == NULL | character(0) + if (length(class) == 0) return() + classes <- getCssClass(class) tagQueryWalk(els, function(el) { if (!isTagEnv(el)) return() @@ -1316,6 +1324,9 @@ tagQueryClassAdd <- function(els, class) { } # remove classes that exist tagQueryClassRemove <- function(els, class) { + # Quit early if class == NULL | character(0) + if (length(class) == 0) return() + classes <- getCssClass(class) tagQueryWalk(els, function(el) { if (!isTagEnv(el)) return() @@ -1328,6 +1339,9 @@ tagQueryClassRemove <- function(els, class) { } # toggle class existence depending on if they already exist or not tagQueryClassToggle <- function(els, class) { + # Quit early if class == NULL | character(0) + if (length(class) == 0) return() + classes <- getCssClass(class) tagQueryWalk(els, function(el) { if (!isTagEnv(el)) return() diff --git a/man/tagQuery.Rd b/man/tagQuery.Rd index 36f62110..5866c7b8 100644 --- a/man/tagQuery.Rd +++ b/man/tagQuery.Rd @@ -186,10 +186,10 @@ selected items set to the top level tag objects. elements. \item \verb{$removeClass(class)}: Removes a set of class values from all of the selected elements. -\item \verb{$hasClass(class)}: Determine whether the selected elements have a -given class. Returns a vector of logical values. \item \verb{$toggleClass(class)}: If the a class value is missing, add it. If a class value already exists, remove it. +\item \verb{$hasClass(class)}: Determine whether the selected elements have a +given class. Returns a vector of logical values. \item \verb{$addAttrs(...)}: Add named attributes to all selected children. Similar to \code{\link[=tagAppendAttributes]{tagAppendAttributes()}}. \item \verb{$removeAttrs(attrs)}: Removes the provided attributes in each of diff --git a/tests/testthat/test-tag-query.R b/tests/testthat/test-tag-query.R index 9171a9b0..2dba27c9 100644 --- a/tests/testthat/test-tag-query.R +++ b/tests/testthat/test-tag-query.R @@ -358,6 +358,20 @@ test_that("tagQuery()$addClass()", { expect_equal(x$selected()[[1]]$attribs$class, "inner test-class") + expect_silent({ + x$addClass(NULL) + x$removeClass(NULL) + x$toggleClass(NULL) + expect_equal(x$hasClass(NULL), c(FALSE)) + }) + + expect_silent({ + x$addClass(character(0)) + x$removeClass(character(0)) + x$toggleClass(character(0)) + expect_equal(x$hasClass(character(0)), c(FALSE)) + }) + }) test_that("tagQuery()$hasClass(), $toggleClass(), $removeClass()", { @@ -684,7 +698,6 @@ test_that("tagQuery() objects can not inherit from mixed objects", { - # From 8868275d9168dd1263f720c4b16db31d9c439a7b Mon Sep 17 00:00:00 2001 From: Barret Schloerke Date: Thu, 22 Apr 2021 15:16:28 -0400 Subject: [PATCH 2/2] Move back docs --- R/tag_query.R | 12 ++++++------ man/tagQuery.Rd | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/R/tag_query.R b/R/tag_query.R index 755189f0..da07f9a5 100644 --- a/R/tag_query.R +++ b/R/tag_query.R @@ -732,6 +732,12 @@ tagQuery_ <- function( tagQueryClassRemove(selected_, class) invisible(self) }, + #' * `$hasClass(class)`: Determine whether the selected elements have a + #' given class. Returns a vector of logical values. + hasClass = function(class) { + rebuild_() + tagQueryClassHas(selected_, class) + }, #' * `$toggleClass(class)`: If the a class value is missing, add it. If #' a class value already exists, remove it. toggleClass = function(class) { @@ -739,12 +745,6 @@ tagQuery_ <- function( tagQueryClassToggle(selected_, class) invisible(self) }, - #' * `$hasClass(class)`: Determine whether the selected elements have a - #' given class. Returns a vector of logical values. - hasClass = function(class) { - rebuild_() - tagQueryClassHas(selected_, class) - }, #' * `$addAttrs(...)`: Add named attributes to all selected children. #' Similar to [`tagAppendAttributes()`]. diff --git a/man/tagQuery.Rd b/man/tagQuery.Rd index 5866c7b8..36f62110 100644 --- a/man/tagQuery.Rd +++ b/man/tagQuery.Rd @@ -186,10 +186,10 @@ selected items set to the top level tag objects. elements. \item \verb{$removeClass(class)}: Removes a set of class values from all of the selected elements. -\item \verb{$toggleClass(class)}: If the a class value is missing, add it. If -a class value already exists, remove it. \item \verb{$hasClass(class)}: Determine whether the selected elements have a given class. Returns a vector of logical values. +\item \verb{$toggleClass(class)}: If the a class value is missing, add it. If +a class value already exists, remove it. \item \verb{$addAttrs(...)}: Add named attributes to all selected children. Similar to \code{\link[=tagAppendAttributes]{tagAppendAttributes()}}. \item \verb{$removeAttrs(attrs)}: Removes the provided attributes in each of