rstudio / htmltools

Tools for HTML generation and output
https://rstudio.github.io/htmltools/
215 stars 68 forks source link

Control `.noWS` via global option #243

Closed gadenbuie closed 3 years ago

gadenbuie commented 3 years ago

I often want the output of a tag function to not include white space, but setting .noWs = c("outside", "inside") in multiple tags can be a bit annoying.

It would be nice to be able to control this setting via a global option, e.g. options(htmltools.noWS = c("outside", "inside")).

wch commented 3 years ago

This unfortunately would result in a lot of unexpected behavior, as it would alter the behavior of code that's buried inside of other functions.

One possibility is that you could generate wrapper functions like this:

span2 <- function(...) {
  span(..., .noWS = c("outside", "inside"))
}

It may make sense to include functions like this in htmltools, although we'd have to think carefully about names.

Or there could be a function like this:

wrapTags <- function(.noWS) {
  force(.noWS)
  lapply(tags, function(tag) {
    function(...) {
      tag(..., .noWS = .noWS)
    }
  })
}

# Usage
tags2 <- wrapTags(.noWS = c("outside", "inside"))
tags2$div(tags2$span("a"), tags2$span("b"))
#> <div><span>a</span><span>b</span></div>
gadenbuie commented 3 years ago

@wch that make sense, I had a feeling it wasn't going to be as simple as adding a global option. I like the sketch you outlined in wrapTags() and wondered if something similar added into withTags() could accomplish the same thing. I think it works nicely, so I submitted it in #245, but I'm not attached to the approach if there are other complications I haven't anticipated.