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

Consider adding tagHasClass() #390

Open
daattali opened this issue Jul 21, 2023 · 2 comments
Open

Consider adding tagHasClass() #390

daattali opened this issue Jul 21, 2023 · 2 comments

Comments

@daattali
Copy link
Contributor

Similarly to #389 , parsing out the classes of a tag in order to know if it contains a specific class is something I do often. I've seen in some clients code that they also try to do it, but I've seen it implemented wrongly many times (often people do a simple grep search within the classes string, which is incorrect).

I have the following function:

has_class <- function(tag, class) {
  if (!inherits(tag, "shiny.tag")) {
    stop("has_class: `tag` must be a shiny tag")
  }
  if (!nzchar(class)) {
    stop("has_class: `class` must be a non-empty string")
  }
  if (grepl("\\s", class)) {
    stop("has_class: `class` cannot contain any whitespace")
  }
  
  classes <- htmltools::tagGetAttribute(tag, "class")
  if (is.null(classes)) {
    return(FALSE)
  }
  classes <- strsplit(classes, "\\s+")[[1]]
  

Would you accept a PR for this function, or would you prefer not to? I'm also open to changing the behaviour (when to error vs return false / what to do when the class is an empty string / doing less error checking)

@cpsievert
Copy link
Collaborator

Now that tagQuery(div(class = "foo"))$hasClass("foo") is a thing, it doesn't seem worth adding (pinging @schloerke to see if he agrees)

@daattali
Copy link
Contributor Author

That's true, if you think it's redundant then I'd understand. Although I still think it's a bit verbose for doing an operation that is common. Perhaps it's not common for other users though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants