bs4cards icon indicating copy to clipboard operation
bs4cards copied to clipboard

escape HTML

Open Andryas opened this issue 3 years ago • 5 comments

I would like to put some html code in text arg like <br> <\sup> etc... is there a way to implement a arg to allow it?

Andryas avatar Oct 07 '21 14:10 Andryas

This is very close to the top of the "to do" list. That capability disappeared when I rewrote the API to make cards() the core function, but I have every intention of reintroducing it in the next release. I'm still pondering what the user-facing options for this would be. One thought would be to have an as_html() function so that you could use like this:

dat %>%
  cards(
    title = title_field,
    link = link_field,
    text = as_html(text_field)
 )

The as_html() function would just wrap the input in a protective class so that rmarkdown/pandoc/etc don't try to escape the HTML characters. That seems to be a common design pattern in other similar packages. Would you find that approach easy to use?

djnavarro avatar Oct 12 '21 01:10 djnavarro

Ah fun that's just what I was wondering about! I was just thinking that it'd be nice if make_text() recognized that something is a node, based on the class... so I would like the approach described above, FWIW. :sweat_smile:

maelle avatar Oct 12 '21 07:10 maelle

It works perfect for me!

Andryas avatar Oct 13 '21 12:10 Andryas

It's annoyingly difficult to use htmltools::HTML() to protect the HTML of a column in a data frame. Would you be open to accepting HTML for title, text, header, and footer by default? In that case, as_html() could be called internally inside make_text() etc.

If we're worried about unsafe HTML made from cards from user input I think it's reasonable to expect the Shiny author to call htmltools::htmlEscape() on the user input. But there could be the possibility of setting escape = TRUE on cards() to turn off as_html() internally.

(Btw, I'd be happy to contribute a PR with the above features.)

gadenbuie avatar Nov 12 '21 20:11 gadenbuie

For those that want to escape the HTML inside the text parameter, I solve this using this JQuery function.

$('#id-your-header .card-text').each(function() { 
    var x=$(this).text(); 
    $(this).html(x);
});

Andryas avatar Apr 20 '22 17:04 Andryas