Skip to content

Commit

Permalink
Allow tag query $*Class() methods to no-op on length 0 inputs (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
schloerke authored Apr 22, 2021
1 parent c37636e commit 0f95558
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
14 changes: 14 additions & 0 deletions R/tag_query.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
15 changes: 14 additions & 1 deletion tests/testthat/test-tag-query.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()", {
Expand Down Expand Up @@ -684,7 +698,6 @@ test_that("tagQuery() objects can not inherit from mixed objects", {






#

0 comments on commit 0f95558

Please sign in to comment.