Skip to content
This repository
Browse code

Ensure ERB source begins with the encoding comment

  • Loading branch information...
commit 94911c7af7f2e1da0a23d64d2055b77bc1f6ece8 1 parent 9537fd0
Jeremy Kemper jeremy authored
4 actionpack/lib/action_view/template/handlers/erb.rb
@@ -16,7 +16,9 @@ class ERB < TemplateHandler
16 16 self.default_format = Mime::HTML
17 17
18 18 def compile(template)
19   - ::ERB.new("<% __in_erb_template=true %>#{template.source}", nil, erb_trim_mode, '@output_buffer').src
  19 + magic = $1 if template.source =~ /\A(<%#.*coding:\s*(\S+)\s*-?%>)/
  20 + erb = "#{magic}<% __in_erb_template=true %>#{template.source}"
  21 + ::ERB.new(erb, nil, erb_trim_mode, '@output_buffer').src
20 22 end
21 23 end
22 24 end
6 actionpack/lib/action_view/template/template.rb
@@ -53,7 +53,11 @@ def compile(locals, view)
53 53 locals_code = locals.keys.map! { |key| "#{key} = local_assigns[:#{key}];" }.join
54 54
55 55 code = @handler.call(self)
56   - encoding_comment = $1 if code.sub!(/\A(#.*coding.*)\n/, '')
  56 + if code.sub!(/\A(#.*coding.*)\n/, '')
  57 + encoding_comment = $1
  58 + elsif defined?(Encoding) && Encoding.respond_to?(:default_external)
  59 + encoding_comment = "#coding:#{Encoding.default_external}"
  60 + end
57 61
58 62 source = <<-end_src
59 63 def #{method_name}(local_assigns)
1  actionpack/test/fixtures/test/utf8.html.erb
... ... @@ -1,4 +1,3 @@
1   -<%# encoding: utf-8 -%>
2 1 Русский текст
3 2 <%= "日".encoding %>
4 3 <%= @output_buffer.encoding %>
5 actionpack/test/fixtures/test/utf8_magic.html.erb
... ... @@ -0,0 +1,5 @@
  1 +<%# encoding: utf-8 -%>
  2 +Русский текст
  3 +<%= "日".encoding %>
  4 +<%= @output_buffer.encoding %>
  5 +<%= __ENCODING__ %>
25 actionpack/test/template/render_test.rb
@@ -247,10 +247,27 @@ def test_render_with_nested_layout
247 247 end
248 248
249 249 if '1.9'.respond_to?(:force_encoding)
250   - def test_render_utf8_template
251   - result = @view.render(:file => "test/utf8.html.erb", :layouts => "layouts/yield")
252   - assert_equal "Русский текст\nUTF-8\nUTF-8\nUTF-8\n", result
253   - assert_equal Encoding::UTF_8, result.encoding
  250 + def test_render_utf8_template_with_magic_comment
  251 + with_external_encoding Encoding::ASCII_8BIT do
  252 + result = @view.render(:file => "test/utf8_magic.html.erb", :layouts => "layouts/yield")
  253 + assert_equal "Русский текст\nUTF-8\nUTF-8\nUTF-8\n", result
  254 + assert_equal Encoding::UTF_8, result.encoding
  255 + end
  256 + end
  257 +
  258 + def test_render_utf8_template_with_default_external_encoding
  259 + with_external_encoding Encoding::UTF_8 do
  260 + result = @view.render(:file => "test/utf8.html.erb", :layouts => "layouts/yield")
  261 + assert_equal "Русский текст\nUTF-8\nUTF-8\nUTF-8\n", result
  262 + assert_equal Encoding::UTF_8, result.encoding
  263 + end
  264 + end
  265 +
  266 + def with_external_encoding(encoding)
  267 + old, Encoding.default_external = Encoding.default_external, encoding
  268 + yield
  269 + ensure
  270 + Encoding.default_external = old
254 271 end
255 272 end
256 273 end

0 comments on commit 94911c7

Please sign in to comment.
Something went wrong with that request. Please try again.