htmlTemplate() errors out with CRLF files

Open
#164 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
web-dev

Research direction

Start in R/template.R at the file-reading code around lines 27–28 and the parse(text = ...) call around line 52. Reproduce the issue with a CRLF-containing template like the examples, then verify that htmlTemplate() processes it without parse errors while preserving the intended template content.

Written by the indexing model from the issue text.

Description

htmlTemplate() reads files byte-for-byte (and so, on Windows, may contain \r\n)

https://github.com/rstudio/htmltools/blob/3b7fb836a14fe32fa9e4864e71b7681d9eebe3af/R/template.R#L27-L28

And after a bit processing, pieces of that string will reach parse(text = ...)

https://github.com/rstudio/htmltools/blob/3b7fb836a14fe32fa9e4864e71b7681d9eebe3af/R/template.R#L52

But parse(text = ...) does not work with \r\n:

parse(text = "a<-1;\r\nb<-2")
#> Error in parse(text = "a<-1;\r\nb<-2") : <text>:1:6: unexpected input 1: a<-1;

Interestingly, according the details section of ?parse, it claims to handle CRLF characters correctly in text connections, but it doesn't properly handle quoting

parse(file = textConnection("a<-'1\r\n'; \r\n b<-2"))
expression(a <- "1\n", b <- 2)

One way to approach a fix might be to:

tryCatch(
  eval(parse(text = piece), env),
  error = function(e) {
     eval(parse(text = gsub("\r\n", "\n", piece)), env)
  }
)

By the way, it appears this has been an issue for at least 2.5 years: https://stackoverflow.com/questions/46193534/error-text11-unexpected-input-in-r

Dominant language
R
Stars
225
Forks
73
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rstudio/htmltools

All issues in rstudio/htmltools

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.