Skip to content

Commit

Permalink
no need for Formtastic::Util.html_safe anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
justinfrench committed Nov 7, 2014
1 parent 3363fd7 commit 6da1d7b
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 34 deletions.
2 changes: 1 addition & 1 deletion lib/formtastic/form_builder.rb
Expand Up @@ -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(%{<abbr title="#{Formtastic::I18n.t(:required)}">*</abbr>}) }
configure :required_string, proc { %{<abbr title="#{Formtastic::I18n.t(:required)}">*</abbr>}.html_safe }
configure :optional_string, ''
configure :inline_errors, :sentence
configure :label_str_method, :humanize
Expand Down
4 changes: 2 additions & 2 deletions lib/formtastic/helpers/errors_helper.rb
Expand Up @@ -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

Expand All @@ -78,4 +78,4 @@ def render_inline_errors?
end
end
end
end
end
7 changes: 5 additions & 2 deletions lib/formtastic/helpers/fieldset_wrapper.rb
Expand Up @@ -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)
)

Expand All @@ -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

Expand Down
8 changes: 4 additions & 4 deletions lib/formtastic/inputs/base/errors.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/formtastic/inputs/base/hints.rb
Expand Up @@ -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
Expand Down
16 changes: 0 additions & 16 deletions 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
Expand Down
16 changes: 8 additions & 8 deletions spec/inputs/country_input_spec.rb
Expand Up @@ -26,7 +26,7 @@

before do
concat(semantic_form_for(@new_post) do |builder|
builder.stub(:country_select).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
builder.stub(:country_select).and_return("<select><option>...</option></select>".html_safe)
concat(builder.input(:country, :as => :country))
end)
end
Expand Down Expand Up @@ -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("<select><option>...</option></select>"))
builder.should_receive(:country_select).with(:country, priority_countries, {}, {:id => "post_country", :required => false, :autofocus => false}).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
builder.stub(:country_select).and_return("<select><option>...</option></select>".html_safe)
builder.should_receive(:country_select).with(:country, priority_countries, {}, {:id => "post_country", :required => false, :autofocus => false}).and_return("<select><option>...</option></select>".html_safe)

concat(builder.input(:country, :as => :country, :priority_countries => priority_countries))
end
Expand All @@ -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("<select><option>...</option></select>"))
builder.should_receive(:country_select).with(:country, priority_countries, {}, {:id => "post_country", :required => false, :autofocus => false}).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
builder.stub(:country_select).and_return("<select><option>...</option></select>".html_safe)
builder.should_receive(:country_select).with(:country, priority_countries, {}, {:id => "post_country", :required => false, :autofocus => false}).and_return("<select><option>...</option></select>".html_safe)

concat(builder.input(:country, :as => :country))
end
Expand All @@ -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("<select><option>...</option></select>"))
builder.should_receive(:country_select).with(:country, [], {}, {:id => "context2_post_country", :required => false, :autofocus => false}).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
builder.stub(:country_select).and_return("<select><option>...</option></select>".html_safe)
builder.should_receive(:country_select).with(:country, [], {}, {:id => "context2_post_country", :required => false, :autofocus => false}).and_return("<select><option>...</option></select>".html_safe)
concat(builder.input(:country, :priority_countries => []))
end)
end
Expand All @@ -101,7 +101,7 @@

before do
concat(semantic_form_for(@new_post) do |builder|
builder.stub(:country_select).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
builder.stub(:country_select).and_return("<select><option>...</option></select>".html_safe)
concat(builder.input(:country))
end)
end
Expand Down

0 comments on commit 6da1d7b

Please sign in to comment.