From 94911c7af7f2e1da0a23d64d2055b77bc1f6ece8 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 1 Jun 2009 12:33:49 -0700 Subject: Ensure ERB source begins with the encoding comment --- actionpack/lib/action_view/template/handlers/erb.rb | 4 +++- actionpack/lib/action_view/template/template.rb | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'actionpack/lib/action_view/template') diff --git a/actionpack/lib/action_view/template/handlers/erb.rb b/actionpack/lib/action_view/template/handlers/erb.rb index d773df7d29..21272ef089 100644 --- a/actionpack/lib/action_view/template/handlers/erb.rb +++ b/actionpack/lib/action_view/template/handlers/erb.rb @@ -16,7 +16,9 @@ module ActionView self.default_format = Mime::HTML def compile(template) - ::ERB.new("<% __in_erb_template=true %>#{template.source}", nil, erb_trim_mode, '@output_buffer').src + magic = $1 if template.source =~ /\A(<%#.*coding:\s*(\S+)\s*-?%>)/ + erb = "#{magic}<% __in_erb_template=true %>#{template.source}" + ::ERB.new(erb, nil, erb_trim_mode, '@output_buffer').src end end end diff --git a/actionpack/lib/action_view/template/template.rb b/actionpack/lib/action_view/template/template.rb index a9897258d2..e7ea42c2eb 100644 --- a/actionpack/lib/action_view/template/template.rb +++ b/actionpack/lib/action_view/template/template.rb @@ -53,7 +53,11 @@ 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/, '') + if code.sub!(/\A(#.*coding.*)\n/, '') + encoding_comment = $1 + elsif defined?(Encoding) && Encoding.respond_to?(:default_external) + encoding_comment = "#coding:#{Encoding.default_external}" + end source = <<-end_src def #{method_name}(local_assigns) -- cgit v1.2.3