Skip to content
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

Add safe html support to arrays of translations #32361

Merged
merged 1 commit into from Jul 3, 2018

Conversation

@ph3t
Copy link

@ph3t ph3t commented Mar 27, 2018

Summary

The translate method ignores the _html suffix, which leads developers to overuse html_safe in places where it shouldn't be used. This PR adds safe html support to arrays of translations whose key includes the _html suffix.

@rails-bot
Copy link

@rails-bot rails-bot commented Mar 27, 2018

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kaspth (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@ph3t ph3t force-pushed the ph3t:safe-html-translation-arrays branch 3 times, most recently Mar 27, 2018
Copy link
Member

@kaspth kaspth left a comment

I find this a little hard to grok, do you have a before/after from your app? Or can you make one?

actionview/lib/action_view/helpers/translation_helper.rb Outdated
@@ -85,8 +85,11 @@ def translate(key, options = {})
end
end
translation = I18n.translate(scope_key_by_partial(key), html_safe_options.merge(raise: i18n_raise))

translation.respond_to?(:html_safe) ? translation.html_safe : translation
if translation.is_a?(Array)

This comment has been minimized.

@kaspth

kaspth Apr 1, 2018
Member

Can I18n.translate return enumerables other than just Array? E.g. would respond_to?(:map) be clearer/better?

actionview/test/template/translation_helper_test.rb Outdated
@@ -164,7 +164,8 @@ def test_translate_with_html_count
assert_equal "<a>Other &lt;One&gt;</a>", translate(:'translations.count_html', count: "<One>")
end

def test_translation_returning_an_array_ignores_html_suffix
def test_translate_marks_array_of_translations_with_a_html_safe_suffix_as_safe_html
assert_equal true, translate(:'translations.array_html').all? { |element| element.html_safe? }

This comment has been minimized.

@kaspth

kaspth Apr 1, 2018
Member

We generally prefer Ruby's definition of true and wouldn't explicitly assert it as the return value.

Think this would be clearer as:

translate(:'translations.array_html').tap do |translated|
  assert_equal %w( foo bar ), translated
  assert translated.all?(&:html_safe?)
end

This comment has been minimized.

@ph3t

ph3t Apr 2, 2018
Author

All the other tests I've seen on this file use assert_equal true so I supposed it was preferred. I will update it.

@ph3t
Copy link
Author

@ph3t ph3t commented Apr 2, 2018

Having a locale like this:

en:
  hello_html: "<i>Hello world</i>"
  foo_html:
    - "One"
    - "<strong>Two</strong>"
    - "Three &#128075; &#128578;"

And a view:

<h1><%= t('hello_html') %></h1>
<% t('foo_html').each do |translation| %>
  <li><%= translation %></li>
<% end %>

As a developer I would expect both translations to have the same result, the html shouldn't be escaped as it's marked as safe with the _html suffix. But this is what it looks like before adding my code:
image
I would need to explicitly mark translation as html safe in the view using sanitize, html_safe, <%== or raw to display the content correctly.

With the new code, using the _html suffix would be enough:
image

@ph3t
Copy link
Author

@ph3t ph3t commented May 24, 2018

Hello @kaspth, could you take another look at this?

@kaspth
Copy link
Member

@kaspth kaspth commented Jul 2, 2018

Nice! Needs a squash down to 1 commit. Then a changelog entry. We probably don't need the changes in the .sqlite file.

@ph3t ph3t force-pushed the ph3t:safe-html-translation-arrays branch to 42c3537 Jul 3, 2018
@kaspth kaspth merged commit a3a876f into rails:master Jul 3, 2018
2 checks passed
2 checks passed
codeclimate All good!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@kaspth
Copy link
Member

@kaspth kaspth commented Jul 3, 2018

Great, thanks!

@ph3t ph3t deleted the ph3t:safe-html-translation-arrays branch Jul 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants