Skip to content

Commit ef998f1

Browse files
committed
Simplify HTML()
1 parent 9490b62 commit ef998f1

File tree

4 files changed

+39
-29
lines changed

4 files changed

+39
-29
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
* 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 `<p>` tag is no longer wrapped around the HTML content. (#306)
66

7+
## New Features & Improvements
8+
9+
* The `HTML()` function has been simplified. It now only adds an `"html"` class and no longer adds an `"html"` attribute. (#315)
10+
711
## Bug fixes
812

913
* 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)

R/tags.R

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ format.html <- function(x, ...) {
259259
}
260260

261261
normalizeText <- function(text) {
262-
if (!is.null(attr(text, "html", TRUE)))
262+
if (inherits(text, "html"))
263263
text
264264
else
265265
htmlEscape(text, attribute=FALSE)
@@ -1154,9 +1154,8 @@ resolveFunctionalDependencies <- function(dependencies) {
11541154
#' Marks the given text as HTML, which means the [tag] functions will know
11551155
#' not to perform HTML escaping on it.
11561156
#'
1157-
#' @param text The text value to mark with HTML
1158-
#' @param ... Any additional values to be converted to character and
1159-
#' concatenated together
1157+
#' @param ... Text to mark with HTML. Any additional values after the first will
1158+
#' to be converted to character, and all will be concatenated together.
11601159
#' @param .noWS Character vector used to omit some of the whitespace that would
11611160
#' normally be written around this HTML. Valid options include `before`,
11621161
#' `after`, and `outside` (equivalent to `before` and
@@ -1168,12 +1167,14 @@ resolveFunctionalDependencies <- function(dependencies) {
11681167
#' cat(as.character(el))
11691168
#'
11701169
#' @export
1171-
HTML <- function(text, ..., .noWS = NULL) {
1172-
htmlText <- c(text, as.character(dots_list(...)))
1170+
HTML <- function(..., .noWS = NULL) {
1171+
htmlText <- as.character(dots_list(...))
1172+
if (length(htmlText) == 0) {
1173+
stop("HTML() requires at least one item")
1174+
}
11731175
htmlText <- paste8(htmlText, collapse=" ")
1174-
attr(htmlText, "html") <- TRUE
11751176
attr(htmlText, "noWS") <- .noWS
1176-
class(htmlText) <- c("html", "character")
1177+
class(htmlText) <- "html"
11771178
htmlText
11781179
}
11791180

man/HTML.Rd

Lines changed: 3 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-tags.r

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,17 @@ test_that("Adding child tags", {
211211
expect_identical(t2a, t2)
212212

213213

214-
# tagSetChildren preserves attributes
215-
x <- tagSetChildren(div(), HTML("text"))
216-
expect_identical(attr(x$children[[1]], "html", TRUE), TRUE)
214+
# tagSetChildren preserves classes and attributes
215+
txt <- HTML("text")
216+
attr(txt, "myattr") <- "foo"
217+
x <- tagSetChildren(div(), txt)
218+
expect_true(inherits(x$children[[1]], "html"))
219+
expect_identical(attr(x$children[[1]], "myattr", TRUE), "foo")
217220

218221
# tagAppendChildren preserves attributes
219-
x <- tagAppendChildren(div(), HTML("text"))
220-
expect_identical(attr(x$children[[1]], "html", TRUE), TRUE)
222+
x <- tagAppendChildren(div(), txt)
223+
expect_true(inherits(x$children[[1]], "html"))
224+
expect_identical(attr(x$children[[1]], "myattr", TRUE), "foo")
221225
})
222226

223227

@@ -389,22 +393,25 @@ test_that("tag/s with invalid noWS fails fast", {
389393
})
390394

391395
test_that("Attributes are preserved", {
392-
# HTML() adds an attribute to the data structure (note that this is
393-
# different from the 'attribs' field in the list)
394-
x <- HTML("<tag>&&</tag>")
395-
expect_identical(attr(x, "html", TRUE), TRUE)
396-
expect_equivalent(format(x), "<tag>&&</tag>")
396+
html_txt <- HTML("<tag>&&</tag>")
397+
attr(html_txt, "myattr") <- "foo"
398+
399+
expect_true(inherits(html_txt, "html"))
400+
expect_identical(attr(html_txt, "myattr", TRUE), "foo")
401+
expect_equivalent(format(html_txt), "<tag>&&</tag>")
397402

398403
# Make sure attributes are preserved when wrapped in other tags
399-
x <- div(HTML("<tag>&&</tag>"))
400-
expect_equivalent(x$children[[1]], HTML("<tag>&&</tag>"))
401-
expect_identical(attr(x$children[[1]], "html", TRUE), TRUE)
404+
x <- div(html_txt)
405+
expect_equivalent(x$children[[1]], html_txt)
406+
expect_true(inherits(x$children[[1]], "html"))
407+
expect_identical(attr(x$children[[1]], "myattr", TRUE), "foo")
402408
expect_equivalent(format(x), "<div><tag>&&</tag></div>")
403409

404410
# Deeper nesting
405-
x <- div(p(HTML("<tag>&&</tag>")))
406-
expect_equivalent(x$children[[1]]$children[[1]], HTML("<tag>&&</tag>"))
407-
expect_identical(attr(x$children[[1]]$children[[1]], "html", TRUE), TRUE)
411+
x <- div(p(html_txt))
412+
expect_equivalent(x$children[[1]]$children[[1]], html_txt)
413+
expect_true(inherits(x$children[[1]]$children[[1]], "html"))
414+
expect_identical(attr(x$children[[1]]$children[[1]], "myattr", TRUE), "foo")
408415
expect_equivalent(format(x), "<div>\n <p><tag>&&</tag></p>\n</div>")
409416
})
410417

0 commit comments

Comments
 (0)