-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix for extra linefeeds in CSS/Script includes #122
Conversation
The newlines do have
What program are you using to view the result? |
I am viewing it either in a browser ("show source"), or in np++, where it shows doubled
I use this in summarytools, and the rest of HTML code is formatted correctly... only the CSS bits have blank lines inserted. For instance, if you try:
and show source, you'll see it. EDIT |
I just tested this on Mac and Windows. I see the problem in Windows (in Edge, but not in Chrome), but not on my Mac (on Chrome, Firefox, or Safari). library(summarytools)
view(dfSummary(iris), method = "browser") This is what it looks like in Chrome and Edge on Windows: And on Chrome, Firefox, and Safari on Mac: On Windows, when I open the output file in a text editor, I see However, when I use the example from my previous comment, but this time take bootstrap.css from summarytools and run it in Windows, I see the same output, with just library(htmltools)
s <- includeCSS(
system.file(package="summarytools", "includes/stylesheets/bootstrap.css")
)
s1 <- substr(s, 1, 200)
print(s1)
#> [1] "<style type=\"text/css\">/*!\r\n * Bootstrap v4.1.2 (https://getbootstrap.com/)\r\n * Copyright 2011-2018 The Bootstrap Authors\r\n * Copyright 2011-2018 Twitter, Inc.\r\n * Licensed under MIT (https://github.c"
cat(s1)
#> <style type="text/css">/*!
#> * Bootstrap v4.1.2 (https://getbootstrap.com/)
#> * Copyright 2011-2018 The Bootstrap Authors
#> * Copyright 2011-2018 Twitter, Inc.
#> * Licensed under MIT (https://github.c So, I agree that there is a problem in Windows when I run your example. However, the proposed fix, which involves changing If you can find a minimal example that involves only the htmltools package, that would help. |
Thanks for putting in the the time and effort to try and get to the bottom of this. I understand your position; I'll try to isolate the problem and keep you posted. |
If the file is being written to a text connection, R will automatically translate library(htmltools)
s <- includeCSS(
system.file(package="shiny", "www/shared/bootstrap/css/bootstrap.css")
)
file <- tempfile()
text <- as.character(s)
writeLines(text, con = file, useBytes = TRUE)
readChar(file, file.info(file)$size, TRUE) You'll see the duplicated |
Thanks for the suggestion, certainly worth a try... I'd rather have only |
@kevinushey you were spot on. I took only the first few lines of the bootstrap css and called it "bogus.css", with CRLF endings.
Reading in HEX mode, we see CR's (Hex
When writing in binary mode, all is fine (i.e. no doubling of CR's). This is a rather annoying flaw in R's core... I didn't see it when googling the issue... Only thing I found has to do with C: Filing a bug report might be the appropriate step to take? What do you guys think? In the meantime I plan on redefining |
I don't think this is a bug; it's the expected and documented behavior when writing That said, I don't understand why |
I agree, it's not clear to me why @jcheng5 do you know why it's used there? Here's where it came from: |
Nope. Let's get rid of it |
Thx for this, it should make things much cleaner on Windows. @yihui Pls note that in my pull request, the @kevinushey Can we really say it's "intended" though? I mean who would want to intentionally produce |
@dcomtois Please feel free to ignore the change in my servr package. It doesn't have anything to do with htmltools. I blindly copied |
We'll need to re-investigate this to make sure the whitespace eating changes didn't change the behavior in an undesirable way. (Because I think we might be writing directly to a binary connection now, I'm not sure.) Also, we'll need to add unit tests. |
I've been trying for 30 minutes now but for the life of me I can't reproduce this issue. I'm using both summarytools and htmltools from CRAN, and I also tried summarytools 0.9.3. I can reproduce something similar enough though: tf <- tempfile(fileext = ".html")
htmltools::save_html(htmltools::HTML("\r\n"), tf)
grepl("\r\r\n", readChar(tf, file.info(tf)$size, TRUE)) Rather than (or in addition to) eliminating |
Update: the reason it won't repro is because summarytools made this change: |
I changed the
collapse=
value from"\r\n"
to"\n"
to prevent having empty lines created in the source HTML.For instance, before this fix, notice the extra empty lines (which do not reflect the true content of bootstrap.min.css)
After the fix, all is normal. Tested on both Windows and Linux,