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

Bad escaping of ">" and "<" #40

Closed
zeehio opened this Issue Nov 17, 2017 · 5 comments

Comments

Projects
None yet
3 participants
@zeehio
Copy link
Contributor

zeehio commented Nov 17, 2017

This simple example does not generate proper HTML code because it does not replace > with &gt;:

htmlTable::htmlTable(data.frame(a = ">3"), rnames = FALSE)

Actual result:

<table class='gmisc_table' style='border-collapse: collapse; margin-top: 1em; margin-bottom: 1em;' >
<thead>
<tr>
<th style='border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>a</th>
</tr>
</thead>
<tbody>
<tr>
<td style='border-bottom: 2px solid grey; text-align: center;'>>3</td>
</tr>
</tbody>
</table>

Expected:

<table class='gmisc_table' style='border-collapse: collapse; margin-top: 1em; margin-bottom: 1em;' >
<thead>
<tr>
<th style='border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>a</th>
</tr>
</thead>
<tbody>
<tr>
<td style='border-bottom: 2px solid grey; text-align: center;'>&gt;3</td>
</tr>
</tbody>
</table>

These characters should be escaped.

  • &lt; represents the < sign.
  • &gt; represents the > sign.
  • &amp; represents the & sign.

Probably a escape=TRUE argument should be added, so the character escaping can be skipped if, for instance, I want to include HTML code inside the table.

lemna pushed a commit to lemna/htmlTable that referenced this issue Nov 21, 2017

@gforge

This comment has been minimized.

Copy link
Owner

gforge commented Nov 23, 2017

Fixed, please check that the fix works and then I'll upload a new version to CRAN.

@zeehio

This comment has been minimized.

Copy link
Contributor Author

zeehio commented Nov 23, 2017

I'm afraid it is not working well in all cases.

I believe that htmltools::htmlEscape does not accept well data frames. This is not the expected output:

df <- data.frame(a = c("1", ">2"), stringsAsFactors = FALSE)
htmltools::htmlEscape(df)
# "c(\"1\", \"&gt;2\")"

With a matrix it works, but with the equivalent data frame it fails:

mat <- matrix(c("2", ">3", "4", "5"), ncol = 2)
df <- as.data.frame(mat, stringsAsFactors = FALSE)
htmltools::htmlEscape(mat) #ok 
#      [,1]    [,2]
# [1,] "2"     "4" 
# [2,] "&gt;3" "5" 
htmltools::htmlEscape(df) # not ok
# [1] "c(\"2\", \"&gt;3\")" "c(\"4\", \"5\")" 

Maybe the easiest solution is to use this:

if (is.data.frame(x)) {
    x <- as.matrix(format.data.frame(x))
}
x <- htmltools::htmlEscape(x)

But I am not sure if that is the right way to solve the issue for all possible and supported htmlTable inputs...

@lemna

This comment has been minimized.

Copy link
Collaborator

lemna commented Nov 24, 2017

Hi @zeehio and Max,

I've given it another try: #44. This approach should be quite general without applying any other formatting: it works at least for matrix, data.frame and tibble. I've also updated the example in the vignette where html code is added to color some cells. Could you check?

tibble is actually not supported according to spec - maybe there should be a check for the class of the input to limit the number of possible inputs.

/Peter

@zeehio

This comment has been minimized.

Copy link
Contributor Author

zeehio commented Nov 24, 2017

Thanks for your help! It seems it works well now 👍

@gforge

This comment has been minimized.

Copy link
Owner

gforge commented Nov 24, 2017

Awesome! I will update the CRAN version in short.

Thanks!

@gforge gforge closed this Nov 24, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment