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

Allow tag query $*Class() methods to no-op on length 0 inputs #236

Merged
merged 2 commits into from
Apr 22, 2021
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
26 changes: 20 additions & 6 deletions R/tag_query.R
Original file line number Diff line number Diff line change
Expand Up @@ -732,19 +732,19 @@ 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) {
rebuild_()
tagQueryClassToggle(selected_, class)
invisible(self)
},
#' * `$hasClass(class)`: Determine whether the selected elements have a
schloerke marked this conversation as resolved.
Show resolved Hide resolved
#' 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()`].
Expand Down 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
4 changes: 2 additions & 2 deletions man/tagQuery.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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", {






#