mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Ruby 1.9: ERB template encoding using a magic comment at the top of the file
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
*Edge*
|
||||
|
||||
* Ruby 1.9: ERB template encoding using a magic comment at the top of the file. [Jeremy Kemper]
|
||||
<%# encoding: utf-8 %>
|
||||
|
||||
* Change integration test helpers to accept Rack environment instead of just HTTP Headers [Pratik Naik]
|
||||
|
||||
Before : get '/path', {}, 'Accept' => 'text/javascript'
|
||||
|
||||
@@ -16,11 +16,7 @@ module ActionView
|
||||
self.default_format = Mime::HTML
|
||||
|
||||
def compile(template)
|
||||
src = ::ERB.new("<% __in_erb_template=true %>#{template.source}", nil, erb_trim_mode, '@output_buffer').src
|
||||
|
||||
# Ruby 1.9 prepends an encoding to the source. However this is
|
||||
# useless because you can only set an encoding on the first line
|
||||
RUBY_VERSION >= '1.9' ? src.sub(/\A#coding:.*\n/, '') : src
|
||||
::ERB.new("<% __in_erb_template=true %>#{template.source}", nil, erb_trim_mode, '@output_buffer').src
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -52,16 +52,26 @@ module ActionView
|
||||
|
||||
locals_code = locals.keys.map! { |key| "#{key} = local_assigns[:#{key}];" }.join
|
||||
|
||||
code = @handler.call(self)
|
||||
encoding_comment = $1 if code.sub!(/\A(#.*coding.*)\n/, '')
|
||||
|
||||
source = <<-end_src
|
||||
def #{method_name}(local_assigns)
|
||||
old_output_buffer = output_buffer;#{locals_code};#{@handler.call(self)}
|
||||
old_output_buffer = output_buffer;#{locals_code};#{code}
|
||||
ensure
|
||||
self.output_buffer = old_output_buffer
|
||||
end
|
||||
end_src
|
||||
|
||||
if encoding_comment
|
||||
source = "#{encoding_comment}\n#{source}"
|
||||
line = -1
|
||||
else
|
||||
line = 0
|
||||
end
|
||||
|
||||
begin
|
||||
ActionView::Base::CompiledTemplates.module_eval(source, identifier, 0)
|
||||
ActionView::Base::CompiledTemplates.module_eval(source, identifier, line)
|
||||
method_name
|
||||
rescue Exception => e # errors from template code
|
||||
if logger = (view && view.logger)
|
||||
@@ -79,4 +89,4 @@ module ActionView
|
||||
"_render_template_#{@identifier.hash}_#{__id__}_#{locals.keys.hash}".gsub('-', "_")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
5
actionpack/test/fixtures/test/utf8.html.erb
vendored
5
actionpack/test/fixtures/test/utf8.html.erb
vendored
@@ -1,2 +1,5 @@
|
||||
<%# encoding: utf-8 -%>
|
||||
Русский текст
|
||||
日本語のテキスト
|
||||
<%= "日".encoding %>
|
||||
<%= @output_buffer.encoding %>
|
||||
<%= __ENCODING__ %>
|
||||
|
||||
@@ -249,7 +249,7 @@ module RenderTestCases
|
||||
if '1.9'.respond_to?(:force_encoding)
|
||||
def test_render_utf8_template
|
||||
result = @view.render(:file => "test/utf8.html.erb", :layouts => "layouts/yield")
|
||||
assert_equal "Русский текст\n日本語のテキスト", result
|
||||
assert_equal "Русский текст\nUTF-8\nUTF-8\nUTF-8\n", result
|
||||
assert_equal Encoding::UTF_8, result.encoding
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user