diff options
author | Prem Sichanugrist <s@sikac.hu> | 2014-02-14 09:57:47 -0500 |
---|---|---|
committer | Prem Sichanugrist <s@sikac.hu> | 2014-02-18 12:08:36 -0500 |
commit | 920f3ba2668e0622335f16f2f1318d9e6b5e6b28 (patch) | |
tree | d7daebde559c09c6d319510d1ed375f9fb96f74c /actionview | |
parent | 8cd9f6d205e5db5331dd5b01be35b537da17cdee (diff) | |
download | rails-920f3ba2668e0622335f16f2f1318d9e6b5e6b28.tar.gz rails-920f3ba2668e0622335f16f2f1318d9e6b5e6b28.tar.bz2 rails-920f3ba2668e0622335f16f2f1318d9e6b5e6b28.zip |
Introduce `render :html` for render HTML string
This is an option for to HTML content with a content type of
`text/html`. This rendering option calls `ERB::Util.html_escape`
internally to escape unsafe HTML string, so you will have to mark your
string as html safe if you have any HTML tag in it.
Please see #12374 for more detail.
Diffstat (limited to 'actionview')
-rw-r--r-- | actionview/lib/action_view/helpers/rendering_helper.rb | 3 | ||||
-rw-r--r-- | actionview/lib/action_view/layouts.rb | 2 | ||||
-rw-r--r-- | actionview/lib/action_view/renderer/template_renderer.rb | 2 | ||||
-rw-r--r-- | actionview/lib/action_view/template.rb | 1 | ||||
-rw-r--r-- | actionview/lib/action_view/template/html.rb | 34 |
5 files changed, 41 insertions, 1 deletions
diff --git a/actionview/lib/action_view/helpers/rendering_helper.rb b/actionview/lib/action_view/helpers/rendering_helper.rb index 4eae80cd93..15b88bcda6 100644 --- a/actionview/lib/action_view/helpers/rendering_helper.rb +++ b/actionview/lib/action_view/helpers/rendering_helper.rb @@ -14,6 +14,9 @@ module ActionView # * <tt>:text</tt> - Renders the text passed in out. # * <tt>:plain</tt> - Renders the text passed in out. Setting the content # type as <tt>text/plain</tt>. + # * <tt>:html</tt> - Renders the html safe string passed in out, otherwise + # performs html escape on the string first. Setting the content type as + # <tt>text/html</tt>. # * <tt>:body</tt> - Renders the text passed in, and does not set content # type in the response. # diff --git a/actionview/lib/action_view/layouts.rb b/actionview/lib/action_view/layouts.rb index 3f049e838a..9ee05bd816 100644 --- a/actionview/lib/action_view/layouts.rb +++ b/actionview/lib/action_view/layouts.rb @@ -420,7 +420,7 @@ module ActionView end def _include_layout?(options) - (options.keys & [:body, :text, :plain, :inline, :partial]).empty? || options.key?(:layout) + (options.keys & [:body, :text, :plain, :html, :inline, :partial]).empty? || options.key?(:layout) end end end diff --git a/actionview/lib/action_view/renderer/template_renderer.rb b/actionview/lib/action_view/renderer/template_renderer.rb index ba946b230a..be17097428 100644 --- a/actionview/lib/action_view/renderer/template_renderer.rb +++ b/actionview/lib/action_view/renderer/template_renderer.rb @@ -27,6 +27,8 @@ module ActionView Template::Text.new(options[:text], formats.first) elsif options.key?(:plain) Template::Text.new(options[:plain]) + elsif options.key?(:html) + Template::HTML.new(options[:html], formats.first) elsif options.key?(:file) with_fallbacks { find_template(options[:file], nil, false, keys, @details) } elsif options.key?(:inline) diff --git a/actionview/lib/action_view/template.rb b/actionview/lib/action_view/template.rb index 9b0619f1aa..961a969b6e 100644 --- a/actionview/lib/action_view/template.rb +++ b/actionview/lib/action_view/template.rb @@ -90,6 +90,7 @@ module ActionView eager_autoload do autoload :Error autoload :Handlers + autoload :HTML autoload :Text autoload :Types end diff --git a/actionview/lib/action_view/template/html.rb b/actionview/lib/action_view/template/html.rb new file mode 100644 index 0000000000..282da1a8a2 --- /dev/null +++ b/actionview/lib/action_view/template/html.rb @@ -0,0 +1,34 @@ +module ActionView #:nodoc: + # = Action View HTML Template + class Template + class HTML #:nodoc: + attr_accessor :type + + def initialize(string, type = nil) + @string = string.to_s + @type = Types[type] || type if type + @type ||= Types[:html] + end + + def identifier + 'html template' + end + + def inspect + 'html template' + end + + def to_str + ERB::Util.h(@string) + end + + def render(*args) + to_str + end + + def formats + [@type.to_sym] + end + end + end +end |