Skip to content

Commit

Permalink
Provide better error message when adding unnamed attrib values (#229)
Browse files Browse the repository at this point in the history
Co-authored-by: Barret Schloerke <[email protected]>
Co-authored-by: Carson Sievert <[email protected]>
  • Loading branch information
3 people authored Apr 21, 2021
1 parent ca8670c commit c37636e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

* Closed #197: Fixed rendering of boolean attributes in <script> tags rendered via renderDependencies() (#197, thanks @atusy).

* Closed #222: Unnamed attributes are no longer allowed to be appended via `tagAppendAttribs()`. When trying to print unnamed tag attribs, a better error message is provided. (#229)


# htmltools 0.5.1.1

Expand Down
23 changes: 18 additions & 5 deletions R/tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,14 @@ tagAddRenderHook <- function(tag, func, replace = FALSE) {
#' @export
tagAppendAttributes <- function(tag, ...) {
throw_if_tag_function(tag)
tag$attribs <- c(tag$attribs, dropNullsOrEmpty(dots_list(...)))
newAttribs <- dropNullsOrEmpty(dots_list(...))
if (any(!nzchar(names2(newAttribs)))) {
stop(
"At least one of the new attribute values did not have a name.\n",
"Did you forget to include an attribute name?"
)
}
tag$attribs <- c(tag$attribs, newAttribs)
tag
}

Expand Down Expand Up @@ -516,9 +523,7 @@ throw_if_tag_function <- function(tag) {
tag <- function(`_tag_name`, varArgs, .noWS = NULL, .renderHook = NULL) {
validateNoWS(.noWS)
# Get arg names; if not a named list, use vector of empty strings
varArgsNames <- names(varArgs)
if (is.null(varArgsNames))
varArgsNames <- character(length=length(varArgs))
varArgsNames <- names2(varArgs)

# Named arguments become attribs, dropping NULL and length-0 values
named_idx <- nzchar(varArgsNames)
Expand Down Expand Up @@ -607,9 +612,17 @@ tagWrite <- function(tag, textWriter, indent=0, eol = "\n") {
textWriter$write(concat8("<", tag$name))

attribs <- flattenTagAttribs(tag$attribs)
attribNames <- names2(attribs)
if (any(!nzchar(attribNames))) {
# Can not display attrib without a key
stop(
"A tag's attribute value did not have a name.\n",
"Did you forget to name all of your attribute values?"
)
}

# write attributes
for (attrib in names(attribs)) {
for (attrib in attribNames) {
attribValue <- attribs[[attrib]]
if (!is.na(attribValue)) {
if (is.logical(attribValue))
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/test-tags.r
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,25 @@ test_that("Adding attributes to tags", {
)
})

test_that("Adding unnamed attributes creates a warning", {
expect_error(
tagAppendAttributes(
tags$div(),
"value"
),
"include an attribute name"
)

x <- div()
x$attribs[[1]] <- "value"
expect_error(
print(x),
"name all of your attribute values"
)
})



test_that("Testing for attributes on tags", {
t1 <- tags$div("foo", class = "c1", class = "c2", id = "foo")

Expand Down

0 comments on commit c37636e

Please sign in to comment.