From 6da1d7bf4c147af879d81b34687ca611f1197473 Mon Sep 17 00:00:00 2001 From: Justin French Date: Fri, 7 Nov 2014 21:34:26 +1100 Subject: [PATCH] no need for Formtastic::Util.html_safe anymore --- lib/formtastic/form_builder.rb | 2 +- lib/formtastic/helpers/errors_helper.rb | 4 ++-- lib/formtastic/helpers/fieldset_wrapper.rb | 7 +++++-- lib/formtastic/inputs/base/errors.rb | 8 ++++---- lib/formtastic/inputs/base/hints.rb | 2 +- lib/formtastic/util.rb | 16 ---------------- spec/inputs/country_input_spec.rb | 16 ++++++++-------- 7 files changed, 21 insertions(+), 34 deletions(-) diff --git a/lib/formtastic/form_builder.rb b/lib/formtastic/form_builder.rb index 31ca7d3a9..265fe155d 100644 --- a/lib/formtastic/form_builder.rb +++ b/lib/formtastic/form_builder.rb @@ -12,7 +12,7 @@ def self.configure(name, value = nil) configure :default_text_area_width configure :all_fields_required_by_default, true configure :include_blank_for_select_by_default, true - configure :required_string, proc { Formtastic::Util.html_safe(%{*}) } + configure :required_string, proc { %{*}.html_safe } configure :optional_string, '' configure :inline_errors, :sentence configure :label_str_method, :humanize diff --git a/lib/formtastic/helpers/errors_helper.rb b/lib/formtastic/helpers/errors_helper.rb index c08f06012..c45d5b90a 100644 --- a/lib/formtastic/helpers/errors_helper.rb +++ b/lib/formtastic/helpers/errors_helper.rb @@ -52,7 +52,7 @@ def semantic_errors(*args) return nil if full_errors.blank? html_options[:class] ||= "errors" template.content_tag(:ul, html_options) do - Formtastic::Util.html_safe(full_errors.map { |error| template.content_tag(:li, Formtastic::Util.html_safe(error)) }.join) + full_errors.map { |error| template.content_tag(:li, error) }.join.html_safe end end @@ -78,4 +78,4 @@ def render_inline_errors? end end end -end \ No newline at end of file +end diff --git a/lib/formtastic/helpers/fieldset_wrapper.rb b/lib/formtastic/helpers/fieldset_wrapper.rb index fe4c26686..64ffedd09 100644 --- a/lib/formtastic/helpers/fieldset_wrapper.rb +++ b/lib/formtastic/helpers/fieldset_wrapper.rb @@ -33,12 +33,15 @@ def field_set_and_list_wrapping(*args, &block) #:nodoc: end end + # Work-around for empty contents block + contents ||= "" + # Ruby 1.9: String#to_s behavior changed, need to make an explicit join. contents = contents.join if contents.respond_to?(:join) legend = field_set_legend(html_options) fieldset = template.content_tag(:fieldset, - Formtastic::Util.html_safe(legend) << template.content_tag(:ol, Formtastic::Util.html_safe(contents)), + legend.html_safe << template.content_tag(:ol, contents.html_safe), html_options.except(:builder, :parent, :name) ) @@ -48,7 +51,7 @@ def field_set_and_list_wrapping(*args, &block) #:nodoc: def field_set_legend(html_options) legend = (html_options[:name] || '').to_s legend %= parent_child_index(html_options[:parent]) if html_options[:parent] - legend = template.content_tag(:legend, template.content_tag(:span, Formtastic::Util.html_safe(legend))) unless legend.blank? + legend = template.content_tag(:legend, template.content_tag(:span, legend.html_safe)) unless legend.blank? legend end diff --git a/lib/formtastic/inputs/base/errors.rb b/lib/formtastic/inputs/base/errors.rb index 3b31da24c..780614a00 100644 --- a/lib/formtastic/inputs/base/errors.rb +++ b/lib/formtastic/inputs/base/errors.rb @@ -9,21 +9,21 @@ def error_html def error_sentence_html error_class = builder.default_inline_error_class - template.content_tag(:p, Formtastic::Util.html_safe(errors.to_sentence.html_safe), :class => error_class) + template.content_tag(:p, errors.to_sentence.html_safe, :class => error_class) end def error_list_html error_class = builder.default_error_list_class list_elements = [] errors.each do |error| - list_elements << template.content_tag(:li, Formtastic::Util.html_safe(error.html_safe)) + list_elements << template.content_tag(:li, error.html_safe) end - template.content_tag(:ul, Formtastic::Util.html_safe(list_elements.join("\n")), :class => error_class) + template.content_tag(:ul, list_elements.join("\n").html_safe, :class => error_class) end def error_first_html error_class = builder.default_inline_error_class - template.content_tag(:p, Formtastic::Util.html_safe(errors.first.untaint), :class => error_class) + template.content_tag(:p, errors.first.untaint.html_safe, :class => error_class) end def error_none_html diff --git a/lib/formtastic/inputs/base/hints.rb b/lib/formtastic/inputs/base/hints.rb index 4092c44fe..d4c89ec5d 100644 --- a/lib/formtastic/inputs/base/hints.rb +++ b/lib/formtastic/inputs/base/hints.rb @@ -7,7 +7,7 @@ def hint_html if hint? template.content_tag( :p, - Formtastic::Util.html_safe(hint_text), + hint_text.html_safe, :class => builder.default_hint_class ) end diff --git a/lib/formtastic/util.rb b/lib/formtastic/util.rb index 8c7d054be..d013260aa 100644 --- a/lib/formtastic/util.rb +++ b/lib/formtastic/util.rb @@ -1,26 +1,10 @@ # encoding: utf-8 -# Adapted from the rails3 compatibility shim in Haml 2.2 module Formtastic # @private module Util extend self - ## Rails XSS Safety - # Returns the given text, marked as being HTML-safe. - # With older versions of the Rails XSS-safety mechanism, - # this destructively modifies the HTML-safety of `text`. - # - # @param text [String] - # @return [String] `text`, marked as HTML-safe - def html_safe(text) - if text.respond_to?(:html_safe) - text.html_safe - else - text - end - end - def deprecated_version_of_rails? false # rails_version < Gem::Version.new("4.1.0") end diff --git a/spec/inputs/country_input_spec.rb b/spec/inputs/country_input_spec.rb index 50233c520..ad43667c2 100644 --- a/spec/inputs/country_input_spec.rb +++ b/spec/inputs/country_input_spec.rb @@ -26,7 +26,7 @@ before do concat(semantic_form_for(@new_post) do |builder| - builder.stub(:country_select).and_return(Formtastic::Util.html_safe("")) + builder.stub(:country_select).and_return("".html_safe) concat(builder.input(:country, :as => :country)) end) end @@ -55,8 +55,8 @@ it "should be passed down to the country_select helper when provided" do priority_countries = ["Foo", "Bah"] semantic_form_for(@new_post) do |builder| - builder.stub(:country_select).and_return(Formtastic::Util.html_safe("")) - builder.should_receive(:country_select).with(:country, priority_countries, {}, {:id => "post_country", :required => false, :autofocus => false}).and_return(Formtastic::Util.html_safe("")) + builder.stub(:country_select).and_return("".html_safe) + builder.should_receive(:country_select).with(:country, priority_countries, {}, {:id => "post_country", :required => false, :autofocus => false}).and_return("".html_safe) concat(builder.input(:country, :as => :country, :priority_countries => priority_countries)) end @@ -68,8 +68,8 @@ priority_countries.should_not be_nil semantic_form_for(@new_post) do |builder| - builder.stub(:country_select).and_return(Formtastic::Util.html_safe("")) - builder.should_receive(:country_select).with(:country, priority_countries, {}, {:id => "post_country", :required => false, :autofocus => false}).and_return(Formtastic::Util.html_safe("")) + builder.stub(:country_select).and_return("".html_safe) + builder.should_receive(:country_select).with(:country, priority_countries, {}, {:id => "post_country", :required => false, :autofocus => false}).and_return("".html_safe) concat(builder.input(:country, :as => :country)) end @@ -84,8 +84,8 @@ mock_everything concat(semantic_form_for(@new_post, :namespace => 'context2') do |builder| - builder.stub(:country_select).and_return(Formtastic::Util.html_safe("")) - builder.should_receive(:country_select).with(:country, [], {}, {:id => "context2_post_country", :required => false, :autofocus => false}).and_return(Formtastic::Util.html_safe("")) + builder.stub(:country_select).and_return("".html_safe) + builder.should_receive(:country_select).with(:country, [], {}, {:id => "context2_post_country", :required => false, :autofocus => false}).and_return("".html_safe) concat(builder.input(:country, :priority_countries => [])) end) end @@ -101,7 +101,7 @@ before do concat(semantic_form_for(@new_post) do |builder| - builder.stub(:country_select).and_return(Formtastic::Util.html_safe("")) + builder.stub(:country_select).and_return("".html_safe) concat(builder.input(:country)) end) end