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

htmltools::tag() and varArgs not documented #431

Open
JosiahParry opened this issue Apr 4, 2024 · 2 comments
Open

htmltools::tag() and varArgs not documented #431

JosiahParry opened this issue Apr 4, 2024 · 2 comments

Comments

@JosiahParry
Copy link

I am trying to understand how to create a custom tag for a component library. However the tag() function does not have an example. It is unclear what the structure of varArgs argument should be.

htmltools/R/tags.R

Lines 639 to 667 in 038ef7b

#' tags$html(
#' tags$head(
#' tags$title('My first page')
#' ),
#' tags$body(
#' h1('My first heading'),
#' p('My first paragraph, with some ', strong('bold'), ' text.'),
#' div(
#' id = 'myDiv', class = 'simpleDiv',
#' 'Here is a div with some attributes.'
#' )
#' )
#' )
#'
#' # html5 <audio> with boolean control attribute
#' # https://www.w3.org/TR/html5/infrastructure.html#sec-boolean-attributes
#' tags$audio(
#' controls = NA,
#' tags$source(
#' src = "myfile.wav",
#' type = "audio/wav"
#' )
#' )
#'
#' # suppress the whitespace between tags
#' tags$span(
#' tags$strong("I'm strong", .noWS="outside")
#' )
#'

@JosiahParry
Copy link
Author

I also noticed that varArgs argument might be out of date or could benefit from better input checking

"List of tag attributes and children."

htmltools::tag("hello-world", list(c("a", "b", "c")))
#> Error in writeImpl(text): Text to be written must be a length-one character vector

@gadenbuie
Copy link
Member

Agreed, we could definitely improve both the docs to add an example and the error message.

varArgs should be a list where the primary restriction is that any character items in the list must be scalar. In other words list("a", "b", "c") or list("a b c") will work but list(c("a", "b", "c")) will not. So Text to be written must be a length-one character vector is accurate but not very helpful.

The hard part in making the error message better is that it happens at a much lower level than where varArgs is provided and it's discovered as a part of a recursive walk of the varArgs list.

library(htmltools)

tag("hello-world", list("a", "b", "c"))
#> <hello-world>
#> a
#> b
#> c
#> </hello-world>

tag("hello-world", list(div("a", "b"), span("c")))
#> <hello-world>
#> <div>
#> a
#> b
#> </div>
#> <span>c</span>
#> </hello-world>

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