From 9a0c82d4ead9091d05a774fbda9a59e7896855eb Mon Sep 17 00:00:00 2001 From: Winston Chang Date: Wed, 16 Feb 2022 13:29:05 -0600 Subject: [PATCH] Simplify HTML() --- R/tags.R | 9 ++++----- tests/testthat/test-tags.r | 39 ++++++++++++++++++++++---------------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/R/tags.R b/R/tags.R index 7dd49dea..ccf16637 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) @@ -1168,12 +1168,11 @@ 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(...)) htmlText <- paste8(htmlText, collapse=" ") - attr(htmlText, "html") <- TRUE attr(htmlText, "noWS") <- .noWS - class(htmlText) <- c("html", "character") + class(htmlText) <- "html" htmlText } 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
") })