diff --git a/NEWS.md b/NEWS.md index 87eaa55d..b5dd57d4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,10 @@ * Closed #305: `htmlPreserve()` no longer uses _inline_ code blocks for Pandoc's raw attribute feature when used inside a _non_-inline knitr/rmarkdown code chunk, and as a result, in this case, an additional `

` tag is no longer wrapped around the HTML content. (#306) +## New Features & Improvements + +* The `HTML()` function has been simplified. It now only adds an `"html"` class and no longer adds an `"html"` attribute. (#315) + ## Bug fixes * Closed #301: `tagQuery()` was failing to copy all `tagList()` html dependencies within nest child tag lists. `tagQuery()` will now relocate html dependencies as child objects. (#302) diff --git a/R/tags.R b/R/tags.R index 7dd49dea..1e10e4e5 100644 --- a/R/tags.R +++ b/R/tags.R @@ -259,7 +259,7 @@ format.html <- function(x, ...) { } normalizeText <- function(text) { - if (!is.null(attr(text, "html", TRUE))) + if (inherits(text, "html")) text else htmlEscape(text, attribute=FALSE) @@ -1154,9 +1154,8 @@ resolveFunctionalDependencies <- function(dependencies) { #' Marks the given text as HTML, which means the [tag] functions will know #' not to perform HTML escaping on it. #' -#' @param text The text value to mark with HTML -#' @param ... Any additional values to be converted to character and -#' concatenated together +#' @param ... Text to mark with HTML. Any additional values after the first will +#' to be converted to character, and all will be concatenated together. #' @param .noWS Character vector used to omit some of the whitespace that would #' normally be written around this HTML. Valid options include `before`, #' `after`, and `outside` (equivalent to `before` and @@ -1168,12 +1167,14 @@ resolveFunctionalDependencies <- function(dependencies) { #' cat(as.character(el)) #' #' @export -HTML <- function(text, ..., .noWS = NULL) { - htmlText <- c(text, as.character(dots_list(...))) +HTML <- function(..., .noWS = NULL) { + htmlText <- as.character(dots_list(...)) + if (length(htmlText) == 0) { + stop("HTML() requires at least one item") + } htmlText <- paste8(htmlText, collapse=" ") - attr(htmlText, "html") <- TRUE attr(htmlText, "noWS") <- .noWS - class(htmlText) <- c("html", "character") + class(htmlText) <- "html" htmlText } diff --git a/man/HTML.Rd b/man/HTML.Rd index 563727ae..6cf0efb9 100644 --- a/man/HTML.Rd +++ b/man/HTML.Rd @@ -4,13 +4,11 @@ \alias{HTML} \title{Mark Characters as HTML} \usage{ -HTML(text, ..., .noWS = NULL) +HTML(..., .noWS = NULL) } \arguments{ -\item{text}{The text value to mark with HTML} - -\item{...}{Any additional values to be converted to character and -concatenated together} +\item{...}{Text to mark with HTML. Any additional values after the first will +to be converted to character, and all will be concatenated together.} \item{.noWS}{Character vector used to omit some of the whitespace that would normally be written around this HTML. Valid options include \code{before}, diff --git a/tests/testthat/test-tags.r b/tests/testthat/test-tags.r index 1be6481a..87637c0c 100644 --- a/tests/testthat/test-tags.r +++ b/tests/testthat/test-tags.r @@ -211,13 +211,17 @@ test_that("Adding child tags", { expect_identical(t2a, t2) - # tagSetChildren preserves attributes - x <- tagSetChildren(div(), HTML("text")) - expect_identical(attr(x$children[[1]], "html", TRUE), TRUE) + # tagSetChildren preserves classes and attributes + txt <- HTML("text") + attr(txt, "myattr") <- "foo" + x <- tagSetChildren(div(), txt) + expect_true(inherits(x$children[[1]], "html")) + expect_identical(attr(x$children[[1]], "myattr", TRUE), "foo") # tagAppendChildren preserves attributes - x <- tagAppendChildren(div(), HTML("text")) - expect_identical(attr(x$children[[1]], "html", TRUE), TRUE) + x <- tagAppendChildren(div(), txt) + expect_true(inherits(x$children[[1]], "html")) + expect_identical(attr(x$children[[1]], "myattr", TRUE), "foo") }) @@ -389,22 +393,25 @@ test_that("tag/s with invalid noWS fails fast", { }) test_that("Attributes are preserved", { - # HTML() adds an attribute to the data structure (note that this is - # different from the 'attribs' field in the list) - x <- HTML("&&") - expect_identical(attr(x, "html", TRUE), TRUE) - expect_equivalent(format(x), "&&") + html_txt <- HTML("&&") + attr(html_txt, "myattr") <- "foo" + + expect_true(inherits(html_txt, "html")) + expect_identical(attr(html_txt, "myattr", TRUE), "foo") + expect_equivalent(format(html_txt), "&&") # Make sure attributes are preserved when wrapped in other tags - x <- div(HTML("&&")) - expect_equivalent(x$children[[1]], HTML("&&")) - expect_identical(attr(x$children[[1]], "html", TRUE), TRUE) + x <- div(html_txt) + expect_equivalent(x$children[[1]], html_txt) + expect_true(inherits(x$children[[1]], "html")) + expect_identical(attr(x$children[[1]], "myattr", TRUE), "foo") expect_equivalent(format(x), "

&&
") # Deeper nesting - x <- div(p(HTML("&&"))) - expect_equivalent(x$children[[1]]$children[[1]], HTML("&&")) - expect_identical(attr(x$children[[1]]$children[[1]], "html", TRUE), TRUE) + x <- div(p(html_txt)) + expect_equivalent(x$children[[1]]$children[[1]], html_txt) + expect_true(inherits(x$children[[1]]$children[[1]], "html")) + expect_identical(attr(x$children[[1]]$children[[1]], "myattr", TRUE), "foo") expect_equivalent(format(x), "
\n

&&

\n
") })