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

Make sure NA attributes are rendered as blank. Fixes #99 #100

Merged
merged 1 commit into from
Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: htmltools
Type: Package
Title: Tools for HTML
Version: 0.3.6.9000
Version: 0.3.6.9001
Date: 2017-04-26
Author: RStudio, Inc.
Maintainer: Joe Cheng <joe@rstudio.com>
Expand Down
6 changes: 5 additions & 1 deletion NEWS
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
htmltools 0.3.6.9000
htmltools 0.3.6.9001
--------------------------------------------------------------------------------

* Updated RcppExports for new version of Rcpp. (#93)

* `as.character.shiny.tags()` will handle non-ASCII attributes correctly if they
are not encoded in native encoding.

* Fixed #99: `NA` attributes were sometimes rendered as `"NA"` in the HTML,
instead of being blank. (#100)


htmltools 0.3.6
--------------------------------------------------------------------------------

Expand Down
14 changes: 12 additions & 2 deletions R/tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,18 @@ tagWrite <- function(tag, textWriter, indent=0, eol = "\n") {
attribs <- lapply(tag$attribs, as.character)
# concatenate attributes
# split() is very slow, so avoid it if possible
if (anyDuplicated(names(attribs)))
attribs <- lapply(split(attribs, names(attribs)), paste, collapse = " ")
if (anyDuplicated(names(attribs))) {
attribs <- lapply(split(attribs, names(attribs)), function(x) {
na_idx <- is.na(x)
if (any(na_idx)) {
if (all(na_idx)) {
return(NA)
}
x <- x[!na_idx]
}
paste(x, collapse = " ")
})
}

# write attributes
for (attrib in names(attribs)) {
Expand Down
36 changes: 36 additions & 0 deletions tests/testthat/test-tags.r
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,42 @@ test_that("Getting attributes from tags", {
)
})

test_that("NA attributes are rendered correctly", {
expect_identical(
as.character(tags$div("text", foo = NA)),
'<div foo>text</div>'
)
expect_identical(
as.character(tags$div("text", class = "a", foo = NA)),
'<div class="a" foo>text</div>'
)
expect_identical(
as.character(tags$div("text", class = "a", foo = NA, class = "b")),
'<div class="a b" foo>text</div>'
)

# Multiple NA's are coalesced
expect_identical(
as.character(tags$div("text", class = "a", foo = NA, class = "b", foo = NA)),
'<div class="a b" foo>text</div>'
)

# A non-NA value supersedes NA
expect_identical(
as.character(tags$div("text", class = "a", foo = NA, foo = "b")),
'<div class="a" foo="b">text</div>'
)
expect_identical(
as.character(tags$div("text", class = "a", foo = "b", foo = NA, foo = "c")),
'<div class="a" foo="b c">text</div>'
)
expect_identical(
as.character(tags$div("text", class = "a", foo = "b", foo = NA, foo = NA, foo = "c")),
'<div class="a" foo="b c">text</div>'
)
})


test_that("Flattening a list of tags", {
# Flatten a nested list
nested <- list(
Expand Down