Skip to content

Commit

Permalink
Allow highlighting of some empty expressions
Browse files Browse the repository at this point in the history
Improved version of #1307 accounting for problems caused by empty expressions consisting of empty
strings or whitespace only.

Prevents highlight markup from being stripped from code that consists entirely of code comments (only
lines beginning with #'s).

A partial fix for #1296, but would still not syntax
highlight code chunks that resulted in syntax
errors (e.g., when knitr chunk options collapse =
TRUE and comment = NA are set)
  • Loading branch information
wkmor1 committed Apr 24, 2020
1 parent c181e5c commit 2513c80
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions R/highlight.r
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ highlight_text <- function(text) {
error = function(e) NULL
)

# Failed to parse, or yielded empty expression
if (length(expr) == 0) {
# Failed to parse or is white-space only/empty
if (is.null(expr) || !nchar(trimws(text))) {
return(escape_html(text))
}

Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-highlight.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,20 @@ test_that("unparsed code is still escaped", {
expect_equal(highlight_text("<"), "&lt;")
})

test_that("code consisting of comments only is not stripped of tags", {
scoped_package_context("test")

expect_equal(highlight_text("#"), "<span class='co'>#</span>")
})

test_that("code consisting of an empty string is parsed correctly", {
scoped_package_context("test")

expect_equal(highlight_text(""), "")
})

test_that("code consisting of white-space only is parsed correctly", {
scoped_package_context("test")

expect_equal(highlight_text("\n\t"), "\n\t")
})

0 comments on commit 2513c80

Please sign in to comment.