rstudio / htmltools

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

Add .noWS argument to withTags() #245

Closed gadenbuie closed 3 years ago

gadenbuie commented 3 years ago

Fixes #243

Offered as a possible solution to #243. Rather than adding a global option to control .noWS, this suggestion adds .noWS to withTags(), using the sketch from @wch in https://github.com/rstudio/htmltools/issues/243#issuecomment-833102373.

If .noWS isn't set, withTags() works as before.

withTags(
  div(
    class = "myclass",
    h3("header"),
    p("One", strong(span("two")), "three")
  )
)
#> <div class="myclass">
#>   <h3>header</h3>
#>   <p>
#>     One
#>     <strong>
#>       <span>two</span>
#>     </strong>
#>     three
#>   </p>
#> </div>

But setting .noWS on withTags() sets the whitespace option for all tags in the withTags() call.

withTags(
  div(
    class = "myclass",
    h3("header"),
    p("One", strong(span("two")), "three")
  ),
  .noWS = c("outside", "inside")
)
#> <div class="myclass"><h3>header</h3><p>One<strong><span>two</span></strong>three</p></div>

You can still set .noWS on individual tags to override the whitespace option for an individual tag.

withTags(
  div(
    class = "myclass",
    h3("header"),
    p("One", strong(span("two"), .noWS = "inside"), "three")
  ),
  .noWS = c("outside")
)
#> <div class="myclass"><h3>header</h3><p>
#>     One
#>     <strong><span>two</span></strong>
#>     three
#>   </p></div>

If this seems like a reasonable approach, I can follow up with additional tests.

wch commented 3 years ago

Can you add some unit tests?

gadenbuie commented 3 years ago

I think the failing checks were just unlucky; they didn't make it past the setup step on macOS. I can't re-run the jobs but I suspect the checks would pass if someone were to do that for me. 😇

schloerke commented 3 years ago

Checks pass. @wch , Ok to merge?