From d01c8fcf07a8497c591253a86558ca1cb0f222cb Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Sun, 23 May 2010 17:00:55 -0300 Subject: [PATCH 1/2] Revert "translation method of TranslationHelper module returns always SafeBuffer [#4194 status:resolved]" This reverts commit 2310aef29be306704c0361f1188200f71df657df. --- .../lib/action_view/helpers/translation_helper.rb | 2 +- .../test/fixtures/test/array_translation.erb | 1 - .../test/template/translation_helper_test.rb | 9 +-------- 3 files changed, 2 insertions(+), 10 deletions(-) delete mode 100644 actionpack/test/fixtures/test/array_translation.erb diff --git a/actionpack/lib/action_view/helpers/translation_helper.rb b/actionpack/lib/action_view/helpers/translation_helper.rb index 9296622..684e250 100644 --- a/actionpack/lib/action_view/helpers/translation_helper.rb +++ b/actionpack/lib/action_view/helpers/translation_helper.rb @@ -13,7 +13,7 @@ module ActionView def translate(key, options = {}) options[:raise] = true translation = I18n.translate(scope_key_by_partial(key), options) - (translation.respond_to?(:join) ? translation.join : translation).html_safe + translation.respond_to?(:html_safe) ? translation.html_safe : translation rescue I18n::MissingTranslationData => e keys = I18n.send(:normalize_translation_keys, e.locale, e.key, e.options[:scope]) content_tag('span', keys.join(', '), :class => 'translation_missing') diff --git a/actionpack/test/fixtures/test/array_translation.erb b/actionpack/test/fixtures/test/array_translation.erb deleted file mode 100644 index 12c0763..0000000 --- a/actionpack/test/fixtures/test/array_translation.erb +++ /dev/null @@ -1 +0,0 @@ -<%= t(['foo', 'bar']) %> \ No newline at end of file diff --git a/actionpack/test/template/translation_helper_test.rb b/actionpack/test/template/translation_helper_test.rb index 24640e4..e59697c 100644 --- a/actionpack/test/template/translation_helper_test.rb +++ b/actionpack/test/template/translation_helper_test.rb @@ -20,14 +20,7 @@ class TranslationHelperTest < Test::Unit::TestCase def test_translation_of_an_array I18n.expects(:translate).with(["foo", "bar"], :raise => true).returns(["foo", "bar"]) - assert_equal "foobar", translate(["foo", "bar"]) - end - - def test_translation_of_an_array_with_html - expected = 'foobar' - I18n.expects(:translate).with(["foo", "bar"], :raise => true).returns(['foo', 'bar']) - @view = ActionView::Base.new(ActionController::Base.view_paths, {}) - assert_equal expected, @view.render(:file => "test/array_translation") + assert_equal ["foo", "bar"], translate(["foo", "bar"]) end def test_delegates_localize_to_i18n -- 1.7.1 From 3a1e4abd234ddedb7baf6cf5f45f44839cf5b05d Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Sun, 23 May 2010 17:40:53 -0300 Subject: [PATCH 2/2] translation method of TranslationHelper module returns a SafeBuffer Array [#4675 state:committed] --- .../lib/action_view/helpers/translation_helper.rb | 3 +-- .../test/fixtures/test/array_translation.erb | 1 + .../test/template/translation_helper_test.rb | 7 +++++++ .../core_ext/string/output_safety.rb | 10 ++++++++++ activesupport/test/core_ext/string_ext_test.rb | 12 ++++++++++++ 5 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 actionpack/test/fixtures/test/array_translation.erb diff --git a/actionpack/lib/action_view/helpers/translation_helper.rb b/actionpack/lib/action_view/helpers/translation_helper.rb index 684e250..65927e2 100644 --- a/actionpack/lib/action_view/helpers/translation_helper.rb +++ b/actionpack/lib/action_view/helpers/translation_helper.rb @@ -12,8 +12,7 @@ module ActionView # prepend the key with a period, nothing is converted. def translate(key, options = {}) options[:raise] = true - translation = I18n.translate(scope_key_by_partial(key), options) - translation.respond_to?(:html_safe) ? translation.html_safe : translation + I18n.translate(scope_key_by_partial(key), options).html_safe rescue I18n::MissingTranslationData => e keys = I18n.send(:normalize_translation_keys, e.locale, e.key, e.options[:scope]) content_tag('span', keys.join(', '), :class => 'translation_missing') diff --git a/actionpack/test/fixtures/test/array_translation.erb b/actionpack/test/fixtures/test/array_translation.erb new file mode 100644 index 0000000..787a6b1 --- /dev/null +++ b/actionpack/test/fixtures/test/array_translation.erb @@ -0,0 +1 @@ +<%= t(['foo', 'bar']).join(", ") %> \ No newline at end of file diff --git a/actionpack/test/template/translation_helper_test.rb b/actionpack/test/template/translation_helper_test.rb index e59697c..6b95fe0 100644 --- a/actionpack/test/template/translation_helper_test.rb +++ b/actionpack/test/template/translation_helper_test.rb @@ -23,6 +23,13 @@ class TranslationHelperTest < Test::Unit::TestCase assert_equal ["foo", "bar"], translate(["foo", "bar"]) end + def test_translation_of_an_array_with_html + expected = 'foo, bar' + I18n.expects(:translate).with(["foo", "bar"], :raise => true).returns(['foo', 'bar']) + @view = ActionView::Base.new(ActionController::Base.view_paths, {}) + assert_equal expected, @view.render(:file => "test/array_translation") + end + def test_delegates_localize_to_i18n @time = Time.utc(2008, 7, 8, 12, 18, 38) I18n.expects(:localize).with(@time) diff --git a/activesupport/lib/active_support/core_ext/string/output_safety.rb b/activesupport/lib/active_support/core_ext/string/output_safety.rb index 1d779a0..169519a 100644 --- a/activesupport/lib/active_support/core_ext/string/output_safety.rb +++ b/activesupport/lib/active_support/core_ext/string/output_safety.rb @@ -158,3 +158,13 @@ class String other.respond_to?(:html_safe?) && other.html_safe? end end + +module Enumerable + def html_safe + map { |e| e.respond_to?(:html_safe) ? e.html_safe : e } + end + + def html_safe? + all? { |e| e.respond_to?(:html_safe?) ? e.html_safe? : false } + end +end diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index af3ec12..7ab1337 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -502,6 +502,18 @@ class OutputSafetyTest < ActiveSupport::TestCase assert string.html_safe? end + test "Marking an array with elements html_safe capable returns the array as safe" do + array = ["a", 1, "b", "c", 2] + assert_equal array, array.html_safe + assert array.html_safe.html_safe? + end + + test "Marking an array with elements some elements not html_safe capable returns the array as unsafe" do + array = ["a", 1, @object, "b", "c", 2] + assert_equal array, array.html_safe + assert !array.html_safe.html_safe? + end + test 'emits normal string yaml' do assert_equal 'foo'.to_yaml, 'foo'.html_safe.to_yaml(:foo => 1) end -- 1.7.1