Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unescape HTML in Ember helpers

Tags:

ember.js

I have the following helper in my Ember app:

Ember.Handlebars.helper "social_profiles", ((person) ->
  person.social_profiles.map (item) ->
    " <a href=''> #{item.type_name}</a>"
), "social_profiles"

Each time I call the helper it returns an escaped string, but I would like ember to show HTML links.

How can I achieve that?

like image 871
Kenny Meyer Avatar asked Jan 13 '23 02:01

Kenny Meyer


1 Answers

You can mark a string as safe with new Handlebars.SafeString("<b>hello world</b>"). Handlebars will not escape any of the input now.

However, you need to be sure that your string is safe. Since you are passing in item.type_name that could contain malicious code that would not be caught since you are declaring the string as safe.

In order to solve this, first your escape the user input, then wrap it in an tag marked for safe.

Example:

Ember.Handlebars.registerHelper('boldItem', function(item) {
  var escaped = Handlebars.Utils.escapeExpression(item);
  return new Handlebars.SafeString("<b>" + escaped + "</b>");
});
like image 158
Ryan Avatar answered Feb 07 '23 13:02

Ryan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!