From 897774db6f056e9aa4bac9bf8bc70bab3fdc4793 Mon Sep 17 00:00:00 2001 From: Brian Morearty Date: Sun, 13 Feb 2011 13:29:22 -0800 Subject: [PATCH] Remove 'escape' parameter/option from helper functions. [#6421 state:resolved] Remove the 'escape' parameter/option from the following helpers: FormTagHelper#text_area TagHelper#content_tag TagHelper#tag The parameter was deprecated in Rails 3.0.5. Instead of using the escape parameter, callers should now call html_safe on any parameters they don't want escaped. --- .../asset_tag_helpers/stylesheet_tag_helpers.rb | 2 +- .../lib/action_view/helpers/form_options_helper.rb | 2 +- .../lib/action_view/helpers/form_tag_helper.rb | 12 +++---- actionpack/lib/action_view/helpers/tag_helper.rb | 39 +++++++++++--------- actionpack/lib/action_view/helpers/text_helper.rb | 7 +++- actionpack/test/template/erb/tag_helper_test.rb | 4 ++ actionpack/test/template/form_tag_helper_test.rb | 6 ++-- actionpack/test/template/tag_helper_test.rb | 23 +++++++----- 8 files changed, 53 insertions(+), 42 deletions(-) diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb index f3e041d..c8335bd 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb @@ -19,7 +19,7 @@ module ActionView end def asset_tag(source, options) - tag("link", { "rel" => "stylesheet", "type" => Mime::CSS, "media" => "screen", "href" => ERB::Util.html_escape(path_to_asset(source)) }.merge(options), false, false) + tag("link", { "rel" => "stylesheet", "type" => Mime::CSS, "media" => "screen", "href" => ERB::Util.html_escape(path_to_asset(source)) }.merge(options)) end def custom_dir diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index 7698602..d30a38e 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -447,7 +447,7 @@ module ActionView # wrap the output in an appropriate ...options... + # content_tag("p", "Hello world!", {:class => ["song", "play>".html_safe]}) + # # =>

Hello world!

# # <%= content_tag :div, :class => "strong" do -%> - # Hello world! + # Hello world! # <% end -%> - # # =>
Hello world!
- def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block) + # # =>
Hello world!
+ def content_tag(name, content_or_options_with_block = nil, options = nil, &block) if block_given? options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash) - content_tag_string(name, capture(&block), options, escape) + content_tag_string(name, capture(&block), options) else - content_tag_string(name, content_or_options_with_block, options, escape) + content_tag_string(name, content_or_options_with_block, options) end end @@ -123,12 +127,12 @@ module ActionView private - def content_tag_string(name, content, options, escape = true) - tag_options = tag_options(options, escape) if options - "<#{name}#{tag_options}>#{escape ? ERB::Util.h(content) : content}".html_safe + def content_tag_string(name, content, options) + tag_options = tag_options(options) if options + "<#{name}#{tag_options}>#{ERB::Util.h(content)}".html_safe end - def tag_options(options, escape = true) + def tag_options(options) unless options.blank? attrs = [] options.each_pair do |key, value| @@ -137,14 +141,13 @@ module ActionView if !v.is_a?(String) && !v.is_a?(Symbol) v = v.to_json end - v = ERB::Util.html_escape(v) if escape + v = ERB::Util.html_escape(v) attrs << %(data-#{k.to_s.dasherize}="#{v}") end elsif BOOLEAN_ATTRIBUTES.include?(key) attrs << %(#{key}="#{key}") if value elsif !value.nil? - final_value = value.is_a?(Array) ? value.join(" ") : value - final_value = ERB::Util.html_escape(final_value) if escape + final_value = value.is_a?(Array) ? value.map{|v|ERB::Util.html_escape(v)}.join(" ") : ERB::Util.html_escape(value) attrs << %(#{key}="#{final_value}") end end diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 4f7f5c4..3b20e8d 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -494,11 +494,14 @@ module ActionView link_text = block_given?? yield(href) : href href = 'http://' + href unless scheme - unless options[:sanitize] == false + if options.fetch(:sanitize, true) link_text = sanitize(link_text) href = sanitize(href) + else + link_text = link_text.html_safe + href = href.html_safe end - content_tag(:a, link_text, link_attributes.merge('href' => href), !!options[:sanitize]) + punctuation.reverse.join('') + content_tag(:a, link_text, link_attributes.merge('href' => href)) + punctuation.reverse.join('') end end.html_safe end diff --git a/actionpack/test/template/erb/tag_helper_test.rb b/actionpack/test/template/erb/tag_helper_test.rb index a384e94..d2c28eb 100644 --- a/actionpack/test/template/erb/tag_helper_test.rb +++ b/actionpack/test/template/erb/tag_helper_test.rb @@ -10,6 +10,10 @@ module ERBTest assert_equal "
Hello world
", render_content("content_tag :div", "Hello world") end + test "percent equals works for content_tag and does not escape_content" do + assert_equal "
Hello world
", render_content("content_tag :div", "Hello world") + end + test "percent equals works for javascript_tag" do expected_output = "" assert_equal expected_output, render_content("javascript_tag", "alert('Hello')") diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index f8671f2..58e2f0f 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -234,14 +234,14 @@ class FormTagHelperTest < ActionView::TestCase assert_dom_equal expected, actual end - def test_text_area_tag_unescaped_content - actual = text_area_tag "body", "hello world", :size => "20x40", :escape => false + def test_text_area_tag_dont_escape_safe_content + actual = text_area_tag "body", "hello world".html_safe, :size => "20x40" expected = %() assert_dom_equal expected, actual end def test_text_area_tag_unescaped_nil_content - actual = text_area_tag "body", nil, :escape => false + actual = text_area_tag "body", nil expected = %() assert_dom_equal expected, actual end diff --git a/actionpack/test/template/tag_helper_test.rb b/actionpack/test/template/tag_helper_test.rb index 60b466a..1f839ad 100644 --- a/actionpack/test/template/tag_helper_test.rb +++ b/actionpack/test/template/tag_helper_test.rb @@ -40,26 +40,29 @@ class TagHelperTest < ActionView::TestCase assert_equal "

<script>evil_js</script>

", content_tag(:p, '') assert_equal "

", - content_tag(:p, '', nil, false) + content_tag(:p, ''.html_safe) end def test_content_tag_with_block_in_erb - buffer = content_tag(:div) { concat "Hello world!" } - assert_dom_equal "
Hello world!
", buffer + buffer = content_tag(:div) { concat "Hello world!".html_safe } + assert_dom_equal "
Hello world!
", buffer end def test_content_tag_with_block_and_options_in_erb - buffer = content_tag(:div, :class => "green") { concat "Hello world!" } - assert_dom_equal %(
Hello world!
), buffer + buffer = content_tag(:div, :class => "green") { concat "Hello world!".html_safe } + assert_dom_equal %(
Hello world!
), buffer end def test_content_tag_with_block_and_options_out_of_erb - assert_dom_equal %(
Hello world!
), content_tag(:div, :class => "green") { "Hello world!" } + assert_dom_equal %(
<b>Hello</b> world!
), content_tag(:div, :class => "green") { "Hello world!" } + assert_dom_equal %(
Hello world!
), content_tag(:div, :class => "green") { "Hello world!".html_safe } end def test_content_tag_with_block_and_options_outside_out_of_erb - assert_equal content_tag("a", "Create", :href => "create"), - content_tag("a", "href" => "create") { "Create" } + assert_equal content_tag("a", "Create", :href => "create"), + content_tag("a", "href" => "create") { "Create" } + assert_equal content_tag("a", "Create".html_safe, :href => "create"), + content_tag("a", "href" => "create") { "Create".html_safe } end def test_content_tag_nested_in_content_tag_out_of_erb @@ -83,7 +86,7 @@ class TagHelperTest < ActionView::TestCase end def test_content_tag_with_unescaped_array_class - str = content_tag('p', "limelight", {:class => ["song", "play>"]}, false) + str = content_tag('p', "limelight", {:class => ["song", "play>".html_safe]}) assert_equal "

\">limelight

", str end @@ -108,7 +111,7 @@ class TagHelperTest < ActionView::TestCase end def test_disable_escaping - assert_equal '', tag('a', { :href => '&' }, false, false) + assert_equal '', tag('a', { :href => '&'.html_safe }) end def test_data_attributes -- 1.6.2.2