From 1a0380e52cc9b39a1d07a6289c50c858606d976f Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Fri, 26 Jun 2009 21:08:26 +0900 Subject: [PATCH] Fix pattern to match various magic comment formats --- .../lib/action_view/template/handlers/erb.rb | 2 +- .../test/fixtures/test/utf8_magic_vim.html.erb | 5 +++++ actionpack/test/template/render_test.rb | 8 ++++++++ 3 files changed, 14 insertions(+), 1 deletions(-) create mode 100644 actionpack/test/fixtures/test/utf8_magic_vim.html.erb diff --git a/actionpack/lib/action_view/template/handlers/erb.rb b/actionpack/lib/action_view/template/handlers/erb.rb index e3a7d96..5e79901 100644 --- a/actionpack/lib/action_view/template/handlers/erb.rb +++ b/actionpack/lib/action_view/template/handlers/erb.rb @@ -17,7 +17,7 @@ module ActionView def compile(template) require 'erb' - magic = $1 if template.source =~ /\A(<%#.*coding:\s*(\S+)\s*-?%>)/ + magic = $1 if template.source =~ /\A(<%#.*coding[:=]\s*([-\w.]+).*-?%>)/ erb = "#{magic}<% __in_erb_template=true %>#{template.source}" ::ERB.new(erb, nil, erb_trim_mode, '@output_buffer').src end diff --git a/actionpack/test/fixtures/test/utf8_magic_vim.html.erb b/actionpack/test/fixtures/test/utf8_magic_vim.html.erb new file mode 100644 index 0000000..9f735d8 --- /dev/null +++ b/actionpack/test/fixtures/test/utf8_magic_vim.html.erb @@ -0,0 +1,5 @@ +<%# vim:set fileencoding=utf-8: -%> +Русский текст +<%= "日".encoding %> +<%= @output_buffer.encoding %> +<%= __ENCODING__ %> diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index 7f30ae8..0c77da7 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -232,6 +232,14 @@ module RenderTestCases end end + def test_render_utf8_template_with_vim_style_magic_comment + with_external_encoding Encoding::ASCII_8BIT do + result = @view.render(:file => "test/utf8_magic_vim.html.erb", :layouts => "layouts/yield") + assert_equal "Русский текст\nUTF-8\nUTF-8\nUTF-8\n", result + assert_equal Encoding::UTF_8, result.encoding + end + end + def test_render_utf8_template_with_default_external_encoding with_external_encoding Encoding::UTF_8 do result = @view.render(:file => "test/utf8.html.erb", :layouts => "layouts/yield") -- 1.6.3.3